51 lines
2.4 KiB
PHP
51 lines
2.4 KiB
PHP
<?php
|
|
opcache_invalidate(__FILE__,true);
|
|
header("Content-Type: application/json");
|
|
header("Access-Control-Allow-Origin: *");
|
|
|
|
$input = json_decode(file_get_contents("php://input"),true);
|
|
$msg = $input["message"] ?? $_GET["msg"] ?? "test";
|
|
$provider = $input["provider"] ?? $_GET["provider"] ?? "auto";
|
|
|
|
$secrets = @file_get_contents("/etc/weval/secrets.env");
|
|
preg_match("/OPENROUTER_KEY=(.+)/", $secrets, $m);
|
|
$or_key = trim($m[1] ?? "");
|
|
|
|
$models = [
|
|
"trinity" => ["id"=>"arcee-ai/trinity-large-preview:free","name"=>"Trinity Large","free"=>true],
|
|
"trinity-think" => ["id"=>"arcee-ai/trinity-large-thinking","name"=>"Trinity Thinking","free"=>false],
|
|
"maestro" => ["id"=>"arcee-ai/maestro-reasoning","name"=>"Maestro Reasoning","free"=>false],
|
|
"minimax" => ["id"=>"minimax/minimax-m2.5:free","name"=>"MiniMax M2.5","free"=>true],
|
|
"minimax-m27" => ["id"=>"minimax/minimax-m2.7","name"=>"MiniMax M2.7","free"=>false],
|
|
"mimo-flash" => ["id"=>"xiaomi/mimo-v2-flash:free","name"=>"MiMo V2 Flash","free"=>true],
|
|
"mimo-omni" => ["id"=>"xiaomi/mimo-v2-omni","name"=>"MiMo V2 Omni","free"=>false],
|
|
"mimo-pro" => ["id"=>"xiaomi/mimo-v2-pro","name"=>"MiMo V2 Pro","free"=>false],
|
|
];
|
|
|
|
if ($provider==="list" || ($_GET["action"] ?? "")==="list") {
|
|
echo json_encode(["ok"=>true,"count"=>count($models),"models"=>$models]); exit;
|
|
}
|
|
|
|
if ($provider==="auto") {
|
|
$free = array_filter($models, function($m){return $m["free"];});
|
|
$keys = array_keys($free);
|
|
$model = $free[$keys[array_rand($keys)]];
|
|
} elseif (isset($models[$provider])) {
|
|
$model = $models[$provider];
|
|
} else {
|
|
$model = $models["trinity"];
|
|
}
|
|
|
|
$ch = curl_init("https://openrouter.ai/api/v1/chat/completions");
|
|
curl_setopt_array($ch, [CURLOPT_RETURNTRANSFER=>1, CURLOPT_POST=>1,
|
|
CURLOPT_POSTFIELDS=>json_encode(["model"=>$model["id"],"messages"=>[["role"=>"user","content"=>$msg]],"max_tokens"=>1200]),
|
|
CURLOPT_HTTPHEADER=>["Content-Type: application/json","Authorization: Bearer ".$or_key],
|
|
CURLOPT_TIMEOUT=>25, CURLOPT_SSL_VERIFYPEER=>false]);
|
|
$r = curl_exec($ch); $d = @json_decode($r,true);
|
|
|
|
if (isset($d["choices"][0]["message"]["content"])) {
|
|
echo json_encode(["ok"=>true,"model"=>$model["name"],"model_id"=>$model["id"],"content"=>$d["choices"][0]["message"]["content"]]);
|
|
} else {
|
|
echo json_encode(["ok"=>false,"model"=>$model["name"],"error"=>$d["error"]["message"] ?? "unknown","hint"=>"OpenRouter 50 free/day. Add 10 credits for 1000/day"]);
|
|
}
|