From 302dd7e9651f2b453d0312de554ae277ed659ad1 Mon Sep 17 00:00:00 2001 From: Joao Figueiredo Date: Sat, 3 May 2025 18:52:37 +0100 Subject: [PATCH] WIP: tunning --- .env.example | 12 ++++++------ ai_service/main.py | 10 +++++----- matrix_service/main.py | 28 +++++++++++++++------------- matrix_service/requirements.txt | 3 ++- 4 files changed, 28 insertions(+), 25 deletions(-) diff --git a/.env.example b/.env.example index 9f7fed6..2911e09 100644 --- a/.env.example +++ b/.env.example @@ -2,12 +2,12 @@ LOG_LEVEL=INFO # Matrix Configuration -MATRIX_HOMESERVER_URL = "https://matrix.org" -MATRIX_USER_ID = "@botbot_user:matrix.org" -MATRIX_PASSWORD = "botbot_password" -MATRIX_LOGIN_TRIES = 5 # Number of login attempts before giving up, default is 5 -MATRIX_LOGIN_DELAY_INCREMENT = 5 # Delay increment,in seconds, between login attempts, default is 5 -AI_HANDLER_URL = "http://ai_service:8000" +MATRIX_HOMESERVER_URL="https://matrix.org" +MATRIX_USER_ID="@botbot_user:matrix.org" +MATRIX_PASSWORD="botbot_password" +MATRIX_LOGIN_TRIES=5 # Number of login attempts before giving up, default is 5 +MATRIX_LOGIN_DELAY_INCREMENT=5 # Delay increment,in seconds, between login attempts, default is 5 +AI_HANDLER_URL="http://ai_service:8000" # OpenAI API Key OPENAI_API_KEY=your_openai_api_key_here diff --git a/ai_service/main.py b/ai_service/main.py index f785b1c..4566499 100644 --- a/ai_service/main.py +++ b/ai_service/main.py @@ -10,11 +10,11 @@ openai.api_key = os.environ["OPENAI_API_KEY"] r = redis.Redis.from_url(os.environ.get("REDIS_URL", "redis://redis:6379")) class MessagePayload(BaseModel): - roomId: str - userId: str - content: str - eventId: str - timestamp: int + roomId: str + userId: str + eventId: str + serverTimestamp: int + content: str app = FastAPI() diff --git a/matrix_service/main.py b/matrix_service/main.py index fdefdc6..aec13cd 100644 --- a/matrix_service/main.py +++ b/matrix_service/main.py @@ -11,14 +11,16 @@ from nio.responses import LoginResponse # --- Load environment variables --- load_dotenv() -MATRIX_HOMESERVER_URL = os.getenv("MATRIX_HOMESERVER_URL") -MATRIX_USER_ID = os.getenv("MATRIX_USER_ID") -MATRIX_PASSWORD = os.getenv("MATRIX_PASSWORD") -MATRIX_LOGIN_TRIES = int(os.getenv("MATRIX_LOGIN_TRIES", 5)) +MATRIX_HOMESERVER_URL = os.getenv("MATRIX_HOMESERVER_URL") +MATRIX_USER_ID = os.getenv("MATRIX_USER_ID") +MATRIX_PASSWORD = os.getenv("MATRIX_PASSWORD") +MATRIX_LOGIN_TRIES = int(os.getenv("MATRIX_LOGIN_TRIES", 5)) MATRIX_LOGIN_DELAY_INCREMENT = int(os.getenv("MATRIX_LOGIN_DELAY_INCREMENT", 5)) + LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO").upper() -AI_URL = os.getenv("AI_URL") -AI_TOKEN = os.getenv("AI_TOKEN") + +AI_HANDLER_URL = os.getenv("AI_HANDLER_URL") +AI_HANDLER_TOKEN = os.getenv("AI_HANDLER_TOKEN") @@ -76,15 +78,15 @@ async def main() -> None: if isinstance(event, RoomMessageText): logger.info(f"Received message in {room.room_id}: {event.body}") payload = { - "roomId": event["room_id"], - "userId": event["sender"], - "content": event["content"]["body"], - "eventId": event["event_id"], - "timestamp": event["origin_server_ts"] + "roomId": event.room_id, + "userId": event.sender, + "eventId": event.event_id, + "serverTimestamp": event.server_timestamp, + "content": event.body } - headers = {"Authorization": f"Bearer {AI_TOKEN}"} + headers = {"Authorization": f"Bearer {AI_HANDLER_TOKEN}"} async with httpx.AsyncClient() as http: - resp = await http.post(f"{AI_URL}/api/v1/message", json=payload, headers=headers) + resp = await http.post(f"{AI_HANDLER_URL}/api/v1/message", json=payload, headers=headers) resp.raise_for_status() data = resp.json() if data.get("reply"): diff --git a/matrix_service/requirements.txt b/matrix_service/requirements.txt index b634f8a..170594a 100644 --- a/matrix_service/requirements.txt +++ b/matrix_service/requirements.txt @@ -1,3 +1,4 @@ matrix-nio[e2e]>=0.25.0 python-dotenv>=1.0.0 -openai \ No newline at end of file +httpx>=0.23.0 +pydantic>=1.10 \ No newline at end of file