-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathllm_tool_calling.py
More file actions
74 lines (61 loc) · 2.21 KB
/
llm_tool_calling.py
File metadata and controls
74 lines (61 loc) · 2.21 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
"""
LLM tool/function calling example.
Usage:
export OG_PRIVATE_KEY="your_private_key"
python examples/llm_tool_calling.py
"""
import asyncio
import os
import opengradient as og
async def main():
llm = og.LLM(private_key=os.environ.get("OG_PRIVATE_KEY"))
# Define a simple tool
tools = [
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "The city to find the weather for, e.g. 'San Francisco'",
},
"state": {
"type": "string",
"description": "The two-letter abbreviation for the state, e.g. 'CA'",
},
"unit": {
"type": "string",
"description": "The unit for temperature",
"enum": ["celsius", "fahrenheit"],
},
},
"required": ["city", "state", "unit"],
},
},
}
]
messages = [
{"role": "system", "content": "You are a helpful assistant. Use tools when needed."},
{"role": "user", "content": "What's the weather like in Dallas, Texas? Give me the temperature in fahrenheit."},
]
# One-time Permit2 approval for OPG spending (idempotent)
llm.ensure_opg_approval(opg_amount=0.1)
print("Testing Gemini tool calls...")
print(f"Model: {og.TEE_LLM.GEMINI_2_5_FLASH_LITE}")
print(f"Messages: {messages}")
print(f"Tools: {tools}")
print("-" * 50)
result = await llm.chat(
model=og.TEE_LLM.GEMINI_2_5_FLASH_LITE,
messages=messages,
tools=tools,
max_tokens=200,
)
print(f"Finish reason: {result.finish_reason}")
print(f"Chat output: {result.chat_output}")
print(f"Transaction hash: {result.transaction_hash}")
asyncio.run(main())