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的结构体内容
相关推荐
花千树-01015 小时前
Proposer-Critic 多轮辩论:两个 LLM Agent 用 loop() 逼近共识
langchain·agent·ai编程·skill·multi-agent·claude code·ai 工程化
云天AI实战派15 小时前
ChatGPT/API 调用故障排查指南:Realtime 音频、智能体浏览器操作与 AI 编码代理全流程修复手册
人工智能·chatgpt·音视频
厚国兄16 小时前
Agent 工程化系列 · 第 08 篇_Skills是什么和Prompt有什么区别
人工智能·prompt·agent
Daydream.V16 小时前
从零搭建 AI Agent:LLM Agent+Function Calling+Dify 本地部署 + Coze 实战全攻略
人工智能·langchain·ollama·functioncalling·大模型部署
CODE2022031817 小时前
promptfoo自定义prompt生成器
java·前端·prompt
shchojj17 小时前
What is Generative AI -Welcome
人工智能·chatgpt
2601_9585990417 小时前
ChatGPT 全能力解析:从核心功能到多模型协同,解锁 AI 生产力天花板
人工智能·chatgpt
Lyon1985052818 小时前
《文字定律》随笔-AI们聊“艺术”-Deepseek、Grok、ChatGPT、Geminni
人工智能·chatgpt·ai写作·grok·gemini·deepseek
大连好光景18 小时前
论文润色/降重的热门skills+Prompt
prompt
JaydenAI18 小时前
[Deep Agents:LangChain的Agent Harness-06]通过HumanInTheLoopMiddleware引入人机交互
langchain·人机交互·hitl·deep agents·harness