Skip to main content

gproxy_protocol/protocol/openai/common/
formats.rs

1use serde::{Deserialize, Serialize};
2
3use super::{Extra, JsonSchema};
4
5#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
6#[serde(untagged)]
7#[non_exhaustive]
8pub enum ChatResponseFormat {
9    ChatJsonSchema(ChatJsonSchemaFormat),
10    Text(TextResponseFormat),
11    JsonObject(JsonObjectResponseFormat),
12}
13
14#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
15#[serde(untagged)]
16#[non_exhaustive]
17pub enum ResponseFormat {
18    JsonSchema(JsonSchemaResponseFormat),
19    Text(TextResponseFormat),
20    JsonObject(JsonObjectResponseFormat),
21}
22
23#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, gproxy_protocol_macros::WireBuilder)]
24#[non_exhaustive]
25pub struct TextResponseFormat {
26    #[serde(rename = "type")]
27    pub type_: TextResponseFormatType,
28    #[serde(
29        default,
30        flatten,
31        skip_serializing_if = "std::collections::BTreeMap::is_empty"
32    )]
33    pub extra: Extra,
34}
35
36#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
37#[non_exhaustive]
38pub enum TextResponseFormatType {
39    #[serde(rename = "text")]
40    Text,
41}
42
43#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, gproxy_protocol_macros::WireBuilder)]
44#[non_exhaustive]
45pub struct ChatJsonSchemaFormat {
46    #[serde(rename = "type")]
47    pub type_: JsonSchemaResponseFormatType,
48    pub json_schema: JsonSchemaFormat,
49    #[serde(
50        default,
51        flatten,
52        skip_serializing_if = "std::collections::BTreeMap::is_empty"
53    )]
54    pub extra: Extra,
55}
56
57#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, gproxy_protocol_macros::WireBuilder)]
58#[non_exhaustive]
59pub struct JsonSchemaResponseFormat {
60    #[serde(rename = "type")]
61    pub type_: JsonSchemaResponseFormatType,
62    pub name: String,
63    pub schema: JsonSchema,
64    #[serde(skip_serializing_if = "Option::is_none")]
65    pub description: Option<String>,
66    #[serde(skip_serializing_if = "Option::is_none")]
67    pub strict: Option<bool>,
68    #[serde(
69        default,
70        flatten,
71        skip_serializing_if = "std::collections::BTreeMap::is_empty"
72    )]
73    pub extra: Extra,
74}
75
76#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
77#[non_exhaustive]
78pub enum JsonSchemaResponseFormatType {
79    #[serde(rename = "json_schema")]
80    JsonSchema,
81}
82
83#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, gproxy_protocol_macros::WireBuilder)]
84#[non_exhaustive]
85pub struct JsonObjectResponseFormat {
86    #[serde(rename = "type")]
87    pub type_: JsonObjectResponseFormatType,
88    #[serde(
89        default,
90        flatten,
91        skip_serializing_if = "std::collections::BTreeMap::is_empty"
92    )]
93    pub extra: Extra,
94}
95
96#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
97#[non_exhaustive]
98pub enum JsonObjectResponseFormatType {
99    #[serde(rename = "json_object")]
100    JsonObject,
101}
102
103#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, gproxy_protocol_macros::WireBuilder)]
104#[non_exhaustive]
105pub struct JsonSchemaFormat {
106    pub name: String,
107    #[serde(skip_serializing_if = "Option::is_none")]
108    pub description: Option<String>,
109    #[serde(skip_serializing_if = "Option::is_none")]
110    pub schema: Option<JsonSchema>,
111    #[serde(skip_serializing_if = "Option::is_none")]
112    pub strict: Option<bool>,
113    #[serde(
114        default,
115        flatten,
116        skip_serializing_if = "std::collections::BTreeMap::is_empty"
117    )]
118    pub extra: Extra,
119}