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的结构体内容
相关推荐
l1258655 分钟前
# RAG上线评估指标体系:六大核心指标与压测实战全解析
数据库·人工智能·python·mysql·langchain·milvus
不灭的程序员阿澄1 小时前
把多个 AI 网站装进一个常驻托盘的桌面窗口里,像切换原生 App 一样切换 AI
人工智能·chatgpt
精彩AI说2 小时前
ChatGPT生成的内容太长怎么办?控制字数、精简回答与输出长度设置方法
chatgpt·ai写作·提示词·ai工具·chatgpt教程·内容精简
Linguwen3 小时前
中山GEO分享会回顾文
人工智能·chatgpt
孤狼GPT20 小时前
ChatGPT、Codex实战:为什么AI改代码越来越快,但你越来越不敢直接合并?
chatgpt·codex·ai agent·chatgpt plus·chatgpt pro·ai coding
用户38845671995420 小时前
LangChain 源码解析:HumanInTheLoopMiddleware 如何实现 Agent 人机协同
langchain
l12586521 小时前
# RAG向量数据库优化实战:HNSW索引调参与生产级性能设计
数据库·python·mysql·langchain
松小鼠呀1 天前
我不敢再生孩子:ChatGPT 曾花 $2m 让他闭嘴,AI 巨头到底隐藏了什么秘密?
人工智能·安全·chatgpt·ai编程·科技热点
l1258651 天前
# RAG噪声知识库治理:一致性与可信度的四层防线设计
数据库·人工智能·python·自然语言处理·langchain
二进制_博客1 天前
rag面试题之rag 系统如何解决 llm 的幻觉问题
langchain·rag