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的结构体内容
相关推荐
杨艺韬11 分钟前
为什么需要理解 LangChain
langchain·agent
坤岭26 分钟前
大模型“入侵”广告推荐
人工智能·langchain·推荐算法
deephub1 小时前
Prompt、Context、Harness:AI Agent 工程的三层架构解析
人工智能·prompt·大语言模型·context
嵌入式-小王2 小时前
LangChain框架(二)---- 提示词模板
python·langchain
zhangshuang-peta2 小时前
MCP 与 Prompt Engineering:协议出现后,Prompt 还重要吗?
人工智能·prompt·ai agent·mcp·peta
来一斤小鲜肉2 小时前
一文搞懂:如何用 Spring AI 搭建 MCP Server 和 Client
后端·langchain
小真zzz3 小时前
搜极星:你的免费“AI内容验真器”
大数据·人工智能·ai·chatgpt·seo·geo
White-Legend3 小时前
codex gpt-5.4 日卡200刀
chatgpt
Agent产品评测局4 小时前
企业 IT 运维自动化落地,故障处理全流程实现方法:2026智能体驱动下的运维架构演进与实战解析
运维·人工智能·ai·chatgpt·架构·自动化
bKYP953cL4 小时前
Prompt 缓存的四种策略:从精确匹配到语义检索
spring·缓存·prompt