feat: chat typing
This commit is contained in:
+23
-31
@@ -6,7 +6,6 @@ use axum::{
|
||||
sse::{KeepAlive, Sse},
|
||||
},
|
||||
};
|
||||
use serde_json::Value;
|
||||
|
||||
use crate::dto::api;
|
||||
use crate::errors::OllamaError;
|
||||
@@ -23,9 +22,7 @@ pub async fn completions(
|
||||
State(state): State<AppState>,
|
||||
Json(body): Json<api::CompletionRequest>,
|
||||
) -> Result<Response, (axum::http::StatusCode, String)> {
|
||||
let wants_stream = body.base.stream;
|
||||
|
||||
if wants_stream {
|
||||
if body.base.stream {
|
||||
let stream = state
|
||||
.ollama
|
||||
.completions_stream(&body)
|
||||
@@ -42,35 +39,30 @@ pub async fn completions(
|
||||
}
|
||||
}
|
||||
|
||||
// pub async fn chat_completions(
|
||||
// State(state): State<AppState>,
|
||||
// Json(body): Json<Value>,
|
||||
// ) -> Result<Response, (axum::http::StatusCode, String)> {
|
||||
// let wants_stream = body
|
||||
// .get("stream")
|
||||
// .and_then(|v| v.as_bool())
|
||||
// .unwrap_or(false);
|
||||
pub async fn chat_completions(
|
||||
State(state): State<AppState>,
|
||||
Json(body): Json<api::ChatRequest>,
|
||||
) -> Result<Response, (axum::http::StatusCode, String)> {
|
||||
if body.base.stream {
|
||||
let stream = state
|
||||
.ollama
|
||||
.chat_completions_stream(&body)
|
||||
.await
|
||||
.map_err(ollama_err)?;
|
||||
|
||||
// if wants_stream {
|
||||
// let stream = state
|
||||
// .ollama
|
||||
// .chat_completions_stream(body)
|
||||
// .await
|
||||
// .map_err(ollama_err)?;
|
||||
Ok(Sse::new(stream)
|
||||
.keep_alive(KeepAlive::default())
|
||||
.into_response())
|
||||
} else {
|
||||
let response = state
|
||||
.ollama
|
||||
.chat_completions(&body)
|
||||
.await
|
||||
.map_err(ollama_err)?;
|
||||
|
||||
// Ok(Sse::new(stream)
|
||||
// .keep_alive(KeepAlive::default())
|
||||
// .into_response())
|
||||
// } else {
|
||||
// let response = state
|
||||
// .ollama
|
||||
// .chat_completions(body)
|
||||
// .await
|
||||
// .map_err(ollama_err)?;
|
||||
|
||||
// Ok(Json(response).into_response())
|
||||
// }
|
||||
// }
|
||||
Ok(Json(response).into_response())
|
||||
}
|
||||
}
|
||||
|
||||
fn ollama_err(e: OllamaError) -> (axum::http::StatusCode, String) {
|
||||
match e {
|
||||
|
||||
Reference in New Issue
Block a user