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的结构体内容
相关推荐
java资料站16 小时前
LangChain 中文入门教程
langchain
薛定猫AI16 小时前
【深度解析】Claude Auto Dream:从“短期对话”到“项目级心智模型”的记忆系统升级
人工智能·chatgpt
QWsin18 小时前
【LangGraph Server】 LangGraph Server是什么?
人工智能·langchain·langgraph·langsmith
米小虾19 小时前
从对话到行动:AI Agent 架构演进与工程实践指南
人工智能·langchain·agent
树谷-胡老师19 小时前
基于AI工具(ChatGPT、OpenClaw等)工作流的高强度论文写作实战
人工智能·chatgpt
Csvn19 小时前
🌟 LangChain 30 天保姆级教程 · Day 4|用 LLMChain 把 Prompt 和 LLM 打包成可复用组件!
langchain
coderlin_21 小时前
langchain 基础
microsoft·langchain
QC·Rex21 小时前
国产大模型应用实践:从 0 到 1 搭建企业级 AI 助手
人工智能·langchain·大语言模型·rag·企业应用·ai 助手
轻舟行71 天前
langchain从入门到入土 (一)langchain的历程及应用场景
人工智能·python·langchain
m0_488913011 天前
收藏备用!大厂AI Agent开发岗位解析+小白友好学习路线(程序员必看)
人工智能·学习·langchain·大模型·agent·就业·大模型学习