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的结构体内容
相关推荐
AskHarries15 小时前
Agent Prompt 是怎么影响行为的
prompt
索西引擎16 小时前
【LangChain 1.0】 接入 Ollama:在本地跑通 DeepSeek-R1 的完整指南
langchain
rannn_11116 小时前
OpenAI Function Calling 全解析:从函数定义到流式调用
人工智能·chatgpt·openai·ai agent
龙骑士baby17 小时前
重建 AI 认知第 3 篇:Prompt Engineering——怎么让 AI 听懂你的话
ai·大模型·llm·prompt
城管不管18 小时前
什么是Prompt?
android·java·数据库·语言模型·llm·prompt
醒醒该学习了!18 小时前
Prompt提示词——风险和误用、对抗性prompt(理论篇)
prompt
染指111018 小时前
12.LangChain框架4-输出解释器
人工智能·langchain·rag
战族狼魂20 小时前
Claude 大模型在真实业务场景中的落地应用指南
人工智能·chatgpt·大模型
索西引擎20 小时前
【LangChain 1.0】环境搭建指南:从 conda 到 uv 的现代化 Python 工程实践
python·langchain·conda
云姜.21 小时前
Langchain快速上手编程-Runnable 与 LCEL
java·开发语言·langchain