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的结构体内容
相关推荐
oscar9994 小时前
Codex 编程智能体入门指南
ai·chatgpt·codex
齐 飞6 小时前
LangChain快速入门-03Retrieval(上)
langchain
怕浪猫7 小时前
第9章 实战项目一:智能数据分析Agent
langchain·aigc·ai编程
云栖梦泽在7 小时前
Claude Code / Codex 使用卡顿怎么办?AI 编程 Agent 连接失败与网络排查思路
网络·人工智能·网络协议·chatgpt·性能优化
阿拉斯攀登9 小时前
AI Agent 入门:从 ChatGPT 到自主智能体
人工智能·chatgpt·agent·ai编程·loop
invicinble10 小时前
总结一下LangChain+LangGraph这套体系
langchain
糖果店的幽灵11 小时前
langchain 所有内置中间件最详细解析
中间件·langchain
阿拉斯攀登13 小时前
Agent 框架对比:LangChain / AutoGPT / CrewAI
人工智能·langchain·agent·rag·function
跨境生态圈14 小时前
2026外贸获客渠道全面洗牌:AI正在重新分配全球流量,你的品牌在答案里吗?
大数据·运维·人工智能·chatgpt
阿拉雷️14 小时前
AI Agent 入门与实战:从对话到干活,理解下一代AI工作方式
人工智能·语言模型·prompt