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的结构体内容
相关推荐
YuanDaima20481 小时前
基于 LangChain 1.0 的检索增强生成(RAG)实战
人工智能·笔记·python·langchain·个人开发·langgraph
qq_5470261793 小时前
LangChain 工具调用(Tool Calling)
java·大数据·langchain
Flying pigs~~4 小时前
Prompt 工程实战总结:文本分类、信息抽取、语义匹配
人工智能·自然语言处理·prompt·文本分类·大模型应用
AI大模型..4 小时前
数据洞察加速器:LLM Copilot 如何让 SQL 查询效率提升 50% 以上?
人工智能·langchain·llm·agent·llama
汀沿河7 小时前
6 LangGraph的stream流式输出
langchain
羊小猪~~9 小时前
LLM--微调(Adapters,Prompt,Prefix)
算法·ai·大模型·llm·prompt·adapters·prefix
Agent产品评测局10 小时前
汽车行业智能自动化平台选型,生产与供应链全优化:2026企业级智能体(Agent)实测与架构解析
java·人工智能·ai·chatgpt·架构·自动化
m0_7381207212 小时前
AI安全——Gandalf靶场 Tongue Tied Gandalf 全关卡绕过详解
服务器·网络·人工智能·安全·web安全·prompt
平凡灵感码头12 小时前
我自己的主力claude
人工智能·chatgpt
Flying pigs~~12 小时前
主流大模型介绍(GPT、Llama、ChatGLM、Qwen、deepseek)
gpt·chatgpt·llm·llama·moe·deepseek·混合专家模式