赞
踩
今天我学习了DeepLearning.AI的 Building Systems with LLM 的在线课程,我想和大家一起分享一下该门课程的一些主要内容。
下面是我们访问LLM模型的主要代码:
- import openai
-
- #您的openai的api key
- openai.api_key ='YOUR-OPENAI-API-KEY'
-
- def get_completion_from_messages(messages,
- model="gpt-3.5-turbo",
- temperature=0,
- max_tokens=500):
- response = openai.ChatCompletion.create(
- model=model,
- messages=messages,
- temperature=temperature,
- max_tokens=max_tokens,
- )
- return response.choices[0].message["content"]

在有些ChatGPT的应用场景中我们可能需要对用户的问题进行分类,下面来看一个例子,在这个例子中我们要把用户所提问题进行分类,有两个主要的分类,在每个主要的分类下面还有若干个次要分类,我们要做的是让ChatGPT根据用户的问题找到主要分类和次要分类,并以JSON格式输出结果。这里我们首先会用课程中的英语prompt来做实验,然后我们会把英语的prompt翻译成中文,然后用中文的promt再做一次实验,这样有助于大家理解prompt的具体含义以及中文prompt和英文prompt的差异。
- delimiter = "####"
- system_message = f"""
- You will be provided with customer service queries. \
- The customer service query will be delimited with \
- {delimiter} characters.
- Classify each query into a primary category \
- and a secondary category.
- Provide your output in json format with the \
- keys: primary and secondary.
- Primary categories: Billing, Technical Support, \
- Account Management, or General Inquiry.
- Billing secondary categories:
- Unsubscribe or upgrade
- Add a payment method
- Explanation for charge
- Dispute a charge
- Technical Support secondary categories:
- General troubleshooting
- Device compatibility
- Software updates
- Account Management secondary categories:
- Password reset
- Update personal information
- Close account
- Account security
- General Inquiry secondary categories:
- Product information
- Pricing
- Feedback
- Speak to a human
- """
-
-
-
- user_message = f"""\
- I want you to delete my profile and all of my user data"""
- messages = [
- {'role':'system',
- 'content': system_message},
- {'role':'user',
- 'content': f"{delimiter}{user_message}{delimiter}"},
- ]
- response = get_completion_from_messages(messages)
- print(response)

- user_message = f"""\
- Tell me more about your flat screen tvs"""
- messages = [
- {'role':'system',
- 'content': system_message},
- {'role':'user',
- 'content': f"{delimiter}{user_message}{delimiter}"},
- ]
- response = get_completion_from_messages(messages)
- print(response)
这里我们要求ChatGPT的输出结果为一个JSON格式,从输出的结果上看ChatGPT的输出完全符合我们的要求。接下来我们把原英语的prompt翻译成中文后再执行一次。
- delimiter = "####"
- system_message_chinese=f"""
- 您将收到客户查询。
- 客户查询将以 {delimiter} 字符分隔。
- 请将每个查询分成主要类别和次要类别。
- 最后输出带有primary和secondary键的json格式的结果。
- 主要类别:
- 计费
- 技术支持
- 账户管理
- 一般查询
- 计费的次要类别:
- 退订或升级
- 添加付款方式
- 收费说明
- 对费用提出异议
- 技术支持的次要类别:
- 一般故障排除
- 设备兼容性
- 软件更新
- 账户管理的次要类别:
- 重设密码
- 更新个人信息
- 关闭账户
- 账户安全
- 一般查询的次要类别:
- 产品信息
- 价钱
- 反馈
- 与人交谈
- """
-
- user_message_chinese = f"""
- 我想要你删除我的个人资料和我所有的用户数据
- """
- messages = [
- {'role':'system',
- 'content': system_message_chinese},
- {'role':'user',
- 'content': f"{delimiter}{user_message_chinese}{delimiter}"},
- ]
- response = get_completion_from_messages(messages)
- print(response)

