feat: return conversation id

This commit is contained in:
2026-05-11 19:04:14 +02:00
parent b5ef7e4881
commit c976db7063
3 changed files with 13 additions and 7 deletions
+2
View File
@@ -155,6 +155,8 @@ pub struct ChatCompletionResponse {
pub model: String,
pub choices: Vec<ChatChoice>,
pub usage: Option<Usage>, // optional (Ollama may not always provide)
pub conversation_id: Option<Uuid>,
}
#[derive(Debug, Serialize, Deserialize, ToSchema)]
+2
View File
@@ -81,6 +81,8 @@ impl From<ollama::OllamaChatResponse> for api::ChatCompletionResponse {
completion_tokens,
total_tokens: prompt_tokens + completion_tokens,
}),
conversation_id: None,
}
}
}
+9 -7
View File
@@ -126,7 +126,7 @@ pub async fn chat_completions(
) -> Result<Response, (axum::http::StatusCode, String)> {
tracing::debug!("Received /chat/completion with body {:?}", body);
if matches!(&auth, Auth::Jwt(_)) {
let conversation_id = if matches!(&auth, Auth::Jwt(_)) {
tracing::debug!(
"Is conversation_id existing: {:?}",
body.conversation_id.is_some()
@@ -137,7 +137,7 @@ pub async fn chat_completions(
.await
.map_err(crate::databases::postgres::errors::into_http_response)?;
let conversation_id = match &conversation_state {
let id = match &conversation_state {
ConversationState::Existing(uuid) => *uuid,
ConversationState::Created(uuid) => {
let conversation_id = *uuid;
@@ -161,8 +161,11 @@ pub async fn chat_completions(
}
};
tracing::debug!("Using conversation_id: {:?}", conversation_id);
}
tracing::debug!("Using conversation_id: {:?}", id);
Some(id)
} else {
None
};
if body.base.stream {
let stream = state
@@ -170,17 +173,16 @@ pub async fn chat_completions(
.chat_completions_stream(&body)
.await
.map_err(into_http_response)?;
Ok(Sse::new(stream)
.keep_alive(KeepAlive::default())
.into_response())
} else {
let response = state
let mut response = state
.ollama
.chat_completions(&body)
.await
.map_err(into_http_response)?;
response.conversation_id = conversation_id;
Ok(Json(response).into_response())
}
}