-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
78 lines (68 loc) · 2.42 KB
/
agent.py
File metadata and controls
78 lines (68 loc) · 2.42 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
75
76
77
78
"""
Anthropic Claude agent with Scalekit-authenticated Gmail tools.
Run: python python/frameworks/anthropic/agent.py
"""
import os
import anthropic
import scalekit.client
from dotenv import find_dotenv, load_dotenv
from google.protobuf.json_format import MessageToDict
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 = anthropic.Anthropic(
base_url=os.getenv("ANTHROPIC_BASE_URL"),
api_key=os.getenv("ANTHROPIC_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 = [
{
"name": MessageToDict(tool.tool).get("definition", {}).get("name"),
"description": MessageToDict(tool.tool).get("definition", {}).get("description", ""),
"input_schema": 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.messages.create(
model=os.getenv("ANTHROPIC_MODEL", "claude-sonnet-4-6"),
max_tokens=1024,
tools=llm_tools,
messages=messages,
)
if response.stop_reason == "end_turn":
print(response.content[0].text)
break
tool_results = []
for block in response.content:
if block.type == "tool_use":
result = actions.execute_tool(
tool_name=block.name,
identifier="user_123",
tool_input=block.input,
)
tool_results.append(
{
"type": "tool_result",
"tool_use_id": block.id,
"content": str(result.data),
}
)
messages.append({"role": "assistant", "content": response.content})
messages.append({"role": "user", "content": tool_results})