"); //-->
让我来做个演示,更清楚地说明为什么我放弃了 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!
*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。