当前位置:   article > 正文

使用大型语言模(LLM)构建系统(一):分类_llm建模代码

llm建模代码

今天我学习了DeepLearning.AI的 Building Systems with  LLM 的在线课程,我想和大家一起分享一下该门课程的一些主要内容。

下面是我们访问LLM模型的主要代码:

  1. import openai
  2. #您的openai的api key
  3. openai.api_key ='YOUR-OPENAI-API-KEY'
  4. def get_completion_from_messages(messages,
  5. model="gpt-3.5-turbo",
  6. temperature=0,
  7. max_tokens=500):
  8. response = openai.ChatCompletion.create(
  9. model=model,
  10. messages=messages,
  11. temperature=temperature,
  12. max_tokens=max_tokens,
  13. )
  14. return response.choices[0].message["content"]

分类(Classification)

在有些ChatGPT的应用场景中我们可能需要对用户的问题进行分类,下面来看一个例子,在这个例子中我们要把用户所提问题进行分类,有两个主要的分类,在每个主要的分类下面还有若干个次要分类,我们要做的是让ChatGPT根据用户的问题找到主要分类和次要分类,并以JSON格式输出结果。这里我们首先会用课程中的英语prompt来做实验,然后我们会把英语的prompt翻译成中文,然后用中文的promt再做一次实验,这样有助于大家理解prompt的具体含义以及中文prompt和英文prompt的差异。

  1. delimiter = "####"
  2. system_message = f"""
  3. You will be provided with customer service queries. \
  4. The customer service query will be delimited with \
  5. {delimiter} characters.
  6. Classify each query into a primary category \
  7. and a secondary category.
  8. Provide your output in json format with the \
  9. keys: primary and secondary.
  10. Primary categories: Billing, Technical Support, \
  11. Account Management, or General Inquiry.
  12. Billing secondary categories:
  13. Unsubscribe or upgrade
  14. Add a payment method
  15. Explanation for charge
  16. Dispute a charge
  17. Technical Support secondary categories:
  18. General troubleshooting
  19. Device compatibility
  20. Software updates
  21. Account Management secondary categories:
  22. Password reset
  23. Update personal information
  24. Close account
  25. Account security
  26. General Inquiry secondary categories:
  27. Product information
  28. Pricing
  29. Feedback
  30. Speak to a human
  31. """
  32. user_message = f"""\
  33. I want you to delete my profile and all of my user data"""
  34. messages = [
  35. {'role':'system',
  36. 'content': system_message},
  37. {'role':'user',
  38. 'content': f"{delimiter}{user_message}{delimiter}"},
  39. ]
  40. response = get_completion_from_messages(messages)
  41. print(response)

  1. user_message = f"""\
  2. Tell me more about your flat screen tvs"""
  3. messages = [
  4. {'role':'system',
  5. 'content': system_message},
  6. {'role':'user',
  7. 'content': f"{delimiter}{user_message}{delimiter}"},
  8. ]
  9. response = get_completion_from_messages(messages)
  10. print(response)

 这里我们要求ChatGPT的输出结果为一个JSON格式,从输出的结果上看ChatGPT的输出完全符合我们的要求。接下来我们把原英语的prompt翻译成中文后再执行一次。

  1. delimiter = "####"
  2. system_message_chinese=f"""
  3. 您将收到客户查询。
  4. 客户查询将以 {delimiter} 字符分隔。
  5. 请将每个查询分成主要类别和次要类别。
  6. 最后输出带有primary和secondary键的json格式的结果。
  7. 主要类别:
  8. 计费
  9. 技术支持
  10. 账户管理
  11. 一般查询
  12. 计费的次要类别:
  13. 退订或升级
  14. 添加付款方式
  15. 收费说明
  16. 对费用提出异议
  17. 技术支持的次要类别:
  18. 一般故障排除
  19. 设备兼容性
  20. 软件更新
  21. 账户管理的次要类别:
  22. 重设密码
  23. 更新个人信息
  24. 关闭账户
  25. 账户安全
  26. 一般查询的次要类别:
  27. 产品信息
  28. 价钱
  29. 反馈
  30. 与人交谈
  31. """
  32. user_message_chinese = f"""
  33. 我想要你删除我的个人资料和我所有的用户数据
  34. """
  35. messages = [
  36. {'role':'system',
  37. 'content': system_message_chinese},
  38. {'role':'user',
  39. 'content': f"{delimiter}{user_message_chinese}{delimiter}"},
  40. ]
  41. response = get_completion_from_messages(messages)
  42. print(response)

