-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
73 lines (66 loc) · 2.31 KB
/
agent.py
File metadata and controls
73 lines (66 loc) · 2.31 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
"""
OpenAI-compatible agent with Scalekit-authenticated Gmail tools.
Run: python python/frameworks/openai/agent.py
"""
import json
import os
import scalekit.client
from dotenv import find_dotenv, load_dotenv
from google.protobuf.json_format import MessageToDict
from openai import OpenAI
load_dotenv(find_dotenv())
scalekit_client = scalekit.client.ScalekitClient(
client_id=os.getenv("SCALEKIT_CLIENT_ID"),
client_secret=os.getenv("SCALEKIT_CLIENT_SECRET"),
env_url=os.getenv("SCALEKIT_ENVIRONMENT_URL"),
)
actions = scalekit_client.actions
client = OpenAI(base_url=os.getenv("OPENAI_BASE_URL"), api_key=os.getenv("OPENAI_API_KEY"))
response = actions.get_or_create_connected_account(
connection_name="gmail",
identifier="user_123",
)
if response.connected_account.status != "ACTIVE":
link = actions.get_authorization_link(connection_name="gmail", identifier="user_123")
print("Authorize Gmail:", link.link)
input("Press Enter after authorizing...")
scoped_response, _ = actions.tools.list_scoped_tools(
identifier="user_123",
filter={"connection_names": ["gmail"]},
)
llm_tools = [
{
"type": "function",
"function": {
"name": MessageToDict(tool.tool).get("definition", {}).get("name"),
"description": MessageToDict(tool.tool).get("definition", {}).get("description", ""),
"parameters": MessageToDict(tool.tool).get("definition", {}).get("input_schema", {}),
},
}
for tool in scoped_response.tools
]
messages = [{"role": "user", "content": "Fetch my last 5 unread emails and summarize them"}]
while True:
response = client.chat.completions.create(
model=os.getenv("OPENAI_MODEL", "claude-sonnet-4-6"),
tools=llm_tools,
messages=messages,
)
message = response.choices[0].message
if not message.tool_calls:
print(message.content)
break
messages.append(message)
for tool_call in message.tool_calls:
result = actions.execute_tool(
tool_name=tool_call.function.name,
identifier="user_123",
tool_input=json.loads(tool_call.function.arguments),
)
messages.append(
{
"role": "tool",
"tool_call_id": tool_call.id,
"content": str(result.data),
}
)