Skip to main content

cognee_llm/
types.rs

1//! Common types for LLM operations.
2
3use serde::{Deserialize, Serialize};
4
5/// Message role in a conversation.
6#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
7#[serde(rename_all = "lowercase")]
8pub enum MessageRole {
9    System,
10    User,
11    Assistant,
12}
13
14/// A message in a conversation.
15#[derive(Debug, Clone, Serialize, Deserialize)]
16pub struct Message {
17    pub role: MessageRole,
18    pub content: String,
19}
20
21impl Message {
22    pub fn system(content: impl Into<String>) -> Self {
23        Self {
24            role: MessageRole::System,
25            content: content.into(),
26        }
27    }
28
29    pub fn user(content: impl Into<String>) -> Self {
30        Self {
31            role: MessageRole::User,
32            content: content.into(),
33        }
34    }
35
36    pub fn assistant(content: impl Into<String>) -> Self {
37        Self {
38            role: MessageRole::Assistant,
39            content: content.into(),
40        }
41    }
42}
43
44/// Default output-token ceiling (`llm_max_completion_tokens`) shared across the
45/// SDK: `GenerationOptions::default`, the config defaults in `cognee-lib` and
46/// `cognee-http-server`, and the Anthropic adapter's fallback. Kept in one place
47/// so all of them move in lockstep. The per-request value is still clamped to
48/// each model's documented cap (see `AnthropicAdapter::effective_max_tokens`).
49pub const DEFAULT_MAX_COMPLETION_TOKENS: u32 = 16384;
50
51/// Options for LLM generation.
52#[derive(Debug, Clone, Serialize, Deserialize)]
53pub struct GenerationOptions {
54    /// Temperature for sampling (0.0 = deterministic, 1.0 = creative).
55    #[serde(skip_serializing_if = "Option::is_none")]
56    pub temperature: Option<f32>,
57
58    /// Maximum number of tokens to generate.
59    #[serde(skip_serializing_if = "Option::is_none")]
60    pub max_tokens: Option<u32>,
61
62    /// Top-p sampling parameter.
63    #[serde(skip_serializing_if = "Option::is_none")]
64    pub top_p: Option<f32>,
65
66    /// Frequency penalty.
67    #[serde(skip_serializing_if = "Option::is_none")]
68    pub frequency_penalty: Option<f32>,
69
70    /// Presence penalty.
71    #[serde(skip_serializing_if = "Option::is_none")]
72    pub presence_penalty: Option<f32>,
73
74    /// Stop sequences.
75    #[serde(skip_serializing_if = "Option::is_none")]
76    pub stop: Option<Vec<String>>,
77}
78
79impl Default for GenerationOptions {
80    fn default() -> Self {
81        Self {
82            temperature: Some(0.0),
83            max_tokens: Some(DEFAULT_MAX_COMPLETION_TOKENS),
84            top_p: None,
85            frequency_penalty: None,
86            presence_penalty: None,
87            stop: None,
88        }
89    }
90}
91
92/// Response from LLM generation.
93#[derive(Debug, Clone, Serialize, Deserialize)]
94pub struct GenerationResponse {
95    /// Generated text content.
96    pub content: String,
97
98    /// Model used for generation.
99    pub model: String,
100
101    /// Token usage information.
102    pub usage: Option<TokenUsage>,
103
104    /// Finish reason (e.g., "stop", "length", "content_filter").
105    pub finish_reason: Option<String>,
106}
107
108/// Token usage statistics.
109#[derive(Debug, Clone, Serialize, Deserialize)]
110pub struct TokenUsage {
111    pub prompt_tokens: u32,
112    pub completion_tokens: u32,
113    pub total_tokens: u32,
114}