SmallAI API调用 Python示例代码&高RPM报错解决方法
需要使用gpt-4o或者claude-3-5-sonnet这种国外商业模型,但是没有梯子,可以用SmallAI的API。
API示例代码可以在https://7vkcqalroo.apifox.cn/找到,藏得比较深,在模型接口->聊天接口->具体模型(以gpts为例)中
往下翻,找到示例代码,然后选择你需要的语言(以python为例)
我一开始使用的是Requests,代码如下
import requests
import json
def smallai_api(input,model):
url = 'https://ai98.vip/v1/chat/completions'
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer sk-xxxxxxxxxxx'
}
data = {
"model": model,
"messages": [{"role": "user", "content": f"{input}"}],
"temperature": 0.7
}
response = requests.post(url, headers=headers, data=json.dumps(data))
return response.json()["choices"][0]["message"]["content"]
结果在单次调用测试中可以正常使用,但是一旦开始跑大量快速多轮次调用,就会报错
SSLError: HTTPSConnectionPool(host='ai98.vip', port=443): Max retries exceeded with url: /v1/chat/completions (Caused by SSLError(SSLEOFError(8, '[SSL: UNEXPECTED_EOF_WHILE_READING] EOF occurred in violation of protocol (_ssl.c:1007)')))
推测是由于超出了RPM限制,在客服的帮助下,使用http.client方法,代码如下
import http.client
import json
def smallai_api(input,model):
conn = http.client.HTTPSConnection("ai98.vip")
payload = json.dumps({
"model": model,
"messages": [
{
"role": "user",
"content": f"{input}"
}
]
})
headers = {
'Accept': 'application/json',
'Authorization': 'Bearer sk-xxxxxxxxxxx',
'Content-Type': 'application/json'
}
conn.request("POST", "/v1/chat/completions", payload, headers)
res = conn.getresponse()
data = res.read()
json_data = json.loads(data.decode("utf-8"))
return json_data['choices'][0]['message']['content']
这样就解决了,再次跑实验无报错
(保险起见可以每次调用中间再time.sleep个0.5秒)
License:
CC BY 4.0