Update llm/aggregator.py
Browse files- llm/aggregator.py +77 -7
llm/aggregator.py
CHANGED
|
@@ -1,10 +1,80 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
import httpx
|
| 3 |
+
import os
|
| 4 |
|
| 5 |
+
OPENROUTER_BASE = "https://openrouter.ai/api/v1/chat/completions"
|
| 6 |
+
HEADERS = {
|
| 7 |
+
"Authorization": f"Bearer {os.getenv('OPENROUTER_API_KEY')}",
|
| 8 |
+
"Content-Type": "application/json",
|
| 9 |
+
}
|
| 10 |
|
| 11 |
+
ALLOWED_MODELS = [
|
| 12 |
+
"deepseek/deepseek-chat-v3-0324:free",
|
| 13 |
+
"google/gemini-2.0-flash-exp:free",
|
| 14 |
+
"meta-llama/llama-4-maverick:free",
|
| 15 |
+
"microsoft/mai-ds-r1:free",
|
| 16 |
+
"meta-llama/llama-4-scout:free",
|
| 17 |
+
"google/gemma-3-27b-it:free",
|
| 18 |
+
"qwen/qwq-32b:free",
|
| 19 |
+
"qwen/qwen2.5-vl-72b-instruct:free",
|
| 20 |
+
"qwen/qwen-2.5-72b-instruct:free",
|
| 21 |
+
"google/gemini-2.5-pro-exp-03-25:free",
|
| 22 |
+
"deepseek/deepseek-r1:free",
|
| 23 |
+
]
|
| 24 |
|
| 25 |
+
async def call_openrouter(model: str, prompt: str, system_prompt: str = None) -> str:
|
| 26 |
+
messages = []
|
| 27 |
+
if system_prompt:
|
| 28 |
+
messages.append({"role": "system", "content": system_prompt})
|
| 29 |
+
messages.append({"role": "user", "content": prompt})
|
| 30 |
+
|
| 31 |
+
body = {
|
| 32 |
+
"model": model,
|
| 33 |
+
"messages": messages,
|
| 34 |
+
"temperature": 0.7,
|
| 35 |
+
}
|
| 36 |
+
async with httpx.AsyncClient(timeout=30) as client:
|
| 37 |
+
response = await client.post(OPENROUTER_BASE, headers=HEADERS, json=body)
|
| 38 |
+
response.raise_for_status()
|
| 39 |
+
return response.json()["choices"][0]["message"]["content"]
|
| 40 |
+
|
| 41 |
+
async def query_llm_agent(name: str, prompt: str, settings: dict) -> str:
|
| 42 |
+
selected_model = settings.get("models", {}).get(name)
|
| 43 |
+
|
| 44 |
+
if not selected_model:
|
| 45 |
+
return f"[{name}] No model selected."
|
| 46 |
+
|
| 47 |
+
if selected_model not in ALLOWED_MODELS:
|
| 48 |
+
return f"[{name}] Model '{selected_model}' is not supported."
|
| 49 |
+
|
| 50 |
+
try:
|
| 51 |
+
response = await call_openrouter(selected_model, prompt)
|
| 52 |
+
return f"[{name}] {response}"
|
| 53 |
+
except Exception as e:
|
| 54 |
+
return f"[{name}] Error: {str(e)}"
|
| 55 |
+
|
| 56 |
+
async def query_all_llms(prompt: str, settings: dict) -> list:
|
| 57 |
+
agents = ["LLM-A", "LLM-B", "LLM-C"]
|
| 58 |
+
tasks = [query_llm_agent(agent, prompt, settings) for agent in agents]
|
| 59 |
+
results = await asyncio.gather(*tasks)
|
| 60 |
+
return results
|
| 61 |
+
|
| 62 |
+
def query_all_llms_sync(prompt: str, settings: dict) -> list:
|
| 63 |
+
return asyncio.run(query_all_llms(prompt, settings))
|
| 64 |
+
|
| 65 |
+
# Aggregator logic
|
| 66 |
+
async def query_aggregator(responses: list, settings: dict) -> str:
|
| 67 |
+
aggregator_model = settings.get("aggregator", "meta-llama/llama-4-scout:free")
|
| 68 |
+
|
| 69 |
+
if aggregator_model not in ALLOWED_MODELS:
|
| 70 |
+
return "[Aggregator] Model not allowed."
|
| 71 |
+
|
| 72 |
+
system_prompt = "You are an AI tasked with synthesizing the following responses from multiple language models into a single, coherent, high-quality answer."
|
| 73 |
+
joined_input = "\n\n".join(responses)
|
| 74 |
+
try:
|
| 75 |
+
return await call_openrouter(aggregator_model, joined_input, system_prompt)
|
| 76 |
+
except Exception as e:
|
| 77 |
+
return f"[Aggregator] Error: {str(e)}"
|
| 78 |
+
|
| 79 |
+
def aggregate_responses(responses: list, settings: dict) -> str:
|
| 80 |
+
return asyncio.run(query_aggregator(responses, settings))
|