Add !ai Matrix command for querying other models via OpenRouter
build-agent / build-and-push (push) Successful in 7s
build-agent / build-and-push (push) Successful in 7s
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
const OPENROUTER_API_KEY = process.env.OPENROUTER_API_KEY;
|
||||
const DEFAULT_MODEL = process.env.OPENROUTER_DEFAULT_MODEL || "openai/gpt-4o-mini";
|
||||
|
||||
export async function askOpenRouter(model, prompt) {
|
||||
if (!OPENROUTER_API_KEY) {
|
||||
throw new Error("OPENROUTER_API_KEY is not set");
|
||||
}
|
||||
|
||||
const res = await fetch("https://openrouter.ai/api/v1/chat/completions", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer ${OPENROUTER_API_KEY}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
model: model || DEFAULT_MODEL,
|
||||
messages: [{ role: "user", content: prompt }],
|
||||
}),
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
throw new Error(`OpenRouter request failed: ${res.status} ${await res.text()}`);
|
||||
}
|
||||
|
||||
const data = await res.json();
|
||||
const content = data.choices?.[0]?.message?.content;
|
||||
if (!content) {
|
||||
throw new Error(`OpenRouter returned no content: ${JSON.stringify(data)}`);
|
||||
}
|
||||
return content;
|
||||
}
|
||||
|
||||
export { DEFAULT_MODEL };
|
||||
Reference in New Issue
Block a user