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的结构体内容
相关推荐
chQHk57BN25 分钟前
解密Prompt系列69. 从上下文管理到Runtime操作系统
prompt
北邮刘老师2 小时前
暗数据:智能体探索世界的下一步
人工智能·大模型·prompt·智能体·智能体互联网
Agent产品评测局3 小时前
企业发票管理自动化落地,验真归档全流程实现方法:2026企业级智能体选型与实测指南
运维·网络·人工智能·ai·chatgpt·自动化
Flying pigs~~3 小时前
从“踩坑”到“可控”:大模型 Prompt 工程实战总结与进阶方法论
大数据·人工智能·大模型·prompt·提示词工程
SQVIoMPLe5 小时前
[拆解LangChain执行引擎]以Actor模型的视角来看Pregel
服务器·数据库·langchain
花千树-0106 小时前
Java AI + TTS:让大模型开口说话
java·人工智能·ai·chatgpt·langchain·aigc·ai编程
蜡笔小马6 小时前
01.[特殊字符] 构建你的第一个 AI 智能体:从 DeepSeek 到结构化对话
人工智能·python·langchain
前端达人7 小时前
第09课:10个高频场景 Prompt 模板库,复制、改几个词、直接用
prompt
InKomorebi7 小时前
LangChain + LangGraph Agent 完全指南:流式传输、结构化输出、提示词缓存
langchain
最初的↘那颗心8 小时前
结构化Prompt与Meta Prompt实战——让AI输出你想要的格式
大模型·prompt·spring ai·结构化输出·meta prompt