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的结构体内容
相关推荐
CCPC不拿奖不改名11 分钟前
RAG基础:基于LangChain 的文本分割实战+文本分块
人工智能·python·langchain·知识库·改行学it·rag·向量库
gentle coder1 小时前
【langchain】AI应用开发框架
langchain·llm·rag
OPEN-Source3 小时前
大模型实战:把 LangChain / LlamaIndex 工作流接入监控与告警体系
人工智能·langchain·企业微信·rag
重生之我要成为代码大佬4 小时前
LangChain-多任务应用开发
langchain·大模型·agent
doll ~CJ5 小时前
Large Language Model(LLM)应用开发学习实践(三)
langchain·llm·提示词工程·ai应用
玄同7656 小时前
LangChain v1.0+ 与 FastAPI 中间件深度解析:从概念到实战
人工智能·中间件·langchain·知识图谱·fastapi·知识库·rag
熙客6 小时前
Claude 与 Claude Code 介绍、安装与使用
chatgpt
石去皿6 小时前
Depth Viewer: 16-bit 深度图可视化工具
人工智能·chatgpt·prompt
猫头虎6 小时前
2026全网最热Claude Skills工具箱,GitHub上最受欢迎的7大Skills开源AI技能库
langchain·开源·prompt·github·aigc·ai编程·agi
朱元禄7 小时前
AI Agent 实战课程 之 《RAG 闭环实操:RAG 闭环实操(讲师逐字稿)DeepSeek + LangChain》
人工智能·langchain