第一个问题似乎没有问题,ChatGPT输出了一个正确的JSON结果,并且找到了正确的主要分类和次要分类。下面我们看第二个问题:

  1. user_message_chinese = f"""\
  2. 告诉我更多关于你们的纯平电视机的信息
  3. """
  4. messages = [
  5. {'role':'system',
  6. 'content': system_message_chinese},
  7. {'role':'user',
  8. 'content': f"{delimiter}{user_message_chinese}{delimiter}"},
  9. ]
  10. response = get_completion_from_messages(messages)
  11. print(response)

 

 我们看到上面输出结果中除了一个JSON结构还有一些多余的信息,这些多余的信息并非是我们想要的,接下来我们再尝试一下第三个中文的问题:

  1. user_message_chinese = f"""\
  2. 如何修改我的登录密码?
  3. """
  4. messages = [
  5. {'role':'system',
  6. 'content': system_message_chinese},
  7. {'role':'user',
  8. 'content': f"{delimiter}{user_message_chinese}{delimiter}"},
  9. ]
  10. response = get_completion_from_messages(messages)
  11. print(response)

 这次的结果和上一次类似,尽管JSON结果是正确的,但是仍然出现了画蛇添足的现象,如何避免出现这种情况呢?看来我们还需要修改一下我们的中文prompt:

  1. delimiter = "####"
  2. system_message_chinese=f"""
  3. 您将收到客户查询。
  4. 客户查询将以 {delimiter} 字符分隔。
  5. 请将每个查询分成主要类别和次要类别。
  6. 最后输出带有primary和secondary键的json格式的结果,\
  7. 其中primary表示主要类别,secondary表示次要类别。
  8. 主要类别:
  9. 计费
  10. 技术支持
  11. 账户管理
  12. 一般查询
  13. 计费的次要类别:
  14. 退订或升级
  15. 添加付款方式
  16. 收费说明
  17. 对费用提出异议
  18. 技术支持的次要类别:
  19. 一般故障排除
  20. 设备兼容性
  21. 软件更新
  22. 账户管理的次要类别:
  23. 重设密码
  24. 更新个人信息
  25. 关闭账户
  26. 账户安全
  27. 一般查询的次要类别:
  28. 产品信息
  29. 价钱
  30. 反馈
  31. 与人交谈
  32. 记住,输出结果必须为一个JSON结构。
  33. """

这里我们给中文prompt内容做了一些修改,主要是增加了两句话:“其中primary表示主要类别,secondary表示次要类别。” 和 “记住,输出结果必须为一个JSON结构。”,其中第一句话主要是让ChatGPT更加深入理解英语单词primary和secondary在上下文中的对应的中文含义,第二句话的作用是再次强调结果必须为一个JSON结果,这样就应该可以防止ChatGPT画蛇添足输出多余的内容。

  1. user_message_chinese = f"""
  2. 我想要你删除我的个人资料和我所有的用户数据
  3. """
  4. messages = [
  5. {'role':'system',
  6. 'content': system_message_chinese},
  7. {'role':'user',
  8. 'content': f"{delimiter}{user_message_chinese}{delimiter}"},
  9. ]
  10. response = get_completion_from_messages(messages)
  11. print(response)

  1. user_message_chinese = f"""\
  2. 告诉我更多关于你们的纯平电视机的信息
  3. """
  4. messages = [
  5. {'role':'system',
  6. 'content': system_message_chinese},
  7. {'role':'user',
  8. 'content': f"{delimiter}{user_message_chinese}{delimiter}"},
  9. ]
  10. response = get_completion_from_messages(messages)
  11. print(response)

 

  1. user_message_chinese = f"""\
  2. 如何修改我的登录密码?
  3. """
  4. messages = [
  5. {'role':'system',
  6. 'content': system_message_chinese},
  7. {'role':'user',
  8. 'content': f"{delimiter}{user_message_chinese}{delimiter}"},
  9. ]
  10. response = get_completion_from_messages(messages)
  11. print(response)

 

 从上面的输出结果看,这次的prompt的修改起到了非常不错的效果,chatGPT没有再输出多余的内容。

总结

今天我们学习了如何让ChatGPT对用户的问题进行分类,以及英文prompt和中文prompt在输出结果上的一些差异,通过恰当的对中文prompt的修改,我们可以让中文prompt的输出结果和英文prompt的输出结果一致。

参考资料

DLAI - Learning Platform Beta

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/255580
推荐阅读
相关标签
  

闽ICP备14008679号