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的结构体内容
相关推荐
Java小白笔记4 小时前
Codex config.toml配置实战指南
人工智能·算法·chatgpt·ai编程·集成学习
大鹅同志8 小时前
Agent Runtime Evaluation Platform — AI Agent 运行时全维度质量评估平台
ai·langchain·agent·rag·langgraph·vibecoding·llm-as-judge
孤墨云起10 小时前
本地 Codex 接口登录报错 account/read failed 的排查与修复记录
人工智能·chatgpt
闲猫10 小时前
深入理解 Skills:从 API 调用到智能体行为封装
人工智能·python·langchain
BraveWang12 小时前
【LangChain 1.x】02、LangChain 1.x 快速上手
langchain
白白白飘12 小时前
【9】Langchain实战案例:构建一个聊天机器人
microsoft·langchain·机器人
白白白飘12 小时前
【8】补充与拓展:Langchain的联网搜索Agent,使用Tavily工具
服务器·网络·langchain
荣--13 小时前
业务智能体实战笔记:分层消除不确定性(一)|总纲:把不确定性从 LLM 侧转移到工程侧
prompt·rag·ai agent·spring ai·业务智能体
爱奥尼欧13 小时前
【LangChain】2.提示词工程
人工智能·microsoft·langchain
白白白飘14 小时前
【10】Langchain读取数据库
数据库·langchain