Enable multiple llms

This commit is contained in:
Niels Geens
2024-11-22 15:05:54 +01:00
parent bbb88f39e8
commit 8766a945c0
28 changed files with 972 additions and 525 deletions

View File

@@ -11,18 +11,13 @@ from .openai_llm_engine import OpenAILlmEngine
from .response_parser import ResponseParser
from .system_metrics import SystemMetrics
from .web_agent import WebAgent
from .web_agent import WebAgent
from .web_io_buffer import WebIOBuffer
from .web_io_buffer import WebIOBuffer
from .web_socket_manager import WebSocketManager
from .web.api import Api
from .web.static import Static
from .web.websockts import Websockets
from .working_memory import WorkingMemory
from .xml_validator import XMLValidator
mimetypes.add_type("application/javascript", ".js")
mimetypes.add_type("application/javascript", ".jsx")
mimetypes.add_type("text/javascript", ".js")
mimetypes.add_type("text/javascript", ".jsx")
class Main:
@classmethod
async def create(cls, config: Config):
@@ -32,68 +27,65 @@ class Main:
self._system_prompt = self._config.system_prompt.read_text()
self._action_schema = self._config.action_schema.read_text()
match self._config.llm_engine:
case "local":
self._llm = LocalLlmEngine(
self._config.model,
self._config.temperature,
self._config.token_limit
)
case "hf":
self._llm = HfLlmEngine(
self._config.model,
self._config.temperature,
self._config.api_token,
)
case "openai":
self._llm = OpenAILlmEngine(
self._config.model,
self._config.temperature,
self._config.token_limit,
self._config.api_token,
)
case "mistral":
self._llm = MistralLlmEngine(
self._config.model,
self._config.temperature,
self._config.api_token,
self._config.token_limit
)
case _:
raise ValueError(f"Invalid LLM engine: {self._config.llm_engine}")
# Initialize LLM engines based on config
self._llms = {}
if config.local_enabled:
self._llms['local'] = LocalLlmEngine(
config.local_model,
config.local_temperature,
config.local_token_limit
)
if config.openai_enabled:
self._llms['openai'] = OpenAILlmEngine(
config.openai_model,
config.openai_temperature,
config.openai_token_limit,
config.openai_api_key,
)
if config.hf_enabled:
self._llms['hf'] = HfLlmEngine(
config.hf_model,
config.hf_temperature,
config.hf_api_key,
)
if config.mistral_enabled:
self._llms['mistral'] = MistralLlmEngine(
config.mistral_model,
config.mistral_temperature,
config.mistral_token_limit,
config.mistral_api_key,
)
if not self._llms:
raise ValueError("No LLM engines enabled in configuration")
self._io_buffer = WebIOBuffer()
self._agent = WebAgent(
system_prompt=self._system_prompt,
action_schema=self._action_schema,
working_memory=WorkingMemory(),
metrics=SystemMetrics(),
llm=self._llm,
llms=self._llms,
validator=XMLValidator(self._action_schema),
parser=ResponseParser(self._io_buffer),
iteration_logger=IterationLogger(self._config.iterations_dir, self._system_prompt, self._action_schema),
)
self._ws_manager = await WebSocketManager.create(
self._agent,
self._io_buffer
)
self._app = web.Application()
self._init_routes()
self._api = Api(self._app, self._agent, self._io_buffer)
self._websockets = Websockets(self._app, self._agent, self._io_buffer)
self._static = Static(self._app, self._config)
return self
@property
def app(self):
return self._app
def _init_routes(self):
"""Initialize application routes."""
self._app.middlewares.append(self._cors_middleware)
self._app.router.add_get("/ws", self._ws_manager.handle_websocket)
self._app.router.add_get("/", self._serve_index)
self._app.router.add_static("/static/", self._config.static_files, show_index=False)
self._app.router.add_static("/assets/", self._config.static_files / "assets", show_index=False)
self._app.router.add_get("/{path:.*}", self._serve_index)
async def _serve_index(self, request: web.Request) -> web.Response:
"""Serve the React application HTML for any unmatched routes."""
index_path = self._config.static_files / "index.html"
@@ -108,22 +100,6 @@ class Main:
content_type="text/html"
)
@web.middleware
async def _cors_middleware(self, request: web.Request, handler):
"""Handle CORS headers."""
if request.method == "OPTIONS":
response = web.Response()
else:
response = await handler(request)
response.headers.update({
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Max-Age': '86400',
})
return response
if __name__ == "__main__":
loop = asyncio.new_event_loop()
config = Config()