Spaces:
Running
Running
Update backend/main.py
Browse files- backend/main.py +26 -78
backend/main.py
CHANGED
|
@@ -55,7 +55,6 @@ from rag.vector_store import VectorStore
|
|
| 55 |
load_dotenv()
|
| 56 |
|
| 57 |
CEREBRAS_API_KEY = os.getenv("CEREBRAS_API_KEY")
|
| 58 |
-
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 59 |
YOUTUBE_API_KEY = os.getenv("YOUTUBE_API_KEY")
|
| 60 |
|
| 61 |
JWT_SECRET_KEY = os.getenv(
|
|
@@ -111,22 +110,7 @@ else:
|
|
| 111 |
print("β οΈ CEREBRAS_API_KEY not set β Cerebras disabled")
|
| 112 |
|
| 113 |
|
| 114 |
-
#
|
| 115 |
-
groq_client = None
|
| 116 |
-
|
| 117 |
-
if GROQ_API_KEY:
|
| 118 |
-
try:
|
| 119 |
-
from groq import Groq
|
| 120 |
-
groq_client = Groq(api_key=GROQ_API_KEY)
|
| 121 |
-
print("β
Groq client initialized (secondary)")
|
| 122 |
-
except Exception as e:
|
| 123 |
-
print(f"β οΈ Groq init failed: {e}")
|
| 124 |
-
groq_client = None
|
| 125 |
-
else:
|
| 126 |
-
print("βΉοΈ GROQ_API_KEY not set β Groq fallback disabled")
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
# OLLAMA (LAST RESORT)
|
| 130 |
OLLAMA_BASE_URL = os.getenv("OLLAMA_BASE_URL", "http://localhost:11434/v1")
|
| 131 |
OLLAMA_MODEL = os.getenv("OLLAMA_MODEL", "llama3")
|
| 132 |
|
|
@@ -373,13 +357,6 @@ class UserLogin(BaseModel):
|
|
| 373 |
password: str
|
| 374 |
|
| 375 |
|
| 376 |
-
GROQ_MODELS = {
|
| 377 |
-
"fast": "openai/gpt-oss-20b",
|
| 378 |
-
"balanced": "openai/gpt-oss-120b",
|
| 379 |
-
}
|
| 380 |
-
GROQ_PREFERRED = GROQ_MODELS["balanced"]
|
| 381 |
-
|
| 382 |
-
|
| 383 |
SYSTEM_PROMPT = """
|
| 384 |
You are Cortexa, a powerful AI assistant.
|
| 385 |
|
|
@@ -411,7 +388,7 @@ IMPORTANT RULES:
|
|
| 411 |
11. For coding questions, provide correct,
|
| 412 |
practical code and explain the important parts.
|
| 413 |
"""
|
| 414 |
-
# AI RESPONSE β FALLBACK CHAIN
|
| 415 |
async def get_ai_response(message: str, context: Optional[str] = None) -> str:
|
| 416 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 417 |
|
|
@@ -439,24 +416,7 @@ async def get_ai_response(message: str, context: Optional[str] = None) -> str:
|
|
| 439 |
except Exception as e:
|
| 440 |
print(f"β οΈ Cerebras failed: {str(e)[:200]}")
|
| 441 |
|
| 442 |
-
# 2οΈβ£
|
| 443 |
-
if groq_client:
|
| 444 |
-
try:
|
| 445 |
-
completion = groq_client.chat.completions.create(
|
| 446 |
-
model=GROQ_PREFERRED,
|
| 447 |
-
messages=messages,
|
| 448 |
-
temperature=0.7,
|
| 449 |
-
max_tokens=1024,
|
| 450 |
-
stream=False,
|
| 451 |
-
)
|
| 452 |
-
text = completion.choices[0].message.content
|
| 453 |
-
if text:
|
| 454 |
-
print("β
Response from Groq")
|
| 455 |
-
return text
|
| 456 |
-
except Exception as e:
|
| 457 |
-
print(f"β οΈ Groq failed: {str(e)[:200]}")
|
| 458 |
-
|
| 459 |
-
# 3οΈβ£ Ollama
|
| 460 |
try:
|
| 461 |
async with httpx.AsyncClient(timeout=120.0) as hc:
|
| 462 |
r = await hc.post(
|
|
@@ -939,19 +899,28 @@ Description:
|
|
| 939 |
except Exception as e:
|
| 940 |
print(f"β οΈ Cerebras summary failed: {e}")
|
| 941 |
|
| 942 |
-
|
| 943 |
-
|
| 944 |
-
|
| 945 |
-
|
| 946 |
-
|
| 947 |
-
|
| 948 |
-
|
| 949 |
-
|
| 950 |
-
|
| 951 |
-
|
| 952 |
-
|
| 953 |
-
|
| 954 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 955 |
|
| 956 |
return {"error": "No AI provider available for summary"}
|
| 957 |
except Exception as e:
|
|
@@ -1026,7 +995,6 @@ async def get_config():
|
|
| 1026 |
"version": "2.5",
|
| 1027 |
"provider_chain": [
|
| 1028 |
"cerebras" if cerebras_client else None,
|
| 1029 |
-
"groq" if groq_client else None,
|
| 1030 |
"ollama"
|
| 1031 |
],
|
| 1032 |
"features": {
|
|
@@ -1043,7 +1011,6 @@ async def get_config():
|
|
| 1043 |
@app.get("/health")
|
| 1044 |
async def health():
|
| 1045 |
cerebras_status = "not_configured"
|
| 1046 |
-
groq_status = "not_configured"
|
| 1047 |
ollama_status = "unknown"
|
| 1048 |
|
| 1049 |
if cerebras_client:
|
|
@@ -1057,17 +1024,6 @@ async def health():
|
|
| 1057 |
except Exception as e:
|
| 1058 |
cerebras_status = f"error: {str(e)[:100]}"
|
| 1059 |
|
| 1060 |
-
if groq_client:
|
| 1061 |
-
try:
|
| 1062 |
-
groq_client.chat.completions.create(
|
| 1063 |
-
model=GROQ_MODELS["fast"],
|
| 1064 |
-
messages=[{"role": "user", "content": "Say OK"}],
|
| 1065 |
-
max_tokens=5,
|
| 1066 |
-
)
|
| 1067 |
-
groq_status = "connected"
|
| 1068 |
-
except Exception as e:
|
| 1069 |
-
groq_status = f"error: {str(e)[:100]}"
|
| 1070 |
-
|
| 1071 |
try:
|
| 1072 |
async with httpx.AsyncClient(timeout=5.0) as hc:
|
| 1073 |
r = await hc.get(f"{OLLAMA_BASE_URL}/models")
|
|
@@ -1089,7 +1045,6 @@ async def health():
|
|
| 1089 |
"message": "β
Cortexa Backend Running",
|
| 1090 |
"provider_chain": [
|
| 1091 |
{"name": "cerebras", "status": cerebras_status, "primary": True},
|
| 1092 |
-
{"name": "groq", "status": groq_status, "primary": False},
|
| 1093 |
{"name": "ollama", "status": ollama_status, "primary": False},
|
| 1094 |
],
|
| 1095 |
"youtube_api": "configured" if YOUTUBE_API_KEY else "not configured",
|
|
@@ -1108,12 +1063,6 @@ async def list_models():
|
|
| 1108 |
"enabled": cerebras_client is not None,
|
| 1109 |
"primary": True,
|
| 1110 |
},
|
| 1111 |
-
"groq": {
|
| 1112 |
-
"models": GROQ_MODELS,
|
| 1113 |
-
"preferred": GROQ_PREFERRED,
|
| 1114 |
-
"enabled": groq_client is not None,
|
| 1115 |
-
"primary": False,
|
| 1116 |
-
},
|
| 1117 |
"ollama": {
|
| 1118 |
"model": OLLAMA_MODEL,
|
| 1119 |
"base_url": OLLAMA_BASE_URL,
|
|
@@ -1128,7 +1077,7 @@ async def root():
|
|
| 1128 |
"message": "π Cortexa Backend Running",
|
| 1129 |
"version": "2.5",
|
| 1130 |
"status": "active",
|
| 1131 |
-
"provider_chain": ["cerebras", "
|
| 1132 |
"endpoints": [
|
| 1133 |
"/api/chat", "/chat",
|
| 1134 |
"/api/register", "/api/login", "/api/logout", "/api/me",
|
|
@@ -1168,7 +1117,6 @@ if __name__ == "__main__":
|
|
| 1168 |
print(f"π Port: {port}")
|
| 1169 |
print(f"πΎ DB: {DB_PATH}")
|
| 1170 |
print(f"π§ Cerebras: {'β
configured' if cerebras_client else 'β missing'}")
|
| 1171 |
-
print(f"π§ Groq: {'β
configured' if groq_client else 'βοΈ skipped'}")
|
| 1172 |
print(f"π¦ Ollama: {OLLAMA_BASE_URL}")
|
| 1173 |
print("================================================")
|
| 1174 |
|
|
|
|
| 55 |
load_dotenv()
|
| 56 |
|
| 57 |
CEREBRAS_API_KEY = os.getenv("CEREBRAS_API_KEY")
|
|
|
|
| 58 |
YOUTUBE_API_KEY = os.getenv("YOUTUBE_API_KEY")
|
| 59 |
|
| 60 |
JWT_SECRET_KEY = os.getenv(
|
|
|
|
| 110 |
print("β οΈ CEREBRAS_API_KEY not set β Cerebras disabled")
|
| 111 |
|
| 112 |
|
| 113 |
+
# OLLAMA (FALLBACK)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
OLLAMA_BASE_URL = os.getenv("OLLAMA_BASE_URL", "http://localhost:11434/v1")
|
| 115 |
OLLAMA_MODEL = os.getenv("OLLAMA_MODEL", "llama3")
|
| 116 |
|
|
|
|
| 357 |
password: str
|
| 358 |
|
| 359 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 360 |
SYSTEM_PROMPT = """
|
| 361 |
You are Cortexa, a powerful AI assistant.
|
| 362 |
|
|
|
|
| 388 |
11. For coding questions, provide correct,
|
| 389 |
practical code and explain the important parts.
|
| 390 |
"""
|
| 391 |
+
# AI RESPONSE β FALLBACK CHAIN (Cerebras -> Ollama)
|
| 392 |
async def get_ai_response(message: str, context: Optional[str] = None) -> str:
|
| 393 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 394 |
|
|
|
|
| 416 |
except Exception as e:
|
| 417 |
print(f"β οΈ Cerebras failed: {str(e)[:200]}")
|
| 418 |
|
| 419 |
+
# 2οΈβ£ Ollama
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 420 |
try:
|
| 421 |
async with httpx.AsyncClient(timeout=120.0) as hc:
|
| 422 |
r = await hc.post(
|
|
|
|
| 899 |
except Exception as e:
|
| 900 |
print(f"β οΈ Cerebras summary failed: {e}")
|
| 901 |
|
| 902 |
+
# Ollama fallback for summary
|
| 903 |
+
try:
|
| 904 |
+
async with httpx.AsyncClient(timeout=120.0) as hc:
|
| 905 |
+
r = await hc.post(
|
| 906 |
+
f"{OLLAMA_BASE_URL}/chat/completions",
|
| 907 |
+
json={
|
| 908 |
+
"model": OLLAMA_MODEL,
|
| 909 |
+
"messages": messages,
|
| 910 |
+
"temperature": 0.7,
|
| 911 |
+
},
|
| 912 |
+
)
|
| 913 |
+
r.raise_for_status()
|
| 914 |
+
rdata = r.json()
|
| 915 |
+
text = rdata["choices"][0]["message"]["content"]
|
| 916 |
+
if text:
|
| 917 |
+
return {
|
| 918 |
+
"summary": text,
|
| 919 |
+
"title": info["title"],
|
| 920 |
+
"video_id": video_id
|
| 921 |
+
}
|
| 922 |
+
except Exception as e:
|
| 923 |
+
print(f"β οΈ Ollama summary failed: {str(e)[:200]}")
|
| 924 |
|
| 925 |
return {"error": "No AI provider available for summary"}
|
| 926 |
except Exception as e:
|
|
|
|
| 995 |
"version": "2.5",
|
| 996 |
"provider_chain": [
|
| 997 |
"cerebras" if cerebras_client else None,
|
|
|
|
| 998 |
"ollama"
|
| 999 |
],
|
| 1000 |
"features": {
|
|
|
|
| 1011 |
@app.get("/health")
|
| 1012 |
async def health():
|
| 1013 |
cerebras_status = "not_configured"
|
|
|
|
| 1014 |
ollama_status = "unknown"
|
| 1015 |
|
| 1016 |
if cerebras_client:
|
|
|
|
| 1024 |
except Exception as e:
|
| 1025 |
cerebras_status = f"error: {str(e)[:100]}"
|
| 1026 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1027 |
try:
|
| 1028 |
async with httpx.AsyncClient(timeout=5.0) as hc:
|
| 1029 |
r = await hc.get(f"{OLLAMA_BASE_URL}/models")
|
|
|
|
| 1045 |
"message": "β
Cortexa Backend Running",
|
| 1046 |
"provider_chain": [
|
| 1047 |
{"name": "cerebras", "status": cerebras_status, "primary": True},
|
|
|
|
| 1048 |
{"name": "ollama", "status": ollama_status, "primary": False},
|
| 1049 |
],
|
| 1050 |
"youtube_api": "configured" if YOUTUBE_API_KEY else "not configured",
|
|
|
|
| 1063 |
"enabled": cerebras_client is not None,
|
| 1064 |
"primary": True,
|
| 1065 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1066 |
"ollama": {
|
| 1067 |
"model": OLLAMA_MODEL,
|
| 1068 |
"base_url": OLLAMA_BASE_URL,
|
|
|
|
| 1077 |
"message": "π Cortexa Backend Running",
|
| 1078 |
"version": "2.5",
|
| 1079 |
"status": "active",
|
| 1080 |
+
"provider_chain": ["cerebras", "ollama"],
|
| 1081 |
"endpoints": [
|
| 1082 |
"/api/chat", "/chat",
|
| 1083 |
"/api/register", "/api/login", "/api/logout", "/api/me",
|
|
|
|
| 1117 |
print(f"π Port: {port}")
|
| 1118 |
print(f"πΎ DB: {DB_PATH}")
|
| 1119 |
print(f"π§ Cerebras: {'β
configured' if cerebras_client else 'β missing'}")
|
|
|
|
| 1120 |
print(f"π¦ Ollama: {OLLAMA_BASE_URL}")
|
| 1121 |
print("================================================")
|
| 1122 |
|