Skip to content

Commit 00186d8

Browse files
committed
first commit
0 parents  commit 00186d8

29 files changed

Lines changed: 4581 additions & 0 deletions

.gitignore

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
share/python-wheels/
24+
*.egg-info/
25+
.installed.cfg
26+
*.egg
27+
MANIFEST
28+
29+
# PyInstaller
30+
# Usually these files are written by a python script from a template
31+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
32+
*.manifest
33+
*.spec
34+
35+
# Installer logs
36+
pip-log.txt
37+
pip-delete-this-directory.txt
38+
39+
# Unit test / coverage reports
40+
htmlcov/
41+
.tox/
42+
.nox/
43+
.coverage
44+
.coverage.*
45+
.cache
46+
.pytest_cache/
47+
.hypothesis/
48+
49+
# Translations
50+
*.mo
51+
*.pot
52+
53+
# Django stuff:
54+
*.log
55+
local_settings.py
56+
db.sqlite3
57+
db.sqlite3-journal
58+
59+
# Flask stuff:
60+
instance/
61+
.webassets-cache
62+
63+
# Scrapy stuff:
64+
.scrapy
65+
66+
# Sphinx documentation
67+
docs/_build/
68+
69+
# Jupyter Notebook
70+
.ipynb_checkpoints
71+
72+
# IPython
73+
profile_default/
74+
ipython_config.py
75+
76+
# pyenv
77+
.python-version
78+
79+
# pipenv
80+
# According to Pylance, this should NOT be hidden
81+
# Pipfile.lock
82+
83+
# PEP 582; remove this if you decide to use it
84+
__pypackages__/
85+
86+
# Environments
87+
.env
88+
.venv
89+
env/
90+
venv/
91+
ENV/
92+
env.bak/
93+
venv.bak/
94+
95+
# IDE / Editor specific
96+
.idea/
97+
.vscode/*
98+
!.vscode/settings.json
99+
!.vscode/tasks.json
100+
!.vscode/launch.json
101+
!.vscode/extensions.json
102+
*.sublime-project
103+
*.sublime-workspace
104+
105+
# OS generated files
106+
.DS_Store
107+
.DS_Store?
108+
._*
109+
.Spotlight-V100
110+
.Trashes
111+
Thumbs.db
112+
ehthumbs.db
113+
114+
# Config files (sensitive information)
115+
.config/logai/config.ini
116+
*.log
117+
118+
# secrets
119+
secrets.py
120+
.secrets
121+
122+
# local run files
123+
results/
124+
logs/

LogManticsAI/__init__.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
# LogManticsAI package initializer
20+
21+
import logging
22+
23+
# Set up logging
24+
logging.basicConfig(
25+
level=logging.INFO,
26+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
27+
)
28+
29+
logger = logging.getLogger(__name__)
30+
31+
# Import key modules
32+
from .config.config_loader import (
33+
load_yaml_config,
34+
save_yaml_config,
35+
ensure_config_dir,
36+
get_config_dir,
37+
reset_config
38+
)
39+
40+
from .config.defaults import initialize_config_files
41+
from .cli_setup import setup
42+
from .monitoring import start_monitoring
43+
from .setup_tool import initial_log_analysis_and_key_id
44+
45+
# Initialize default configurations
46+
if not initialize_config_files():
47+
logger.warning("Failed to initialize default configurations. Some features may not work correctly.")
48+
49+
__version__ = '0.1.0'
50+
__all__ = [
51+
'setup',
52+
'start_monitoring',
53+
'initial_log_analysis_and_key_id',
54+
'load_yaml_config',
55+
'save_yaml_config',
56+
'ensure_config_dir',
57+
'get_config_dir',
58+
'reset_config'
59+
]

LogManticsAI/agno_utils.py

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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

Comments
 (0)