ChatGPT | 使用自己Prompt替换LangChain默认Prompt

某些场景会要求ChatGPT重复处理同一个操作,要么在问题里面加入Prompt,要么用自己Prompt替换LangChain默认Prompt。

直接看看前后对比结果

LangChain默认的Prompt

template="Use the following pieces of context to answer the users question. \nIf you don't know the answer, just say that you don't know, don't try to make up an answer.\n----------------\n{context}"

我自己Prompt替换了之后

template="Use the following pieces of context to answer the users question in Chinese.\n If you don't know the answer, just say that you don't know, don't try to make up an answer.\n There are multiple answers, provide each answer in Chinese,specify the source file for each answer.\n \n\n{context}

源代码如下:

python 复制代码
def getMyPrompt():
    '''自定义Prompt模板'''
    prompt_template = """Use the following pieces of context to answer the users question in Chinese.
    If you don't know the answer, just say that you don't know, don't try to make up an answer.
    There are multiple answers, provide each answer in Chinese,specify the source file for each answer.
    \n\n{context}\n\nQuestion: {question}\n\nAnswer in Chinese:"""
    
    MyPrompt = PromptTemplate(
        template=prompt_template, input_variables=["context","question"]#必须有上下文context和问题question
    )
    return MyPrompt

db_RTCS = Chroma(persist_directory="./RCTS/", embedding_function=embeddings)
print('----------------')
chain_type_kwargs = {"prompt": getMyPrompt()}#用自己的Prompt替换掉langchain默认的Prompt
qa_RTCS = RetrievalQA.from_chain_type(llm=openAiLLm,chain_type="stuff",
                                      retriever=db_RTCS.as_retriever(),
                                      chain_type_kwargs=chain_type_kwargs)

print(qa_RTCS)#查看自定义Prompt的结构体内容
相关推荐
倾颜14 分钟前
从手写 Runner 到 LangGraph:受控 Agent 接入 LangGraph
前端·后端·langchain
wuhen_n23 分钟前
从零到一!前端搭建本地轻量化 RAG 问答系统
前端·langchain·ai编程
monsion1 小时前
Loop Engineering:你不再 prompt agent,而是设计 prompt agent 的系统
大数据·人工智能·prompt
一楼的猫1 小时前
茄子写作助手——品牌搜索突破9万后的技术型品牌认知与官网入口指南
人工智能·学习·机器学习·chatgpt·ai写作
xianghongtao01163 小时前
把 Prompt 当成“可训练参数“:SkillOpt 如何用深度学习的纪律去优化 Agent 技能
人工智能·深度学习·性能优化·prompt
Solis程序员4 小时前
LangChain从入门到精通(2)
langchain
战族狼魂4 小时前
AI巨头IPO热潮引爆资本市场
人工智能·chatgpt·大模型·大语言模型·ai工程化
杨杨杨大侠5 小时前
ChatGPT 订阅与客户端下载指南
chatgpt
kishu_iOS&AI5 小时前
LLM —— LangChain
人工智能·langchain
garmin Chen5 小时前
Prompt工程入门:让AI按你的要求工作(3)--Prompt工程与提示词安全评测概述
java·人工智能·python·安全·prompt