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的结构体内容
相关推荐
进阶的小名16 小时前
Spring AI 2.0 探索:多 OpenAI-Compatible 模型接入,以及下一代 Session 记忆管理
java·人工智能·后端·gpt·spring·ai·chatgpt
玫瑰互动GEO17 小时前
海外GEO优化案例-ChatGPT搜索关键词排名GEO优化案例详解(含RAG机制与Tokenization技术拆解)
人工智能·ai·chatgpt·geo优化
AI模型调用笔记18 小时前
GPT-5.4 8月31日退出 Codex?先分清 ChatGPT 登录与 API Key,再迁移 Terra/Luna
人工智能·gpt·chatgpt·ai编程
淼澄研学21 小时前
Python实战:基于LangChain与Chroma构建企业级RAG知识库问答系统
开发语言·python·langchain
xierui12312321 小时前
AI创作工作流状态管理:为什么保存 Prompt仍然无法复现作品
人工智能·自动化·prompt·aigc·软件工程
minji...1 天前
LangChain-AI应用开发框架(2) - LLM 大语言模型的三种接入方式 : API 远程调用、开源模型本地部署和 SDK 接入
langchain
AIHR数智引擎1 天前
如何用WorkBuddy跑通HR自动化流程?
人工智能·经验分享·chatgpt·职场和发展·自动化·ai-native
喜欢吃豆1 天前
GEO 到底怎么做?从 Prompt 研究到 AI Citation 的完整落地方法
人工智能·prompt
yingyuecom1 天前
映悦AI × Joverse全球发布:四大重磅更新,AI创作进入工业化时代
人工智能·gpt·chatgpt·prompt·aigc
wujian83111 天前
【 文心手机版生成的表格怎么复制下来 】?这个问题,[ AI 导出鸭 ] 给出了完全不同的答案
人工智能·ai·chatgpt·智能手机·ai导出鸭