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的结构体内容
相关推荐
Mac的实验室12 分钟前
『保姆级教程』2026 最新 ChatGPT 注册教程来了!无需手机号,三分钟搞定!
chatgpt
ty_sj1 小时前
AI验证prompt
prompt
Barkamin3 小时前
LangChain简单介绍
langchain
百年੭ ᐕ)੭*⁾⁾6 小时前
Chroma简单上手
人工智能·语言模型·langchain·chroma·rag
极客老王说Agent6 小时前
适合IT运维人员进行服务器监控和故障预警的Agent有哪些?2026智能运维全攻略
运维·服务器·人工智能·ai·chatgpt
LaughingZhu7 小时前
Product Hunt 每日热榜 | 2026-03-29
数据库·人工智能·经验分享·神经网络·chatgpt
我是胡杨学长7 小时前
ChatGPT 连续三月流量下滑,是热度凉了还是 AI 泡沫要来了?
人工智能·ai·chatgpt
华农DrLai8 小时前
什么是远程监督?怎么自动生成训练数据?
人工智能·算法·llm·prompt·知识图谱
Roselind_Yi8 小时前
【吴恩达2026 Agentic AI】面试向+项目实战(含面试题+项目案例)-2
人工智能·python·机器学习·面试·职场和发展·langchain·agent
JaydenAI8 小时前
[RAG在LangChain中的实现-04]常用的向量存储和基于向量存储的检索器
python·langchain·ai编程