init
This commit is contained in:
commit
3ffc8bdbc2
15 changed files with 1221 additions and 0 deletions
66
llm-service/main.py
Normal file
66
llm-service/main.py
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
import asyncio
|
||||
import json
|
||||
import toml
|
||||
from loguru import logger
|
||||
import nats
|
||||
from providers import get_provider
|
||||
|
||||
# Загрузка конфигурации
|
||||
config = toml.load("config.toml")
|
||||
|
||||
NATS_URL = config["nats"]["url"]
|
||||
TOPIC_IN = config["nats"]["topic_in"]
|
||||
TOPIC_OUT = config["nats"]["topic_out"]
|
||||
|
||||
provider = get_provider(
|
||||
config["llm"]["provider"],
|
||||
api_key=config["llm"]["api_key"],
|
||||
model=config["llm"]["model"],
|
||||
system_prompt=config["llm"].get("system_prompt", "")
|
||||
)
|
||||
|
||||
|
||||
async def message_handler(msg):
|
||||
logger.info(f"Received message on {msg.subject}: {msg.data}")
|
||||
|
||||
try:
|
||||
data = json.loads(msg.data.decode())
|
||||
user_id = data.get("user_id")
|
||||
text = data.get("text")
|
||||
if not text:
|
||||
logger.warning("Empty text field in message")
|
||||
return
|
||||
|
||||
response = await provider.chat([{"role": "user", "content": text}])
|
||||
|
||||
reply = {
|
||||
"user_id": user_id,
|
||||
"response": response
|
||||
}
|
||||
|
||||
await msg.respond(json.dumps(reply).encode())
|
||||
logger.success(f"Ответ отправлен в {TOPIC_OUT} для {user_id}")
|
||||
|
||||
except Exception as e:
|
||||
logger.exception("Error handling message")
|
||||
|
||||
|
||||
async def main():
|
||||
logger.info("Connecting to NATS...")
|
||||
nc = await nats.connect(servers=[NATS_URL])
|
||||
await nc.subscribe(TOPIC_IN, cb=message_handler)
|
||||
await nc.flush()
|
||||
logger.info(f"Subscribed to {TOPIC_IN}")
|
||||
|
||||
# Просто держим процесс активным
|
||||
try:
|
||||
logger.info("LLM service is running. Waiting for messages...")
|
||||
await asyncio.Event().wait()
|
||||
except KeyboardInterrupt:
|
||||
logger.info("Shutting down LLM service...")
|
||||
finally:
|
||||
await nc.drain()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
Loading…
Add table
Add a link
Reference in a new issue