From a6c2e67c6abdeb7658287c506f4ed50f44443f69 Mon Sep 17 00:00:00 2001 From: Claude AI Date: Thu, 29 Jan 2026 22:05:10 +0000 Subject: [PATCH] refactor(ai): replace OpenAI with DeepSeek API - Update config.py with DEEPSEEK_API_KEY, DEEPSEEK_MODEL, DEEPSEEK_BASE_URL - Update ai.py to use DeepSeek endpoint (OpenAI-compatible) - Update docker-compose.yml environment variables --- docker-compose.yml | 5 +++-- services/flow-engine/app/config.py | 7 ++++--- services/flow-engine/app/nodes/ai.py | 22 +++++++++++----------- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 607a233..e86e7bc 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -92,8 +92,9 @@ services: REDIS_URL: redis://redis:6379 API_GATEWAY_URL: http://api-gateway:8000 WHATSAPP_CORE_URL: http://whatsapp-core:3001 - OPENAI_API_KEY: ${OPENAI_API_KEY:-} - OPENAI_MODEL: ${OPENAI_MODEL:-gpt-4o-mini} + DEEPSEEK_API_KEY: ${DEEPSEEK_API_KEY:-} + DEEPSEEK_MODEL: ${DEEPSEEK_MODEL:-deepseek-chat} + DEEPSEEK_BASE_URL: ${DEEPSEEK_BASE_URL:-https://api.deepseek.com} depends_on: postgres: condition: service_healthy diff --git a/services/flow-engine/app/config.py b/services/flow-engine/app/config.py index bd5a365..396f48e 100644 --- a/services/flow-engine/app/config.py +++ b/services/flow-engine/app/config.py @@ -6,9 +6,10 @@ class Settings(BaseSettings): DATABASE_URL: str = "postgresql://whatsapp_admin:password@localhost:5432/whatsapp_central" REDIS_URL: str = "redis://localhost:6379" - # OpenAI - OPENAI_API_KEY: str = "" - OPENAI_MODEL: str = "gpt-3.5-turbo" + # DeepSeek AI (OpenAI-compatible API) + DEEPSEEK_API_KEY: str = "" + DEEPSEEK_MODEL: str = "deepseek-chat" + DEEPSEEK_BASE_URL: str = "https://api.deepseek.com" API_GATEWAY_URL: str = "http://localhost:8000" WHATSAPP_CORE_URL: str = "http://localhost:3001" diff --git a/services/flow-engine/app/nodes/ai.py b/services/flow-engine/app/nodes/ai.py index c660e28..989970f 100644 --- a/services/flow-engine/app/nodes/ai.py +++ b/services/flow-engine/app/nodes/ai.py @@ -10,7 +10,7 @@ settings = get_settings() class AIResponseExecutor(NodeExecutor): - """Generate AI response using OpenAI""" + """Generate AI response using DeepSeek API""" async def execute( self, config: dict, context: FlowContext, session: Any @@ -21,8 +21,8 @@ class AIResponseExecutor(NodeExecutor): max_tokens = config.get("max_tokens", 500) temperature = config.get("temperature", 0.7) - if not settings.OPENAI_API_KEY: - context.set("_ai_error", "OpenAI API key not configured") + if not settings.DEEPSEEK_API_KEY: + context.set("_ai_error", "DeepSeek API key not configured") return "error" if not prompt: @@ -43,13 +43,13 @@ class AIResponseExecutor(NodeExecutor): async with httpx.AsyncClient() as client: response = await client.post( - "https://api.openai.com/v1/chat/completions", + f"{settings.DEEPSEEK_BASE_URL}/v1/chat/completions", headers={ - "Authorization": f"Bearer {settings.OPENAI_API_KEY}", + "Authorization": f"Bearer {settings.DEEPSEEK_API_KEY}", "Content-Type": "application/json", }, json={ - "model": settings.OPENAI_MODEL, + "model": settings.DEEPSEEK_MODEL, "messages": messages, "max_tokens": max_tokens, "temperature": temperature, @@ -68,7 +68,7 @@ class AIResponseExecutor(NodeExecutor): class AISentimentExecutor(NodeExecutor): - """Analyze sentiment of user message""" + """Analyze sentiment of user message using DeepSeek""" async def execute( self, config: dict, context: FlowContext, session: Any @@ -78,18 +78,18 @@ class AISentimentExecutor(NodeExecutor): ) output_variable = config.get("output_variable", "_sentiment") - if not settings.OPENAI_API_KEY or not text: + if not settings.DEEPSEEK_API_KEY or not text: return "neutral" async with httpx.AsyncClient() as client: response = await client.post( - "https://api.openai.com/v1/chat/completions", + f"{settings.DEEPSEEK_BASE_URL}/v1/chat/completions", headers={ - "Authorization": f"Bearer {settings.OPENAI_API_KEY}", + "Authorization": f"Bearer {settings.DEEPSEEK_API_KEY}", "Content-Type": "application/json", }, json={ - "model": "gpt-3.5-turbo", + "model": settings.DEEPSEEK_MODEL, "messages": [ { "role": "system",