-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathagents.py
More file actions
44 lines (32 loc) · 1.3 KB
/
agents.py
File metadata and controls
44 lines (32 loc) · 1.3 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
import json
from typing import Any
from agents import FunctionTool, RunContextWrapper
from thirdweb_ai import Tool
def _get_openai_schema(schema: Any):
if not isinstance(schema, dict):
return None
schema["additionalProperties"] = False
if "properties" in schema:
schema["required"] = list(schema["properties"].keys())
for prop in schema["properties"].values():
if isinstance(prop, dict):
prop.pop("default", None)
_get_openai_schema(prop)
for field in ["anyOf", "oneOf", "allOf"]:
if field in schema:
for subschema in schema[field]:
_get_openai_schema(subschema)
return schema
def get_agents_tools(tools: list[Tool]):
def _get_tool(tool: Tool):
async def _invoke_tool(ctx: RunContextWrapper[Any], tool_input: str, _t: Tool = tool) -> str:
input_args = json.loads(tool_input) if tool_input else {}
return _t.return_value_as_string(_t.run_json(input_args))
schema = _get_openai_schema(tool.schema.get("parameters"))
return FunctionTool(
name=tool.name,
description=tool.description,
params_json_schema=schema or {},
on_invoke_tool=_invoke_tool,
)
return [_get_tool(tool) for tool in tools]