Spaces:
Sleeping
Sleeping
Create ai_engine.py
Browse files- ai_engine.py +43 -0
ai_engine.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import requests
|
| 4 |
+
import re
|
| 5 |
+
|
| 6 |
+
API_KEY = os.getenv("OPENROUTER_API_KEY")
|
| 7 |
+
MODEL = os.getenv("OPENROUTER_MODEL", "google/gemma-2-9b-it:free")
|
| 8 |
+
|
| 9 |
+
# Singleton for embedding model
|
| 10 |
+
_embed_model = None
|
| 11 |
+
|
| 12 |
+
def get_embedding(text):
|
| 13 |
+
global _embed_model
|
| 14 |
+
if _embed_model is None:
|
| 15 |
+
from sentence_transformers import SentenceTransformer
|
| 16 |
+
_embed_model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 17 |
+
return _embed_model.encode(text).tolist()
|
| 18 |
+
|
| 19 |
+
def chat_stream(messages):
|
| 20 |
+
headers = {
|
| 21 |
+
"Authorization": f"Bearer {API_KEY}",
|
| 22 |
+
"Content-Type": "application/json",
|
| 23 |
+
"HTTP-Referer": "http://localhost:5000",
|
| 24 |
+
"X-Title": "VisuMem AI"
|
| 25 |
+
}
|
| 26 |
+
payload = {"model": MODEL, "messages": messages, "stream": True}
|
| 27 |
+
|
| 28 |
+
try:
|
| 29 |
+
resp = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload, stream=True)
|
| 30 |
+
resp.raise_for_status()
|
| 31 |
+
for line in resp.iter_lines():
|
| 32 |
+
if line:
|
| 33 |
+
decoded = line.decode('utf-8')
|
| 34 |
+
if decoded.startswith("data: ") and decoded != "data: [DONE]":
|
| 35 |
+
try:
|
| 36 |
+
data = json.loads(decoded[6:])
|
| 37 |
+
if "choices" in data:
|
| 38 |
+
content = data["choices"][0].get("delta", {}).get("content", "")
|
| 39 |
+
if content: yield content
|
| 40 |
+
except: pass
|
| 41 |
+
except Exception as e:
|
| 42 |
+
yield f" [Error: {str(e)}]"
|
| 43 |
+
|