[model-gateway] minor code clean up (#15578)
This commit is contained in:
@@ -14,65 +14,59 @@ use crate::{
|
||||
protocols::parser::{ParseFunctionCallRequest, SeparateReasoningRequest},
|
||||
};
|
||||
|
||||
/// Helper to create error responses
|
||||
fn error_response(status: StatusCode, message: &str) -> Response {
|
||||
(
|
||||
status,
|
||||
Json(serde_json::json!({
|
||||
"error": message,
|
||||
"success": false
|
||||
})),
|
||||
)
|
||||
.into_response()
|
||||
}
|
||||
|
||||
/// Parse function calls from model output text
|
||||
pub async fn parse_function_call(
|
||||
context: Option<&Arc<AppContext>>,
|
||||
req: &ParseFunctionCallRequest,
|
||||
) -> Response {
|
||||
match context {
|
||||
Some(ctx) => match &ctx.tool_parser_factory {
|
||||
Some(factory) => match factory.registry().get_pooled_parser(&req.tool_call_parser) {
|
||||
Some(pooled_parser) => {
|
||||
let parser = pooled_parser.lock().await;
|
||||
match parser.parse_complete(&req.text).await {
|
||||
Ok((remaining_text, tool_calls)) => (
|
||||
StatusCode::OK,
|
||||
Json(serde_json::json!({
|
||||
"remaining_text": remaining_text,
|
||||
"tool_calls": tool_calls,
|
||||
"success": true
|
||||
})),
|
||||
)
|
||||
.into_response(),
|
||||
Err(e) => {
|
||||
error!("Failed to parse function calls: {}", e);
|
||||
(
|
||||
StatusCode::BAD_REQUEST,
|
||||
Json(serde_json::json!({
|
||||
"error": format!("Failed to parse function calls: {}", e),
|
||||
"success": false
|
||||
})),
|
||||
)
|
||||
.into_response()
|
||||
}
|
||||
}
|
||||
}
|
||||
None => (
|
||||
StatusCode::BAD_REQUEST,
|
||||
Json(serde_json::json!({
|
||||
"error": format!("Unknown tool parser: {}", req.tool_call_parser),
|
||||
"success": false
|
||||
})),
|
||||
)
|
||||
.into_response(),
|
||||
},
|
||||
None => (
|
||||
StatusCode::SERVICE_UNAVAILABLE,
|
||||
Json(serde_json::json!({
|
||||
"error": "Tool parser factory not initialized",
|
||||
"success": false
|
||||
})),
|
||||
)
|
||||
.into_response(),
|
||||
},
|
||||
None => (
|
||||
let Some(ctx) = context else {
|
||||
return error_response(StatusCode::SERVICE_UNAVAILABLE, "Context not initialized");
|
||||
};
|
||||
|
||||
let Some(factory) = &ctx.tool_parser_factory else {
|
||||
return error_response(
|
||||
StatusCode::SERVICE_UNAVAILABLE,
|
||||
"Tool parser factory not initialized",
|
||||
);
|
||||
};
|
||||
|
||||
let Some(pooled_parser) = factory.registry().get_pooled_parser(&req.tool_call_parser) else {
|
||||
return error_response(
|
||||
StatusCode::BAD_REQUEST,
|
||||
&format!("Unknown tool parser: {}", req.tool_call_parser),
|
||||
);
|
||||
};
|
||||
|
||||
let parser = pooled_parser.lock().await;
|
||||
match parser.parse_complete(&req.text).await {
|
||||
Ok((remaining_text, tool_calls)) => (
|
||||
StatusCode::OK,
|
||||
Json(serde_json::json!({
|
||||
"error": "Context not initialized",
|
||||
"success": false
|
||||
"remaining_text": remaining_text,
|
||||
"tool_calls": tool_calls,
|
||||
"success": true
|
||||
})),
|
||||
)
|
||||
.into_response(),
|
||||
Err(e) => {
|
||||
error!("Failed to parse function calls: {}", e);
|
||||
error_response(
|
||||
StatusCode::BAD_REQUEST,
|
||||
&format!("Failed to parse function calls: {}", e),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,59 +75,41 @@ pub async fn parse_reasoning(
|
||||
context: Option<&Arc<AppContext>>,
|
||||
req: &SeparateReasoningRequest,
|
||||
) -> Response {
|
||||
match context {
|
||||
Some(ctx) => match &ctx.reasoning_parser_factory {
|
||||
Some(factory) => match factory.registry().get_pooled_parser(&req.reasoning_parser) {
|
||||
Some(pooled_parser) => {
|
||||
let mut parser = pooled_parser.lock().await;
|
||||
match parser.detect_and_parse_reasoning(&req.text) {
|
||||
Ok(result) => (
|
||||
StatusCode::OK,
|
||||
Json(serde_json::json!({
|
||||
"normal_text": result.normal_text,
|
||||
"reasoning_text": result.reasoning_text,
|
||||
"success": true
|
||||
})),
|
||||
)
|
||||
.into_response(),
|
||||
Err(e) => {
|
||||
error!("Failed to separate reasoning: {}", e);
|
||||
(
|
||||
StatusCode::BAD_REQUEST,
|
||||
Json(serde_json::json!({
|
||||
"error": format!("Failed to separate reasoning: {}", e),
|
||||
"success": false
|
||||
})),
|
||||
)
|
||||
.into_response()
|
||||
}
|
||||
}
|
||||
}
|
||||
None => (
|
||||
StatusCode::BAD_REQUEST,
|
||||
Json(serde_json::json!({
|
||||
"error": format!("Unknown reasoning parser: {}", req.reasoning_parser),
|
||||
"success": false
|
||||
})),
|
||||
)
|
||||
.into_response(),
|
||||
},
|
||||
None => (
|
||||
StatusCode::SERVICE_UNAVAILABLE,
|
||||
Json(serde_json::json!({
|
||||
"error": "Reasoning parser factory not initialized",
|
||||
"success": false
|
||||
})),
|
||||
)
|
||||
.into_response(),
|
||||
},
|
||||
None => (
|
||||
let Some(ctx) = context else {
|
||||
return error_response(StatusCode::SERVICE_UNAVAILABLE, "Context not initialized");
|
||||
};
|
||||
|
||||
let Some(factory) = &ctx.reasoning_parser_factory else {
|
||||
return error_response(
|
||||
StatusCode::SERVICE_UNAVAILABLE,
|
||||
"Reasoning parser factory not initialized",
|
||||
);
|
||||
};
|
||||
|
||||
let Some(pooled_parser) = factory.registry().get_pooled_parser(&req.reasoning_parser) else {
|
||||
return error_response(
|
||||
StatusCode::BAD_REQUEST,
|
||||
&format!("Unknown reasoning parser: {}", req.reasoning_parser),
|
||||
);
|
||||
};
|
||||
|
||||
let mut parser = pooled_parser.lock().await;
|
||||
match parser.detect_and_parse_reasoning(&req.text) {
|
||||
Ok(result) => (
|
||||
StatusCode::OK,
|
||||
Json(serde_json::json!({
|
||||
"error": "Context not initialized",
|
||||
"success": false
|
||||
"normal_text": result.normal_text,
|
||||
"reasoning_text": result.reasoning_text,
|
||||
"success": true
|
||||
})),
|
||||
)
|
||||
.into_response(),
|
||||
Err(e) => {
|
||||
error!("Failed to separate reasoning: {}", e);
|
||||
error_response(
|
||||
StatusCode::BAD_REQUEST,
|
||||
&format!("Failed to separate reasoning: {}", e),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user