Spaces:
Running
Running
AI Assistant commited on
Commit ·
ad67d8e
1
Parent(s): bfe9d27
Refactor LLMService to integrate gemini client and update dependencies in pyproject.toml
Browse files- app/services/llm_service.py +32 -16
- pyproject.toml +1 -1
app/services/llm_service.py
CHANGED
|
@@ -1,15 +1,22 @@
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
-
|
|
|
|
| 4 |
from openai import AsyncOpenAI
|
| 5 |
|
| 6 |
class LLMService:
|
| 7 |
def __init__(self, api_key: str, base_url: str = "https://models.inference.ai.azure.com"):
|
| 8 |
self.client = AsyncOpenAI(api_key=api_key, base_url=base_url)
|
|
|
|
| 9 |
|
| 10 |
gemini_key = os.getenv("GEMINI_API_KEY")
|
| 11 |
if gemini_key:
|
| 12 |
-
genai.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
async def summarize_history(self, history: list, model: str = "gpt-4o") -> str:
|
| 15 |
history_text = "\n".join([f"{msg['role']}: {msg['content']}" for msg in history if msg['role'] != 'system'])
|
|
@@ -21,9 +28,9 @@ Chat History:
|
|
| 21 |
{history_text}"""
|
| 22 |
|
| 23 |
if "gemini" in model.lower():
|
| 24 |
-
|
| 25 |
-
response =
|
| 26 |
-
return response.text
|
| 27 |
else:
|
| 28 |
response = await self.client.chat.completions.create(
|
| 29 |
model=model,
|
|
@@ -63,9 +70,10 @@ Reply ONLY in this exact JSON format:
|
|
| 63 |
}}"""
|
| 64 |
|
| 65 |
if "gemini" in model.lower():
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
| 69 |
final_nodes = result.get("node_list", [])
|
| 70 |
else:
|
| 71 |
response = await self.client.chat.completions.create(
|
|
@@ -118,16 +126,24 @@ Available Context from the document:
|
|
| 118 |
{context_msg}"""
|
| 119 |
|
| 120 |
if "gemini" in model.lower():
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
|
|
|
|
|
|
| 124 |
for msg in chat_history:
|
| 125 |
role = "user" if msg["role"] == "user" else "model"
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
for chunk in response:
|
| 132 |
if chunk.text:
|
| 133 |
yield chunk.text
|
|
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
+
from google import genai
|
| 4 |
+
from google.genai import types
|
| 5 |
from openai import AsyncOpenAI
|
| 6 |
|
| 7 |
class LLMService:
|
| 8 |
def __init__(self, api_key: str, base_url: str = "https://models.inference.ai.azure.com"):
|
| 9 |
self.client = AsyncOpenAI(api_key=api_key, base_url=base_url)
|
| 10 |
+
self.gemini_client = None
|
| 11 |
|
| 12 |
gemini_key = os.getenv("GEMINI_API_KEY")
|
| 13 |
if gemini_key:
|
| 14 |
+
self.gemini_client = genai.Client(api_key=gemini_key)
|
| 15 |
+
|
| 16 |
+
def _require_gemini_client(self) -> genai.Client:
|
| 17 |
+
if not self.gemini_client:
|
| 18 |
+
raise ValueError("GEMINI_API_KEY is not configured.")
|
| 19 |
+
return self.gemini_client
|
| 20 |
|
| 21 |
async def summarize_history(self, history: list, model: str = "gpt-4o") -> str:
|
| 22 |
history_text = "\n".join([f"{msg['role']}: {msg['content']}" for msg in history if msg['role'] != 'system'])
|
|
|
|
| 28 |
{history_text}"""
|
| 29 |
|
| 30 |
if "gemini" in model.lower():
|
| 31 |
+
client = self._require_gemini_client()
|
| 32 |
+
response = client.models.generate_content(model=model, contents=prompt)
|
| 33 |
+
return response.text or ""
|
| 34 |
else:
|
| 35 |
response = await self.client.chat.completions.create(
|
| 36 |
model=model,
|
|
|
|
| 70 |
}}"""
|
| 71 |
|
| 72 |
if "gemini" in model.lower():
|
| 73 |
+
client = self._require_gemini_client()
|
| 74 |
+
config = types.GenerateContentConfig(response_mime_type="application/json")
|
| 75 |
+
response = client.models.generate_content(model=model, contents=prompt, config=config)
|
| 76 |
+
result = json.loads(response.text or "{}")
|
| 77 |
final_nodes = result.get("node_list", [])
|
| 78 |
else:
|
| 79 |
response = await self.client.chat.completions.create(
|
|
|
|
| 126 |
{context_msg}"""
|
| 127 |
|
| 128 |
if "gemini" in model.lower():
|
| 129 |
+
client = self._require_gemini_client()
|
| 130 |
+
gemini_contents = [
|
| 131 |
+
types.Content(role="user", parts=[types.Part(text=system_prompt)]),
|
| 132 |
+
]
|
| 133 |
+
|
| 134 |
for msg in chat_history:
|
| 135 |
role = "user" if msg["role"] == "user" else "model"
|
| 136 |
+
gemini_contents.append(
|
| 137 |
+
types.Content(role=role, parts=[types.Part(text=msg["content"])])
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
+
gemini_contents.append(
|
| 141 |
+
types.Content(role="user", parts=[types.Part(text=query)])
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
response = client.models.generate_content_stream(
|
| 145 |
+
model=model, contents=gemini_contents
|
| 146 |
+
)
|
| 147 |
for chunk in response:
|
| 148 |
if chunk.text:
|
| 149 |
yield chunk.text
|
pyproject.toml
CHANGED
|
@@ -7,7 +7,7 @@ requires-python = ">=3.14"
|
|
| 7 |
dependencies = [
|
| 8 |
"fastapi>=0.136.1",
|
| 9 |
"google>=3.0.0",
|
| 10 |
-
"google-
|
| 11 |
"openai>=2.36.0",
|
| 12 |
"pageindex>=0.2.8",
|
| 13 |
"python-dotenv>=1.2.2",
|
|
|
|
| 7 |
dependencies = [
|
| 8 |
"fastapi>=0.136.1",
|
| 9 |
"google>=3.0.0",
|
| 10 |
+
"google-genai>=0.8.0",
|
| 11 |
"openai>=2.36.0",
|
| 12 |
"pageindex>=0.2.8",
|
| 13 |
"python-dotenv>=1.2.2",
|