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: tool_choice encoding/decoding error #141

Merged
merged 2 commits into from
Jan 15, 2025
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
39 changes: 2 additions & 37 deletions crates/forge_provider/src/open_router/mod.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,6 @@
use serde::{Deserialize, Serialize};

mod model;
mod parameters;
mod provider;
mod request;
mod response;

#[derive(Serialize, Deserialize, Debug)]
pub struct ParameterData {
pub model: String,
pub supported_parameters: Option<Vec<String>>,
pub frequency_penalty_p10: Option<f64>,
pub frequency_penalty_p50: Option<f64>,
pub frequency_penalty_p90: Option<f64>,
pub min_p_p10: Option<f64>,
pub min_p_p50: Option<f64>,
pub min_p_p90: Option<f64>,
pub presence_penalty_p10: Option<f64>,
pub presence_penalty_p50: Option<f64>,
pub presence_penalty_p90: Option<f64>,
pub repetition_penalty_p10: Option<f64>,
pub repetition_penalty_p50: Option<f64>,
pub repetition_penalty_p90: Option<f64>,
pub temperature_p10: Option<f64>,
pub temperature_p50: Option<f64>,
pub temperature_p90: Option<f64>,
pub top_a_p10: Option<f64>,
pub top_a_p50: Option<f64>,
pub top_a_p90: Option<f64>,
pub top_k_p10: Option<f64>,
pub top_k_p50: Option<f64>,
pub top_k_p90: Option<f64>,
pub top_p_p10: Option<f64>,
pub top_p_p50: Option<f64>,
pub top_p_p90: Option<f64>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct ParameterResponse {
pub data: ParameterData,
}
mod tool_choice;
36 changes: 36 additions & 0 deletions crates/forge_provider/src/open_router/parameters.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug)]
pub struct ParameterData {
pub model: String,
pub supported_parameters: Option<Vec<String>>,
pub frequency_penalty_p10: Option<f64>,
pub frequency_penalty_p50: Option<f64>,
pub frequency_penalty_p90: Option<f64>,
pub min_p_p10: Option<f64>,
pub min_p_p50: Option<f64>,
pub min_p_p90: Option<f64>,
pub presence_penalty_p10: Option<f64>,
pub presence_penalty_p50: Option<f64>,
pub presence_penalty_p90: Option<f64>,
pub repetition_penalty_p10: Option<f64>,
pub repetition_penalty_p50: Option<f64>,
pub repetition_penalty_p90: Option<f64>,
pub temperature_p10: Option<f64>,
pub temperature_p50: Option<f64>,
pub temperature_p90: Option<f64>,
pub top_a_p10: Option<f64>,
pub top_a_p50: Option<f64>,
pub top_a_p90: Option<f64>,
pub top_k_p10: Option<f64>,
pub top_k_p50: Option<f64>,
pub top_k_p90: Option<f64>,
pub top_p_p10: Option<f64>,
pub top_p_p50: Option<f64>,
pub top_p_p90: Option<f64>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct ParameterResponse {
pub data: ParameterData,
}
2 changes: 1 addition & 1 deletion crates/forge_provider/src/open_router/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use reqwest_eventsource::{Event, EventSource};
use tokio_stream::StreamExt;

use super::model::{ListModelResponse, OpenRouterModel};
use super::parameters::ParameterResponse;
use super::request::OpenRouterRequest;
use super::response::OpenRouterResponse;
use super::ParameterResponse;
use crate::error::Result;
use crate::provider::ProviderService;
use crate::{Error, Live, Service};
Expand Down
26 changes: 4 additions & 22 deletions crates/forge_provider/src/open_router/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use forge_domain::{Context, ContextMessage, ModelId, Role, ToolCallId, ToolDefin
use serde::{Deserialize, Serialize};

use super::response::{FunctionCall, OpenRouterToolCall};
use super::tool_choice::{FunctionType, ToolChoice};

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct TextContent {
Expand Down Expand Up @@ -91,17 +92,10 @@ pub struct FunctionDescription {
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct OpenRouterTool {
// TODO: should be an enum
pub r#type: String,
pub r#type: FunctionType,
pub function: FunctionDescription,
}

#[derive(Debug, Deserialize, Serialize, Clone)]
pub enum ToolChoice {
None,
Auto,
Function { name: String },
}

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct ResponseFormat {
pub r#type: String,
Expand Down Expand Up @@ -174,7 +168,7 @@ pub struct OpenRouterRequest {
impl From<ToolDefinition> for OpenRouterTool {
fn from(value: ToolDefinition) -> Self {
OpenRouterTool {
r#type: "function".to_string(),
r#type: FunctionType,
function: FunctionDescription {
description: Some(value.description),
name: value.name.into_string(),
Expand Down Expand Up @@ -247,7 +241,7 @@ impl From<ContextMessage> for OpenRouterMessage {
// FIXME: All the tool_calls should be added, instead of just one of them
vec![OpenRouterToolCall {
id: tool_call.call_id,
r#type: "function".to_string(),
r#type: FunctionType,
function: FunctionCall {
arguments: serde_json::to_string(&tool_call.arguments).unwrap(),
name: Some(tool_call.name),
Expand Down Expand Up @@ -297,18 +291,6 @@ pub enum OpenRouterRole {
Tool,
}

impl From<forge_domain::ToolChoice> for ToolChoice {
fn from(value: forge_domain::ToolChoice) -> Self {
match value {
forge_domain::ToolChoice::None => ToolChoice::None,
forge_domain::ToolChoice::Auto => ToolChoice::Auto,
forge_domain::ToolChoice::Call(tool_name) => {
ToolChoice::Function { name: tool_name.into_string() }
}
}
}
}

#[cfg(test)]
mod tests {
use forge_domain::{
Expand Down
3 changes: 2 additions & 1 deletion crates/forge_provider/src/open_router/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use forge_domain::{
};
use serde::{Deserialize, Serialize};

use super::tool_choice::FunctionType;
use crate::error::Error;

#[derive(Debug, Deserialize, Serialize, Clone)]
Expand Down Expand Up @@ -80,7 +81,7 @@ pub struct ResponseMessage {
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct OpenRouterToolCall {
pub id: Option<ToolCallId>,
pub r#type: String,
pub r#type: FunctionType,
pub function: FunctionCall,
}

Expand Down
79 changes: 79 additions & 0 deletions crates/forge_provider/src/open_router/tool_choice.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "snake_case")]
pub enum ToolChoice {
None,
Auto,
#[serde(untagged)]
Function {
r#type: FunctionType,
function: FunctionName,
},
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "snake_case")]
pub struct FunctionName {
pub name: String,
}

#[derive(Default, Debug, Clone)]
pub struct FunctionType;

impl Serialize for FunctionType {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str("function")
}
}

impl<'de> Deserialize<'de> for FunctionType {
fn deserialize<D>(_deserializer: D) -> Result<FunctionType, D::Error>
where
D: serde::Deserializer<'de>,
{
Ok(FunctionType)
}
}

impl From<forge_domain::ToolChoice> for ToolChoice {
fn from(value: forge_domain::ToolChoice) -> Self {
match value {
forge_domain::ToolChoice::None => ToolChoice::None,
forge_domain::ToolChoice::Auto => ToolChoice::Auto,
forge_domain::ToolChoice::Call(tool_name) => ToolChoice::Function {
function: FunctionName { name: tool_name.into_string() },
r#type: FunctionType,
},
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_tool_choice_serialization() {
// Test None variant
let choice_none = ToolChoice::None;
assert_eq!(serde_json::to_string(&choice_none).unwrap(), r#""none""#);

// Test Auto variant
let choice_auto = ToolChoice::Auto;
assert_eq!(serde_json::to_string(&choice_auto).unwrap(), r#""auto""#);

// Test Function variant
let choice_function = ToolChoice::Function {
function: FunctionName { name: "test_tool".to_string() },
r#type: FunctionType,
};
assert_eq!(
serde_json::to_string(&choice_function).unwrap(),
r#"{"type":"function","function":{"name":"test_tool"}}"#
);
}
}
Loading