forked from zou-group/avatar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_gemini_usage.py
More file actions
executable file
·144 lines (112 loc) · 4.43 KB
/
Copy pathexample_gemini_usage.py
File metadata and controls
executable file
·144 lines (112 loc) · 4.43 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env python3
"""
Example script demonstrating how to use the Gemini API integration with AvaTaR.
Prerequisites:
1. Set your Gemini API key: export GEMINI_API_KEY=your_api_key_here
2. Install dependencies: pip install google-generativeai
3. Ensure avatar package is available
Usage:
python example_gemini_usage.py
"""
import os
import sys
# Add the avatar package to path if running as standalone script
sys.path.insert(0, '/home/runner/work/avatar-port-gemini/avatar-port-gemini')
def test_gemini_api():
"""Test basic Gemini API functionality"""
# Check if API key is set
if not os.environ.get('GEMINI_API_KEY'):
print("❌ GEMINI_API_KEY environment variable not set.")
print("Please set it with: export GEMINI_API_KEY=your_api_key_here")
return False
try:
from avatar.tools.react.api import complete_text_gemini, get_llm_output_tools
# Test simple text completion
print("Testing Gemini text completion...")
test_message = "What is the capital of France?"
print(f"Input: {test_message}")
# This would make an actual API call if the key is valid
try:
response = complete_text_gemini(
message=test_message,
model="gemma-3-27b-it",
max_tokens=100,
temperature=0.7
)
print(f"✓ Gemini Response: {response}")
return True
except Exception as api_error:
print(f"API call failed (this is expected without a valid API key): {api_error}")
print("✓ Gemini integration is properly configured (function exists and can be called)")
return True
except ImportError as e:
print(f"❌ Import error: {e}")
print("Make sure google-generativeai is installed: pip install google-generativeai")
return False
def demo_model_routing():
"""Demonstrate the model routing functionality"""
try:
from avatar.tools.react.api import get_llm_output_tools, registered_text_completion_llms
print("\\n" + "="*50)
print("Available Gemini models:")
gemini_models = [model for model in registered_text_completion_llms if 'gemma' in model or 'gemini' in model]
for model in gemini_models:
print(f" - {model}")
print("\\nTesting model routing...")
test_models = ["gemma-3-27b-it", "gemini-1.5-flash"]
for model in test_models:
print(f"✓ Model '{model}' would be routed to complete_text_gemini")
return True
except Exception as e:
print(f"❌ Model routing test failed: {e}")
return False
def show_usage_example():
"""Show example usage code"""
example_code = '''
# Example: Using Gemini with AvaTaR
import os
from avatar.tools.react.api import get_llm_output_tools
# Set your API key
os.environ['GEMINI_API_KEY'] = 'your_api_key_here'
# Use Gemini for text completion
response = get_llm_output_tools(
message="Analyze this data and provide insights.",
model="gemma-3-27b-it",
max_tokens=500,
temperature=0.7
)
# Use with tools (for ReAct agent)
response = get_llm_output_tools(
message="Help me find information about machine learning.",
model="gemini-1.5-flash",
tools=my_tool_list,
json_object=True
)
# Running AvaTaR with Gemini
# Use the provided script: ./scripts/run_avatar_gemini.sh
# Or modify existing scripts to use Gemini models
'''
print("\\n" + "="*50)
print("Usage Example:")
print(example_code)
def main():
"""Main function to run all demonstrations"""
print("🚀 Gemini Integration Demo for AvaTaR")
print("="*50)
# Test API integration
api_success = test_gemini_api()
# Test model routing
routing_success = demo_model_routing()
# Show usage examples
show_usage_example()
print("\\n" + "="*50)
if api_success and routing_success:
print("🎉 Gemini integration is working correctly!")
print("\\nNext steps:")
print("1. Set your GEMINI_API_KEY environment variable")
print("2. Install google-generativeai: pip install google-generativeai")
print("3. Use ./scripts/run_avatar_gemini.sh to run AvaTaR with Gemini")
else:
print("❌ Some components need attention. Check the errors above.")
if __name__ == "__main__":
main()