-
Notifications
You must be signed in to change notification settings - Fork 4
/
lib.py
29 lines (20 loc) · 965 Bytes
/
lib.py
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 os
import aiohttp
import asyncio
API_KEY = os.environ.get("OPENAI_API_KEY")
async def get_next_iteration(prompt, model="gpt-3.5-turbo") -> str:
url = "https://api.openai.com/v1/chat/completions"
headers = {"Content-Type": "application/json", "Authorization": f"Bearer {API_KEY}"}
data = {"model": model, "messages": [{"role": "user", "content": prompt}]}
async with aiohttp.ClientSession() as session:
async with session.post(url, headers=headers, json=data) as response:
response_json = await response.json()
return response_json["choices"][0]["message"]["content"].strip()
async def _get_chidren(prompt, n):
tasks = [get_next_iteration(prompt) for _ in range(n)]
return await asyncio.gather(*tasks)
def get_children(prompt, n, lineage):
print(f"get_children start {lineage}")
res = asyncio.run(_get_chidren(prompt, n))
print(f"get_children end {lineage}")
return res