diff --git a/chat/chat_core/src/lib.rs b/chat/chat_core/src/lib.rs index e69a286..7cf1fae 100644 --- a/chat/chat_core/src/lib.rs +++ b/chat/chat_core/src/lib.rs @@ -138,6 +138,8 @@ pub enum AdapterType { Openai, #[serde(alias = "ollama", alias = "Ollama")] Ollama, + #[serde(alias = "test", alias = "Test")] + Test, } #[derive(Debug, Clone, FromRow, ToSchema, Serialize, Deserialize, PartialEq)] diff --git a/chat/chat_server/fixtures/test.sql b/chat/chat_server/fixtures/test.sql index 6c38a79..b29d571 100644 --- a/chat/chat_server/fixtures/test.sql +++ b/chat/chat_server/fixtures/test.sql @@ -24,9 +24,8 @@ INSERT INTO chats(ws_id, type, members) (1, 'group', '{1,3,4}'); -- insert agent to chat -INSERT INTO chat_agents(chat_id, name, type, prompt, args) - VALUES (1, 'translation', 'proxy', - 'If language is Chinese, translate to English, if language is English, translate to Chinese. Please reply with the translated content directly. No explanation is needed. Here is the content: ', '{}'); +INSERT INTO chat_agents(chat_id, name, type, adapter, model, prompt, args) + VALUES (1, 'translation', 'proxy', 'test', 'gpt-4o', 'If language is Chinese, translate to English, if language is English, translate to Chinese. Please reply with the translated content directly. No explanation is needed. Here is the content: ', '{}'); INSERT INTO messages(chat_id, sender_id, content) VALUES (1, 1, 'Hello, world!'), diff --git a/chat/chat_server/src/agent.rs b/chat/chat_server/src/agent.rs index 748069d..5de13ce 100644 --- a/chat/chat_server/src/agent.rs +++ b/chat/chat_server/src/agent.rs @@ -8,6 +8,7 @@ pub enum AgentVariant { Proxy(ProxyAgent), Reply(ReplyAgent), Tap(TapAgent), + Test(TestAgent), } #[allow(unused)] @@ -34,6 +35,9 @@ pub struct TapAgent { pub args: serde_json::Value, } +#[allow(unused)] +pub struct TestAgent; + impl Agent for ProxyAgent { async fn process(&self, msg: &str, _ctx: &AgentContext) -> Result { // If we need it to be flexible: prompt is a jinja2 template, and args is a json @@ -60,12 +64,19 @@ impl Agent for TapAgent { } } +impl Agent for TestAgent { + async fn process(&self, _msg: &str, _ctx: &AgentContext) -> Result { + Ok(AgentDecision::None) + } +} + impl Agent for AgentVariant { async fn process(&self, msg: &str, ctx: &AgentContext) -> Result { match self { AgentVariant::Reply(agent) => agent.process(msg, ctx).await, AgentVariant::Proxy(agent) => agent.process(msg, ctx).await, AgentVariant::Tap(agent) => agent.process(msg, ctx).await, + AgentVariant::Test(agent) => agent.process(msg, ctx).await, } } } @@ -78,6 +89,7 @@ impl From for AgentVariant { OpenaiAdapter::new(api_key, agent.model).into() } AdapterType::Ollama => OllamaAdapter::new_local(agent.model).into(), + AdapterType::Test => return AgentVariant::Test(TestAgent), }; match agent.r#type { diff --git a/chat/chat_server/src/models/messages.rs b/chat/chat_server/src/models/messages.rs index 62847f4..e5088a7 100644 --- a/chat/chat_server/src/models/messages.rs +++ b/chat/chat_server/src/models/messages.rs @@ -158,7 +158,7 @@ mod tests { files: vec![], }; let message = state - .create_message(input, 2, 1) + .create_message(input, 1, 1) .await .expect("create message failed"); assert_eq!(message.content, "hello"); @@ -169,7 +169,7 @@ mod tests { files: vec!["1".to_string()], }; - let err = state.create_message(input, 2, 1).await.unwrap_err(); + let err = state.create_message(input, 1, 1).await.unwrap_err(); assert_eq!(err.to_string(), "Invalid chat file path: 1"); // valid files should work diff --git a/chat/chat_test/tests/chat.rs b/chat/chat_test/tests/chat.rs index 3b2286c..cc396e9 100644 --- a/chat/chat_test/tests/chat.rs +++ b/chat/chat_test/tests/chat.rs @@ -168,7 +168,7 @@ impl ChatServer { .header("Authorization", format!("Bearer {}", self.token)) .header("Content-Type", "application/json") .body( - r#"{"name": "test agent", "type": "proxy", "adapter": "openai", "model": "gpt-4o", "prompt": "You are a helpful agent"}"#, + r#"{"name": "test agent", "type": "proxy", "adapter": "test", "model": "gpt-4o", "prompt": "You are a helpful agent"}"#, ); let res = res.send().await?; assert_eq!(res.status(), StatusCode::CREATED); diff --git a/chat/migrations/20241002035527_agent_adapter.sql b/chat/migrations/20241002035527_agent_adapter.sql index 64cbcc3..8891d66 100644 --- a/chat/migrations/20241002035527_agent_adapter.sql +++ b/chat/migrations/20241002035527_agent_adapter.sql @@ -1,7 +1,8 @@ -- add adapter_type, adapter and model to agent CREATE TYPE adapter_type AS ENUM( 'openai', - 'ollama' + 'ollama', + 'test' ); ALTER TABLE chat_agents diff --git a/test.rest b/test.rest index 649b2dc..786e06a 100644 --- a/test.rest +++ b/test.rest @@ -183,7 +183,7 @@ Content-Type: application/json { "name": "translator1", "type": "proxy", - "adapter": "openai", + "adapter": "test", "model": "gpt-4o", "prompt": "You're the world's best translator. You understand English and Chinese well, also their culture and idioms. You will translate user input between English and Chinese. If the original text is English, you will translate it to elegant, authentic Simplified Chinese. If the original text is Chinese, you will translate it to elegant, authentic English. Only return the translated sentences, no other text or comments. below are the text to translate:" }