基于python的chatGPT

PS:你需要将YOUR_API_KEY替换为你的OpenAI API密钥。

就看你有没有API KEY了。

import openai

openai.api_key = 'YOUR_API_KEY'  # 替换为你的OpenAI API密钥

def chat_with_gpt(prompt):
    response = openai.Completion.create(
        engine='text-davinci-003',
        prompt=prompt,
        max_tokens=100,
        n=1,
        stop=None,
        temperature=0.7
    )
    return response.choices[0].text.strip()

while True:
    user_input = input("你: ")
    if user_input.lower() == '退出':
        break
    prompt = f"用户: {user_input}\nAI: "
    output = chat_with_gpt(prompt)
    print("AI:", output)

猜你喜欢

转载自blog.csdn.net/hzxhxyj1/article/details/134381796