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的结构体内容
相关推荐
亓才孓14 小时前
【提示词五要素】
python·ai·prompt
LaughingZhu14 小时前
Product Hunt 每日热榜 | 2026-03-28
数据库·人工智能·经验分享·神经网络·chatgpt
Jay叶湘伦16 小时前
【极简】用 Vue 写一个 ChatGPT 前端应用,支持连续对话、Markdown 渲染与本地记忆
前端·vue.js·chatgpt
balmtv16 小时前
从“知识检索”到“深度推理”:Gemini 3.1如何用三层思考模式解决学术难题
人工智能·gpt·chatgpt
怕浪猫17 小时前
第6章 链(Chains):构建可组合的工作流
langchain·llm·ai编程
斌味代码17 小时前
RAG 实战:用 LangChain + DeepSeek 搭建企业私有知识库问答系统
开发语言·langchain·c#
风吹心凉18 小时前
AI Agent、MCP、Prompt、Function Calling
人工智能·prompt
JavaGuide18 小时前
万字拆解 LLM 运行机制:Token、上下文与采样参数
ai·llm·prompt·ai编程·token
偷光19 小时前
大模型核心技术概述:Token、Prompt、Tool与Agent的关系详解
前端·ai·prompt·ai编程
java1234_小锋20 小时前
基于LangChain的RAG与Agent智能体开发 - 文档分割器
langchain·rag