Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix test issue by introducing dummy test adapter #1

Merged
merged 2 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions chat/chat_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
5 changes: 2 additions & 3 deletions chat/chat_server/fixtures/test.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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!'),
Expand Down
12 changes: 12 additions & 0 deletions chat/chat_server/src/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub enum AgentVariant {
Proxy(ProxyAgent),
Reply(ReplyAgent),
Tap(TapAgent),
Test(TestAgent),
}

#[allow(unused)]
Expand All @@ -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<AgentDecision, AgentError> {
// If we need it to be flexible: prompt is a jinja2 template, and args is a json
Expand All @@ -60,12 +64,19 @@ impl Agent for TapAgent {
}
}

impl Agent for TestAgent {
async fn process(&self, _msg: &str, _ctx: &AgentContext) -> Result<AgentDecision, AgentError> {
Ok(AgentDecision::None)
}
}

impl Agent for AgentVariant {
async fn process(&self, msg: &str, ctx: &AgentContext) -> Result<AgentDecision, AgentError> {
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,
}
}
}
Expand All @@ -78,6 +89,7 @@ impl From<ChatAgent> 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 {
Expand Down
4 changes: 2 additions & 2 deletions chat/chat_server/src/models/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion chat/chat_test/tests/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion chat/migrations/20241002035527_agent_adapter.sql
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion test.rest
Original file line number Diff line number Diff line change
Expand Up @@ -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:"
}
Expand Down