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的结构体内容
相关推荐
Logintern092 小时前
deep agents 多智能体模式——规划与委派
python·langchain
hqk5 小时前
AI Agent 学习总结(二):上下文、记忆与工具
langchain·aigc·agent
Jame哥7 小时前
Dify / LangChain / LlamaIndex 三者定位差异:一张表看清该用哪个
langchain·dify·llamaindex·llm应用
叼烟扛炮7 小时前
[LangChain] 学习笔记(026):模型根本没有记忆——「多轮对话」是你每次把历史重发一遍
笔记·学习·langchain
zc.z9 小时前
提示词工程(十一):深入理解Planning Prompt底层原理,从任务拆解到执行计划
人工智能·prompt·提示词工程·prompt planning
tong_li12 小时前
给娃做个 AI 陪写作业助手:LangChain4j 入门(七):给工具循环上保险
后端·langchain
2601_9679791213 小时前
OpenAI创始成员发布开源项目nanochat,仅需100美元就能实现类ChatGPT
chatgpt·openai·llm训练·nanochat·安德烈·卡帕西
En^_^Joy14 小时前
LangChain消息系统全解析:从基础到实战(四)
数据库·microsoft·langchain
ndz19 小时前
一把 Key 接多家模型,再查 AI 会不会推荐你的品牌
chatgpt
深海鱼肝油ya1 天前
LangChain(一)概念&初识
langchain·大模型·llm·agent·思维链·智能体