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

Better ChatCompletionFunctionCall interface #118

Merged
merged 8 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
33 changes: 29 additions & 4 deletions async-openai/src/types/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use std::{
path::{Path, PathBuf},
};

use serde::{ser::SerializeMap, Serialize};

64bit marked this conversation as resolved.
Show resolved Hide resolved
use crate::{
download::{download_url, save_b64},
error::OpenAIError,
Expand Down Expand Up @@ -351,13 +353,36 @@ impl_from_for_array_of_integer_array!(u16, Prompt);

impl From<&str> for ChatCompletionFunctionCall {
fn from(value: &str) -> Self {
ChatCompletionFunctionCall::String(value.to_string())
match value {
"none" => Self::None,
"auto" => Self::Auto,
_ => Self::Function(value.to_string()),
64bit marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

impl From<serde_json::Value> for ChatCompletionFunctionCall {
fn from(value: serde_json::Value) -> Self {
ChatCompletionFunctionCall::Object(value)
impl Serialize for ChatCompletionFunctionCall {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self {
ChatCompletionFunctionCall::None => serializer.serialize_str("none"),
ChatCompletionFunctionCall::Auto => serializer.serialize_str("auto"),
ChatCompletionFunctionCall::Function(s) => {
let mut map = serializer.serialize_map(Some(1))?;
map.serialize_entry("name", s)?;
map.end()
}
}
}
}

impl<'de> serde::de::Deserialize<'de> for ChatCompletionFunctionCall {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;

Ok(s.as_str().into())
64bit marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
16 changes: 8 additions & 8 deletions async-openai/src/types/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,14 @@ pub enum Stop {
StringArray(Vec<String>), // minItems: 1; maxItems: 4
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
#[serde(untagged)]
#[derive(Debug, Clone, PartialEq)]
pub enum ChatCompletionFunctionCall {
String(String),
Object(serde_json::Value),
/// The model does not call a function, and responds to the end-user.
None,
/// The model can pick between an end-user or calling a function.
Auto,
/// Forces the model to call the specified function.
Function(String),
64bit marked this conversation as resolved.
Show resolved Hide resolved
}

#[derive(Clone, Serialize, Default, Debug, Builder, PartialEq)]
Expand Down Expand Up @@ -781,10 +784,7 @@ pub struct CreateChatCompletionRequest {
pub functions: Option<Vec<ChatCompletionFunctions>>,

/// Controls how the model responds to function calls.
/// "none" means the model does not call a function, and responds to the end-user.
/// "auto" means the model can pick between an end-user or calling a function.
/// Specifying a particular function via `{"name":\ "my_function"}` forces the model to call that function.
/// "none" is the default when no functions are present. "auto" is the default if functions are present.
/// None is the default when no functions are present. Auto is the default if functions are present.
64bit marked this conversation as resolved.
Show resolved Hide resolved
#[serde(skip_serializing_if = "Option::is_none")]
pub function_call: Option<ChatCompletionFunctionCall>,

Expand Down