-
Notifications
You must be signed in to change notification settings - Fork 2
/
main_local.py
99 lines (79 loc) · 2.52 KB
/
main_local.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
import asyncio
import multiprocessing
import signal
import time
from util import get_environment_variable
from logging_util import setup_logger
import os
from dotenv import load_dotenv
from llm.LLM import LLM
from ARCANE.agent_factory import AgentFactory
from channels.web.fastapi_app import FastApiApp
import shutil
def clear_llm_logs():
log_folder = "llm_logs"
if os.path.exists(log_folder):
shutil.rmtree(log_folder)
os.makedirs(log_folder)
print(f"Cleared contents of {log_folder}")
def run_server(agent_config, common_actions, llm_config, port, api_key, agent_factory):
from llm.LLM import LLM
from channels.web.fastapi_app import FastApiApp
logger = setup_logger(agent_config["name"])
llm = LLM(logger, **llm_config)
agent = agent_factory.create_agent(
agent_config, common_actions, llm, logger, api_key
)
app = FastApiApp(agent, llm, port)
asyncio.run(app.run())
def main():
load_dotenv()
logger = setup_logger("SYS")
agent_factory = AgentFactory()
# Clear LLM logs before starting
clear_llm_logs()
anthropic_api_key = get_environment_variable("ANT_API_KEY")
agent_configs = agent_factory.load_agent_config()
llm_config = {
"api_key": os.getenv("ANT_API_KEY"),
"model": os.getenv("CLAUDE_DEFAULT_MODEL"),
}
common_actions = agent_configs["common_actions"]
processes = []
for agent_config in agent_configs["agents"]:
p = multiprocessing.Process(
target=run_server,
args=(
agent_config,
common_actions,
llm_config,
agent_config["port"],
anthropic_api_key,
agent_factory,
),
)
p.start()
processes.append((p, agent_config["name"]))
logger.info(
{
"event": "server_start",
"name": agent_config["name"],
"port": agent_config["port"],
}
)
def shutdown(signal, frame):
logger.info("Shutdown signal received.")
for p, name in processes:
p.terminate()
logger.info({"event": "server_terminate", "name": name})
for p, name in processes:
p.join()
signal.signal(signal.SIGINT, shutdown)
signal.signal(signal.SIGTERM, shutdown)
try:
while any(p.is_alive() for p, _ in processes):
time.sleep(1)
except KeyboardInterrupt:
shutdown(None, None)
if __name__ == "__main__":
main()