forked from KarmaloopAI/Jiva
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
176 lines (153 loc) · 5.28 KB
/
main.py
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# main.py
import json
import logging
import os
import asyncio
import uvicorn
from api.main import app
from typing import Dict, Any
from core.agent import Agent
from actions.action_registry import get_action_registry
logger = logging.getLogger("Jiva.Main")
def load_config(config_file: str = 'config.json') -> Dict[str, Any]:
"""
Load configuration from a JSON file, with fallback to default values.
Args:
config_file (str): Path to the configuration JSON file. Defaults to 'config.json'.
Returns:
Dict[str, Any]: A dictionary containing the configuration.
"""
# Default configuration
default_config = {
'memory': {
'qdrant_host': 'localhost',
'qdrant_port': 6333,
'collection_name': 'jiva_memories',
'max_short_term_memory': 100
},
'llm': {
'api_base_url': 'http://localhost:11434/api',
'model': 'gemma2',
'max_retries': 3,
'timeout': 90
},
'sensors': {
'chat_interface': {
'prompt': "Jiva> "
}
},
'memory_consolidation_threshold': 2,
'actions': {},
'agent_loop_delay': 0.1,
'awake_duration': 80,
'sleep_duration': 20,
}
# Load configuration from file if it exists
if os.path.exists(config_file):
try:
with open(config_file, 'r') as f:
file_config = json.load(f)
# Deep update of default config with file config
config = deep_update(default_config, file_config)
print(f"Configuration loaded from {config_file}")
except json.JSONDecodeError as e:
print(f"Error decoding {config_file}: {e}. Using default configuration.")
config = default_config
except Exception as e:
print(f"Error reading {config_file}: {e}. Using default configuration.")
config = default_config
else:
print(f"Configuration file {config_file} not found. Using default configuration.")
config = default_config
return config
def deep_update(d: Dict[str, Any], u: Dict[str, Any]) -> Dict[str, Any]:
"""
Perform a deep update of one dictionary with another.
Args:
d (Dict[str, Any]): The original dictionary to update.
u (Dict[str, Any]): The dictionary with updates.
Returns:
Dict[str, Any]: The updated dictionary.
"""
for k, v in u.items():
if isinstance(v, dict):
d[k] = deep_update(d.get(k, {}), v)
else:
d[k] = v
return d
def setup_environment():
# Create necessary directories
os.makedirs('data', exist_ok=True)
os.makedirs('logs', exist_ok=True)
def print_welcome_message():
infinity_symbol = """
@@@@@@ @@@@@@
@@ @@ @@ @@
@@ @@ @@ @@
@@ @@ @@ @@
@@ @@ @@ @@
@@ @@@@ @@
@@ @@ @@ @@
@@ @@ @@ @@
@@ @@ @@ @@
@@ @@ @@ @@
@@@@@@ @@@@@@
"""
print(infinity_symbol)
print("Welcome to the Jiva Framework!")
print("Embracing the infinite potential of ethical AI")
print("--------------------------------------------")
async def run_agent_and_api(agent: Agent, host: str = "0.0.0.0", port: int = 8000):
"""
Run both the Jiva agent and the API server concurrently.
Args:
agent (Agent): The Jiva agent instance
host (str): Host address to bind the API server
port (int): Port number for the API server
"""
# Store agent instance in FastAPI app state
app.state.agent = agent
# Configure the uvicorn server
config = uvicorn.Config(
app,
host=host,
port=port,
loop="asyncio",
log_level="info"
)
server = uvicorn.Server(config)
# Run both the agent and API server
try:
await asyncio.gather(
agent.run(),
server.serve()
)
except Exception as e:
logging.error(f"Error running agent and API: {str(e)}")
raise
def main():
print("Initializing Jiva Framework...")
setup_environment()
config = load_config()
# Get API configuration from environment or config
host = os.environ.get('API_HOST', config.get('api', {}).get('host', '0.0.0.0'))
port = int(os.environ.get('API_PORT', config.get('api', {}).get('port', 8000)))
logger.info("Creating Agent...")
agent = Agent(config)
# Register file actions
actions = get_action_registry(agent.llm_interface, agent.memory)
for action_name, action_func in actions.items():
print(f"Discovered action: {action_name}")
print_welcome_message()
print("Jiva is ready. Starting main loop...")
logger.info(f"Starting main loop and API server on {host}:{port}...")
print("(Press CTRL+C to exit)")
try:
asyncio.run(run_agent_and_api(agent, host, port))
except KeyboardInterrupt:
logger.info("\nShutting down Jiva...")
finally:
# Perform any cleanup if necessary
pass
if __name__ == "__main__":
main()