From 35c75506e2fd94e2285d8a3cb66208d518d5f992 Mon Sep 17 00:00:00 2001 From: sigoden Date: Mon, 27 Nov 2023 14:04:50 +0800 Subject: feat: support vision (#249) * feat: support vision * clippy * implement vision * resolve data url to local file * add model openai:gpt-4-vision-preview * use newline to concate embeded text files * set max_tokens for gpt-4-vision-preview --- src/client/common.rs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'src/client/common.rs') diff --git a/src/client/common.rs b/src/client/common.rs index cf5ba9b..2716d87 100644 --- a/src/client/common.rs +++ b/src/client/common.rs @@ -1,7 +1,7 @@ use super::{openai::OpenAIConfig, ClientConfig, Message}; use crate::{ - config::GlobalConfig, + config::{GlobalConfig, Input}, render::ReplyHandler, utils::{ init_tokio_runtime, prompt_input_integer, prompt_input_string, tokenize, AbortSignal, @@ -50,7 +50,7 @@ macro_rules! register_client { } impl $client { - pub const NAME: &str = $name; + pub const NAME: &'static str = $name; pub fn init(global_config: &$crate::config::GlobalConfig) -> Option> { let model = global_config.read().model.clone(); @@ -186,22 +186,22 @@ pub trait Client { Ok(client) } - fn send_message(&self, content: &str) -> Result { + fn send_message(&self, input: Input) -> Result { init_tokio_runtime()?.block_on(async { let global_config = self.config().0; if global_config.read().dry_run { - let content = global_config.read().echo_messages(content); + let content = global_config.read().echo_messages(&input); return Ok(content); } let client = self.build_client()?; - let data = global_config.read().prepare_send_data(content, false)?; + let data = global_config.read().prepare_send_data(&input, false)?; self.send_message_inner(&client, data) .await .with_context(|| "Failed to get answer") }) } - fn send_message_streaming(&self, content: &str, handler: &mut ReplyHandler) -> Result<()> { + fn send_message_streaming(&self, input: &Input, handler: &mut ReplyHandler) -> Result<()> { async fn watch_abort(abort: AbortSignal) { loop { if abort.aborted() { @@ -211,12 +211,13 @@ pub trait Client { } } let abort = handler.get_abort(); - init_tokio_runtime()?.block_on(async { + let input = input.clone(); + init_tokio_runtime()?.block_on(async move { tokio::select! { ret = async { let global_config = self.config().0; if global_config.read().dry_run { - let content = global_config.read().echo_messages(content); + let content = global_config.read().echo_messages(&input); let tokens = tokenize(&content); for token in tokens { tokio::time::sleep(Duration::from_millis(10)).await; @@ -225,7 +226,7 @@ pub trait Client { return Ok(()); } let client = self.build_client()?; - let data = global_config.read().prepare_send_data(content, true)?; + let data = global_config.read().prepare_send_data(&input, true)?; self.send_message_streaming_inner(&client, handler, data).await } => { handler.done()?; -- cgit v1.2.3