initial commit
This commit is contained in:
parent
5ad1c553ef
commit
fc250eb8f8
@ -6,7 +6,6 @@ import logging
|
|||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
from typing import Dict, List
|
from typing import Dict, List
|
||||||
|
|
||||||
|
|
||||||
from app.core.config import settings
|
from app.core.config import settings
|
||||||
|
|
||||||
# Configure logger
|
# Configure logger
|
||||||
@ -37,7 +36,7 @@ class OpenAIService(LLMService):
|
|||||||
"""Initialize the OpenAI service."""
|
"""Initialize the OpenAI service."""
|
||||||
try:
|
try:
|
||||||
import openai
|
import openai
|
||||||
self.client = openai.AsyncOpenAI(api_key=settings.openai_api_key)
|
self.client = openai.AsyncOpenAI(api_key=settings.OPENAI_API_KEY)
|
||||||
self.model = settings.OPENAI_MODEL
|
self.model = settings.OPENAI_MODEL
|
||||||
except (ImportError, AttributeError) as e:
|
except (ImportError, AttributeError) as e:
|
||||||
logger.error(f"Failed to initialize OpenAI service: {e}")
|
logger.error(f"Failed to initialize OpenAI service: {e}")
|
||||||
@ -122,21 +121,21 @@ class GeminiService(LLMService):
|
|||||||
- priority: The priority level (high, medium, low)
|
- priority: The priority level (high, medium, low)
|
||||||
- status: The status (defaults to "pending")
|
- status: The status (defaults to "pending")
|
||||||
|
|
||||||
Return ONLY a JSON array of task objects without any additional text or explanation.
|
Return ONLY a JSON object with a "tasks" key that contains an array of task objects.
|
||||||
Format your response as valid JSON with a "tasks" key that contains an array of task objects.
|
Do not include any text or explanation outside the JSON.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
# Start the chat session with the system prompt
|
||||||
chat = self.model.start_chat(history=[
|
chat = self.model.start_chat(history=[
|
||||||
{"role": "user", "parts": [system_prompt]},
|
{"role": "system", "content": system_prompt}
|
||||||
{"role": "model", "parts": ["I understand. I'll convert user inputs into JSON task objects with the specified properties."]}
|
|
||||||
])
|
])
|
||||||
|
|
||||||
|
# Send the user prompt asynchronously
|
||||||
response = await chat.send_message_async(prompt)
|
response = await chat.send_message_async(prompt)
|
||||||
content = response.text
|
content = response.text
|
||||||
|
|
||||||
# Extract JSON from the response
|
# Remove possible markdown code blocks wrapping the JSON response
|
||||||
# This handles cases where the model might add markdown code blocks
|
|
||||||
if "```json" in content:
|
if "```json" in content:
|
||||||
json_str = content.split("```json")[1].split("```")[0].strip()
|
json_str = content.split("```json")[1].split("```")[0].strip()
|
||||||
elif "```" in content:
|
elif "```" in content:
|
||||||
@ -144,12 +143,13 @@ class GeminiService(LLMService):
|
|||||||
else:
|
else:
|
||||||
json_str = content.strip()
|
json_str = content.strip()
|
||||||
|
|
||||||
|
# Parse JSON response
|
||||||
result = json.loads(json_str)
|
result = json.loads(json_str)
|
||||||
|
|
||||||
# Expect a "tasks" key in the response JSON
|
# Return the list under "tasks" or the whole as single-item list
|
||||||
if "tasks" in result:
|
if "tasks" in result:
|
||||||
return result["tasks"]
|
return result["tasks"]
|
||||||
return [result] # Return as a single-item list if no "tasks" key
|
return [result]
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Gemini API error: {e}")
|
logger.error(f"Gemini API error: {e}")
|
||||||
@ -169,7 +169,6 @@ class MockLLMService(LLMService):
|
|||||||
Returns:
|
Returns:
|
||||||
List of task dictionaries
|
List of task dictionaries
|
||||||
"""
|
"""
|
||||||
# Simple parsing logic for testing
|
|
||||||
words = prompt.lower().split()
|
words = prompt.lower().split()
|
||||||
priority = "medium"
|
priority = "medium"
|
||||||
|
|
||||||
@ -178,7 +177,6 @@ class MockLLMService(LLMService):
|
|||||||
elif "low" in words or "minor" in words:
|
elif "low" in words or "minor" in words:
|
||||||
priority = "low"
|
priority = "low"
|
||||||
|
|
||||||
# Create a basic task from the prompt
|
|
||||||
return [{
|
return [{
|
||||||
"title": prompt[:50] + ("..." if len(prompt) > 50 else ""),
|
"title": prompt[:50] + ("..." if len(prompt) > 50 else ""),
|
||||||
"description": prompt,
|
"description": prompt,
|
||||||
@ -188,7 +186,6 @@ class MockLLMService(LLMService):
|
|||||||
}]
|
}]
|
||||||
|
|
||||||
|
|
||||||
# Factory function to create the appropriate LLM service
|
|
||||||
def get_llm_service() -> LLMService:
|
def get_llm_service() -> LLMService:
|
||||||
"""
|
"""
|
||||||
Factory function for LLM service dependency injection.
|
Factory function for LLM service dependency injection.
|
||||||
@ -198,14 +195,12 @@ def get_llm_service() -> LLMService:
|
|||||||
"""
|
"""
|
||||||
llm_provider = settings.LLM_PROVIDER.lower()
|
llm_provider = settings.LLM_PROVIDER.lower()
|
||||||
|
|
||||||
if llm_provider == "openai" and settings.OPEANAI_API_KEY:
|
if llm_provider == "openai" and settings.OPENAI_API_KEY:
|
||||||
return OpenAIService()
|
return OpenAIService()
|
||||||
elif llm_provider == "gemini" and settings.GEMINI_API_KEY:
|
elif llm_provider == "gemini" and settings.GEMINI_API_KEY:
|
||||||
return GeminiService()
|
return GeminiService()
|
||||||
elif llm_provider == "mock" or settings.environment == "test":
|
elif llm_provider == "mock" or settings.environment == "test":
|
||||||
# Use mock service for testing or when configured
|
|
||||||
return MockLLMService()
|
return MockLLMService()
|
||||||
else:
|
else:
|
||||||
# Default to mock service if configuration is incomplete
|
|
||||||
logger.warning(f"LLM provider '{llm_provider}' not properly configured - using mock service")
|
logger.warning(f"LLM provider '{llm_provider}' not properly configured - using mock service")
|
||||||
return MockLLMService()
|
return MockLLMService()
|
Loading…
x
Reference in New Issue
Block a user