Skip to content

Commit

Permalink
fix multiple functions
Browse files Browse the repository at this point in the history
  • Loading branch information
neowu committed Apr 18, 2024
1 parent 8f855da commit 7f07333
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 12 deletions.
40 changes: 38 additions & 2 deletions src/bot/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ fn load_function_store(config: &BotConfig) -> FunctionStore {
Function {
name: "get_random_number".to_string(),
description: "generate random number".to_string(),
parameters: serde_json::json!({
parameters: Some(serde_json::json!({
"type": "object",
"properties": {
"max": {
Expand All @@ -79,7 +79,7 @@ fn load_function_store(config: &BotConfig) -> FunctionStore {
},
},
"required": ["max"]
}),
})),
},
Box::new(|request| {
let max = request.get("max").unwrap().as_i64().unwrap();
Expand All @@ -92,6 +92,42 @@ fn load_function_store(config: &BotConfig) -> FunctionStore {
}),
);
}
if let "close_door" = function.as_str() {
function_store.add(
Function {
name: "close_door".to_string(),
description: "close door of home".to_string(),
parameters: None,
},
Box::new(|_request| {
json!({
"success": true
})
}),
);
}
if let "close_window" = function.as_str() {
function_store.add(
Function {
name: "close_window".to_string(),
description: "close window of home with id".to_string(),
parameters: Some(serde_json::json!({
"type": "object",
"properties": {
"id": {
"type": "string",
"description": "id of window"
}
}
})),
},
Box::new(|_request| {
json!({
"success": true
})
}),
);
}
}
function_store
}
3 changes: 2 additions & 1 deletion src/bot/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ use crate::util::exception::Exception;
pub struct Function {
pub name: String,
pub description: String,
pub parameters: serde_json::Value,
#[serde(skip_serializing_if = "Option::is_none")]
pub parameters: Option<serde_json::Value>,
}

pub type FunctionImplementation = dyn Fn(serde_json::Value) -> serde_json::Value + Send + Sync;
Expand Down
13 changes: 4 additions & 9 deletions src/gcloud/vertex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,9 @@ impl Vertex {
url,
messages: Rc::new(vec![]),
system_message: Rc::new(system_message.map(|message| Content::new_text(Role::Model, message))),
tools: Rc::new(
function_store
.declarations
.iter()
.map(|f| Tool {
function_declarations: vec![f.clone()],
})
.collect(),
),
tools: Rc::new(vec![Tool {
function_declarations: function_store.declarations.to_vec(),
}]),
function_store,
data: vec![],
usage: Usage::default(),
Expand Down Expand Up @@ -164,6 +158,7 @@ impl Vertex {

async fn post(&self, request: StreamGenerateContent) -> Result<Response, Exception> {
let body = json::to_json(&request)?;
info!("body={body}");
let response = http_client::http_client()
.post(&self.url)
.bearer_auth(token())
Expand Down
1 change: 1 addition & 0 deletions src/openai/chatgpt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ async fn process_event_source(mut source: EventSource, tx: Sender<InternalEvent>
if !function_calls.is_empty() {
tx.send(InternalEvent::FunctionCall(function_calls)).await.unwrap();
} else {
// chatgpt doesn't support token usage with stream mode
tx.send(InternalEvent::Event(ChatEvent::End(Usage::default()))).await.unwrap();
}
}

0 comments on commit 7f07333

Please sign in to comment.