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的结构体内容
相关推荐
BU摆烂会噶20 小时前
【LangGraph】持久化实现的三大能力——时间旅行
数据库·人工智能·python·postgresql·langchain
风停又起21 小时前
从零实现一个最小版 LangChain
langchain
幸福巡礼1 天前
【LangChain 1.2 实战(一)】 概述
笔记·学习·langchain
茉莉玫瑰花茶1 天前
LangChain 核心组件 [ 4 ]
langchain
戋风1 天前
从源码到实战:LangChain4j 1.14 完整学习指南(14 课全解)
langchain
老陈说编程1 天前
12. LangChain 6大核心调用方法:invoke/stream/batch同步异步全解析,新手也能轻松学会
开发语言·人工智能·python·深度学习·机器学习·ai·langchain
月诸清酒1 天前
51-260503 AI 科技日报 (ChatGPT图像功能用户量暴涨,新用户占六成)
人工智能·chatgpt
网络工程小王1 天前
【LangChain Output Parser 输出解析器】输出篇
人工智能·学习·langchain
Ally4411 天前
LLM,Token,Context,Prompt,MCP,Agent等概念
prompt
qcx231 天前
【AI Agent通识九课】01 · Agent 和 ChatGPT 到底差在哪?
人工智能·ai·chatgpt·agent