Skip to main content

abu_base/chat/
request.rs

1use super::message::*;
2use super::tool::*;
3use derive_builder::Builder;
4use serde::Serialize;
5use strum::{Display, EnumString, EnumVariantNames};
6
7#[derive(Debug, Clone, Builder)]
8#[builder(setter(into, strip_option))]
9pub struct ChatRequest {
10    /// ID of the model to use. See the model endpoint compatibility table for details on which models work with the Chat API.
11    pub model: String,
12
13    /// A list of messages comprising the conversation so far.
14    #[builder(default)]
15    pub messages: Vec<ChatMessage>,
16
17    /// A list of tools the model may call. Currently, only functions are supported as a tool. Use this to provide a list of functions the model may generate JSON inputs for.
18    #[builder(default)]
19    pub tools: Vec<ToolDefinition>,
20
21    /// If set, partial message deltas will be sent, like in ChatGPT. Tokens will be sent as data-only server-sent events as they become available, with the stream terminated by a data: [DONE] message.
22    #[builder(default)]
23    pub stream: Option<bool>,   
24
25    /// What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. We generally recommend altering this or top_p but not both.
26    #[builder(default)]
27    pub temperature: Option<f64>,
28}
29
30#[derive(Debug, Clone, Serialize)]
31pub struct ChatResponseFormatObject {
32    r#type: ChatResponseFormat,
33}
34
35#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, EnumString, Display, EnumVariantNames)]
36#[serde(rename_all = "snake_case")]
37pub enum ChatResponseFormat {
38    Text,
39    #[default]
40    Json,
41}
42
43impl ChatRequest {
44    pub fn new(model: impl Into<String>, messages: impl Into<Vec<ChatMessage>>) -> Self {
45        ChatRequestBuilder::default()
46            .model(model.into())
47            .messages(messages)
48            .build()
49            .unwrap()
50    }
51
52    pub fn new_with_tools(
53        model: impl Into<String>,
54        messages: impl Into<Vec<ChatMessage>>,
55        tools: impl Into<Vec<ToolDefinition>>,
56    ) -> Self {
57        ChatRequestBuilder::default()
58            .model(model.into())
59            .messages(messages)
60            .tools(tools)
61            .build()
62            .unwrap()
63    }
64}