Openai.Модерация не работает (проблема с версией openai)Python

Программы на Python
Ответить Пред. темаСлед. тема
Anonymous
 Openai.Модерация не работает (проблема с версией openai)

Сообщение Anonymous »

Когда я пытаюсь протестировать своего чат-бота на основе OpenAI API, я получаю следующую ошибку на терминале:
'Вы пытались получить доступ к openai.Moderation, но это больше не поддерживается в openai>=1.0.0 — API см. в README на https://github.com/openai/openai-python
.
Вы можете запустите openaimigrate, чтобы автоматически обновить вашу кодовую базу для использования интерфейса 1.0.0.
В качестве альтернативы вы можете закрепить свою установку за старой версией, например. pip install openai==0.28'
Код выглядит следующим образом:

Код: Выделить всё

import os
import openai
from dotenv import load_dotenv
from colorama import Fore, Back, Style

# load values from the .env file if it exists
load_dotenv()

# configure OpenAI
openai.api_key = os.getenv("OPENAI_API_KEY")

INSTRUCTIONS = """You are an AI assistant that is an expert in alcoholic beverages.
You know about cocktails, wines, spirits and beers.
You can provide advice on drink menus, cocktail ingredients, how to make cocktails, and anything else related to alcoholic drinks.
If you are unable to provide an answer to a question, please respond with the phrase "I'm just a simple barman, I can't help with that."
Please aim to be as helpful, creative, and friendly as possible in all of your responses.
Do not use any external URLs in your answers.  Do not refer to any blogs in your answers.
Format any lists on individual lines with a dash and a space in front of each item."""
ANSWER_SEQUENCE = "\nAI:"
QUESTION_SEQUENCE = "\nHUMAN:"
TEMPERATURE = 0.5
MAX_TOKENS = 500
FREQUENCY_PENALTY = 0
PRESENCE_PENALTY = 0.6
# limits how many questions we include in the prompt
MAX_CONTEXT_QUESTIONS = 10

def get_response(instructions, previous_questions_and_answers, new_question):
"""Get a response from ChatCompletion

Args:
instructions: The instructions for the chat bot - this determines how it will behave
previous_questions_and_answers: Chat history
new_question: The new question to ask the bot

Returns:
The response text
"""
# build the messages
messages = [
{ "role": "system", "content": instructions },
]
# add the previous questions and answers
for question, answer in previous_questions_and_answers[-MAX_CONTEXT_QUESTIONS:]:
messages.append({ "role": "user", "content": question })
messages.append({ "role": "assistant", "content": answer })
# add the new question
messages.append({ "role": "user", "content": new_question })

completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages,
temperature=TEMPERATURE,
max_tokens=MAX_TOKENS,
top_p=1,
frequency_penalty=FREQUENCY_PENALTY,
presence_penalty=PRESENCE_PENALTY,
)
return completion.choices[0].message.content

def get_moderation(question):
"""
Check the question is safe to ask the model

Parameters:
question (str): The question to check

Returns a list of errors if the question is not safe, otherwise returns None
"""

errors = {
"hate": "Content that expresses, incites, or promotes hate based on race, gender, ethnicity, religion, nationality, sexual orientation, disability status, or caste.",
"hate/threatening": "Hateful content that also includes violence or serious harm towards the targeted group.",
"self-harm": "Content that promotes, encourages, or depicts acts of self-harm, such as suicide, cutting, and eating disorders.",
"sexual": "Content meant to arouse sexual excitement, such as the description of sexual activity, or that promotes sexual services (excluding sex education and wellness).",
"sexual/minors": "Sexual content that includes an individual who is under 18 years old.",
"violence": "Content that promotes or glorifies violence or celebrates the suffering or humiliation of others.",
"violence/graphic": "Violent content that depicts death, violence, or serious physical injury in extreme graphic detail.",
}
response = openai.Moderation.create(input=question)
if response.results[0].flagged:
# get the categories that are flagged and generate a message
result = [
error
for category, error in errors.items()
if response.results[0].categories[category]
]
return result
return None

def main():
os.system("cls" if os.name == "nt" else "clear")
# keep track of previous questions and answers
previous_questions_and_answers = []
while True:
# ask the user for their question
new_question = input(
Fore.GREEN + Style.BRIGHT + "What can I get you?: "  + Style.RESET_ALL
)
# check the question is safe
errors = get_moderation(new_question)
if errors:
print(
Fore.RED
+ Style.BRIGHT
+ "Sorry, you're question didn't pass the moderation check:"
)
for error in errors:
print(error)
print(Style.RESET_ALL)
continue
response = get_response(INSTRUCTIONS, previous_questions_and_answers, new_question)

# add the new question and answer to the list of previous questions and answers
previous_questions_and_answers.append((new_question, response))

# print the response
print(Fore.CYAN + Style.BRIGHT + "Here you go: " + Style.NORMAL + response)

if __name__ == "__main__":
main()
Мне удалось создать виртуальную среду, но в тот момент, когда я пишу что-либо боту, я получаю сообщение об ошибке, связанной с openai.Модерация. Я попробовал «openaimigrate», но, видимо, он не поддерживается в Windows, и я также попробовал pip install openai==0.28но, похоже, ничего не меняется.
Заранее спасибо, и прошу прощения, если я пропустил какую-то важную информацию или плохо изложил свою проблему, я все еще новичок в этом.

Подробнее здесь: https://stackoverflow.com/questions/777 ... sion-issue
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Python»