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的结构体内容
相关推荐
吴法刚3 小时前
14-Hugging Face 模型微调训练(基于 BERT 的中文评价情感分析(二分类))
人工智能·深度学习·自然语言处理·分类·langchain·bert·langgraph
技术程序猿华锋4 小时前
Zotero PDF Translate 翻译插件使用OpenAI API配置教程
人工智能·chatgpt·机器翻译
明月落乌江6 小时前
Prompt攻击
prompt
量子位9 小时前
ChatGPT 有了完整记忆!像朋友一样记住所有聊天记录,回复更加私人订制
人工智能·chatgpt
Lonwayne9 小时前
为什么ChatGPT选择SSE而非WebSocket?
websocket·网络协议·chatgpt·程序那些事
进击的松鼠9 小时前
Next.js + LangChain.js + DeepSeek 实现一个 AI 智能助手
langchain·next.js·deepseek
Justin3go10 小时前
GPT4o生图风格大全
chatgpt·openai·midjourney
玲小珑15 小时前
LLMOps开发(三) RAG
langchain·node.js·ai编程
福宝plus16 小时前
如何白嫖Grok3 API?Grok3 API价格? 如何使用Grok3 API调用实例?怎么使用Grok3模型?
chatgpt·claude·grok
小尹呀17 小时前
LangChain-提示模板 (Prompt Templates)
搜索引擎·langchain·prompt