mirror of
https://github.com/perstarkse/minne.git
synced 2026-04-23 01:08:33 +02:00
response parsing
This commit is contained in:
@@ -5,7 +5,7 @@ use async_openai::types::{
|
|||||||
ChatCompletionRequestSystemMessage, ChatCompletionRequestUserMessage,
|
ChatCompletionRequestSystemMessage, ChatCompletionRequestUserMessage,
|
||||||
CreateChatCompletionRequestArgs, ResponseFormat, ResponseFormatJsonSchema,
|
CreateChatCompletionRequestArgs, ResponseFormat, ResponseFormatJsonSchema,
|
||||||
};
|
};
|
||||||
use axum::{response::IntoResponse, Extension, Json};
|
use axum::{http::StatusCode, response::IntoResponse, Extension, Json};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
@@ -16,6 +16,22 @@ pub struct QueryInput {
|
|||||||
query: String,
|
query: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
struct Reference {
|
||||||
|
reference: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
struct LLMResponseFormat {
|
||||||
|
answer: String,
|
||||||
|
references: Vec<Reference>,
|
||||||
|
}
|
||||||
|
// impl IntoResponse for LLMResponseFormat {
|
||||||
|
// fn into_response(self) -> axum::response::Response {
|
||||||
|
// (StatusCode::OK, Json(self))
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
pub async fn query_handler(
|
pub async fn query_handler(
|
||||||
Extension(db_client): Extension<Arc<SurrealDbClient>>,
|
Extension(db_client): Extension<Arc<SurrealDbClient>>,
|
||||||
Json(query): Json<QueryInput>,
|
Json(query): Json<QueryInput>,
|
||||||
@@ -127,12 +143,16 @@ pub async fn query_handler(
|
|||||||
.first()
|
.first()
|
||||||
.and_then(|choice| choice.message.content.as_ref())
|
.and_then(|choice| choice.message.content.as_ref())
|
||||||
.ok_or(ApiError::QueryError(
|
.ok_or(ApiError::QueryError(
|
||||||
"No content found in LLM response".to_string(),
|
"No content found in LLM response".into(),
|
||||||
))?;
|
))?;
|
||||||
|
|
||||||
info!("{:?}", answer);
|
info!("{:?}", answer);
|
||||||
|
|
||||||
// info!("{:#?}", entities_json);
|
let parsed: LLMResponseFormat = serde_json::from_str(answer).map_err(|e| {
|
||||||
|
ApiError::QueryError(format!("Failed to parse LLM response into analysis: {}", e))
|
||||||
|
})?;
|
||||||
|
|
||||||
Ok(answer.clone().into_response())
|
info!("{:?}", parsed);
|
||||||
|
|
||||||
|
Ok(parsed.answer.into_response())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user