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的结构体内容
相关推荐
大志说编程1 小时前
LangChain框架入门16:智能客服系统RAG应用实战
后端·langchain·aigc
lisuwen1161 小时前
AI三国杀:马斯克炮轰苹果“偏袒”OpenAI,Grok与ChatGPT的应用商店战争揭秘
人工智能·chatgpt
暮小暮1 小时前
从ChatGPT到智能助手:Agent智能体如何颠覆AI应用
人工智能·深度学习·神经网络·ai·语言模型·chatgpt
John_ToDebug2 小时前
大模型提示词(Prompt)终极指南:从原理到实战,让AI输出质量提升300%
人工智能·chatgpt·prompt
猫头虎5 小时前
猫头虎AI分享|一款Coze、Dify类开源AI应用超级智能体快速构建工具:FastbuildAI
人工智能·开源·prompt·github·aigc·ai编程·ai-native
AI大模型6 小时前
从零开始,亲手开发你的第一个AI大模型(一)基础知识
程序员·langchain·agent
哪吒编程6 小时前
谁是最强编程大模型?横向对比Claude Opus 4.1、GPT-5、Gemini 2.5 Pro、Grok4
chatgpt·openai·claude
网络研究院9 小时前
OpenAI 的浏览器将使用 ChatGPT Agent 来控制浏览器
chatgpt
sinat_2869451917 小时前
AI应用安全 - Prompt注入攻击
人工智能·安全·prompt
致Great1 天前
DeepResearch开源与闭源方案对比
人工智能·chatgpt