mirror of
https://github.com/mii443/openai-api-rs.git
synced 2025-12-03 11:08:23 +00:00
Support function calling
This commit is contained in:
@@ -1,18 +1,26 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::v1::common;
|
||||
|
||||
pub const GPT3_5_TURBO: &str = "gpt-3.5-turbo";
|
||||
pub const GPT3_5_TURBO_0301: &str = "gpt-3.5-turbo-0301";
|
||||
pub const GPT3_5_TURBO_0613: &str = "gpt-3.5-turbo-0613";
|
||||
|
||||
pub const GPT4: &str = "gpt-4";
|
||||
pub const GPT4_0314: &str = "gpt-4-0314";
|
||||
pub const GPT4_32K: &str = "gpt-4-32k";
|
||||
pub const GPT4_32K_0314: &str = "gpt-4-32k-0314";
|
||||
pub const GPT4_0613: &str = "gpt-4-0613";
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct ChatCompletionRequest {
|
||||
pub model: String,
|
||||
pub messages: Vec<ChatCompletionMessage>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub functions: Option<Vec<Function>>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub function_call: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
@@ -26,14 +34,19 @@ pub enum MessageRole {
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct ChatCompletionMessage {
|
||||
pub role: MessageRole,
|
||||
pub content: String,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub content: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub name: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub function_call: Option<FunctionCall>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct ChatCompletionChoice {
|
||||
pub index: i64,
|
||||
pub message: ChatCompletionMessage,
|
||||
pub finish_reason: String,
|
||||
pub finish_reason: FinishReason,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
@@ -45,3 +58,67 @@ pub struct ChatCompletionResponse {
|
||||
pub choices: Vec<ChatCompletionChoice>,
|
||||
pub usage: common::Usage,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct Function {
|
||||
pub name: String,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub description: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub parameters: Option<FunctionParameters>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum JSONSchemaType {
|
||||
Object,
|
||||
Number,
|
||||
String,
|
||||
Array,
|
||||
Null,
|
||||
Boolean,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct JSONSchemaDefine {
|
||||
#[serde(rename = "type")]
|
||||
pub schema_type: Option<JSONSchemaType>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub description: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub enum_values: Option<Vec<String>>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub properties: Option<HashMap<String, Box<JSONSchemaDefine>>>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub required: Option<Vec<String>>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub items: Option<Box<JSONSchemaDefine>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct FunctionParameters {
|
||||
#[serde(rename = "type")]
|
||||
pub schema_type: JSONSchemaType,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub properties: Option<HashMap<String, Box<JSONSchemaDefine>>>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub required: Option<Vec<String>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
#[allow(non_camel_case_types)]
|
||||
pub enum FinishReason {
|
||||
stop,
|
||||
length,
|
||||
function_call,
|
||||
content_filter,
|
||||
null,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct FunctionCall {
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub name: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub arguments: Option<String>,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user