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的结构体内容
相关推荐
枫叶丹41 小时前
知识库为什么搜不到答案:多向量检索与 Late Interaction
人工智能·chatgpt·开源·agent·codex
BD_Marathon2 小时前
使用@tool装饰器定义工具:args_schema
langchain
BD_Marathon15 小时前
工具的定义方式2:使用@tool装饰器(推荐)
langchain
不会是要长脑子了吧15 小时前
ChatGPT 网页端报 “Model not found“
人工智能·chatgpt
史努比的蓝色皮肤15 小时前
从 ChatGPT 到 Agent :AI 发展脉络
人工智能·chatgpt
面包狗AI4S15 小时前
ChatGPT/Codex Windows:本地桌面端应用离线包安装与免安装启动方法(无需管理员权限)
人工智能·windows·ai·chatgpt
爱泡脚的鸡腿15 小时前
langchain学习-D1-了解langchain家族与rag
langchain
读书读傻了哟16 小时前
langfuse部署相关
langchain·language model·langfuse
小易老师AI实战18 小时前
LangChain + MCP 协议:Agent 接入联网搜索(Tavily MCP)完整实践
langchain·agent·ai智能体·大模型应用·mcp·联网搜索
单向箔19 小时前
10|Skill:把重复 prompt 固化成自己的 AI 工作流
人工智能·prompt·ai编程