2024-04-07 22:27:00 -06:00
|
|
|
import logging
|
|
|
|
|
2024-04-10 22:47:15 -06:00
|
|
|
from nio import MatrixRoom, Event
|
|
|
|
|
2024-04-09 19:26:44 -06:00
|
|
|
from matrix_gpt import MatrixClientHelper
|
2024-04-07 22:27:00 -06:00
|
|
|
from matrix_gpt.config import global_config
|
|
|
|
from matrix_gpt.generate_clients.anthropic import AnthropicApiClient
|
2024-04-10 16:42:52 -06:00
|
|
|
from matrix_gpt.generate_clients.copilot import CopilotClient
|
2024-04-07 22:27:00 -06:00
|
|
|
from matrix_gpt.generate_clients.openai import OpenAIClient
|
|
|
|
|
|
|
|
"""
|
|
|
|
Global variable to sync importing and sharing the configured module.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
class ApiClientManager:
|
|
|
|
def __init__(self):
|
|
|
|
self._openai_api_key = None
|
|
|
|
self._openai_api_base = None
|
|
|
|
self._anth_api_key = None
|
|
|
|
self.logger = logging.getLogger('MatrixGPT').getChild('ApiClientManager')
|
|
|
|
|
|
|
|
def _set_from_config(self):
|
|
|
|
"""
|
|
|
|
Have to update the config because it may not be instantiated yet.
|
|
|
|
"""
|
|
|
|
self._openai_api_key = global_config['openai'].get('api_key', 'MatrixGPT')
|
|
|
|
self._anth_api_key = global_config['anthropic'].get('api_key')
|
2024-04-10 16:42:52 -06:00
|
|
|
self._copilot_cookie = global_config['copilot'].get('api_key')
|
2024-04-07 22:27:00 -06:00
|
|
|
|
2024-04-10 22:47:15 -06:00
|
|
|
def get_client(self, mode: str, client_helper: MatrixClientHelper, room: MatrixRoom, event: Event):
|
2024-04-07 22:27:00 -06:00
|
|
|
if mode == 'openai':
|
2024-04-10 22:47:15 -06:00
|
|
|
return self.openai_client(client_helper, room, event)
|
2024-04-10 16:42:52 -06:00
|
|
|
elif mode == 'anthropic':
|
2024-04-10 22:47:15 -06:00
|
|
|
return self.anth_client(client_helper, room, event)
|
2024-04-10 16:42:52 -06:00
|
|
|
elif mode == 'copilot':
|
2024-04-10 22:47:15 -06:00
|
|
|
return self.copilot_client(client_helper, room, event)
|
2024-04-07 22:27:00 -06:00
|
|
|
else:
|
|
|
|
raise Exception
|
|
|
|
|
2024-04-10 22:47:15 -06:00
|
|
|
def openai_client(self, client_helper: MatrixClientHelper, room: MatrixRoom, event: Event):
|
2024-04-07 22:27:00 -06:00
|
|
|
self._set_from_config()
|
|
|
|
if not self._openai_api_key:
|
|
|
|
self.logger.error('Missing an OpenAI API key!')
|
|
|
|
return None
|
|
|
|
return OpenAIClient(
|
|
|
|
api_key=self._openai_api_key,
|
2024-04-10 22:47:15 -06:00
|
|
|
client_helper=client_helper,
|
|
|
|
room=room,
|
|
|
|
event=event
|
2024-04-07 22:27:00 -06:00
|
|
|
)
|
|
|
|
|
2024-04-10 22:47:15 -06:00
|
|
|
def anth_client(self, client_helper: MatrixClientHelper, room: MatrixRoom, event: Event):
|
2024-04-07 22:27:00 -06:00
|
|
|
self._set_from_config()
|
|
|
|
if not self._anth_api_key:
|
|
|
|
self.logger.error('Missing an Anthropic API key!')
|
|
|
|
return None
|
|
|
|
return AnthropicApiClient(
|
|
|
|
api_key=self._anth_api_key,
|
2024-04-10 22:47:15 -06:00
|
|
|
client_helper=client_helper,
|
|
|
|
room=room,
|
|
|
|
event=event
|
2024-04-07 22:27:00 -06:00
|
|
|
)
|
|
|
|
|
2024-04-10 22:47:15 -06:00
|
|
|
def copilot_client(self, client_helper, room: MatrixRoom, event: Event):
|
2024-04-10 16:42:52 -06:00
|
|
|
self._set_from_config()
|
|
|
|
if not self._copilot_cookie:
|
|
|
|
self.logger.error('Missing a Copilot API key!')
|
|
|
|
return None
|
|
|
|
return CopilotClient(
|
|
|
|
api_key=self._copilot_cookie,
|
|
|
|
client_helper=client_helper,
|
2024-04-10 22:47:15 -06:00
|
|
|
room=room,
|
|
|
|
event=event
|
2024-04-10 16:42:52 -06:00
|
|
|
)
|
|
|
|
|
2024-04-07 22:27:00 -06:00
|
|
|
|
|
|
|
api_client_helper = ApiClientManager()
|