Upload 3 files
Browse files- app/services/enrichment.py +11 -7
- app/services/generation.py +6 -4
- app/services/retrieval.py +2 -1
app/services/enrichment.py
CHANGED
|
@@ -4,8 +4,8 @@ from litellm import completion
|
|
| 4 |
from app.core.config import settings
|
| 5 |
|
| 6 |
class AIEnrichmentService:
|
| 7 |
-
def __init__(self, model: str =
|
| 8 |
-
self.model = model
|
| 9 |
self._local_embedding_model = None
|
| 10 |
|
| 11 |
def _get_local_embedding_model(self):
|
|
@@ -42,6 +42,7 @@ class AIEnrichmentService:
|
|
| 42 |
response = await completion(
|
| 43 |
model=self.model,
|
| 44 |
messages=[{"role": "user", "content": prompt}],
|
|
|
|
| 45 |
response_format={"type": "json_object"}
|
| 46 |
)
|
| 47 |
import json
|
|
@@ -52,17 +53,20 @@ class AIEnrichmentService:
|
|
| 52 |
return {"entities": [], "tags": [], "sensitivity": "unknown", "pii_detected": False}
|
| 53 |
|
| 54 |
async def generate_embedding(self, text: str) -> List[float]:
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
| 57 |
try:
|
| 58 |
from litellm import embedding
|
| 59 |
response = await embedding(
|
| 60 |
-
model=
|
| 61 |
-
input=[text]
|
|
|
|
| 62 |
)
|
| 63 |
return response['data'][0]['embedding']
|
| 64 |
except Exception as e:
|
| 65 |
-
print(f"
|
| 66 |
|
| 67 |
# 2. Fallback to Local Sentence Transformers (CPU friendly)
|
| 68 |
try:
|
|
|
|
| 4 |
from app.core.config import settings
|
| 5 |
|
| 6 |
class AIEnrichmentService:
|
| 7 |
+
def __init__(self, model: Optional[str] = None):
|
| 8 |
+
self.model = model or settings.get_effective_llm()
|
| 9 |
self._local_embedding_model = None
|
| 10 |
|
| 11 |
def _get_local_embedding_model(self):
|
|
|
|
| 42 |
response = await completion(
|
| 43 |
model=self.model,
|
| 44 |
messages=[{"role": "user", "content": prompt}],
|
| 45 |
+
api_key=settings.OPENAI_API_KEY or settings.HUGGINGFACE_API_TOKEN,
|
| 46 |
response_format={"type": "json_object"}
|
| 47 |
)
|
| 48 |
import json
|
|
|
|
| 53 |
return {"entities": [], "tags": [], "sensitivity": "unknown", "pii_detected": False}
|
| 54 |
|
| 55 |
async def generate_embedding(self, text: str) -> List[float]:
|
| 56 |
+
effective_model = settings.get_effective_embedding()
|
| 57 |
+
|
| 58 |
+
# 1. Try API if key is present
|
| 59 |
+
if settings.OPENAI_API_KEY or (settings.HUGGINGFACE_API_TOKEN and "huggingface" in effective_model):
|
| 60 |
try:
|
| 61 |
from litellm import embedding
|
| 62 |
response = await embedding(
|
| 63 |
+
model=effective_model,
|
| 64 |
+
input=[text],
|
| 65 |
+
api_key=settings.OPENAI_API_KEY or settings.HUGGINGFACE_API_TOKEN
|
| 66 |
)
|
| 67 |
return response['data'][0]['embedding']
|
| 68 |
except Exception as e:
|
| 69 |
+
print(f"API embedding ({effective_model}) failed, trying fallback: {e}")
|
| 70 |
|
| 71 |
# 2. Fallback to Local Sentence Transformers (CPU friendly)
|
| 72 |
try:
|
app/services/generation.py
CHANGED
|
@@ -1,10 +1,11 @@
|
|
| 1 |
-
from typing import List, Dict, Any
|
|
|
|
| 2 |
from litellm import completion
|
| 3 |
from app.core.config import settings
|
| 4 |
|
| 5 |
class GenerationService:
|
| 6 |
-
def __init__(self, model: str =
|
| 7 |
-
self.model = model
|
| 8 |
|
| 9 |
async def generate_answer(self, query: str, context_chunks: List[Dict[str, Any]]) -> Dict[str, Any]:
|
| 10 |
context_text = "\n\n".join([
|
|
@@ -35,8 +36,9 @@ class GenerationService:
|
|
| 35 |
}
|
| 36 |
|
| 37 |
try:
|
|
|
|
| 38 |
response = await completion(
|
| 39 |
-
model=
|
| 40 |
messages=[{"role": "user", "content": prompt}],
|
| 41 |
api_key=settings.OPENAI_API_KEY or settings.HUGGINGFACE_API_TOKEN
|
| 42 |
)
|
|
|
|
| 1 |
+
from typing import List, Dict, Any, Optional
|
| 2 |
+
|
| 3 |
from litellm import completion
|
| 4 |
from app.core.config import settings
|
| 5 |
|
| 6 |
class GenerationService:
|
| 7 |
+
def __init__(self, model: Optional[str] = None):
|
| 8 |
+
self.model = model or settings.get_effective_llm()
|
| 9 |
|
| 10 |
async def generate_answer(self, query: str, context_chunks: List[Dict[str, Any]]) -> Dict[str, Any]:
|
| 11 |
context_text = "\n\n".join([
|
|
|
|
| 36 |
}
|
| 37 |
|
| 38 |
try:
|
| 39 |
+
effective_model = self.model # Already adjusted in __init__
|
| 40 |
response = await completion(
|
| 41 |
+
model=effective_model,
|
| 42 |
messages=[{"role": "user", "content": prompt}],
|
| 43 |
api_key=settings.OPENAI_API_KEY or settings.HUGGINGFACE_API_TOKEN
|
| 44 |
)
|
app/services/retrieval.py
CHANGED
|
@@ -15,9 +15,10 @@ class RetrievalService:
|
|
| 15 |
return query
|
| 16 |
|
| 17 |
try:
|
|
|
|
| 18 |
prompt = f"Rewrite the following user query to be more search-friendly for a RAG system. Original: {query}"
|
| 19 |
response = await completion(
|
| 20 |
-
model=
|
| 21 |
messages=[{"role": "user", "content": prompt}],
|
| 22 |
api_key=settings.OPENAI_API_KEY or settings.HUGGINGFACE_API_TOKEN
|
| 23 |
)
|
|
|
|
| 15 |
return query
|
| 16 |
|
| 17 |
try:
|
| 18 |
+
effective_model = settings.get_effective_llm()
|
| 19 |
prompt = f"Rewrite the following user query to be more search-friendly for a RAG system. Original: {query}"
|
| 20 |
response = await completion(
|
| 21 |
+
model=effective_model,
|
| 22 |
messages=[{"role": "user", "content": prompt}],
|
| 23 |
api_key=settings.OPENAI_API_KEY or settings.HUGGINGFACE_API_TOKEN
|
| 24 |
)
|