summaryrefslogtreecommitdiffstats
path: root/src/client
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2025-02-03 22:40:13 +0800
committerGitHub <noreply@github.com>2025-02-03 22:40:13 +0800
commite8aa5d12a366a1fe14192e56321d39a7c9e5d87f (patch)
tree066263a768cb007b3c13ac0aff1b4a92d09132c3 /src/client
parent700d8a3245f1133c37e035039e511a1e5dce1a5d (diff)
downloadaichat-e8aa5d12a366a1fe14192e56321d39a7c9e5d87f.tar.gz
refactor: improve call_chat_completions function (#1143)
Diffstat (limited to 'src/client')
-rw-r--r--src/client/common.rs28
1 files changed, 7 insertions, 21 deletions
diff --git a/src/client/common.rs b/src/client/common.rs
index d8909ee..5ad27ae 100644
--- a/src/client/common.rs
+++ b/src/client/common.rs
@@ -14,7 +14,7 @@ use inquire::{required, Text};
use reqwest::{Client as ReqwestClient, RequestBuilder};
use serde::Deserialize;
use serde_json::{json, Value};
-use std::{future::Future, time::Duration};
+use std::time::Duration;
use tokio::sync::mpsc::unbounded_channel;
const MODELS_YAML: &str = include_str!("../../models.yaml");
@@ -378,6 +378,7 @@ pub fn create_openai_compatible_client_config(client: &str) -> Result<Option<(St
pub async fn call_chat_completions(
input: &Input,
+ print: bool,
extract_code: bool,
client: &dyn Client,
abort_signal: AbortSignal,
@@ -397,10 +398,12 @@ pub async fn call_chat_completions(
..
} = ret;
if !text.is_empty() {
- if extract_code && text.trim_start().starts_with("```") {
- text = extract_block(&text);
+ if extract_code {
+ text = extract_code_block(&text).to_string();
+ }
+ if print {
+ client.global_config().read().print_markdown(&text)?;
}
- client.global_config().read().print_markdown(&text)?;
}
Ok((text, eval_tool_calls(client.global_config(), tool_calls)?))
}
@@ -444,23 +447,6 @@ pub async fn call_chat_completions_streaming(
}
}
-#[allow(unused)]
-pub async fn chat_completions_as_streaming<F, Fut>(
- builder: RequestBuilder,
- handler: &mut SseHandler,
- f: F,
-) -> Result<()>
-where
- F: FnOnce(RequestBuilder) -> Fut,
- Fut: Future<Output = Result<String>>,
-{
- let text = f(builder).await?;
- handler.text(&text)?;
- handler.done();
-
- Ok(())
-}
-
pub fn noop_prepare_embeddings<T>(_client: &T, _data: &EmbeddingsData) -> Result<RequestData> {
bail!("The client doesn't support embeddings api")
}