1+ """
2+ LogManticsAI
3+ Copyright (C) 2024 LogManticsAI
4+
5+ This program is free software: you can redistribute it and/or modify
6+ it under the terms of the GNU Affero General Public License as
7+ published by the Free Software Foundation, either version 3 of the
8+ License, or (at your option) any later version.
9+
10+ This program is distributed in the hope that it will be useful,
11+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+ GNU Affero General Public License for more details.
14+
15+ You should have received a copy of the GNU Affero General Public License
16+ along with this program. If not, see <https://www.gnu.org/licenses/>
17+ """
18+
19+ """
20+ Utilities for working with Agno models and agents.
21+ This module adapts functionality from the reference project, simplified for LogAI needs.
22+ """
23+
24+ from agno .agent import Agent
25+ from agno .models .openai import OpenAIChat
26+ import logging
27+ import os
28+ from typing import Dict , Any , Optional , List
29+
30+ logger = logging .getLogger (__name__ )
31+
32+ def create_model (model_type : str , api_key : str , model_name : str , max_tokens : int = 1000 ) -> Any :
33+ """
34+ Create and return the specified Agno model based on model_type.
35+
36+ Args:
37+ model_type: The type of model ('OPENAI', 'ANTHROPIC', etc.)
38+ api_key: The API key
39+ model_name: The name/ID of the model (e.g., 'gpt-4', 'claude-3-sonnet')
40+ max_tokens: Maximum tokens for model output
41+
42+ Returns:
43+ The initialized Agno model object
44+
45+ Raises:
46+ ValueError: If the model type is not supported
47+ """
48+ model_type = model_type .upper ()
49+
50+ try :
51+ if model_type == 'OPENAI' :
52+ return OpenAIChat (api_key = api_key , id = model_name , max_tokens = max_tokens )
53+ elif model_type == 'ANTHROPIC' :
54+ from agno .models .anthropic import Claude
55+ return Claude (api_key = api_key , id = model_name , max_tokens = max_tokens )
56+ elif model_type == 'GEMINI' or model_type == 'GOOGLE' :
57+ from agno .models .google import Gemini
58+ return Gemini (api_key = api_key , id = model_name , max_output_tokens = max_tokens )
59+ elif model_type == 'GROQ' :
60+ from agno .models .groq import Groq
61+ return Groq (api_key = api_key , id = model_name , max_tokens = max_tokens )
62+
63+ else :
64+ raise ValueError (f"Unsupported model type: { model_type } " )
65+ except ImportError as e :
66+ logger .error (f"Import error when creating model type { model_type } : { str (e )} " )
67+ raise RuntimeError (f"Model type { model_type } is not available in this installation: { str (e )} " )
68+ except Exception as e :
69+ logger .error (f"Error creating model type { model_type } : { str (e )} " )
70+ raise RuntimeError (f"Failed to initialize model: { str (e )} " )
71+
72+ async def test_model_api_key (api_key : str , model_type : str , model_name : str ) -> bool :
73+ """
74+ Test if the API key is valid for the given model type.
75+
76+ Args:
77+ api_key: The API key to test
78+ model_type: The type of model
79+ model_name: The name of the model
80+
81+ Returns:
82+ bool: True if the API key is valid, False otherwise
83+ """
84+ try :
85+ # Create a simple model and agent for testing
86+ model = create_model (model_type , api_key , model_name )
87+ test_agent = Agent (
88+ name = "LogAI Test Agent" ,
89+ model = model ,
90+ instructions = "You are a test agent for LogAI. Just respond with 'Test successful.'" ,
91+ debug_mode = False
92+ )
93+
94+ # Run a simple test query
95+ await test_agent .arun (message = "This is a test message to verify API key validity." )
96+ return True
97+ except Exception as e :
98+ logger .error (f"API key test failed for { model_type } /{ model_name } : { str (e )} " )
99+ return False
100+
101+ def create_log_analysis_agent (model_type : str , api_key : str , model_name : str , important_keys : List [str ], instructions : Optional [List [str ]] = None ) -> Agent :
102+ """
103+ Create an Agno agent specifically for log analysis.
104+
105+ Args:
106+ model_type: The type of model ('OPENAI', 'ANTHROPIC', etc.)
107+ api_key: The API key
108+ model_name: The name/ID of the model
109+ important_keys: List of keys identified as important for analysis
110+ instructions: Optional list of additional instructions for the agent
111+
112+ Returns:
113+ An Agno agent configured for log analysis
114+ """
115+ model = create_model (model_type , api_key , model_name , max_tokens = 1500 )
116+
117+ # Default instructions for log analysis
118+ default_instructions = [
119+ "You are an expert log analysis assistant for LogAI." ,
120+ "Analyze log entries for anomalies, errors, and security concerns." ,
121+ f"Focus on these important keys: { ', ' .join (important_keys )} " ,
122+ "Provide clear, concise descriptions of any issues you detect." ,
123+ "When appropriate, suggest possible root causes and solutions."
124+ ]
125+
126+ # Combine default and custom instructions
127+ if instructions :
128+ agent_instructions = default_instructions + instructions
129+ else :
130+ agent_instructions = default_instructions
131+
132+ # Create and return the agent
133+ return Agent (
134+ name = "LogAI Analysis Agent" ,
135+ model = model ,
136+ instructions = agent_instructions ,
137+ debug_mode = False
138+ )
0 commit comments