将Python脚本转换为使用“GPT-3.5-turbo”模型

uidvcgyl  于 2023-04-07  发布在  Python
关注(0)|答案(1)|浏览(252)

我有下面的python代码工作的文本-davinci-003

import openai
import time

openai.api_key = "skXXXXXXX"
model_engine = "text-davinci-003"

# Define the prompt for the conversation
prompt = "Conversation with an AI:"

while True:
    # Get the user's input
    user_input = input(prompt + " ")
    
    # Check if the user wants to exit
    if user_input.lower() in ["quit", "exit", "bye"]:
        print("Goodbye!")
        break
    
    # Generate a response from the OpenAI API
    response = openai.Completion.create(
        engine=model_engine,
        prompt=prompt + " " + user_input,
        max_tokens=1024,
        n=1,
        stop=None,
        temperature=0.7,
    )

    # Print the AI's response
    message = response.choices[0].text.strip()
    print("AI: " + message)

    # Wait for a bit before continuing
    time.sleep(1)

对于我的生活,我不能让它与“GPT-3.5-turbo”一起工作。我已经尝试了以下代码从github仓库,但我得到错误:

import openai

# load and set our key
openai.api_key = open("key.txt", "r").read().strip("\n")

completion = openai.ChatCompletion.create(
  model="gpt-3.5-turbo", # this is "ChatGPT" $0.002 per 1k tokens
  messages=[{"role": "user", "content": "What is the circumference in km of the planet Earth?"}]
)

reply_content = completion.choices[0].message.content
print(reply_content)

但它失败了,错误:AttributeError: module 'openai' has no attribute 'ChatCompletion'
有人好心帮忙吗!!!
谢谢。

jhiyze9q

jhiyze9q1#

听起来你可能有一个旧版本的OpenAI包。你可以试试这个:

pip install --upgrade openai

相关问题