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的结构体内容
相关推荐
VIP_CQCRE2 小时前
用 Ace Data Cloud 快速接入 OpenAI Chat Completion API:从基础调用到多模态应用
ai·chatgpt·openai·api·acedatacloud
To_OC2 小时前
手搓 LangChain 工具调用:原来 Agent 的核心逻辑,就是个 while 循环
人工智能·langchain·llm
承受失4 小时前
Cloud Agent 开发笔记(4):Skill 与 MCP 集成、项目后记
人工智能·prompt
daphne odera�6 小时前
PyCharm 中 Codex 插件启动失败:unknown variant default 的解决方法
python·chatgpt·pycharm
一次旅行9 小时前
AI 前沿日报 | 2026年7月10日
人工智能·chatgpt
Boop_wu9 小时前
[LangChain]LangChain聊天模型
langchain
米小虾9 小时前
GPT-5.6一小时破解50年数学猜想!OpenAI公开700词Agent编排密文
人工智能·chatgpt
前端_逍遥生9 小时前
一文搞懂 GPT-5.6,Sol、Terra和Luna有什么区别?
chatgpt
常常有10 小时前
LangChain 学习记录 01:为什么需要 LangChain?从 API 调用到 AI 应用框架
人工智能·langchain
2601_9549165911 小时前
ChatGPT 5.6 深度实践:用 AI 重构云成本治理体系,而不是简单“降本”
人工智能·sql·chatgpt·重构·测试用例