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的结构体内容
相关推荐
@不误正业4 小时前
从LangChain到OpenClaw:AI Agent框架选型指南(性能对比+源码分析)
人工智能·langchain
七夜zippoe5 小时前
AI Agent 框架选型指南:OpenClaw、LangChain、AutoGPT、CrewAI 深度对比
人工智能·langchain·autogpt·crewai·openclaw
老刘说AI5 小时前
浅聊Prompt、向量知识库、RAG
人工智能·prompt
智算菩萨8 小时前
ChatGPT 5.4 API深度解析:从Transformer架构到企业级应用实践
人工智能·深度学习·ai·chatgpt·ai编程
此生只爱蛋8 小时前
【LangChain】少样本提示(few-shotting)
langchain
睡醒了叭8 小时前
langChain-介绍、安装与测试
人工智能·langchain·大模型开发应用
java1234_小锋9 小时前
基于LangChain的RAG与Agent智能体开发 - OpenAI库介绍和使用
langchain·rag
智算菩萨9 小时前
AI赋能游戏创作:基于ChatGPT 5.4的小游戏智能设计与开发全解析
人工智能·python·ai·chatgpt·ai编程
Tzarevich9 小时前
Agent记忆模块:让大模型“记住”你,还能省Token!
后端·langchain·agent
汀沿河10 小时前
2 LangChain 1.0 中间件(Middleware)- wrap_model_call、wrap_tool_call
spring·中间件·langchain