-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
101 lines (85 loc) · 3.26 KB
/
model.py
File metadata and controls
101 lines (85 loc) · 3.26 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
#create a model using gemini llm to use the model in the agent
import os
from typing import Optional, Dict, Any
from dotenv import load_dotenv
import google.generativeai as genai
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_core.language_models.chat_models import BaseChatModel
from IPython.display import display, Markdown
from langchain_core.messages import AIMessage
def create_gemini_model(
model_name: str = "gemini-1.5-flash",
temperature: float = 0.1,
max_tokens: Optional[int] = None,
top_p: float = 0.8,
top_k: int = 40,
api_key: Optional[str] = None
) -> BaseChatModel:
"""
Create a Gemini model instance for use in the agent.
Args:
model_name (str): The Gemini model to use (default: "gemini-1.5-flash")
temperature (float): Controls randomness in responses (0.0-1.0)
max_tokens (Optional[int]): Maximum tokens in response
top_p (float): Nucleus sampling parameter
top_k (int): Top-k sampling parameter
api_key (Optional[str]): Google API key (will use env var if not provided)
Returns:
BaseChatModel: Configured Gemini chat model
Raises:
ValueError: If API key is not found
Exception: If model creation fails
"""
# Load environment variables
load_dotenv()
# Get API key
api_key = api_key or os.getenv("GOOGLE_API_KEY")
if not api_key:
raise ValueError(
"Google API key not found. Please set GOOGLE_API_KEY environment variable "
"or pass api_key parameter."
)
try:
# Configure the model
model = ChatGoogleGenerativeAI(
model=model_name,
google_api_key=api_key,
temperature=temperature,
max_output_tokens=max_tokens,
top_p=top_p,
top_k=top_k,
convert_system_message_to_human=True,
verbose=True
)
print(f"✅ Successfully created Gemini model: {model_name}")
print(f" Temperature: {temperature}")
print(f" Max tokens: {max_tokens or 'Not set'}")
print(f" Top-p: {top_p}")
print(f" Top-k: {top_k}")
return model
except Exception as e:
print(f"❌ Error creating Gemini model: {str(e)}")
raise Exception(f"Failed to create Gemini model: {str(e)}")
# Example usage and testing
if __name__ == "__main__":
# Create a model for math tutoring
model = create_gemini_model(model_name="gemini-2.5-pro")
result = model.invoke("How to find the area of a circle?")
print(f"Result_type: {type(result)}")
# Extract and print the actual content
if isinstance(result, AIMessage):
content = result.content
if isinstance(content, list):
# If content is a list (e.g., multiple text blocks), join them
combined = "\n\n".join([str(item) for item in content])
else:
# If content is a string, use it directly
combined = str(content)
print("\n" + "="*50)
print("🤖 GEMINI RESPONSE:")
print("="*50)
print(combined)
print("="*50)
else:
print(f"Unexpected result type: {type(result)}")
print(f"Result: {result}")