Spaces:
Runtime error
Runtime error
Delete services
Browse files- services/glm.py +0 -76
services/glm.py
DELETED
|
@@ -1,76 +0,0 @@
|
|
| 1 |
-
import openai
|
| 2 |
-
import logging
|
| 3 |
-
from typing import List, Dict, Any
|
| 4 |
-
from config import config
|
| 5 |
-
|
| 6 |
-
logger = logging.getLogger(__name__)
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
class GLMService:
|
| 10 |
-
def __init__(self) -> None:
|
| 11 |
-
self.client = openai.AsyncOpenAI(
|
| 12 |
-
base_url=config.NVIDIA_BASE_URL,
|
| 13 |
-
api_key=config.NVIDIA_API_KEY,
|
| 14 |
-
timeout=60.0,
|
| 15 |
-
max_retries=2,
|
| 16 |
-
)
|
| 17 |
-
self.model = config.MODEL_NAME
|
| 18 |
-
async def chat(self, messages: List[Dict[str, str]]) -> str:
|
| 19 |
-
try:
|
| 20 |
-
response = await self.client.chat.completions.create(
|
| 21 |
-
model=self.model,
|
| 22 |
-
messages=messages,
|
| 23 |
-
temperature=config.GLM_TEMPERATURE,
|
| 24 |
-
top_p=config.GLM_TOP_P,
|
| 25 |
-
frequency_penalty=config.GLM_FREQUENCY_PENALTY,
|
| 26 |
-
presence_penalty=config.GLM_PRESENCE_PENALTY,
|
| 27 |
-
max_tokens=config.GLM_MAX_TOKENS,
|
| 28 |
-
)
|
| 29 |
-
content = response.choices[0].message.content
|
| 30 |
-
if not content:
|
| 31 |
-
logger.warning("Empty response from GLM")
|
| 32 |
-
return "(пустой ответ от модели)"
|
| 33 |
-
logger.info("GLM response received, length: %d", len(content))
|
| 34 |
-
return content
|
| 35 |
-
except openai.APIError as e:
|
| 36 |
-
logger.error("GLM API error: %s", e)
|
| 37 |
-
raise
|
| 38 |
-
except Exception as e:
|
| 39 |
-
logger.error("Unexpected GLM error: %s", e)
|
| 40 |
-
raise
|
| 41 |
-
|
| 42 |
-
async def summarize(self, dialog_text: str) -> str:
|
| 43 |
-
"""Суммаризировать диалог через GLM."""
|
| 44 |
-
try:
|
| 45 |
-
messages = [
|
| 46 |
-
{
|
| 47 |
-
"role": "system",
|
| 48 |
-
"content": (
|
| 49 |
-
"Суммаризируй следующий диалог между пользователем и ассистентом. "
|
| 50 |
-
"Сохрани ключевые факты, предпочтения пользователя, важные детали и контекст. "
|
| 51 |
-
"Будь краток, максимум 4096 токенов. Используй русский язык."
|
| 52 |
-
)
|
| 53 |
-
},
|
| 54 |
-
{"role": "user", "content": dialog_text}
|
| 55 |
-
]
|
| 56 |
-
response = await self.client.chat.completions.create(
|
| 57 |
-
model=self.model,
|
| 58 |
-
messages=messages,
|
| 59 |
-
temperature=0.1,
|
| 60 |
-
max_tokens=config.SUMMARY_MAX_TOKENS,
|
| 61 |
-
)
|
| 62 |
-
content = response.choices[0].message.content
|
| 63 |
-
if not content:
|
| 64 |
-
logger.warning("Empty summary from GLM")
|
| 65 |
-
return ""
|
| 66 |
-
logger.info("Summary generated, length: %d", len(content))
|
| 67 |
-
return content
|
| 68 |
-
except Exception as e:
|
| 69 |
-
logger.error("Summary generation failed: %s", e)
|
| 70 |
-
return ""
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
glm_service = GLMService()
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|