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的结构体内容
相关推荐
M--Y1 小时前
LangGraph 架构基石:持久化与运行控制
python·langchain·大模型·llm·持久化·langgraph
xyphf_和派孔明1 小时前
第十章: LangGraph的人机交互Human-in-the-loop实践
langchain·llm
xyphf_和派孔明5 小时前
第六章: LangGraph的工具调用代理-Tool Calling Agent
langchain·llm
一缕82年的清风8 小时前
告别手动搬砖!我是如何搭出一套“AI 全自动自媒体运营系统”,每天 10 分钟铺满 7 大平台的?
chatgpt·claude·gemini
M--Y18 小时前
LangGraph 工作流模式
开发语言·javascript·python·langchain·大模型·llm·langgraph
forest_0919 小时前
多 Agent 协作第二课:交叉验证≠独立验证
人工智能·python·算法·机器学习·prompt·agi
xyphf_和派孔明1 天前
第五章: LangGraph的路由代理-Router Agent介绍
langchain·llm
Flynn Liang1 天前
Agentic 工作流:子代理、Supervisor 与人工介入
java·langchain·多智能体·supervisor·agentic·langchain4j
syso_稻草人1 天前
qwen-image-2.1 PE 增强编辑:QwenImage21_EditPromptRewrite 一句话中文扩写成完整编辑指令
人工智能·stable diffusion·prompt·comfyui·qwen-image2.1
GISer_Jing2 天前
Agent核心内容
后端·ai·langchain