Skip to main content

systemprompt_provider_contracts/llm/
request.rs

1//! Request-shape types passed into [`crate::llm::LlmProvider::chat`].
2//!
3//! Copyright (c) systemprompt.io — Business Source License 1.1.
4//! See <https://systemprompt.io> for licensing details.
5
6use serde::{Deserialize, Serialize};
7use serde_json::Value as JsonValue;
8
9use super::message::ChatMessage;
10use crate::tool::ToolDefinition;
11
12#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
13pub struct SamplingParameters {
14    #[serde(skip_serializing_if = "Option::is_none")]
15    pub temperature: Option<f32>,
16    #[serde(skip_serializing_if = "Option::is_none")]
17    pub top_p: Option<f32>,
18    #[serde(skip_serializing_if = "Option::is_none")]
19    pub top_k: Option<u32>,
20}
21
22impl SamplingParameters {
23    #[must_use]
24    pub const fn new() -> Self {
25        Self {
26            temperature: None,
27            top_p: None,
28            top_k: None,
29        }
30    }
31
32    #[must_use]
33    pub const fn with_temperature(mut self, temperature: f32) -> Self {
34        self.temperature = Some(temperature);
35        self
36    }
37
38    #[must_use]
39    pub const fn with_top_p(mut self, top_p: f32) -> Self {
40        self.top_p = Some(top_p);
41        self
42    }
43
44    #[must_use]
45    pub const fn with_top_k(mut self, top_k: u32) -> Self {
46        self.top_k = Some(top_k);
47        self
48    }
49}
50
51impl Default for SamplingParameters {
52    fn default() -> Self {
53        Self::new()
54    }
55}
56
57#[derive(Debug, Clone)]
58pub struct ChatRequest {
59    pub messages: Vec<ChatMessage>,
60    pub model: String,
61    pub max_output_tokens: u32,
62    pub sampling: Option<SamplingParameters>,
63    pub tools: Option<Vec<ToolDefinition>>,
64    pub response_schema: Option<JsonValue>,
65}
66
67impl ChatRequest {
68    #[must_use]
69    pub fn new(
70        messages: Vec<ChatMessage>,
71        model: impl Into<String>,
72        max_output_tokens: u32,
73    ) -> Self {
74        Self {
75            messages,
76            model: model.into(),
77            max_output_tokens,
78            sampling: None,
79            tools: None,
80            response_schema: None,
81        }
82    }
83
84    #[must_use]
85    pub const fn with_sampling(mut self, sampling: SamplingParameters) -> Self {
86        self.sampling = Some(sampling);
87        self
88    }
89
90    #[must_use]
91    pub fn with_tools(mut self, tools: Vec<ToolDefinition>) -> Self {
92        self.tools = Some(tools);
93        self
94    }
95
96    #[must_use]
97    pub fn with_response_schema(mut self, schema: JsonValue) -> Self {
98        self.response_schema = Some(schema);
99        self
100    }
101}