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的结构体内容
相关推荐
极客先躯4 分钟前
高级java每日一道面试题-2025年7月17日-基础篇[LangChain4j]-如何实现模型的负载均衡和故障转移?
java·langchain·负载均衡·重试机制·负载均衡实现·故障转移实现·多级降级
麦麦大数据1 小时前
M004_基于Langchain+RAG的银行智能客服系统设计与开发
typescript·langchain·flask·vue3·faiss·rag
敲代码兔斯基3 小时前
从零搭建你的第一个 AI Agent:用 LangChain + OpenAI 实现智能助手
langchain
XLYcmy3 小时前
chatgpt数据库检索文献 上
ai·chatgpt·llm·prompt·检索·gpt-4o·doi
Emotional。5 小时前
AI Agent 开发实战:用 LangChain 构建智能邮件助手
linux·服务器·网络·人工智能·python·langchain
lhxcc_fly7 小时前
1.LangChain--安装篇
langchain·安装包
SmartBrain7 小时前
技术洞察:SpringAI与LangGraph选型对比
人工智能·spring boot·架构·langchain·aigc·fastapi
南_山无梅落8 小时前
从LangChain到LangGraph:构建智能Agent的实战指南(四)——核心知识体系全景解析
langchain
lhxcc_fly8 小时前
1.LangChain--初识篇
ai·langchain·vibe coding
天一生水water9 小时前
LangChain的智能体教程
开发语言·人工智能·langchain·php·智慧油田