matrix_chatgpt_bot/ask_gpt.py

52 lines
1.3 KiB
Python
Raw Normal View History

2023-03-05 14:07:25 +00:00
"""
api_endpoint from https://github.com/ayaka14732/ChatGPTAPIFree
"""
import aiohttp
import asyncio
import json
import time
api_endpoint_free = "https://chatgpt-api.shn.hk/v1/"
headers = {'Content-Type': "application/json"}
async def ask(prompt: str) -> str:
jsons = {
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "user",
"content": prompt,
},
],
}
async with aiohttp.ClientSession() as session:
while True:
try:
async with session.post(url=api_endpoint_free,
json=jsons, headers=headers, timeout=10) as response:
status_code = response.status
if not status_code == 200:
# wait 2s
time.sleep(2)
continue
resp = await response.read()
await session.close()
return json.loads(resp)['choices'][0]['message']['content']
except Exception as e:
print(e)
pass
async def test() -> None:
resp = await ask("Hello World")
print(resp)
# type: str
print(type(resp))
if __name__ == "__main__":
asyncio.run(test())