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的结构体内容
相关推荐
小陈phd16 小时前
langchain从入门到精通(四十一)——基于ReACT架构的Agent智能体设计与实现
react.js·架构·langchain
聚客AI16 小时前
🎯 RAG系统工业级部署指南:六步实现<3%幻觉率的问答系统
人工智能·langchain·llm
陈敬雷-充电了么-CEO兼CTO21 小时前
主流大模型Agent框架 AutoGPT详解
人工智能·python·gpt·ai·chatgpt·nlp·aigc
悟乙己1 天前
通过Prompt生成互动式网页HTML案例探索(二)
prompt·秘塔·互动式网页
风雨中的小七1 天前
解密prompt系列56.Agent context Engineering - 单智能体代码剖析
prompt
redreamSo1 天前
AI 绘图效率神器:Mermaid Previewer Sidebar Chrome 插件
chrome·langchain·aigc
cooldream20092 天前
理解大模型智能体生态:从 Prompt 到 Agent 的完整信息流解析
prompt·ai agent·mcp·fuction calling
代码能跑就行管它可读性2 天前
【论文复现】利用生成式AI进行选股和分配权重
人工智能·chatgpt
前端小盆友2 天前
从零实现一个GPT 【React + Express】--- 【4】实现文生图的功能
react.js·chatgpt·express
聚客AI2 天前
⚡ 突破LLM三大局限:LangChain架构核心解析与最佳实践
人工智能·langchain·llm