Python调用大模型API

怎样用Python调用大语言模型?

使用text/event-stream来输出。

示例代码:

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
import requests
import json
import sseclient

API_KEY = '[INSERT YOUR OPENAI API KEY HERE]'

def performRequestWithStreaming():
reqUrl = 'https://api.openai.com/v1/completions'
reqHeaders = {
'Accept': 'text/event-stream',
'Authorization': 'Bearer ' + API_KEY
}
reqBody = {
"model": "text-davinci-003",
"prompt": "What is Python?",
"max_tokens": 100,
"temperature": 0,
"stream": True,
}
request = requests.post(reqUrl, stream=True, headers=reqHeaders, json=reqBody)
client = sseclient.SSEClient(request)
for event in client.events():
if event.data != '[DONE]':
print(json.loads(event.data)['choices'][0]['text'], end="", flush=True),

if __name__ == '__main__':
performRequestWithStreaming()