-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom_tools.py
More file actions
80 lines (57 loc) · 2.07 KB
/
custom_tools.py
File metadata and controls
80 lines (57 loc) · 2.07 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
#!/usr/bin/env python3
"""
Custom tools example - define Python functions that Copilot can call.
Demonstrates tool/function calling with the Copilot SDK.
"""
import asyncio
from datetime import datetime
from pydantic import BaseModel, Field
from copilot import CopilotClient, define_tool
# Define custom tools using Pydantic models for parameters
class CalculateParams(BaseModel):
expression: str = Field(description="A mathematical expression to evaluate")
class ReverseStringParams(BaseModel):
text: str = Field(description="The string to reverse")
@define_tool(description="Get the current time in ISO format")
def get_current_time() -> str:
return datetime.now().isoformat()
@define_tool(description="Safely evaluate a mathematical expression")
def calculate(params: CalculateParams) -> str:
try:
# Only allow safe math operations
result = eval(params.expression, {"__builtins__": {}}, {})
return str(result)
except Exception as e:
return f"Error: {str(e)}"
@define_tool(description="Reverse a string")
def reverse_string(params: ReverseStringParams) -> str:
return params.text[::-1]
async def main():
"""Demonstrate custom tools with Copilot."""
client = CopilotClient()
await client.start()
try:
session = await client.create_session({
"model": "gpt-5-mini",
"tools": [get_current_time, calculate, reverse_string],
})
print("🔧 Copilot with Custom Tools\n")
print("Available tools:")
print(" - get_current_time()")
print(" - calculate(expression)")
print(" - reverse_string(text)\n")
prompt = """Please:
1. Tell me the current time
2. Calculate 42 * 137
3. Reverse the string 'Hello Copilot'
"""
print(f"📝 Task: {prompt}\n")
print("🤖 Response:\n")
response = await session.send_and_wait({"prompt": prompt})
print(response.data.content)
print("\n✅ Done!")
await session.destroy()
finally:
await client.stop()
if __name__ == "__main__":
asyncio.run(main())