-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathopenai_compatible_host.py
More file actions
74 lines (57 loc) · 1.98 KB
/
Copy pathopenai_compatible_host.py
File metadata and controls
74 lines (57 loc) · 1.98 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""Run a Skillware tool loop through the OpenAI-compatible Groq API."""
import json
import os
from openai import OpenAI
from skillware.core.env import load_env_file
from skillware.core.loader import SkillLoader
load_env_file()
bundle = SkillLoader.load_skill("compliance/tos_evaluator")
print(f"Loaded Skill: {bundle['manifest']['name']}")
tos_skill = bundle["class"]()
openai_tool = SkillLoader.to_openai_tool(bundle)
tool_name = openai_tool["function"]["name"]
print(f"OpenAI tool name: {tool_name}")
client = OpenAI(
api_key=os.environ["GROQ_API_KEY"],
base_url="https://api.groq.com/openai/v1",
)
model = os.environ["GROQ_MODEL"]
user_query = (
"Before an agent crawls https://hackernoon.com/tagged/ai for research, "
"check whether that appears allowed."
)
print(f"User: {user_query}")
messages = [
{"role": "system", "content": bundle["instructions"]},
{"role": "user", "content": user_query},
]
response = client.chat.completions.create(
model=model,
messages=messages,
tools=[openai_tool],
)
while response.choices[0].message.tool_calls:
assistant_message = response.choices[0].message
messages.append(assistant_message)
for tool_call in assistant_message.tool_calls:
if tool_call.function.name != tool_name:
raise RuntimeError(f"Unexpected tool: {tool_call.function.name}")
fn_args = json.loads(tool_call.function.arguments)
print(f"OpenAI-compatible host requested tool: {tool_call.function.name}")
print(f"Input: {fn_args}")
result = tos_skill.execute(fn_args)
print(json.dumps(result, indent=2))
messages.append(
{
"role": "tool",
"tool_call_id": tool_call.id,
"content": json.dumps(result),
}
)
response = client.chat.completions.create(
model=model,
messages=messages,
tools=[openai_tool],
)
print("\nFinal Response:")
print(response.choices[0].message.content or "")