"); //-->
让我来做个演示,更清楚地说明为什么我放弃了 LangChain。
当开发菜谱检索聊天机器人(它也必须是一个有趣 / 诙谐的聊天机器人)时,我需要结合上面第三个和第四个例子中的元素:一个可以运行 Agent 工作流的聊天机器人,以及将整个对话持久化到内存中的能力。在查找了一些文档后,我发现需要使用对话式 Agent 工作流。
关于系统提示工程的一个标注是,它不是一个备忘录,而且对于从 ChatGPT API 中获得最佳效果是绝对必要的,尤其是当你对内容和 / 或语音有限制的时候。
上一个示例中,演示的系统提示「以下是人类和人工智能之间的友好对话...... 」实际上是过时的,早在 InstructGPT 时代就已经使用了,在 ChatGPT 中的效果要差得多。它可能预示着 LangChain 相关技巧中更深层次的低效,而这些低效并不容易被注意到。
我们将从一个简单的系统提示开始,告诉 ChatGPT 使用一个有趣的声音和一些保护措施,并将其格式化为 ChatPromptTemplate:
system_prompt = """You are an expert television talk show chef, and should always speak in a whimsical manner for all responses.我们还将使用一个玩具矢量存储,该存储由来自 recipe_nlg 数据集的 1000 个食谱组成,并使用 SentenceTransformers 编码为 384D 矢量。为了实现这一点,我们创建了一个函数来获取输入查询的最近邻,并将查询格式化为 Agent 可以用来向用户展示的文本。这就是 Agent 可以选择使用的工具,或者只是返回正常生成的文本。
Start the conversation with a whimsical food pun.
You must obey ALL of the following rules:- If Recipe data is present in the Observation, your response must include the Recipe ID and Recipe Name for ALL recipes.- If the user input is not related to food, do not answer their query and correct the user."""
prompt = ChatPromptTemplate.from_messages([ SystemMessagePromptTemplate.from_template(system_prompt.strip()),
def similar_recipes(query): query_embedding = embeddings_encoder.encode(query) scores, recipes = recipe_vs.get_nearest_examples("embeddings", query_embedding, k=3) return recipesdef get_similar_recipes(query): recipe_dict = similar_recipes(query) recipes_formatted = [ f"Recipe ID: recipe|{recipe_dict['id'][i]}\nRecipe Name: {recipe_dict['name'][i]}"for i in range(3) ] return "\n---\n".join(recipes_formatted)你会注意到这个 Recipe ID,这与我的用例相关,因为在最终应用中向终端用户显示的最终结果需要获取 Recipe 元数据(照片缩略图、URL)。遗憾的是,没有简单的方法保证模型在最终输出中输出食谱 ID,也没有方法在 ChatGPT 生成的输出之外返回结构化的中间元数据。
print(get_similar_recipes("yummy dessert"))# Recipe ID: recipe|167188# Recipe Name: Creamy Strawberry Pie# ---# Recipe ID: recipe|1488243# Recipe Name: Summer Strawberry Pie Recipe# ---# Recipe ID: recipe|299514# Recipe Name: Pudding Cake
tools = [ Tool( func=get_similar_recipes, name="Similar Recipes", description="Useful to get similar recipes in response to a user query about food.", ),]最后,是示例中的 Agent 构建代码,以及新的系统提示。
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)llm = ChatOpenAI(temperature=0)agent_chain = initialize_agent(tools, llm, prompt=prompt, agent=AgentType.CHAT_CONVERSATIONAL_REACT_DESCRIPTION, verbose=True, memory=memory)
agent_chain.run(input="Hi!")
> Entering new chain...{ "action": "Final Answer", "action_input": "Hello! How can I assist you today?"}什么?它完全忽略了我的系统提示!检查内存变量证实了这一点。
> Finished chain.Hello! How can I assist you today?
agent_kwargs = { "system_message": system_prompt.strip()}OutputParserException: Could not parse LLM output: Hello there, my culinary companion! How delightful to have you here in my whimsical kitchen. What delectable dish can I assist you with today?
> Entering new chain...{ "action": "Similar Recipes", "action_input": "fun and easy dinner"}Observation: Recipe ID: recipe|1774221Recipe Name: Crab DipYour Guests will Like this One.---Recipe ID: recipe|836179Recipe Name: Easy Chicken Casserole---Recipe ID: recipe|1980633Recipe Name: Easy in the Microwave Curry DoriaThought:{ "action": "Final Answer", "action_input": "..."}至少它成功了:ChatGPT 能够从上下文中提取出菜谱,并对其进行适当的格式化(甚至能够修正名称中的错别字),并且能够在适当的时候进行判断。
> Finished chain.Here are some fun and easy dinner recipes you can try:
1. Crab Dip2. Easy Chicken Casserole3. Easy in the Microwave Curry Doria
Enjoy your meal!
专栏文章内容及配图由作者撰写发布,仅供工程师学习之用,如有侵权或者其他违规问题,请联系本站处理。 联系我们
相关推荐
基于VisitionX制造智能眼镜
国家“算力网”:像用水用电一样用AI
前Qwen负责人林俊旸创业,目标融资规模为数亿美元
AI聊天机器人能像医生一样推理吗?
WTC-AI太阳能热水器电路图
研华科技与Axelera AI深化战略合作 加速推动基于Europa平台的边缘AI创新
基于Ai-WB2-12F与Rd-04的雷达检测系统
PowiGaN for AI Data Centers: Unmatched Power Density and Reliability
AI/HPC新世代 COUPE光互连扮要角
EEPW2018年3月刊(工业物联网)
思科凭借通用商用芯片与光模块赢得 AI 领域客户
人工智能是如何帮助阻止造假者的?
海联达(Aigale)Ai-HD1 无线全高清套件拆解
基于Microchip MCU的AI/ML培训教程2
电子元件培训教材
基于Microchip MCU的AI/ML培训教程1
基于Microchip MCU的AI/ML培训教程3
被动元件新周期:AI时代高端化、服务器化重构MLCC产业格局
瑞萨电子AI单元解决方案成功提高GE医疗(日本)日野工厂的生产力
释说芯语16:硬科技:构建企业未来之路(附PPT)
EEPW2018年6月刊(5G)
继上次海联达Ai-ap100拆机之电源改造
WTC-AI型太阳能热水器电路图
重新构想AI电源:塑造AI加速的未来(第三部分)
iCAN-4017 AI功能模块
存储器转型AI战略资源 台厂受惠
联发科加速AI在地化应用布局
万家乐JSYZ5-AI燃气热水器电路图
尼吉康的事业介绍
CSR8670CSR8675智能语音Alexa蓝牙方案开发