-
-
Notifications
You must be signed in to change notification settings - Fork 224
Expand file tree
/
Copy pathchatgpt.py
More file actions
24 lines (22 loc) · 698 Bytes
/
chatgpt.py
File metadata and controls
24 lines (22 loc) · 698 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import json
import requests
def translate(api_key, ipt):
prompt = f'假設你是一位python工程師,請將下列文件翻譯為繁體中文 “{ipt}”'
response = requests.post(
'https://api.openai.com/v1/completions',
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {api_key}'
},
json = {
'model': 'text-davinci-003',
'prompt': prompt,
'temperature': 0.4,
'max_tokens': 1000
}
)
return response
def Translator(ipt, api_key):
response = translate(api_key, ipt)
opt = json.loads(response.text)['choices'][0]['text']
print(opt)