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的结构体内容
相关推荐
勇往直前plus11 分钟前
大模型开发手记(二):基于 LangChain 的 RAG 架构全面解析与落地实践
架构·langchain
Familyism14 分钟前
Prompt概述
prompt
加加今天也要加油20 分钟前
Oinone × AI Agent 落地指南:元数据即 Prompt、BPM 状态机护栏、SAGA 补偿、GenUI
人工智能·低代码·prompt
老蒋每日coding42 分钟前
LangChain 实战系列(一) —— 在 Windows 上安装 LangChain 的详细步骤
langchain
问道飞鱼1 小时前
【大模型学习】提示词工程(Prompt Engineering)技术深度报告
学习·prompt·提示词
哆啦code梦1 小时前
Prompt攻防实战演练
prompt·prompt安全
njsgcs2 小时前
langchain框架怎么让 Skills 与 MCP(tools)协同使用
人工智能·langchain·skills
LaughingZhu3 小时前
Product Hunt 每日热榜 | 2026-01-28
数据库·经验分享·神经网络·搜索引擎·chatgpt
哆啦code梦3 小时前
浅谈Prompt攻击与防御
prompt·大模型安全·prompt安全
梁下轻语的秋缘16 小时前
Prompt工程核心指南:从入门到精通,让AI精准响应你的需求
大数据·人工智能·prompt