Spaces:
Running
Running
Commit ·
1929382
1
Parent(s): 2ebf97a
ok
Browse files- app/api/v1/chat.py +2 -2
- app/services/chat_service.py +27 -40
app/api/v1/chat.py
CHANGED
|
@@ -5,9 +5,9 @@ from typing import Any, Dict, List, Optional
|
|
| 5 |
from fastapi import APIRouter, Depends, HTTPException, Request
|
| 6 |
|
| 7 |
from app.api.deps import require_auth
|
| 8 |
-
from app.services.chat_service import
|
| 9 |
|
| 10 |
-
VALID_MODELS =
|
| 11 |
VALID_PROVIDERS = ["openprovider", "meganova", "aionlabs"]
|
| 12 |
|
| 13 |
router = APIRouter()
|
|
|
|
| 5 |
from fastapi import APIRouter, Depends, HTTPException, Request
|
| 6 |
|
| 7 |
from app.api.deps import require_auth
|
| 8 |
+
from app.services.chat_service import chat_completion
|
| 9 |
|
| 10 |
+
VALID_MODELS = ["agentdeck-1.0", "agentdeck-flash", "agentdeck-0.1"]
|
| 11 |
VALID_PROVIDERS = ["openprovider", "meganova", "aionlabs"]
|
| 12 |
|
| 13 |
router = APIRouter()
|
app/services/chat_service.py
CHANGED
|
@@ -31,12 +31,6 @@ PREFIX = "ai_lb"
|
|
| 31 |
AION_PREFIX = "ai_lb:aion"
|
| 32 |
DEFAULT_JSON_PROMPT = "Return your response as a valid JSON object inside a JSON code block (```json)."
|
| 33 |
|
| 34 |
-
MODEL_MAP: Dict[str, str] = {
|
| 35 |
-
"agentdeck-flash": "aionlabs",
|
| 36 |
-
"agentdeck-1.0": "meganova",
|
| 37 |
-
"agentdeck-0.1": "openprovider",
|
| 38 |
-
}
|
| 39 |
-
|
| 40 |
KEY_IDS: List[str] = []
|
| 41 |
KEY_MAP: Dict[str, str] = {}
|
| 42 |
|
|
@@ -439,7 +433,7 @@ async def chat_completion(
|
|
| 439 |
messages = inject_system_identity(messages, model)
|
| 440 |
|
| 441 |
if not provider:
|
| 442 |
-
provider =
|
| 443 |
|
| 444 |
logger.info(
|
| 445 |
"Chat completion: model=%s provider=%s messages=%d",
|
|
@@ -448,13 +442,7 @@ async def chat_completion(
|
|
| 448 |
len(messages),
|
| 449 |
)
|
| 450 |
|
| 451 |
-
if
|
| 452 |
-
result = await call_openrouter_mimika(
|
| 453 |
-
messages, response_format, max_tokens, temperature, top_p,
|
| 454 |
-
)
|
| 455 |
-
if result:
|
| 456 |
-
return result
|
| 457 |
-
raise RuntimeError("OpenRouter Mimika request failed")
|
| 458 |
|
| 459 |
if provider == "aionlabs":
|
| 460 |
if redis and scripts:
|
|
@@ -462,56 +450,55 @@ async def chat_completion(
|
|
| 462 |
redis, messages, response_format, model or AION_LABS_DEFAULT_MODEL,
|
| 463 |
max_tokens, temperature, top_p,
|
| 464 |
)
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
messages, response_format, model or AION_LABS_DEFAULT_MODEL,
|
| 471 |
-
max_tokens, temperature, top_p,
|
| 472 |
-
)
|
| 473 |
if result:
|
| 474 |
return result
|
| 475 |
raise RuntimeError("AionLabs request failed")
|
| 476 |
|
| 477 |
-
if provider
|
| 478 |
-
|
| 479 |
messages, response_format, max_tokens, temperature, top_p,
|
| 480 |
)
|
| 481 |
-
if
|
| 482 |
-
|
| 483 |
-
|
| 484 |
|
| 485 |
-
target = model if model in MODELS else None
|
| 486 |
if redis and scripts:
|
| 487 |
-
|
| 488 |
redis, scripts, messages, response_format,
|
| 489 |
max_tokens, temperature, top_p, target,
|
| 490 |
)
|
| 491 |
-
if meganova_result is not None:
|
| 492 |
-
return meganova_result
|
| 493 |
else:
|
| 494 |
-
|
| 495 |
messages, response_format, max_tokens, temperature, top_p, target,
|
| 496 |
)
|
| 497 |
-
|
| 498 |
-
|
| 499 |
|
| 500 |
logger.info("MegaNova failed, falling back to AionLabs")
|
| 501 |
if redis and scripts:
|
| 502 |
-
|
| 503 |
redis, messages, response_format, model or AION_LABS_DEFAULT_MODEL,
|
| 504 |
max_tokens, temperature, top_p,
|
| 505 |
)
|
| 506 |
-
if aion_result is not None:
|
| 507 |
-
return aion_result
|
| 508 |
else:
|
| 509 |
-
|
| 510 |
messages, response_format, model or AION_LABS_DEFAULT_MODEL,
|
| 511 |
max_tokens, temperature, top_p,
|
| 512 |
)
|
| 513 |
-
|
| 514 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 515 |
|
| 516 |
raise RuntimeError("All AI providers exhausted")
|
| 517 |
|
|
|
|
| 31 |
AION_PREFIX = "ai_lb:aion"
|
| 32 |
DEFAULT_JSON_PROMPT = "Return your response as a valid JSON object inside a JSON code block (```json)."
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
KEY_IDS: List[str] = []
|
| 35 |
KEY_MAP: Dict[str, str] = {}
|
| 36 |
|
|
|
|
| 433 |
messages = inject_system_identity(messages, model)
|
| 434 |
|
| 435 |
if not provider:
|
| 436 |
+
provider = "meganova"
|
| 437 |
|
| 438 |
logger.info(
|
| 439 |
"Chat completion: model=%s provider=%s messages=%d",
|
|
|
|
| 442 |
len(messages),
|
| 443 |
)
|
| 444 |
|
| 445 |
+
target = model if model in MODELS else None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 446 |
|
| 447 |
if provider == "aionlabs":
|
| 448 |
if redis and scripts:
|
|
|
|
| 450 |
redis, messages, response_format, model or AION_LABS_DEFAULT_MODEL,
|
| 451 |
max_tokens, temperature, top_p,
|
| 452 |
)
|
| 453 |
+
else:
|
| 454 |
+
result = await call_aion_labs_no_redis(
|
| 455 |
+
messages, response_format, model or AION_LABS_DEFAULT_MODEL,
|
| 456 |
+
max_tokens, temperature, top_p,
|
| 457 |
+
)
|
|
|
|
|
|
|
|
|
|
| 458 |
if result:
|
| 459 |
return result
|
| 460 |
raise RuntimeError("AionLabs request failed")
|
| 461 |
|
| 462 |
+
if provider == "openprovider":
|
| 463 |
+
result = await call_openrouter_mimika(
|
| 464 |
messages, response_format, max_tokens, temperature, top_p,
|
| 465 |
)
|
| 466 |
+
if result:
|
| 467 |
+
return result
|
| 468 |
+
raise RuntimeError("OpenRouter Mimika request failed")
|
| 469 |
|
|
|
|
| 470 |
if redis and scripts:
|
| 471 |
+
result = await call_meganova(
|
| 472 |
redis, scripts, messages, response_format,
|
| 473 |
max_tokens, temperature, top_p, target,
|
| 474 |
)
|
|
|
|
|
|
|
| 475 |
else:
|
| 476 |
+
result = await call_meganova_no_redis(
|
| 477 |
messages, response_format, max_tokens, temperature, top_p, target,
|
| 478 |
)
|
| 479 |
+
if result:
|
| 480 |
+
return result
|
| 481 |
|
| 482 |
logger.info("MegaNova failed, falling back to AionLabs")
|
| 483 |
if redis and scripts:
|
| 484 |
+
result = await call_aion_labs(
|
| 485 |
redis, messages, response_format, model or AION_LABS_DEFAULT_MODEL,
|
| 486 |
max_tokens, temperature, top_p,
|
| 487 |
)
|
|
|
|
|
|
|
| 488 |
else:
|
| 489 |
+
result = await call_aion_labs_no_redis(
|
| 490 |
messages, response_format, model or AION_LABS_DEFAULT_MODEL,
|
| 491 |
max_tokens, temperature, top_p,
|
| 492 |
)
|
| 493 |
+
if result:
|
| 494 |
+
return result
|
| 495 |
+
|
| 496 |
+
logger.info("AionLabs failed, falling back to OpenRouter Mimika")
|
| 497 |
+
result = await call_openrouter_mimika(
|
| 498 |
+
messages, response_format, max_tokens, temperature, top_p,
|
| 499 |
+
)
|
| 500 |
+
if result:
|
| 501 |
+
return result
|
| 502 |
|
| 503 |
raise RuntimeError("All AI providers exhausted")
|
| 504 |
|