forked from universal-tool-calling-protocol/code-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_usage.py
More file actions
97 lines (79 loc) 路 2.51 KB
/
Copy pathbasic_usage.py
File metadata and controls
97 lines (79 loc) 路 2.51 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"""
Basic usage example for CodeModeClient.
This demonstrates simple code execution with tool access.
"""
import asyncio
from code_mode import CodeModeClient
from code_mode.code_mode_client import Tool
async def mock_calculator_add(args):
"""Mock calculator add function."""
return {'result': args['a'] + args['b']}
async def mock_calculator_multiply(args):
"""Mock calculator multiply function."""
return {'result': args['a'] * args['b']}
async def main():
# Create client
client = CodeModeClient.create()
# Register mock tools
add_tool = Tool(
name='calculator.add',
description='Adds two numbers',
inputs={
'type': 'object',
'properties': {
'a': {'type': 'number', 'description': 'First number'},
'b': {'type': 'number', 'description': 'Second number'}
},
'required': ['a', 'b']
},
outputs={
'type': 'object',
'properties': {
'result': {'type': 'number', 'description': 'Sum'}
}
},
tags=['math'],
tool_call_template={'call_template_type': 'mock'}
)
multiply_tool = Tool(
name='calculator.multiply',
description='Multiplies two numbers',
inputs={
'type': 'object',
'properties': {
'a': {'type': 'number', 'description': 'First number'},
'b': {'type': 'number', 'description': 'Second number'}
},
'required': ['a', 'b']
},
outputs={
'type': 'object',
'properties': {
'result': {'type': 'number', 'description': 'Product'}
}
},
tags=['math'],
tool_call_template={'call_template_type': 'mock'}
)
client.add_tool(add_tool, mock_calculator_add)
client.add_tool(multiply_tool, mock_calculator_multiply)
# Execute code that uses tools
code = """
# Simple arithmetic using tools
a = await calculator.add(a=5, b=3)
b = await calculator.multiply(a=a['result'], b=2)
print(f"(5 + 3) * 2 = {b['result']}")
return {
'addition_result': a['result'],
'final_result': b['result']
}
"""
response = await client.call_tool_chain(code)
print("\n=== Execution Result ===")
print(f"Result: {response['result']}")
print(f"\n=== Console Logs ===")
for log in response['logs']:
print(log)
await client.close()
if __name__ == "__main__":
asyncio.run(main())