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的结构体内容
相关推荐
全栈弄潮儿1 小时前
AI 编程进阶实战:我为什么要做这个专栏
chatgpt·openai·ai编程
嘿嘿-662 小时前
gpt-6-astra测试,测试你的模型有没有降智
人工智能·gpt·chatgpt·web
大模型码小白3 小时前
ChatGPT 的回答也能被结构化抓取?AI Bot Scraper 对比测评
服务器·开发语言·数据库·人工智能·spring·chatgpt
阡陌数智4 小时前
多模型兼容Prompt工程体系:统一结构化指令如何提升全模型输出稳定性
prompt
夫子3965 小时前
【第三部分:第一个 Agent 应用】12. 使用状态机开发一个可控 Agent
langchain·llm·agent
顿哥GPT5 小时前
ChatGPT Plus / Pro + Codex 深度实战指南(2026年9月5日):从模型能力对比到 Codex 自动化编程工作流全解析
人工智能·chatgpt·自动化
旖旎夜光5 小时前
【LangChain实战】LangChain 学习笔记(二):结构化输出、流式传输与消息管理
人工智能·笔记·python·学习·ai·langchain
AI 思录6 小时前
Prompt 事故档案(五):AI 道歉,还是“道德漂白”?
人工智能·安全·prompt·用户体验·ai安全·ai幻觉
天衍四九-6 小时前
Agent Skills从入门到工程化(十六):面试中如何讲清楚 Agent Skills?
大数据·数据库·人工智能·python·chatgpt·面试
Maiko Star7 小时前
[LangChain Agent] 03 ReAct 框架:思考→行动→观察
langchain