Spaces:
Runtime error
Runtime error
update
Browse files- common/auth.py +1 -1
- components/dbo/models/llm_config.py +1 -1
- components/dbo/models/llm_prompt.py +1 -1
- components/llm/common.py +1 -0
- routes/llm.py +12 -5
common/auth.py
CHANGED
|
@@ -14,7 +14,7 @@ ACCESS_TOKEN_EXPIRE_MINUTES = 30
|
|
| 14 |
# Захардкоженные пользователи
|
| 15 |
USERS = [
|
| 16 |
{"username": "admin", "password": "admin123"},
|
| 17 |
-
{"username": "
|
| 18 |
]
|
| 19 |
|
| 20 |
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/auth/login/token")
|
|
|
|
| 14 |
# Захардкоженные пользователи
|
| 15 |
USERS = [
|
| 16 |
{"username": "admin", "password": "admin123"},
|
| 17 |
+
{"username": "demo", "password": "sTrUPsORPA"},
|
| 18 |
]
|
| 19 |
|
| 20 |
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/auth/login/token")
|
components/dbo/models/llm_config.py
CHANGED
|
@@ -16,7 +16,7 @@ class LLMConfig(Base):
|
|
| 16 |
|
| 17 |
__tablename__ = "llm_config"
|
| 18 |
|
| 19 |
-
is_default: Mapped[bool] = mapped_column(Boolean,
|
| 20 |
model: Mapped[String] = mapped_column(String)
|
| 21 |
temperature: Mapped[float] = mapped_column(Float)
|
| 22 |
top_p: Mapped[float] = mapped_column(Float)
|
|
|
|
| 16 |
|
| 17 |
__tablename__ = "llm_config"
|
| 18 |
|
| 19 |
+
is_default: Mapped[bool] = mapped_column(Boolean, default=False)
|
| 20 |
model: Mapped[String] = mapped_column(String)
|
| 21 |
temperature: Mapped[float] = mapped_column(Float)
|
| 22 |
top_p: Mapped[float] = mapped_column(Float)
|
components/dbo/models/llm_prompt.py
CHANGED
|
@@ -14,7 +14,7 @@ class LlmPrompt(Base):
|
|
| 14 |
|
| 15 |
__tablename__ = "llm_prompt"
|
| 16 |
|
| 17 |
-
is_default: Mapped[bool] = mapped_column(Boolean,
|
| 18 |
name: Mapped[String] = mapped_column(String)
|
| 19 |
text: Mapped[String] = mapped_column(String)
|
| 20 |
type: Mapped[String] = mapped_column(String)
|
|
|
|
| 14 |
|
| 15 |
__tablename__ = "llm_prompt"
|
| 16 |
|
| 17 |
+
is_default: Mapped[bool] = mapped_column(Boolean, default=False)
|
| 18 |
name: Mapped[String] = mapped_column(String)
|
| 19 |
text: Mapped[String] = mapped_column(String)
|
| 20 |
type: Mapped[String] = mapped_column(String)
|
components/llm/common.py
CHANGED
|
@@ -73,6 +73,7 @@ class Message(BaseModel):
|
|
| 73 |
role: str
|
| 74 |
content: str
|
| 75 |
searchResults: str
|
|
|
|
| 76 |
|
| 77 |
class ChatRequest(BaseModel):
|
| 78 |
history: List[Message]
|
|
|
|
| 73 |
role: str
|
| 74 |
content: str
|
| 75 |
searchResults: str
|
| 76 |
+
searchEntities: Optional[List[str]] = []
|
| 77 |
|
| 78 |
class ChatRequest(BaseModel):
|
| 79 |
history: List[Message]
|
routes/llm.py
CHANGED
|
@@ -1,9 +1,10 @@
|
|
| 1 |
import json
|
| 2 |
import logging
|
| 3 |
import os
|
| 4 |
-
from typing import Annotated, AsyncGenerator, Optional
|
| 5 |
from uuid import UUID
|
| 6 |
|
|
|
|
| 7 |
from components.services.dialogue import DialogueService
|
| 8 |
from fastapi.responses import StreamingResponse
|
| 9 |
|
|
@@ -70,11 +71,12 @@ def insert_search_results_to_message(
|
|
| 70 |
return False
|
| 71 |
|
| 72 |
def try_insert_search_results(
|
| 73 |
-
chat_request: ChatRequest, search_results: str
|
| 74 |
) -> bool:
|
| 75 |
for msg in reversed(chat_request.history):
|
| 76 |
if msg.role == "user" and not msg.searchResults:
|
| 77 |
msg.searchResults = search_results
|
|
|
|
| 78 |
return True
|
| 79 |
return False
|
| 80 |
|
|
@@ -117,8 +119,9 @@ async def sse_generator(request: ChatRequest, llm_api: DeepInfraApi, system_prom
|
|
| 117 |
"""
|
| 118 |
Генератор для стриминга ответа LLM через SSE.
|
| 119 |
"""
|
|
|
|
| 120 |
try:
|
| 121 |
-
qe_result = await dialogue_service.get_qe_result(request.history)
|
| 122 |
|
| 123 |
except Exception as e:
|
| 124 |
logger.error(f"Error in SSE chat stream while dialogue_service.get_qe_result: {str(e)}", stack_info=True)
|
|
@@ -134,13 +137,16 @@ async def sse_generator(request: ChatRequest, llm_api: DeepInfraApi, system_prom
|
|
| 134 |
text_chunks = entity_service.build_text(chunks, scores)
|
| 135 |
search_results_event = {
|
| 136 |
"event": "search_results",
|
| 137 |
-
"data":
|
|
|
|
|
|
|
|
|
|
| 138 |
}
|
| 139 |
yield f"data: {json.dumps(search_results_event, ensure_ascii=False)}\n\n"
|
| 140 |
|
| 141 |
# new_message = f'<search-results>\n{text_chunks}\n</search-results>\n{last_query.content}'
|
| 142 |
|
| 143 |
-
try_insert_search_results(request, text_chunks)
|
| 144 |
except Exception as e:
|
| 145 |
logger.error(f"Error in SSE chat stream while searching: {str(e)}", stack_info=True)
|
| 146 |
yield "data: {\"event\": \"error\", \"data\":\""+str(e)+"\" }\n\n"
|
|
@@ -171,6 +177,7 @@ async def chat_stream(
|
|
| 171 |
entity_service: Annotated[EntityService, Depends(DI.get_entity_service)],
|
| 172 |
dataset_service: Annotated[DatasetService, Depends(DI.get_dataset_service)],
|
| 173 |
dialogue_service: Annotated[DialogueService, Depends(DI.get_dialogue_service)],
|
|
|
|
| 174 |
):
|
| 175 |
try:
|
| 176 |
p = llm_config_service.get_default()
|
|
|
|
| 1 |
import json
|
| 2 |
import logging
|
| 3 |
import os
|
| 4 |
+
from typing import Annotated, AsyncGenerator, List, Optional
|
| 5 |
from uuid import UUID
|
| 6 |
|
| 7 |
+
from common import auth
|
| 8 |
from components.services.dialogue import DialogueService
|
| 9 |
from fastapi.responses import StreamingResponse
|
| 10 |
|
|
|
|
| 71 |
return False
|
| 72 |
|
| 73 |
def try_insert_search_results(
|
| 74 |
+
chat_request: ChatRequest, search_results: str, entities: List[str]
|
| 75 |
) -> bool:
|
| 76 |
for msg in reversed(chat_request.history):
|
| 77 |
if msg.role == "user" and not msg.searchResults:
|
| 78 |
msg.searchResults = search_results
|
| 79 |
+
msg.searchEntities = entities
|
| 80 |
return True
|
| 81 |
return False
|
| 82 |
|
|
|
|
| 119 |
"""
|
| 120 |
Генератор для стриминга ответа LLM через SSE.
|
| 121 |
"""
|
| 122 |
+
qe_result = None
|
| 123 |
try:
|
| 124 |
+
qe_result = await dialogue_service.get_qe_result(request.history)
|
| 125 |
|
| 126 |
except Exception as e:
|
| 127 |
logger.error(f"Error in SSE chat stream while dialogue_service.get_qe_result: {str(e)}", stack_info=True)
|
|
|
|
| 137 |
text_chunks = entity_service.build_text(chunks, scores)
|
| 138 |
search_results_event = {
|
| 139 |
"event": "search_results",
|
| 140 |
+
"data": {
|
| 141 |
+
"text": text_chunks,
|
| 142 |
+
"ids": chunk_ids.tolist()
|
| 143 |
+
}
|
| 144 |
}
|
| 145 |
yield f"data: {json.dumps(search_results_event, ensure_ascii=False)}\n\n"
|
| 146 |
|
| 147 |
# new_message = f'<search-results>\n{text_chunks}\n</search-results>\n{last_query.content}'
|
| 148 |
|
| 149 |
+
try_insert_search_results(request, text_chunks, chunk_ids.tolist())
|
| 150 |
except Exception as e:
|
| 151 |
logger.error(f"Error in SSE chat stream while searching: {str(e)}", stack_info=True)
|
| 152 |
yield "data: {\"event\": \"error\", \"data\":\""+str(e)+"\" }\n\n"
|
|
|
|
| 177 |
entity_service: Annotated[EntityService, Depends(DI.get_entity_service)],
|
| 178 |
dataset_service: Annotated[DatasetService, Depends(DI.get_dataset_service)],
|
| 179 |
dialogue_service: Annotated[DialogueService, Depends(DI.get_dialogue_service)],
|
| 180 |
+
current_user: Annotated[any, Depends(auth.get_current_user)]
|
| 181 |
):
|
| 182 |
try:
|
| 183 |
p = llm_config_service.get_default()
|