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的结构体内容
相关推荐
维维180-3121-14551 小时前
AI赋能生态学暨“ChatGPT+”多技术融合在生态系统服务中的实践技术应用与论文撰写
人工智能·chatgpt
♡喜欢做梦2 小时前
Deepseek 与 ChatGPT:AI 浪潮中的双子星较量
ai·chatgpt·deepseek
Orange--Lin4 小时前
【用deepseek和chatgpt做算法竞赛】——还得DeepSeek来 -Minimum Cost Trees_5
人工智能·算法·chatgpt
技术程序猿华锋6 小时前
OpenAI 周活用户破 4 亿,GPT-4.5 或下周发布,微软加紧扩容服务器
microsoft·chatgpt·deepseek
背太阳的牧羊人8 小时前
RAG检索中使用一个 长上下文重排序器(Long Context Reorder) 对检索到的文档进行进一步的处理和排序,优化输出顺序
开发语言·人工智能·python·langchain·rag
Java知识技术分享8 小时前
使用LangChain构建第一个ReAct Agent
python·react.js·ai·语言模型·langchain
南 阳19 小时前
百度搜索全面接入DeepSeek-R1满血版:AI与搜索的全新融合
人工智能·chatgpt
workflower20 小时前
Prompt Engineering的重要性
大数据·人工智能·设计模式·prompt·软件工程·需求分析·ai编程
forestsea1 天前
DeepSeek 提示词:定义、作用、分类与设计原则
人工智能·prompt·deepseek
播播资源1 天前
ChatGPT付费创作系统V3.1.3独立版 WEB端+H5端+小程序端 (DeepSeek高级通道+推理输出格式)安装教程
前端·ai·chatgpt·ai作画·小程序·deepseek·deepseek-v3