-
Notifications
You must be signed in to change notification settings - Fork 2
/
openai_example.py
51 lines (42 loc) · 1.34 KB
/
openai_example.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import json
import openai
import asyncio
import requests
from rich import print
async def run(model: str):
client = openai.AsyncOpenAI()
messages = [
{
"role": "user",
"content": "Find open ports on 127.0.0.1",
}
]
response = await client.chat.completions.create(
model=model,
messages=messages,
# get the tools from the Robopages server
tools=requests.get("http://localhost:8000/").json(),
)
print(response)
# if the response contains tool calls
if response.choices[0].message.tool_calls:
# execute them via the API
results = requests.post(
"http://localhost:8000/process",
json=[
{
"id": tool_call.id,
"type": tool_call.type,
"function": {
"name": tool_call.function.name,
# for some reason the arguments are returned as a string
"arguments": json.loads(tool_call.function.arguments),
},
}
for tool_call in response.choices[0].message.tool_calls
],
)
results.raise_for_status()
# do whatever you want with the results
print(results.json())
asyncio.run(run("gpt-3.5-turbo"))