59 lines
1.6 KiB
Python
59 lines
1.6 KiB
Python
import os
|
|
import mysql.connector
|
|
import requests
|
|
from arango import ArangoClient
|
|
import schedule
|
|
import time
|
|
|
|
# MariaDB
|
|
def get_db():
|
|
return mysql.connector.connect(
|
|
host=os.environ['DB_HOST'],
|
|
user=os.environ['DB_USER'],
|
|
password=os.environ['DB_PASSWORD'],
|
|
database=os.environ['DB_NAME']
|
|
)
|
|
|
|
# ArangoDB
|
|
def get_arango():
|
|
client = ArangoClient(hosts=os.environ['ARANGO_URL'])
|
|
db = client.db('llm_facts', username=os.environ['ARANGO_USER'], password=os.environ['ARANGO_PASSWORD'])
|
|
return db.collection('facts')
|
|
|
|
# Analysiere neue Chats und überführe sie in ArangoDB
|
|
def analyze_chats():
|
|
db = get_db()
|
|
cursor = db.cursor(dictionary=True)
|
|
cursor.execute("SELECT * FROM chats WHERE llm_output IS NULL")
|
|
chats = cursor.fetchall()
|
|
|
|
arango = get_arango()
|
|
|
|
for chat in chats:
|
|
# Anfrage an LLM zur Analyse / Fakt-Extraktion
|
|
resp = requests.post(f"{os.environ['LM_API_URL']}/generate", json={"prompt": f"Extrahiere Fakten und Kontext aus diesem Text:\n{chat['user_input']}"})
|
|
fact = resp.json()
|
|
|
|
# Speichern in ArangoDB
|
|
arango.insert({
|
|
"_raw": chat['user_input'],
|
|
"_source": chat['project'],
|
|
"content": fact,
|
|
"tags": ["auto"],
|
|
"relations": []
|
|
})
|
|
|
|
# Update llm_output in MariaDB
|
|
cursor.execute("UPDATE chats SET llm_output=%s WHERE id=%s", (fact, chat['id']))
|
|
|
|
db.commit()
|
|
cursor.close()
|
|
db.close()
|
|
|
|
# Alle 30 Sekunden ausführen
|
|
schedule.every(30).seconds.do(analyze_chats)
|
|
|
|
while True:
|
|
schedule.run_pending()
|
|
time.sleep(1)
|