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 1.3 完全教程:从入门到精通-Part 7: Documents(文档处理)
java·python·langchain
deephub19 分钟前
Prompt Engineering 的本质:角色、任务、上下文、格式、约束
人工智能·prompt·大语言模型·多智能体
FelixBitSoul21 分钟前
面试必考!RAG 知识库全链路深度解析:父子分块 × Rerank × 查询重写 × 标准化改写
人工智能·langchain·aigc
兆。44 分钟前
LangChain自动化工具集成指南:面向爬虫开发者
爬虫·langchain·自动化
LaughingZhu1 小时前
Product Hunt 每日热榜 | 2026-05-31
前端·人工智能·经验分享·搜索引擎·chatgpt·html
兆。1 小时前
LangChain语音音频集成指南:面向多媒体开发者
langchain·音视频
sleven fung1 小时前
GPT4All 本地大语言模型运行环境介绍
python·gpt·ai·langchain
abigale031 小时前
LangChain 实践4: 7个人AI助手全栈项目:完整拆解+分阶段开发指南
缓存·langchain·prompt·token·rag·lcel
叶修_A2 小时前
【COZE-08】Prompt工程进阶 - 结构化输出与思维链
大数据·人工智能·prompt
兆。2 小时前
Ollama_Moondream轻量级多模态模型实操教程
langchain·openai·moondream