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的结构体内容
相关推荐
一缕82年的清风8 小时前
OpenAI 突发 GPT-6.2:Sol 提速 8 倍跑分追平旗舰,但这 1 处暗改会让缓存直接失效!
gpt·chatgpt
死磕音视频20 小时前
Businesses in ChatGPT 入门:在 Business Hub 认领商家并完成验证
chatgpt·入门教程·business hub·商家资料
泯泷21 小时前
OpenClaw 退潮背后,是 AI 圈“技术自嗨”的终结
人工智能·chatgpt·llm
泯泷1 天前
当"孤独的开发者"成为常态,AI正在悄悄杀死那个我们熟悉的互联网
人工智能·chatgpt·llm
仍然.1 天前
LangChain核心组件---消息
langchain
xyphf_和派孔明1 天前
第十章: Agent Skill的调用机制解读
langchain·llm·agent
掘黄金的打工人1 天前
LangChain 少样本示例 和 动态示例选择
langchain·aigc·ai编程
kevin_org1 天前
LangChain入门学习(四)
python·学习·langchain·fastapi
IvanCodes2 天前
LangChain(04):Runnable 与 LCEL 的执行与组合机制
python·langchain·agent
Cosolar2 天前
从 Model 到 System:AI Agent Infra 工程师的技术全景与学习路径
langchain·aigc·agent