mirror of
https://github.com/meilisearch/meilisearch.git
synced 2025-06-06 20:25:40 +00:00
Better stop the stream
This commit is contained in:
parent
afadfa420a
commit
a51579e923
4
Cargo.lock
generated
4
Cargo.lock
generated
@ -470,7 +470,7 @@ dependencies = [
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "async-openai"
|
name = "async-openai"
|
||||||
version = "0.28.1"
|
version = "0.28.1"
|
||||||
source = "git+https://github.com/meilisearch/async-openai?branch=optional-type-function#dd328d4c35ca24c30284c8aff616541ac82eb47a"
|
source = "git+https://github.com/meilisearch/async-openai?branch=optional-type-function#603f1d17bb4530c45fb9a6e93294ab715a7af869"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"async-openai-macros",
|
"async-openai-macros",
|
||||||
"backoff",
|
"backoff",
|
||||||
@ -495,7 +495,7 @@ dependencies = [
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "async-openai-macros"
|
name = "async-openai-macros"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
source = "git+https://github.com/meilisearch/async-openai?branch=optional-type-function#dd328d4c35ca24c30284c8aff616541ac82eb47a"
|
source = "git+https://github.com/meilisearch/async-openai?branch=optional-type-function#603f1d17bb4530c45fb9a6e93294ab715a7af869"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
|
@ -268,30 +268,25 @@ async fn streamed_chat(
|
|||||||
let _join_handle = Handle::current().spawn(async move {
|
let _join_handle = Handle::current().spawn(async move {
|
||||||
let client = Client::with_config(config.clone());
|
let client = Client::with_config(config.clone());
|
||||||
let mut global_tool_calls = HashMap::<u32, Call>::new();
|
let mut global_tool_calls = HashMap::<u32, Call>::new();
|
||||||
|
let mut finish_reason = None;
|
||||||
|
|
||||||
'main: loop {
|
'main: while finish_reason.map_or(true, |fr| fr == FinishReason::ToolCalls) {
|
||||||
let mut response = client.chat().create_stream(chat_completion.clone()).await.unwrap();
|
let mut response = client.chat().create_stream(chat_completion.clone()).await.unwrap();
|
||||||
while let Some(result) = response.next().await {
|
while let Some(result) = response.next().await {
|
||||||
match result {
|
match result {
|
||||||
Ok(resp) => {
|
Ok(resp) => {
|
||||||
let delta = &resp.choices[0].delta;
|
let choice = &resp.choices[0];
|
||||||
|
finish_reason = choice.finish_reason;
|
||||||
|
|
||||||
#[allow(deprecated)]
|
#[allow(deprecated)]
|
||||||
let ChatCompletionStreamResponseDelta {
|
let ChatCompletionStreamResponseDelta {
|
||||||
content,
|
content,
|
||||||
// Using deprecated field but keeping for compatibility
|
// Using deprecated field but keeping for compatibility
|
||||||
function_call: _,
|
function_call: _,
|
||||||
ref tool_calls,
|
ref tool_calls,
|
||||||
role,
|
role: _,
|
||||||
refusal: _,
|
refusal: _,
|
||||||
} = delta;
|
} = &choice.delta;
|
||||||
|
|
||||||
if content.as_ref().map_or(true, |s| s.is_empty())
|
|
||||||
&& tool_calls.is_none()
|
|
||||||
&& global_tool_calls.is_empty()
|
|
||||||
&& role.is_none()
|
|
||||||
{
|
|
||||||
break 'main;
|
|
||||||
}
|
|
||||||
|
|
||||||
if content.is_some() {
|
if content.is_some() {
|
||||||
tx.send(Event::Data(sse::Data::new_json(&resp).unwrap())).await.unwrap()
|
tx.send(Event::Data(sse::Data::new_json(&resp).unwrap())).await.unwrap()
|
||||||
@ -409,6 +404,7 @@ async fn streamed_chat(
|
|||||||
Err(err) => {
|
Err(err) => {
|
||||||
// writeln!(lock, "error: {err}").unwrap();
|
// writeln!(lock, "error: {err}").unwrap();
|
||||||
tracing::error!("{err:?}");
|
tracing::error!("{err:?}");
|
||||||
|
break 'main;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user