Skip to main content

agixt_sdk/models/
mod.rs

1//! Model types for the AGiXT SDK.
2
3use serde::{Deserialize, Serialize};
4use std::collections::HashMap;
5
6/// Chat completion request for OpenAI-compatible API.
7#[derive(Debug, Clone, Serialize, Deserialize)]
8pub struct ChatCompletions {
9    /// The model/agent name to use
10    #[serde(default = "default_model")]
11    pub model: String,
12    /// The messages for the conversation
13    #[serde(skip_serializing_if = "Option::is_none")]
14    pub messages: Option<Vec<Message>>,
15    /// Sampling temperature (0.0 to 2.0)
16    #[serde(skip_serializing_if = "Option::is_none")]
17    pub temperature: Option<f32>,
18    /// Nucleus sampling parameter
19    #[serde(skip_serializing_if = "Option::is_none")]
20    pub top_p: Option<f32>,
21    /// Available tools for the model
22    #[serde(skip_serializing_if = "Option::is_none")]
23    pub tools: Option<Vec<Tool>>,
24    /// How to choose which tools to use
25    #[serde(skip_serializing_if = "Option::is_none")]
26    pub tools_choice: Option<String>,
27    /// Number of completions to generate
28    #[serde(skip_serializing_if = "Option::is_none")]
29    pub n: Option<i32>,
30    /// Whether to stream the response
31    #[serde(skip_serializing_if = "Option::is_none")]
32    pub stream: Option<bool>,
33    /// Sequences where the model should stop generating
34    #[serde(skip_serializing_if = "Option::is_none")]
35    pub stop: Option<Vec<String>>,
36    /// Maximum number of tokens to generate
37    #[serde(skip_serializing_if = "Option::is_none")]
38    pub max_tokens: Option<i32>,
39    /// Penalty for repeated tokens
40    #[serde(skip_serializing_if = "Option::is_none")]
41    pub presence_penalty: Option<f32>,
42    /// Penalty for token frequency
43    #[serde(skip_serializing_if = "Option::is_none")]
44    pub frequency_penalty: Option<f32>,
45    /// Token biases
46    #[serde(skip_serializing_if = "Option::is_none")]
47    pub logit_bias: Option<HashMap<String, f32>>,
48    /// The conversation ID
49    #[serde(skip_serializing_if = "Option::is_none")]
50    pub user: Option<String>,
51}
52
53/// Message in a chat conversation.
54#[derive(Debug, Clone, Serialize, Deserialize)]
55pub struct Message {
56    /// The role of the message sender (user, assistant, system)
57    pub role: String,
58    /// The content of the message
59    pub content: MessageContent,
60    /// Optional message ID
61    #[serde(skip_serializing_if = "Option::is_none")]
62    pub id: Option<String>,
63    /// Optional timestamp
64    #[serde(skip_serializing_if = "Option::is_none")]
65    pub timestamp: Option<String>,
66}
67
68/// Content of a message, can be text or structured.
69#[derive(Debug, Clone, Serialize, Deserialize)]
70#[serde(untagged)]
71pub enum MessageContent {
72    /// Text content
73    Text(String),
74    /// Structured content with multiple parts
75    Structured(Vec<ContentPart>),
76}
77
78/// Part of structured message content.
79#[derive(Debug, Clone, Serialize, Deserialize)]
80pub struct ContentPart {
81    /// Optional text content
82    #[serde(skip_serializing_if = "Option::is_none")]
83    pub text: Option<String>,
84    /// Optional image URL
85    #[serde(skip_serializing_if = "Option::is_none")]
86    pub image_url: Option<ImageUrl>,
87    /// Optional file URL
88    #[serde(skip_serializing_if = "Option::is_none")]
89    pub file_url: Option<FileUrl>,
90}
91
92/// Image URL reference.
93#[derive(Debug, Clone, Serialize, Deserialize)]
94pub struct ImageUrl {
95    pub url: String,
96}
97
98/// File URL reference.
99#[derive(Debug, Clone, Serialize, Deserialize)]
100pub struct FileUrl {
101    pub url: String,
102}
103
104/// Tool definition for function calling.
105#[derive(Debug, Clone, Serialize, Deserialize)]
106pub struct Tool {
107    /// The type of tool (usually "function")
108    #[serde(rename = "type")]
109    pub tool_type: String,
110    /// Function definition
111    pub function: ToolFunction,
112}
113
114/// Function definition within a tool.
115#[derive(Debug, Clone, Serialize, Deserialize)]
116pub struct ToolFunction {
117    /// The name of the function
118    pub name: String,
119    /// Description of the function
120    pub description: String,
121    /// JSON Schema for the function's parameters
122    pub parameters: serde_json::Value,
123}
124
125fn default_model() -> String {
126    "gpt4free".to_string()
127}
128
129impl Default for ChatCompletions {
130    fn default() -> Self {
131        Self {
132            model: default_model(),
133            messages: None,
134            temperature: Some(0.9),
135            top_p: Some(1.0),
136            tools: None,
137            tools_choice: Some("auto".to_string()),
138            n: Some(1),
139            stream: Some(false),
140            stop: None,
141            max_tokens: Some(4096),
142            presence_penalty: Some(0.0),
143            frequency_penalty: Some(0.0),
144            logit_bias: None,
145            user: Some("Chat".to_string()),
146        }
147    }
148}
149
150/// Response from chat completion API.
151#[derive(Debug, Clone, Serialize, Deserialize)]
152pub struct ChatResponse {
153    pub id: String,
154    pub object: String,
155    pub created: i64,
156    pub model: String,
157    pub choices: Vec<Choice>,
158    pub usage: Usage,
159}
160
161/// Choice in chat completion response.
162#[derive(Debug, Clone, Serialize, Deserialize)]
163pub struct Choice {
164    pub index: i32,
165    pub message: Message,
166    pub finish_reason: String,
167    #[serde(skip_serializing_if = "Option::is_none")]
168    pub logprobs: Option<serde_json::Value>,
169}
170
171/// Token usage in chat completion response.
172#[derive(Debug, Clone, Serialize, Deserialize)]
173pub struct Usage {
174    pub prompt_tokens: i32,
175    pub completion_tokens: i32,
176    pub total_tokens: i32,
177}
178
179/// Agent configuration.
180#[derive(Debug, Clone, Serialize, Deserialize)]
181pub struct Agent {
182    pub id: String,
183    pub name: String,
184    #[serde(default)]
185    pub settings: HashMap<String, serde_json::Value>,
186    #[serde(default)]
187    pub commands: HashMap<String, serde_json::Value>,
188}
189
190/// Conversation information.
191#[derive(Debug, Clone, Serialize, Deserialize)]
192pub struct Conversation {
193    pub id: String,
194    pub name: String,
195    #[serde(skip_serializing_if = "Option::is_none")]
196    pub agent_id: Option<String>,
197}
198
199/// Chain information.
200#[derive(Debug, Clone, Serialize, Deserialize)]
201pub struct Chain {
202    pub id: String,
203    pub name: String,
204    #[serde(skip_serializing_if = "Option::is_none")]
205    pub steps: Option<Vec<ChainStep>>,
206}
207
208/// Step in a chain.
209#[derive(Debug, Clone, Serialize, Deserialize)]
210pub struct ChainStep {
211    pub step_number: i32,
212    pub agent_id: String,
213    pub prompt_type: String,
214    pub prompt: serde_json::Value,
215}
216
217/// Prompt information.
218#[derive(Debug, Clone, Serialize, Deserialize)]
219pub struct Prompt {
220    pub id: String,
221    pub name: String,
222    pub content: String,
223    #[serde(skip_serializing_if = "Option::is_none")]
224    pub category: Option<String>,
225}
226
227/// Provider information.
228#[derive(Debug, Clone, Serialize, Deserialize)]
229pub struct Provider {
230    pub name: String,
231    #[serde(default)]
232    pub settings: HashMap<String, serde_json::Value>,
233    #[serde(default)]
234    pub supports_embeddings: bool,
235}
236
237/// Company information.
238#[derive(Debug, Clone, Serialize, Deserialize)]
239pub struct Company {
240    pub id: String,
241    pub name: String,
242    #[serde(skip_serializing_if = "Option::is_none")]
243    pub agents: Option<Vec<Agent>>,
244}
245
246/// User information.
247#[derive(Debug, Clone, Serialize, Deserialize)]
248pub struct User {
249    pub id: String,
250    pub email: String,
251    #[serde(skip_serializing_if = "Option::is_none")]
252    pub first_name: Option<String>,
253    #[serde(skip_serializing_if = "Option::is_none")]
254    pub last_name: Option<String>,
255}
256
257/// Extension information.
258#[derive(Debug, Clone, Serialize, Deserialize)]
259pub struct Extension {
260    pub name: String,
261    pub description: String,
262    #[serde(default)]
263    pub settings: HashMap<String, serde_json::Value>,
264    #[serde(default)]
265    pub commands: Vec<ExtensionCommand>,
266}
267
268/// Command within an extension.
269#[derive(Debug, Clone, Serialize, Deserialize)]
270pub struct ExtensionCommand {
271    pub name: String,
272    pub description: String,
273    #[serde(default)]
274    pub args: HashMap<String, serde_json::Value>,
275}