gpt4 book ai didi

python - 如何让openAI API只响应特定类别的请求?

转载 作者:行者123 更新时间:2023-12-02 22:47:06 24 4
gpt4 key购买 nike

我使用 python 创建了一个 openAI API,以响应任何类型的提示。

我想让 API 仅响应与产品说明中的广告相关的请求和问候请求,并且如果用户发送的请求与以下内容无关执行此任务时,API 应发送类似我不适合此类任务的消息。


import os
import openai

openai.api_key = os.getenv("OPENAI_API_KEY")

response = openai.Completion.create(
model="text-davinci-003",
prompt="Write a creative ad for the following product to run on Facebook aimed at parents:\n\nProduct: Learning Room is a virtual environment to help students from kindergarten to high school excel in school.",
temperature=0.5,
max_tokens=100,
top_p=1.0,
frequency_penalty=0.0,
presence_penalty=0.0
)

我想更新代码以生成这样的聊天。 让机器人理解生成广告和问候请求并忽略其他

例如:-

用户:-你好

api:-您好,今天我可以为您的品牌提供什么帮助吗?

用户:- 为以下产品撰写社交媒体帖子,在 Facebook 上针对家长运行:\n\n产品:Learning Room 是一个虚拟环境,可帮助从幼儿园到高中的学生取得优异成绩在学校。

api:- 您是否正在寻找一种让您的 child 在学校取得领先地位的方法?学习室就是您的最佳选择!我们的虚拟环境旨在帮助从幼儿园到高中的学生取得优异的学习成绩。我们独特的平台提供个性化的学习计划、互动事件和实时反馈,以确保您的 child 从教育中获得最大 yield 。通过学习室,为您的 child 提供在学校取得成功的最佳机会!

用户:-美国位于哪里?

api:-我不适合此类任务。

那么,如何更新我的代码?

最佳答案

尝试 gpt3.5-turbo(比 davinci 便宜 10 倍)。

# example in python=3.9 /// openai==0.27.0

import openai
openai.api_key = "YOUR_TOKEN"



messages = []

# be as specific as possible in the behavior it should have
system_content = '''You are a marketing assistant called MarkBot.
You only respond to greetings and marketing-related questions.
For any other question you must answer "I'm not suitable for this type of tasks.".'''

messages.append({"role": "system", "content": system_content})

prompt_text = 'Hi, How can i improve my sellings of cakes?'

messages.append({"role": "user", "content": prompt_text})

response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages,
max_tokens=1000,
temperature=0.5)

# In my tests

# Q: 'How many states does Brazil have?'
# A: "I'm not suitable for this type of tasks."

# Q: 'Hi, What can you do? Can you help me sell more cakes?'
# A:
'''"Hello! As a marketing assistant, I can assist you in
developing a marketing plan for your cake business,
including identifying your target audience, creating
advertising materials, and implementing promotional campaigns.
Let me know if you have any specific questions or concerns!"'''



请注意,它不会 100% 准确,但告诉 CHATGPT 应如何表现应该有助于获得您想要的行为。你可以只问一个问题,而不保留上下文。您可能还想降低温度,在文档中您会发现:对于温度,较高的值(如 0.8)将使输出更加随机,而较低的值(如 0.2)将使其更加集中和确定性. ( https://platform.openai.com/docs/guides/chat/instructing-chat-models ).

观察:如果您想保留上下文,据我测试,创建“聊天”功能包括将整个对话添加到“消息”中。因此,如果您希望您的机器人保留对话的上下文(例如chatgpt网站),您需要发送整个对话历史记录+新问题以接收新答案(这在代币方面会更昂贵,但您会得到更好的答案)。

关于python - 如何让openAI API只响应特定类别的请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75744277/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com