Fix: worker sender Authorization-header til AI Gateway
Uten nøkkelen returnerte LiteLLM 401, og AI-jobber feilet etter 3 forsøk. Ny CLI-param --ai-gateway-key (env: AI_GATEWAY_KEY). dev.sh leser LITELLM_MASTER_KEY fra .env.local og eksporterer som AI_GATEWAY_KEY. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
0a560bdf2d
commit
4b8da64fc5
4 changed files with 19 additions and 4 deletions
1
dev.sh
1
dev.sh
|
|
@ -57,6 +57,7 @@ echo ""
|
||||||
echo "=== Starter worker + frontend ==="
|
echo "=== Starter worker + frontend ==="
|
||||||
|
|
||||||
cd "$ROOT/worker"
|
cd "$ROOT/worker"
|
||||||
|
export AI_GATEWAY_KEY=$(grep LITELLM_MASTER_KEY "$ROOT/.env.local" | cut -d= -f2)
|
||||||
cargo run -- --spacetimedb-url "$SPACETIME_URL" --sync-interval 1 --warmup-limit 100 2>&1 | sed 's/^/[worker] /' &
|
cargo run -- --spacetimedb-url "$SPACETIME_URL" --sync-interval 1 --warmup-limit 100 2>&1 | sed 's/^/[worker] /' &
|
||||||
WORKER_PID=$!
|
WORKER_PID=$!
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -34,13 +34,15 @@ struct AiResponse {
|
||||||
pub struct AiTextProcessHandler {
|
pub struct AiTextProcessHandler {
|
||||||
http: reqwest::Client,
|
http: reqwest::Client,
|
||||||
ai_gateway_url: String,
|
ai_gateway_url: String,
|
||||||
|
ai_gateway_key: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AiTextProcessHandler {
|
impl AiTextProcessHandler {
|
||||||
pub fn new(http: reqwest::Client, ai_gateway_url: String) -> Self {
|
pub fn new(http: reqwest::Client, ai_gateway_url: String, ai_gateway_key: String) -> Self {
|
||||||
Self {
|
Self {
|
||||||
http,
|
http,
|
||||||
ai_gateway_url,
|
ai_gateway_url,
|
||||||
|
ai_gateway_key,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -223,10 +225,16 @@ impl AiTextProcessHandler {
|
||||||
"max_tokens": 4096
|
"max_tokens": 4096
|
||||||
});
|
});
|
||||||
|
|
||||||
let response = self
|
let mut req = self
|
||||||
.http
|
.http
|
||||||
.post(format!("{}/chat/completions", self.ai_gateway_url))
|
.post(format!("{}/chat/completions", self.ai_gateway_url))
|
||||||
.json(&request_body)
|
.json(&request_body);
|
||||||
|
|
||||||
|
if !self.ai_gateway_key.is_empty() {
|
||||||
|
req = req.header("Authorization", format!("Bearer {}", self.ai_gateway_key));
|
||||||
|
}
|
||||||
|
|
||||||
|
let response = req
|
||||||
.send()
|
.send()
|
||||||
.await
|
.await
|
||||||
.context("HTTP-kall til AI Gateway feilet")?;
|
.context("HTTP-kall til AI Gateway feilet")?;
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@ pub trait JobHandler: Send + Sync {
|
||||||
pub type HandlerRegistry = HashMap<String, Box<dyn JobHandler>>;
|
pub type HandlerRegistry = HashMap<String, Box<dyn JobHandler>>;
|
||||||
|
|
||||||
/// Bygg registeret med alle tilgjengelige handlers.
|
/// Bygg registeret med alle tilgjengelige handlers.
|
||||||
pub fn build_registry(http: reqwest::Client, ai_gateway_url: String) -> HandlerRegistry {
|
pub fn build_registry(http: reqwest::Client, ai_gateway_url: String, ai_gateway_key: String) -> HandlerRegistry {
|
||||||
let mut registry: HandlerRegistry = HashMap::new();
|
let mut registry: HandlerRegistry = HashMap::new();
|
||||||
|
|
||||||
// Echo-handler for testing
|
// Echo-handler for testing
|
||||||
|
|
@ -34,6 +34,7 @@ pub fn build_registry(http: reqwest::Client, ai_gateway_url: String) -> HandlerR
|
||||||
Box::new(ai_text_process::AiTextProcessHandler::new(
|
Box::new(ai_text_process::AiTextProcessHandler::new(
|
||||||
http.clone(),
|
http.clone(),
|
||||||
ai_gateway_url.clone(),
|
ai_gateway_url.clone(),
|
||||||
|
ai_gateway_key.clone(),
|
||||||
)),
|
)),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,10 @@ struct Cli {
|
||||||
)]
|
)]
|
||||||
ai_gateway_url: String,
|
ai_gateway_url: String,
|
||||||
|
|
||||||
|
/// AI Gateway API-nøkkel (LiteLLM master key)
|
||||||
|
#[arg(long, env = "AI_GATEWAY_KEY", default_value = "")]
|
||||||
|
ai_gateway_key: String,
|
||||||
|
|
||||||
/// Maks samtidige jobber
|
/// Maks samtidige jobber
|
||||||
#[arg(long, default_value = "3")]
|
#[arg(long, default_value = "3")]
|
||||||
max_concurrent: usize,
|
max_concurrent: usize,
|
||||||
|
|
@ -80,6 +84,7 @@ async fn main() -> anyhow::Result<()> {
|
||||||
let registry = Arc::new(handlers::build_registry(
|
let registry = Arc::new(handlers::build_registry(
|
||||||
reqwest::Client::new(),
|
reqwest::Client::new(),
|
||||||
cli.ai_gateway_url,
|
cli.ai_gateway_url,
|
||||||
|
cli.ai_gateway_key,
|
||||||
));
|
));
|
||||||
|
|
||||||
let registered: Vec<&str> = registry.keys().map(|k| k.as_str()).collect();
|
let registered: Vec<&str> = registry.keys().map(|k| k.as_str()).collect();
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue