Skip to content

Commit

Permalink
add function to deserialize json arguments into string
Browse files Browse the repository at this point in the history
  • Loading branch information
linusbierhoff committed Oct 6, 2024
1 parent e8158e5 commit b744708
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
10 changes: 10 additions & 0 deletions async-openai/src/types/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,19 @@ pub struct FunctionCall {
/// The name of the function to call.
pub name: String,
/// The arguments to call the function with, as generated by the model in JSON format. Note that the model does not always generate valid JSON, and may hallucinate parameters not defined by your function schema. Validate the arguments in your code before calling your function.
#[serde(deserialize_with = "deserialize_arguments_as_string")]
pub arguments: String,
}

fn deserialize_arguments_as_string<'de, D>(deserializer: D) -> Result<String, D::Error>
where
D: serde::Deserializer<'de>,
{
let value = serde_json::Value::deserialize(deserializer)?;
Ok(value.to_string())
}


/// Usage statistics for the completion request.
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
pub struct CompletionUsage {
Expand Down
9 changes: 9 additions & 0 deletions async-openai/src/types/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,20 @@ pub struct RunStepFunctionObject {
/// The name of the function.
pub name: String,
/// The arguments passed to the function.
#[serde(deserialize_with = "deserialize_arguments_as_string")]
pub arguments: String,
/// The output of the function. This will be `null` if the outputs have not been [submitted](https://platform.openai.com/docs/api-reference/runs/submitToolOutputs) yet.
pub output: Option<String>,
}

fn deserialize_arguments_as_string<'de, D>(deserializer: D) -> Result<String, D::Error>
where
D: serde::Deserializer<'de>,
{
let value = serde_json::Value::deserialize(deserializer)?;
Ok(value.to_string())
}

#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
pub struct RunStepFunctionObjectDelta {
/// The name of the function.
Expand Down

0 comments on commit b744708

Please sign in to comment.