feat: return conversation id
This commit is contained in:
@@ -155,6 +155,8 @@ pub struct ChatCompletionResponse {
|
|||||||
pub model: String,
|
pub model: String,
|
||||||
pub choices: Vec<ChatChoice>,
|
pub choices: Vec<ChatChoice>,
|
||||||
pub usage: Option<Usage>, // optional (Ollama may not always provide)
|
pub usage: Option<Usage>, // optional (Ollama may not always provide)
|
||||||
|
|
||||||
|
pub conversation_id: Option<Uuid>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize, ToSchema)]
|
#[derive(Debug, Serialize, Deserialize, ToSchema)]
|
||||||
|
|||||||
@@ -81,6 +81,8 @@ impl From<ollama::OllamaChatResponse> for api::ChatCompletionResponse {
|
|||||||
completion_tokens,
|
completion_tokens,
|
||||||
total_tokens: prompt_tokens + completion_tokens,
|
total_tokens: prompt_tokens + completion_tokens,
|
||||||
}),
|
}),
|
||||||
|
|
||||||
|
conversation_id: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -126,7 +126,7 @@ pub async fn chat_completions(
|
|||||||
) -> Result<Response, (axum::http::StatusCode, String)> {
|
) -> Result<Response, (axum::http::StatusCode, String)> {
|
||||||
tracing::debug!("Received /chat/completion with body {:?}", body);
|
tracing::debug!("Received /chat/completion with body {:?}", body);
|
||||||
|
|
||||||
if matches!(&auth, Auth::Jwt(_)) {
|
let conversation_id = if matches!(&auth, Auth::Jwt(_)) {
|
||||||
tracing::debug!(
|
tracing::debug!(
|
||||||
"Is conversation_id existing: {:?}",
|
"Is conversation_id existing: {:?}",
|
||||||
body.conversation_id.is_some()
|
body.conversation_id.is_some()
|
||||||
@@ -137,7 +137,7 @@ pub async fn chat_completions(
|
|||||||
.await
|
.await
|
||||||
.map_err(crate::databases::postgres::errors::into_http_response)?;
|
.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::Existing(uuid) => *uuid,
|
||||||
ConversationState::Created(uuid) => {
|
ConversationState::Created(uuid) => {
|
||||||
let conversation_id = *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 {
|
if body.base.stream {
|
||||||
let stream = state
|
let stream = state
|
||||||
@@ -170,17 +173,16 @@ pub async fn chat_completions(
|
|||||||
.chat_completions_stream(&body)
|
.chat_completions_stream(&body)
|
||||||
.await
|
.await
|
||||||
.map_err(into_http_response)?;
|
.map_err(into_http_response)?;
|
||||||
|
|
||||||
Ok(Sse::new(stream)
|
Ok(Sse::new(stream)
|
||||||
.keep_alive(KeepAlive::default())
|
.keep_alive(KeepAlive::default())
|
||||||
.into_response())
|
.into_response())
|
||||||
} else {
|
} else {
|
||||||
let response = state
|
let mut response = state
|
||||||
.ollama
|
.ollama
|
||||||
.chat_completions(&body)
|
.chat_completions(&body)
|
||||||
.await
|
.await
|
||||||
.map_err(into_http_response)?;
|
.map_err(into_http_response)?;
|
||||||
|
response.conversation_id = conversation_id;
|
||||||
Ok(Json(response).into_response())
|
Ok(Json(response).into_response())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user