Skip to content

Commit b856552

Browse files
committed
fix: try to fix FunctionDefinition for Gemini pro's MALFORMED_FUNCTION_CALL
1 parent f38311a commit b856552

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

anda_engine/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "anda_engine"
33
description = "Agents engine for Anda -- an AI agent framework built with Rust, powered by ICP and TEEs."
44
repository = "https://github.com/ldclabs/anda/tree/main/anda_engine"
55
publish = true
6-
version = "0.9.15"
6+
version = "0.9.16"
77
edition.workspace = true
88
keywords.workspace = true
99
categories.workspace = true

anda_engine/src/memory.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ pub static FUNCTION_DEFINITION: LazyLock<FunctionDefinition> = LazyLock::new(||
4646
},
4747
"parameters": {
4848
"type": "object",
49-
"description": "JSON object of key-value pairs for safe placeholder substitution (placeholders start with ':', e.g., :name). Must be complete JSON tokens; do not embed in quoted strings."
49+
"description": "Optional JSON object of key-value pairs for safe placeholder substitution (placeholders start with ':', e.g., :name). Must be complete JSON tokens; do not embed in quoted strings. Only provide if placeholders are used in the command."
5050
}
5151
},
52-
"required": ["command", "parameters"]
52+
"required": ["command"]
5353
}
5454
})).unwrap()
5555
});
@@ -263,6 +263,7 @@ pub struct MemoryManagement {
263263
logs: Arc<Collection>,
264264
resources: Arc<Collection>,
265265
enable_kip_logging: bool,
266+
kip_function_definitions: FunctionDefinition,
266267
}
267268

268269
impl MemoryManagement {
@@ -342,9 +343,15 @@ impl MemoryManagement {
342343
logs,
343344
resources,
344345
enable_kip_logging: true,
346+
kip_function_definitions: FUNCTION_DEFINITION.clone(),
345347
})
346348
}
347349

350+
pub fn with_kip_function_definitions(mut self, def: FunctionDefinition) -> Self {
351+
self.kip_function_definitions = def;
352+
self
353+
}
354+
348355
pub fn disable_kip_logging(&mut self) {
349356
self.enable_kip_logging = false;
350357
}
@@ -630,15 +637,15 @@ impl Tool<BaseCtx> for Arc<MemoryManagement> {
630637
type Output = Response;
631638

632639
fn name(&self) -> String {
633-
FUNCTION_DEFINITION.name.clone()
640+
self.kip_function_definitions.name.clone()
634641
}
635642

636643
fn description(&self) -> String {
637-
FUNCTION_DEFINITION.description.clone()
644+
self.kip_function_definitions.description.clone()
638645
}
639646

640647
fn definition(&self) -> FunctionDefinition {
641-
FUNCTION_DEFINITION.clone()
648+
self.kip_function_definitions.clone()
642649
}
643650

644651
async fn call(

anda_engine/src/model/gemini.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ impl Client {
8989
pub struct CompletionModel {
9090
/// Gemini client instance
9191
client: Client,
92+
/// Default request template
93+
default_request: types::GenerateContentRequest,
9294
/// Model identifier
9395
pub model: String,
9496
}
@@ -100,11 +102,20 @@ impl CompletionModel {
100102
/// * `client` - Gemini client instance
101103
/// * `model` - Model identifier string
102104
pub fn new(client: Client, model: &str) -> Self {
105+
let mut default_request = types::GenerateContentRequest::default();
106+
default_request.generation_config.top_p = Some(0.95);
103107
Self {
104108
client,
109+
default_request,
105110
model: model.to_string(),
106111
}
107112
}
113+
114+
/// Sets a default request template for the model
115+
pub fn with_default_request(mut self, greq: types::GenerateContentRequest) -> Self {
116+
self.default_request = greq;
117+
self
118+
}
108119
}
109120

110121
impl CompletionFeatures for CompletionModel {
@@ -129,12 +140,12 @@ impl CompletionFeaturesDyn for CompletionModel {
129140
fn completion(&self, req: CompletionRequest) -> BoxPinFut<Result<AgentOutput, BoxError>> {
130141
let model = self.model.clone();
131142
let client = self.client.clone();
143+
let mut greq = self.default_request.clone();
132144

133145
Box::pin(async move {
134146
let timestamp = unix_ms();
135147
let mut raw_history: Vec<Json> = Vec::new();
136148
let mut chat_history: Vec<Message> = Vec::new();
137-
let mut greq = types::GenerateContentRequest::default();
138149

139150
if !req.instructions.is_empty() {
140151
greq.system_instruction = Some(types::Content {
@@ -185,7 +196,6 @@ impl CompletionFeaturesDyn for CompletionModel {
185196
greq.contents.push(msg);
186197
}
187198

188-
greq.generation_config.top_p = Some(0.95);
189199
if let Some(temperature) = req.temperature {
190200
greq.generation_config.temperature = Some(temperature);
191201
}

0 commit comments

Comments
 (0)