diff --git a/content/Building Systems with the ChatGPT API/3.Classification.ipynb b/content/Building Systems with the ChatGPT API/3.Classification.ipynb index 181fddd..f06b026 100644 --- a/content/Building Systems with the ChatGPT API/3.Classification.ipynb +++ b/content/Building Systems with the ChatGPT API/3.Classification.ipynb @@ -9,6 +9,23 @@ "第三章 评估输入——分类" ] }, + { + "attachments": {}, + "cell_type": "markdown", + "id": "b12f80c9", + "metadata": {}, + "source": [ + "在本节中,我们将专注于评估输入的任务,这对于确保系统的质量和安全性非常重要。\n", + "\n", + "对于需要处理不同情况下的许多独立指令集的任务,首先对查询类型进行分类,然后根据该分类确定要使用哪些指令会很有好处。\n", + "\n", + "这可以通过定义固定的类别和hard-coding与处理给定类别任务相关的指令来实现。\n", + "\n", + "例如,在构建客户服务助手时,首先对查询类型进行分类,然后根据该分类确定要使用哪些指令可能比较重要。\n", + "\n", + "因此,例如,如果用户要求关闭其帐户,您可能会给出不同的辅助指令,而如果用户询问特定产品,则可能会添加其他产品信息。\n" + ] + }, { "attachments": {}, "cell_type": "markdown", @@ -21,7 +38,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 9, "id": "55ee24ab", "metadata": {}, "outputs": [], @@ -30,12 +47,12 @@ "import openai\n", "from dotenv import load_dotenv, find_dotenv\n", "_ = load_dotenv(find_dotenv()) # read local .env file\n", - "openai.api_key = os.environ['OPENAI_API_KEY']" + "openai.api_key = os.environ['OPENAI_API_KEY']\n" ] }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 2, "id": "0318b89e", "metadata": {}, "outputs": [], @@ -53,25 +70,6 @@ " return response.choices[0].message[\"content\"]" ] }, - { - "attachments": {}, - "cell_type": "markdown", - "id": "af0ff2b2", - "metadata": {}, - "source": [ - "在本节中,我们将专注于评估输入的任务,这对于确保系统的质量和安全性非常重要。\n", - "\n", - "对于需要处理不同情况下的许多独立指令集的任务,首先对查询类型进行分类,然后根据该分类确定要使用哪些指令会很有好处。\n", - "\n", - "这可以通过定义固定的类别和hard-coding与处理给定类别任务相关的指令来实现。\n", - "\n", - "例如,在构建客户服务助手时,首先对查询类型进行分类,然后根据该分类确定要使用哪些指令可能比较重要。\n", - "\n", - "因此,例如,如果用户要求关闭其帐户,您可能会给出不同的辅助指令,而如果用户询问特定产品,则可能会添加其他产品信息。\n", - "\n", - "让我们看一个例子,帮助我们理解的更清晰。" - ] - }, { "attachments": {}, "cell_type": "markdown", @@ -87,9 +85,9 @@ "id": "c3216166", "metadata": {}, "source": [ - "在这里,我们有我们的系统消息,它是对整个系统的指导,并且我们正在使用这个分隔符。\n", + "在这里,我们有我们的系统消息,它是对整个系统的指导,并且我们正在使用这个分隔符——#。\n", "\n", - "分隔符只是一种分隔指令或输出不同部分的方式,并且它有助于模型确定不同的部分。\n", + "分隔符只是一种分隔指令或输出不同部分的方式,它有助于模型确定不同的部分。\n", "\n", "因此,对于这个例子,我们将使用#作为分隔符。\n", "\n", @@ -98,7 +96,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 3, "id": "3b406ba8", "metadata": {}, "outputs": [], @@ -117,7 +115,7 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 4, "id": "29e2d170", "metadata": {}, "outputs": [], @@ -160,6 +158,48 @@ "\"\"\"" ] }, + { + "cell_type": "code", + "execution_count": 5, + "id": "61f4b474", + "metadata": {}, + "outputs": [], + "source": [ + "# 中文 Prompt\n", + "system_message = f\"\"\"\n", + "你将获得客户服务查询。\n", + "每个客户服务查询都将用{delimiter}字符分隔。\n", + "将每个查询分类到一个主要类别和一个次要类别中。\n", + "以JSON格式提供你的输出,包含以下键:primary和secondary。\n", + "\n", + "主要类别:计费(Billing)、技术支持(Technical Support)、账户管理(Account Management)或一般咨询(General Inquiry)。\n", + "\n", + "计费次要类别:\n", + "取消订阅或升级(Unsubscribe or upgrade)\n", + "添加付款方式(Add a payment method)\n", + "收费解释(Explanation for charge)\n", + "争议费用(Dispute a charge)\n", + "\n", + "技术支持次要类别:\n", + "常规故障排除(General troubleshooting)\n", + "设备兼容性(Device compatibility)\n", + "软件更新(Software updates)\n", + "\n", + "账户管理次要类别:\n", + "重置密码(Password reset)\n", + "更新个人信息(Update personal information)\n", + "关闭账户(Close account)\n", + "账户安全(Account security)\n", + "\n", + "一般咨询次要类别:\n", + "产品信息(Product information)\n", + "定价(Pricing)\n", + "反馈(Feedback)\n", + "与人工对话(Speak to a human)\n", + "\n", + "\"\"\"" + ] + }, { "attachments": {}, "cell_type": "markdown", @@ -180,6 +220,17 @@ "I want you to delete my profile and all of my user data\"\"\"" ] }, + { + "cell_type": "code", + "execution_count": 6, + "id": "3b8070bf", + "metadata": {}, + "outputs": [], + "source": [ + "user_message = f\"\"\"\\ \n", + "我希望你删除我的个人资料和所有用户数据。\"\"\"" + ] + }, { "attachments": {}, "cell_type": "markdown", @@ -195,7 +246,7 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 7, "id": "6e2b9049", "metadata": {}, "outputs": [], @@ -225,7 +276,7 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": 10, "id": "77328388", "metadata": {}, "outputs": [ @@ -234,8 +285,8 @@ "output_type": "stream", "text": [ "{\n", - " \"primary\": \"General Inquiry\",\n", - " \"secondary\": \"Product information\"\n", + " \"primary\": \"账户管理\",\n", + " \"secondary\": \"关闭账户\"\n", "}\n" ] } @@ -251,8 +302,6 @@ "id": "2f6b353b", "metadata": {}, "source": [ - "我将向您展示另一个示例,但您也可以随时暂停视频,自己尝试提问,并查看模型如何对其进行分类。\n", - "\n", "这是另一个用户消息: \"告诉我更多关于你们的平板电视\"\n", "\n", "我们只是有相同的消息列表,模型的响应,然后我们打印它。\n", @@ -290,6 +339,40 @@ "print(response)" ] }, + { + "cell_type": "code", + "execution_count": 12, + "id": "f1d738e1", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "以下是针对平板电脑的一般咨询:\n", + "\n", + "{\n", + " \"primary\": \"General Inquiry\",\n", + " \"secondary\": \"Product information\"\n", + "}\n", + "\n", + "如果您有任何特定的问题或需要更详细的信息,请告诉我,我会尽力回答。\n" + ] + } + ], + "source": [ + "user_message = f\"\"\"\\\n", + "告诉我更多有关你们的平板电脑的信息\"\"\"\n", + "messages = [ \n", + "{'role':'system', \n", + " 'content': system_message}, \n", + "{'role':'user', \n", + " 'content': f\"{delimiter}{user_message}{delimiter}\"}, \n", + "] \n", + "response = get_completion_from_messages(messages)\n", + "print(response)" + ] + }, { "attachments": {}, "cell_type": "markdown", @@ -322,7 +405,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.16" + "version": "3.10.11" } }, "nbformat": 4,