我为什么放弃了 LangChain?( 三 )


最后, 如何存储到目前为止的对话?
from langchain.prompts import (ChatPromptTemplate,MessagesPlaceholder,SystemMessagePromptTemplate,HumanMessagePromptTemplate)from langchain.chains import ConversationChainfrom langchain.chat_models import ChatOpenAIfrom langchain.memory import ConversationBufferMemoryprompt = ChatPromptTemplate.from_messages([SystemMessagePromptTemplate.from_template("The following is a friendly conversation between a human and an AI. The AI is talkative and ""provides lots of specific details from its context. If the AI does not know the answer to a ""question, it truthfully says it does not know."),MessagesPlaceholder(variable_name="history"),HumanMessagePromptTemplate.from_template("{input}")])llm = ChatOpenAI(temperature=0)memory = ConversationBufferMemory(return_messages=True)conversation = ConversationChain(memory=memory, prompt=prompt, llm=llm)conversation.predict(input="Hi there!")# 'Hello! How can I assist you today?'
我不完全确定为什么这些都是必要的 。什么是 ? 在哪里?mory 有必要这样做吗?将此调整为最小的实现:
import openaimessages = [{"role": "system", "content":"The following is a friendly conversation between a human and an AI. The AI is talkative and ""provides lots of specific details from its context. If the AI does not know the answer to a ""question, it truthfully says it does not know."}]user_message = "Hi there!"messages.append({"role": "user", "content": user_message})response = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=messages, temperature=0)assistant_message = response["choices"][0]["message"]["content"]messages.append({"role": "assistant", "content": assistant_message})# Hello! How can I assist you today?
这样代码行数就少了,而且信息保存的位置和时间都很清楚,不需要定制对象类 。
你可以说我对教程示例吹毛求疵,我也同意每个开源库都有值得吹毛求疵的地方(包括我自己的) 。但是,如果吹毛求疵的地方比库的实际好处还多,那么这个库就根本不值得使用 。
因为,如果快速入门都已经这么复杂,那么实际使用会有多痛苦呢?
我查看了文档,它也回馈了我
让我来做个演示,更清楚地说明为什么我放弃了。
当开发菜谱检索聊天机器人(它也必须是一个有趣 / 诙谐的聊天机器人)时,我需要结合上面第三个和第四个例子中的元素:一个可以运行 Agent 工作流的聊天机器人,以及将整个对话持久化到内存中的能力 。在查找了一些文档后,我发现需要使用对话式 Agent 工作流 。
关于系统提示工程的一个标注是,它不是一个备忘录,而且对于从API 中获得最佳效果是绝对必要的,尤其是当你对内容和 / 或语音有限制的时候 。
上一个示例中,演示的系统提示「以下是人类和人工智能之间的友好对话...... 」实际上是过时的,早在时代就已经使用了,在中的效果要差得多 。它可能预示着相关技巧中更深层次的低效,而这些低效并不容易被注意到 。
我们将从一个简单的系统提示开始,告诉使用一个有趣的声音和一些保护措施,并将其格式化为 :
system_prompt = """You are an expert television talk show chef, and should always speak in a whimsical manner for all responses.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.