第一个问题似乎没有问题,ChatGPT输出了一个正确的JSON结果,并且找到了正确的主要分类和次要分类。下面我们看第二个问题:
- user_message_chinese = f"""\
- 告诉我更多关于你们的纯平电视机的信息
- """
- messages = [
- {'role':'system',
- 'content': system_message_chinese},
- {'role':'user',
- 'content': f"{delimiter}{user_message_chinese}{delimiter}"},
- ]
- response = get_completion_from_messages(messages)
- print(response)
我们看到上面输出结果中除了一个JSON结构还有一些多余的信息,这些多余的信息并非是我们想要的,接下来我们再尝试一下第三个中文的问题:
- user_message_chinese = f"""\
- 如何修改我的登录密码?
- """
- messages = [
- {'role':'system',
- 'content': system_message_chinese},
- {'role':'user',
- 'content': f"{delimiter}{user_message_chinese}{delimiter}"},
- ]
- response = get_completion_from_messages(messages)
- print(response)
这次的结果和上一次类似,尽管JSON结果是正确的,但是仍然出现了画蛇添足的现象,如何避免出现这种情况呢?看来我们还需要修改一下我们的中文prompt:
- delimiter = "####"
- system_message_chinese=f"""
- 您将收到客户查询。
- 客户查询将以 {delimiter} 字符分隔。
- 请将每个查询分成主要类别和次要类别。
- 最后输出带有primary和secondary键的json格式的结果,\
- 其中primary表示主要类别,secondary表示次要类别。
- 主要类别:
- 计费
- 技术支持
- 账户管理
- 一般查询
- 计费的次要类别:
- 退订或升级
- 添加付款方式
- 收费说明
- 对费用提出异议
- 技术支持的次要类别:
- 一般故障排除
- 设备兼容性
- 软件更新
- 账户管理的次要类别:
- 重设密码
- 更新个人信息
- 关闭账户
- 账户安全
- 一般查询的次要类别:
- 产品信息
- 价钱
- 反馈
- 与人交谈
- 记住,输出结果必须为一个JSON结构。
- """

这里我们给中文prompt内容做了一些修改,主要是增加了两句话:“其中primary表示主要类别,secondary表示次要类别。” 和 “记住,输出结果必须为一个JSON结构。”,其中第一句话主要是让ChatGPT更加深入理解英语单词primary和secondary在上下文中的对应的中文含义,第二句话的作用是再次强调结果必须为一个JSON结果,这样就应该可以防止ChatGPT画蛇添足输出多余的内容。
- user_message_chinese = f"""
- 我想要你删除我的个人资料和我所有的用户数据
- """
- messages = [
- {'role':'system',
- 'content': system_message_chinese},
- {'role':'user',
- 'content': f"{delimiter}{user_message_chinese}{delimiter}"},
- ]
- response = get_completion_from_messages(messages)
- print(response)
- user_message_chinese = f"""\
- 告诉我更多关于你们的纯平电视机的信息
- """
- messages = [
- {'role':'system',
- 'content': system_message_chinese},
- {'role':'user',
- 'content': f"{delimiter}{user_message_chinese}{delimiter}"},
- ]
- response = get_completion_from_messages(messages)
- print(response)
- user_message_chinese = f"""\
- 如何修改我的登录密码?
- """
- messages = [
- {'role':'system',
- 'content': system_message_chinese},
- {'role':'user',
- 'content': f"{delimiter}{user_message_chinese}{delimiter}"},
- ]
- response = get_completion_from_messages(messages)
- print(response)
从上面的输出结果看,这次的prompt的修改起到了非常不错的效果,chatGPT没有再输出多余的内容。
今天我们学习了如何让ChatGPT对用户的问题进行分类,以及英文prompt和中文prompt在输出结果上的一些差异,通过恰当的对中文prompt的修改,我们可以让中文prompt的输出结果和英文prompt的输出结果一致。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。