summaryrefslogtreecommitdiffstats
path: root/src/client
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2024-07-28 08:56:00 +0800
committerGitHub <noreply@github.com>2024-07-28 08:56:00 +0800
commit49b61129c95a3528eaf25dabcb55825b5ed7be72 (patch)
tree18f4e8b46c8f5ca8093f1684a4c1dfa7d9f92a51 /src/client
parent3441630c6223ebbab55d62c969390edf865d3d34 (diff)
downloadaichat-49b61129c95a3528eaf25dabcb55825b5ed7be72.tar.gz
feat: add `config.stream` and `.set stream` repl command (#759)
Diffstat (limited to 'src/client')
-rw-r--r--src/client/common.rs35
1 files changed, 28 insertions, 7 deletions
diff --git a/src/client/common.rs b/src/client/common.rs
index 2108941..ec1f37d 100644
--- a/src/client/common.rs
+++ b/src/client/common.rs
@@ -397,7 +397,28 @@ pub fn create_openai_compatible_client_config(client: &str) -> Result<Option<(St
}
}
-pub async fn chat_completion_streaming(
+pub async fn call_chat_completions(
+ input: &Input,
+ client: &dyn Client,
+ config: &GlobalConfig,
+) -> Result<(String, Vec<ToolResult>)> {
+ let task = client.chat_completions(input.clone());
+ let ret = run_with_spinner(task, "Generating").await;
+ match ret {
+ Ok(ret) => {
+ let ChatCompletionsOutput {
+ text, tool_calls, ..
+ } = ret;
+ if !text.is_empty() {
+ config.read().print_markdown(&text)?;
+ }
+ Ok((text, eval_tool_calls(config, tool_calls)?))
+ }
+ Err(err) => Err(err),
+ }
+}
+
+pub async fn call_chat_completions_streaming(
input: &Input,
client: &dyn Client,
config: &GlobalConfig,
@@ -406,23 +427,23 @@ pub async fn chat_completion_streaming(
let (tx, rx) = unbounded_channel();
let mut handler = SseHandler::new(tx, abort.clone());
- let (send_ret, rend_ret) = tokio::join!(
+ let (send_ret, render_ret) = tokio::join!(
client.chat_completions_streaming(input, &mut handler),
render_stream(rx, config, abort.clone()),
);
- if let Err(err) = rend_ret {
+ if let Err(err) = render_ret {
render_error(err, config.read().highlight);
}
- let (output, calls) = handler.take();
+ let (text, tool_calls) = handler.take();
match send_ret {
Ok(_) => {
- if !output.is_empty() && !output.ends_with('\n') {
+ if !text.is_empty() && !text.ends_with('\n') {
println!();
}
- Ok((output, eval_tool_calls(config, calls)?))
+ Ok((text, eval_tool_calls(config, tool_calls)?))
}
Err(err) => {
- if !output.is_empty() {
+ if !text.is_empty() {
println!();
}
Err(err)