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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
//! Text generation and inference operations for `HuggingFace` API.
mod private
{
use crate::
{
client::Client,
components::
{
inference_shared::
{
InferenceRequest, InferenceResponse, InferenceOptions,
ChatCompletionRequest, ChatCompletionResponse, ChatMessage,
},
input::InferenceParameters,
output::InferenceOutput,
},
error::{ Result, HuggingFaceError },
validation::{ validate_input_text, validate_model_identifier },
};
#[ cfg( feature = "env-config" ) ]
use crate::environment::{ HuggingFaceEnvironment, EnvironmentInterface };
/// API group for `HuggingFace` inference operations
#[ derive( Debug ) ]
pub struct Inference< E >
where
E : Clone,
{
client : Client< E >,
}
#[ cfg( feature = "env-config" ) ]
impl< E > Inference< E >
where
E : HuggingFaceEnvironment + EnvironmentInterface + Send + Sync + 'static + Clone,
{
/// Create a new Inference API group
#[ inline ]
#[ must_use ]
pub fn new( client : &Client< E > ) -> Self
{
Self
{
client : (*client).clone(),
}
}
/// Create a text generation inference request
///
/// **Updated for Router API**: Now uses the new chat completions format
///
/// # Arguments
/// - `inputs`: Input text or prompt
/// - `model`: Model identifier (e.g., "moonshotai/Kimi-K2-Instruct-0905:groq")
///
/// # Errors
/// Returns error if the request fails or response is invalid
#[ inline ]
pub async fn create(
&self,
inputs : impl Into< String >,
model : impl AsRef< str >
) -> Result< InferenceResponse >
{
let input_text = inputs.into();
let model_id = model.as_ref();
// Validate input parameters
validate_input_text( &input_text )?;
validate_model_identifier( model_id )?;
// Convert to chat completions format
let chat_request = ChatCompletionRequest
{
messages : vec!
[
ChatMessage
{
role : "user".to_string(),
content : input_text,
tool_calls : None,
tool_call_id : None,
}
],
model : model_id.to_string(),
temperature : None,
max_tokens : None,
top_p : Some( 1.0 ),
stream : Some( false ),
tools : None,
tool_choice : None,
};
let endpoint = "chat/completions";
let url = self.client.environment.endpoint_url( endpoint )?;
// Post request and convert response
let chat_response : ChatCompletionResponse = self.client.post( url.as_str(), &chat_request ).await?;
// Convert chat completion response to inference response format
convert_chat_response_to_inference( &chat_response )
}
/// Create a text generation inference request with parameters
///
/// **Updated for Router API**: Now uses the new chat completions format
///
/// # Arguments
/// - `inputs`: Input text or prompt
/// - `model`: Model identifier
/// - `parameters`: Inference parameters (temperature, `max_tokens`, etc.)
///
/// # Errors
/// Returns error if the request fails or response is invalid
#[ inline ]
pub async fn create_with_parameters(
&self,
inputs : impl Into< String >,
model : impl AsRef< str >,
parameters : InferenceParameters
) -> Result< InferenceResponse >
{
let input_text = inputs.into();
let model_id = model.as_ref();
// Validate input parameters
validate_input_text( &input_text )?;
validate_model_identifier( model_id )?;
parameters.validate()?;
// Convert to chat completions format
let chat_request = ChatCompletionRequest
{
messages : vec!
[
ChatMessage
{
role : "user".to_string(),
content : input_text,
tool_calls : None,
tool_call_id : None,
}
],
model : model_id.to_string(),
temperature : parameters.temperature,
max_tokens : parameters.max_new_tokens,
top_p : parameters.top_p,
stream : Some( false ),
tools : None,
tool_choice : None,
};
let endpoint = "chat/completions";
let url = self.client.environment.endpoint_url( endpoint )?;
// Post request and convert response
let chat_response : ChatCompletionResponse = self.client.post( url.as_str(), &chat_request ).await?;
// Convert chat completion response to inference response format
convert_chat_response_to_inference( &chat_response )
}
/// Create a text generation inference request with full options
///
/// # Arguments
/// - `inputs`: Input text or prompt
/// - `model`: Model identifier
/// - `parameters`: Inference parameters
/// - `options`: Request options
///
/// # Errors
/// Returns error if the request fails or response is invalid
#[ inline ]
pub async fn create_with_options(
&self,
inputs : impl Into< String >,
model : impl AsRef< str >,
parameters : Option< InferenceParameters >,
options : Option< InferenceOptions >
) -> Result< InferenceResponse >
{
let input_text = inputs.into();
let model_id = model.as_ref();
// Validate input parameters
validate_input_text( &input_text )?;
validate_model_identifier( model_id )?;
let mut request = InferenceRequest::new( input_text );
if let Some( params ) = parameters
{
params.validate()?;
request = request.with_parameters( params );
}
if let Some( opts ) = options
{
request = request.with_options( opts );
}
let endpoint = format!( "models/{model_id}" );
let url = self.client.environment.endpoint_url( &endpoint )?;
self.client.post( url.as_str(), &request ).await
}
/// Create a streaming text generation request
///
/// # Arguments
/// - `inputs`: Input text or prompt
/// - `model`: Model identifier
/// - `parameters`: Inference parameters with streaming enabled
///
/// # Returns
/// A receiver channel for streaming response chunks
///
/// # Errors
/// Returns error if the request fails
#[ cfg( feature = "inference-streaming" ) ]
#[ inline ]
pub async fn create_stream(
&self,
inputs : impl Into< String >,
model : impl AsRef< str >,
parameters : InferenceParameters
) -> Result< tokio::sync::mpsc::Receiver< Result< String > > >
{
let input_text = inputs.into();
let model_id = model.as_ref();
validate_input_text( &input_text )?;
validate_model_identifier( model_id )?;
// Use chat/completions with stream=true (Router API, OpenAI-compatible SSE)
let chat_request = ChatCompletionRequest
{
messages : vec![ ChatMessage { role : "user".to_string(), content : input_text, tool_calls : None, tool_call_id : None } ],
model : model_id.to_string(),
temperature : parameters.temperature,
max_tokens : parameters.max_new_tokens,
top_p : parameters.top_p,
stream : Some( true ),
tools : None,
tool_choice : None,
};
let endpoint = "chat/completions";
let url = self.client.environment.endpoint_url( endpoint )?;
// Raw SSE stream: each event.data is an OpenAI streaming chunk JSON
let mut raw_rx = self.client.post_stream( url.as_str(), &chat_request ).await?;
// Extract text from choices[0].delta.content and forward as plain strings
let ( tx, rx ) = tokio::sync::mpsc::channel( 100 );
tokio::spawn( async move
{
while let Some( result ) = raw_rx.recv().await
{
match result
{
Ok( data ) if data == "[DONE]" => break,
Ok( data ) =>
{
let content = serde_json::from_str::< serde_json::Value >( &data )
.ok()
.and_then( | v | v[ "choices" ][ 0 ][ "delta" ][ "content" ].as_str().map( str::to_string ) );
if let Some( text ) = content
{
if !text.is_empty() && tx.send( Ok( text ) ).await.is_err()
{
break;
}
}
},
Err( e ) =>
{
let _ = tx.send( Err( e ) ).await;
break;
}
}
}
} );
Ok( rx )
}
/// Create a controlled stream with pause/resume/cancel support
///
/// This returns a tuple of (`ControlledStream`, `ControlHandle`) that allows
/// runtime control of the streaming operation.
///
/// # Arguments
/// - `inputs`: Text input for generation
/// - `model`: Model identifier to use
/// - `parameters`: Inference parameters with streaming enabled
///
/// # Returns
/// A tuple of (`ControlledStream` for consuming events, `ControlHandle` for control)
///
/// # Errors
/// Returns error if the request fails
///
/// # Example
///
/// ```rust,ignore
/// let ( stream, control ) = inference
/// .create_controlled_stream( "Hello", "gpt2", params )
/// .await?;
///
/// // Pause streaming
/// control.pause().await?;
///
/// // Resume streaming
/// control.resume().await?;
///
/// // Consume stream
/// while let Some( result ) = stream.next().await
/// {
/// match result
/// {
/// Ok( text ) => println!( "{}", text ),
/// Err( e ) => eprintln!( "Error: {}", e ),
/// }
/// }
/// ```
#[ cfg( feature = "streaming-control" ) ]
#[ inline ]
pub async fn create_controlled_stream(
&self,
inputs : impl Into< String >,
model : impl AsRef< str >,
parameters : InferenceParameters,
) -> Result< ( crate::streaming_control::ControlledStream, crate::streaming_control::ControlHandle ) >
{
let receiver = self.create_stream( inputs, model, parameters ).await?;
Ok( crate::streaming_control::wrap_stream( receiver ) )
}
}
// Basic implementation for when env-config is not available
#[ cfg( not( feature = "env-config" ) ) ]
impl< E > Inference< E >
where
E : Clone,
{
/// Create a new Inference API group
#[ inline ]
#[ must_use ]
pub fn new( client : &Client< E > ) -> Self
{
Self
{
client : (*client).clone(),
}
}
}
/// Helper function to convert chat completion response to inference response format
///
/// This maintains backward compatibility with existing code while using the new API
fn convert_chat_response_to_inference( chat_response : &ChatCompletionResponse ) -> Result< InferenceResponse >
{
// Extract the generated text from the first choice
let generated_text = chat_response.choices
.first()
.ok_or_else( || HuggingFaceError::Api( crate::error::ApiErrorWrap::new( "No choices in response".to_string() ) ) )?
.message
.content
.clone();
// Create inference output
let output = InferenceOutput
{
generated_text,
input_tokens : chat_response.usage.as_ref().map( | u | u.prompt_tokens ),
generated_tokens : chat_response.usage.as_ref().map( | u | u.completion_tokens ),
metadata : None,
};
// Create inference response
Ok( InferenceResponse::Single( output ) )
}
} // end mod private
crate::mod_interface!
{
exposed use
{
private::Inference,
};
}