fix: more

This commit is contained in:
2026-09-28 00:47:52 -04:00
parent c233fcda38
commit d454635832
4 changed files with 38 additions and 23 deletions

View File

@@ -12,10 +12,10 @@ mod server;
#[derive(Parser, Debug)]
struct AppArgs {
#[arg(long, env)]
#[arg(long, env, default_value_t = [127, 0, 0, 1].into())]
ip: IpAddr,
#[arg(long, env)]
#[arg(long, env, default_value_t = 3126)]
port: u16,
#[arg(long)]
@@ -106,6 +106,7 @@ async fn main() {
};
join!(stream_responses, async move {
tracing::info!(?addr, "listening");
axum::serve(listener, app).await.expect("TODO");
});
}

View File

@@ -45,8 +45,8 @@ pub struct AppState {
}
#[derive(Deserialize, TypedPath)]
#[typed_path("/chat/completions")]
struct ChatCompletionsPath;
#[typed_path("/v1/chat/completions")]
struct V1ChatCompletionsPath;
#[derive(Debug, Clone)]
enum Provider {
@@ -59,6 +59,7 @@ enum Provider {
}
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "lowercase")]
enum Shortcut {
XS,
S,
@@ -142,8 +143,8 @@ impl Shortcut {
}
fn take_random_by_swapping<T>(mut vec1: Vec1<T>, mut rng: impl Rng) -> (T, Option<Vec1<T>>) {
let end = vec1.len().get();
let index = rng.random_range(0..end);
let end = vec1.len().get() - 1;
let index = rng.random_range(0..=end);
vec1.swap(index, end);
let (body, element) = vec1.into_rtail_and_head();
@@ -206,7 +207,7 @@ impl IntoResponse for ChatCompletionError {
#[axum::debug_handler]
#[tracing::instrument(skip(state, request))]
async fn create_chat_completion(
_: ChatCompletionsPath,
_: V1ChatCompletionsPath,
State(state): State<AppState>,
Json(request): Json<openai::chat::completion::CreateChatCompletionRequest<Shortcut>>,
) -> Result<ChatCompletionResponse, ChatCompletionError> {
@@ -293,6 +294,8 @@ async fn create_chat_completion(
model_upstreams.push_back((model.clone(), upstreams));
}
tracing::info!(?model, ?upstream);
let openai::chat::completion::CreateChatCompletionRequest {
model: _shortcut,
messages,
@@ -375,18 +378,25 @@ async fn create_chat_completion(
let client = reqwest::Client::new();
let mut upstream_request = client.post({
let mut url = upstream.base_url.clone();
let mut upstream_request = client
.post({
let mut url = upstream.base_url;
{
let mut segments = url.path_segments_mut().expect("TODO");
{
let mut path_segments = url.path_segments_mut().expect("TODO");
segments.push("chat");
segments.push("completions");
}
let s = V1ChatCompletionsPath.to_string();
let s = s.strip_prefix("/").unwrap_or(&s);
let s = s.strip_suffix("/").unwrap_or(&s);
url
});
path_segments.extend(s.split("/"));
}
tracing::info!(?url);
url
})
.json(&request);
if let Some(api_key) = upstream.api_key {
let authorization =
@@ -410,12 +420,13 @@ async fn create_chat_completion(
}
};
let upstream_response = match upstream_response.error_for_status() {
Ok(upstream_response) => upstream_response,
Err(upstream_error) => {
tracing::error!(?upstream_error);
continue;
}
if let Err(upstream_error) = upstream_response.error_for_status_ref() {
tracing::error!(?upstream_error);
match upstream_response.text().await {
Ok(body) => tracing::error!(?body),
Err(body_error) => tracing::error!(?body_error),
};
continue;
};
if request.stream.unwrap_or(false) {