-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathchunk_builder.rs
More file actions
292 lines (269 loc) · 8.99 KB
/
Copy pathchunk_builder.rs
File metadata and controls
292 lines (269 loc) · 8.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
//! Shared chunk builder for creating OpenAI-compatible streaming chunks
//!
//! All external backends (Anthropic, Gemini, OpenAI-compatible) convert their
//! native streaming formats to OpenAI-compatible `ChatCompletionChunk`. This
//! module provides helper functions to reduce duplication across backends.
use crate::{
ChatChoice, ChatCompletionChunk, ChatDelta, FinishReason, FunctionCallDelta, MessageRole,
TokenUsage, ToolCallDelta,
};
/// Parameters common to all chunks in a streaming response
#[derive(Debug, Clone)]
pub struct ChunkContext {
pub id: String,
pub model: String,
pub created: i64,
}
impl ChunkContext {
pub fn new(id: String, model: String, created: i64) -> Self {
Self { id, model, created }
}
/// Create a chunk with the given delta, finish reason, and usage
fn build(
&self,
delta: ChatDelta,
finish_reason: Option<FinishReason>,
usage: Option<TokenUsage>,
) -> ChatCompletionChunk {
ChatCompletionChunk {
id: self.id.clone(),
object: "chat.completion.chunk".to_string(),
created: self.created,
model: self.model.clone(),
system_fingerprint: None,
choices: vec![ChatChoice {
index: 0,
delta: Some(delta),
logprobs: None,
finish_reason,
token_ids: None,
}],
usage,
prompt_token_ids: None,
modality: None,
extra: Default::default(),
}
}
/// Create a chunk with assistant role (typically first chunk)
pub fn role_chunk(&self) -> ChatCompletionChunk {
self.build(
ChatDelta {
role: Some(MessageRole::Assistant),
content: None,
name: None,
tool_call_id: None,
tool_calls: None,
reasoning_content: None,
reasoning: None,
extra: Default::default(),
},
None,
None,
)
}
/// Create a chunk with text content
pub fn text_chunk(&self, text: String) -> ChatCompletionChunk {
self.build(
ChatDelta {
role: None,
content: Some(text),
name: None,
tool_call_id: None,
tool_calls: None,
reasoning_content: None,
reasoning: None,
extra: Default::default(),
},
None,
None,
)
}
/// Create a chunk starting a tool call (with id and function name)
pub fn tool_call_start_chunk(
&self,
index: i64,
tool_call_id: String,
function_name: String,
) -> ChatCompletionChunk {
self.build(
ChatDelta {
role: None,
content: None,
name: None,
tool_call_id: None,
tool_calls: Some(vec![ToolCallDelta {
index: Some(index),
id: Some(tool_call_id),
type_: Some("function".to_string()),
function: Some(FunctionCallDelta {
name: Some(function_name),
arguments: None,
}),
thought_signature: None,
}]),
reasoning_content: None,
reasoning: None,
extra: Default::default(),
},
None,
None,
)
}
/// Create a chunk with tool call argument delta
pub fn tool_call_args_chunk(&self, index: i64, arguments: String) -> ChatCompletionChunk {
self.build(
ChatDelta {
role: None,
content: None,
name: None,
tool_call_id: None,
tool_calls: Some(vec![ToolCallDelta {
index: Some(index),
id: None,
type_: None,
function: Some(FunctionCallDelta {
name: None,
arguments: Some(arguments),
}),
thought_signature: None,
}]),
reasoning_content: None,
reasoning: None,
extra: Default::default(),
},
None,
None,
)
}
/// Create a chunk with complete tool calls (for providers like Gemini that don't stream tool calls)
pub fn tool_calls_chunk(
&self,
tool_calls: Vec<crate::ToolCall>,
finish_reason: Option<FinishReason>,
usage: Option<TokenUsage>,
) -> ChatCompletionChunk {
// Convert ToolCall to ToolCallDelta
let deltas: Vec<ToolCallDelta> = tool_calls
.into_iter()
.map(|tc| ToolCallDelta {
index: tc.index,
id: tc.id,
type_: tc.type_,
function: Some(FunctionCallDelta {
name: tc.function.name,
arguments: tc.function.arguments,
}),
thought_signature: tc.thought_signature,
})
.collect();
self.build(
ChatDelta {
role: None,
content: None,
name: None,
tool_call_id: None,
tool_calls: Some(deltas),
reasoning_content: None,
reasoning: None,
extra: Default::default(),
},
finish_reason,
usage,
)
}
/// Create a finish chunk with reason and usage stats
pub fn finish_chunk(
&self,
finish_reason: Option<FinishReason>,
usage: TokenUsage,
) -> ChatCompletionChunk {
self.build(
ChatDelta {
role: None,
content: None,
name: None,
tool_call_id: None,
tool_calls: None,
reasoning_content: None,
reasoning: None,
extra: Default::default(),
},
finish_reason,
Some(usage),
)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn test_context() -> ChunkContext {
ChunkContext::new("test-id".to_string(), "test-model".to_string(), 1234567890)
}
#[test]
fn test_role_chunk() {
let ctx = test_context();
let chunk = ctx.role_chunk();
assert_eq!(chunk.id, "test-id");
assert_eq!(chunk.model, "test-model");
assert_eq!(chunk.created, 1234567890);
assert_eq!(chunk.choices.len(), 1);
assert_eq!(
chunk.choices[0].delta.as_ref().unwrap().role,
Some(MessageRole::Assistant)
);
assert!(chunk.choices[0].delta.as_ref().unwrap().content.is_none());
}
#[test]
fn test_text_chunk() {
let ctx = test_context();
let chunk = ctx.text_chunk("Hello world".to_string());
assert_eq!(
chunk.choices[0].delta.as_ref().unwrap().content,
Some("Hello world".to_string())
);
assert!(chunk.choices[0].delta.as_ref().unwrap().role.is_none());
}
#[test]
fn test_tool_call_start_chunk() {
let ctx = test_context();
let chunk = ctx.tool_call_start_chunk(0, "call_123".to_string(), "web_search".to_string());
let delta = chunk.choices[0].delta.as_ref().unwrap();
let tool_calls = delta.tool_calls.as_ref().unwrap();
assert_eq!(tool_calls.len(), 1);
assert_eq!(tool_calls[0].index, Some(0));
assert_eq!(tool_calls[0].id, Some("call_123".to_string()));
assert_eq!(tool_calls[0].type_, Some("function".to_string()));
assert_eq!(
tool_calls[0].function.as_ref().unwrap().name,
Some("web_search".to_string())
);
}
#[test]
fn test_tool_call_args_chunk() {
let ctx = test_context();
let chunk = ctx.tool_call_args_chunk(0, r#"{"query":"test"}"#.to_string());
let delta = chunk.choices[0].delta.as_ref().unwrap();
let tool_calls = delta.tool_calls.as_ref().unwrap();
assert_eq!(tool_calls[0].index, Some(0));
assert!(tool_calls[0].id.is_none());
assert_eq!(
tool_calls[0].function.as_ref().unwrap().arguments,
Some(r#"{"query":"test"}"#.to_string())
);
}
#[test]
fn test_finish_chunk() {
let ctx = test_context();
let usage = TokenUsage {
prompt_tokens: 10,
completion_tokens: 20,
total_tokens: 30,
prompt_tokens_details: None,
};
let chunk = ctx.finish_chunk(Some(FinishReason::Stop), usage);
assert_eq!(chunk.choices[0].finish_reason, Some(FinishReason::Stop));
assert!(chunk.usage.is_some());
assert_eq!(chunk.usage.as_ref().unwrap().prompt_tokens, 10);
assert_eq!(chunk.usage.as_ref().unwrap().completion_tokens, 20);
}
}