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的结构体内容
相关推荐
空空潍9 小时前
Spring AI与LangChain对比:组件对应关系、设计差异与选型指南
人工智能·spring·langchain
liu****11 小时前
LangChain-AI应用开发框架(二)
人工智能·python·langchain·大模型部署
追风哥哥11 小时前
transformer 注意力机制解析
人工智能·深度学习·神经网络·机器学习·语言模型·chatgpt·transformer
renhongxia113 小时前
大模型Prompt实战:精准生成专业技术文档
人工智能·微服务·语言模型·自然语言处理·机器人·prompt
老王熬夜敲代码13 小时前
Planner
langchain
张驰咨询公司15 小时前
AI替代MBB?实测DeepSeek、Kimi、ChatGPT做六西格玛分析的准确度对比
人工智能·chatgpt·六西格玛·六西格玛绿带培训·六西格玛培训公司·精益六西格玛培训
weixin_4166600716 小时前
解决方案:DeepSeek/ChatGPT生成的内容,如何无损转为Word文档?
chatgpt·word·论文·数学公式·deepseek
青梅煮酒与君饮16 小时前
浅谈大模型、Agent、Function Calling、MCP、Skill、Subagent、Langchain、Workflow
人工智能·python·语言模型·langchain·llama
liu****16 小时前
LangChain-AI应用开发框架(一)
c++·python·langchain·本地部署大模型
147API16 小时前
Claude Code 本地化实践:Prompt 缓存机制解析与国内接入成本优化
缓存·prompt·开发工具·降本增效