File size: 1,784 Bytes
bf2a1f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import json
import urllib.request
import urllib.parse

# 获取OpenAI API Key
while True:
    api_key = input("请输入您的OpenAI API Key:")

    # 检查API Key是否有效
    url = "https://api.openai.com/v1/engines/davinci/completions"

    headers = {
        "Content-Type": "application/json",
        "Authorization": f"Bearer {api_key}"
    }

    data = {
        "prompt": "test",
        "max_tokens": 1,
        "n": 1,
        "stop": None,
        "temperature": 0.5
    }

    req = urllib.request.Request(url, json.dumps(data).encode(), headers)

    try:
        with urllib.request.urlopen(req) as f:
            response = json.load(f)
        break
    except urllib.error.HTTPError as error:
        print(f"OpenAI API Key无效,请重新输入:{error}")
    except Exception as error:
        print(f"发生错误,请重新输入:{error}")

print("OpenAI API Key已验证成功!")

# 向ChatGPT发送文本并获取回答


def get_answer(prompt):
    url = "https://api.openai.com/v1/engines/davinci/completions"

    headers = {
        "Content-Type": "application/json",
        "Authorization": f"Bearer {api_key}"
    }

    data = {
        "prompt": prompt,
        "max_tokens": 60,
        "n": 1,
        "stop": None,
        "temperature": 0.5
    }

    req = urllib.request.Request(url, json.dumps(data).encode(), headers)

    with urllib.request.urlopen(req) as f:
        response = json.load(f)

    answer = response["choices"][0]["text"].strip()
    return answer


# 与用户交互
while True:
    # 获取用户输入
    user_input = input("请问有什么需要我帮助的吗?")

    # 向ChatGPT发送用户输入,并获取回答
    answer = get_answer(user_input)

    # 输出ChatGPT的回答
    print(answer)