rag-toolchain 0.1.9

is a Rust native library designed to empower developers with seamless access to common Gen AI workflows.
Documentation
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
use futures::StreamExt;
use reqwest_eventsource::{Event, EventSource};
use serde_json::{Map, Value};
use std::env::VarError;

use crate::clients::open_ai::model::chat_completions::{
    ChatCompletionChoices, ChatCompletionRequest, ChatCompletionResponse, OpenAIModel,
};
use crate::clients::open_ai::open_ai_core::OpenAIHttpClient;
use crate::clients::{
    AsyncChatClient, AsyncStreamedChatClient, ChatCompletionStream, PromptMessage,
};

use super::model::chat_completions::{
    ChatCompletionDelta, ChatCompletionStreamedResponse, ChatMessage,
};

use super::model::errors::OpenAIError;

/// # [`OpenAIChatCompletionClient`]
/// Allows for interacting with open ai models
/// # Examples
/// ```
/// use rag_toolchain::common::*;
/// use rag_toolchain::clients::*;
/// use serde_json::Map;
/// use serde_json::Value;
/// async fn generate_completion() {
///     let model: OpenAIModel = OpenAIModel::Gpt3Point5Turbo;
///     let mut additional_config: Map<String, Value> = Map::new();
///     additional_config.insert("temperature".into(), 0.5.into());
///
///     let client: OpenAIChatCompletionClient =
///         OpenAIChatCompletionClient::try_new_with_additional_config(model, additional_config).unwrap();
///     let system_message: PromptMessage = PromptMessage::SystemMessage(
///         "You are a comedian that cant ever reply to someone unless its phrased as a sarcastic joke"
///         .into());
///     let user_message: PromptMessage =
///     PromptMessage::HumanMessage("What is the weather like today ?".into());
///     let reply = client
///         .invoke(vec![system_message, user_message])
///         .await
///         .unwrap();
///     println!("{:?}", reply.content());
/// }
/// ```
/// # Required Environment Variables
/// OPENAI_API_KEY: The API key to use for the OpenAI API
pub struct OpenAIChatCompletionClient {
    url: String,
    client: OpenAIHttpClient,
    model: OpenAIModel,
    additional_config: Option<Map<String, Value>>,
}

impl OpenAIChatCompletionClient {
    const OPENAI_CHAT_COMPLETIONS_URL: &str = "https://api.openai.com/v1/chat/completions";

    /// # [`OpenAIChatCompletionClient::try_new`]
    ///
    /// This method creates a new OpenAIChatCompletionClient. All inference parameters used
    /// will be the default ones provided by the OpenAI API.
    ///
    /// # Arguments
    /// * `model`: [`OpenAIModel`] - The model to use for the chat completion.
    ///
    /// # Errors
    /// * [`VarError`] - if the OPENAI_API_KEY environment variable is not set.
    ///
    /// # Returns
    /// * [`OpenAIChatCompletionClient`] - the chat completion client.
    pub fn try_new(model: OpenAIModel) -> Result<OpenAIChatCompletionClient, VarError> {
        let client: OpenAIHttpClient = OpenAIHttpClient::try_new()?;
        Ok(OpenAIChatCompletionClient {
            url: Self::OPENAI_CHAT_COMPLETIONS_URL.into(),
            client,
            model,
            additional_config: None,
        })
    }

    /// # [`OpenAIChatCompletionClient::try_new_with_additional_config`]
    ///
    /// This method creates a new OpenAIChatCompletionClient. All inference parameters provided
    /// in the additional_config will be used in the chat completion request. an example of this
    /// could be 'temperature', 'top_p', 'seed' etc.
    ///
    /// # Forbidden Properties
    /// * "stream": this cannot be set as it is used internally by the client.
    /// * "n": n can be set but will result in wasted tokens as the client is built for single
    ///        chat completions. We intend to add support for multiple completions in the future.
    ///
    /// # Arguments
    /// * `model`: [`OpenAIModel`] - The model to use for the chat completion.
    /// * `additional_config`: [`Map<String, Value>`] - The additional configuration to use for the chat completion.
    ///
    /// # Errors
    /// * [`VarError`] - if the OPENAI_API_KEY environment variable is not set.
    ///
    /// # Returns
    /// * [`OpenAIChatCompletionClient`] - the chat completion client.
    pub fn try_new_with_additional_config(
        model: OpenAIModel,
        additional_config: Map<String, Value>,
    ) -> Result<OpenAIChatCompletionClient, VarError> {
        let client: OpenAIHttpClient = OpenAIHttpClient::try_new()?;
        Ok(OpenAIChatCompletionClient {
            url: Self::OPENAI_CHAT_COMPLETIONS_URL.into(),
            client,
            model,
            additional_config: Some(additional_config),
        })
    }

    /// # [`OpenAIChatCompletionClient::try_new_with_url`]
    ///
    /// This method creates a new OpenAIChatCompletionClient. All inference parameters used
    /// will be the default ones provided by the OpenAI API. You can pass the url in directly
    /// # Arguments
    /// * `model`: [`OpenAIModel`] - The model to use for the chat completion.
    /// * `url`: [`String`] - The url to use for the api call.
    ///
    /// # Errors
    /// * [`VarError`] - if the OPENAI_API_KEY environment variable is not set.
    ///
    /// # Returns
    /// * [`OpenAIChatCompletionClient`] - the chat completion client.
    pub fn try_new_with_url(
        model: OpenAIModel,
        url: String,
    ) -> Result<OpenAIChatCompletionClient, VarError> {
        let client: OpenAIHttpClient = OpenAIHttpClient::try_new()?;
        Ok(OpenAIChatCompletionClient {
            url,
            client,
            model,
            additional_config: None,
        })
    }

    /// # [`OpenAIChatCompletionClient::try_new_with_url_and_additional_config`]
    ///
    /// This method creates a new OpenAIChatCompletionClient. All inference parameters provided
    /// in the additional_config will be used in the chat completion request. an example of this
    /// could be 'temperature', 'top_p', 'seed' etc. You can pass the url in directly.
    ///
    /// # Forbidden Properties
    /// * "stream": this cannot be set as it is used internally by the client.
    /// * "n": n can be set but will result in wasted tokens as the client is built for single
    ///        chat completions. We intend to add support for multiple completions in the future.
    ///
    /// # Arguments
    /// * `model`: [`OpenAIModel`] - The model to use for the chat completion.
    /// * `url`: [`String`] - The url to use for the api call.
    /// * `additional_config`: [`Map<String, Value>`] - The additional configuration to use for the chat completion.
    ///
    /// # Errors
    /// * [`VarError`] - if the OPENAI_API_KEY environment variable is not set.
    ///
    /// # Returns
    /// * [`OpenAIChatCompletionClient`] - the chat completion client.
    pub fn try_new_with_url_and_additional_config(
        model: OpenAIModel,
        url: String,
        additional_config: Map<String, Value>,
    ) -> Result<OpenAIChatCompletionClient, VarError> {
        let client: OpenAIHttpClient = OpenAIHttpClient::try_new()?;
        Ok(OpenAIChatCompletionClient {
            url,
            client,
            model,
            additional_config: Some(additional_config),
        })
    }
}

impl AsyncChatClient for OpenAIChatCompletionClient {
    type ErrorType = OpenAIError;

    /// # [`OpenAIChatCompletionClient::invoke`]
    ///
    /// function to execute the ChatCompletion given a list of prompt messages.
    ///
    /// # Arguments
    /// * `prompt_messages`: [`Vec<PromptMessage>`] - the list of prompt messages that will be sent to the LLM.
    ///
    /// # Errors
    /// * [`OpenAIError`] - if the chat client invocation fails.
    ///
    /// # Returns
    /// [`PromptMessage::AIMessage`] - the response from the chat client.
    async fn invoke(
        &self,
        prompt_messages: Vec<PromptMessage>,
    ) -> Result<PromptMessage, Self::ErrorType> {
        let mapped_messages: Vec<ChatMessage> =
            prompt_messages.into_iter().map(ChatMessage::from).collect();

        let body: ChatCompletionRequest = ChatCompletionRequest {
            model: self.model,
            messages: mapped_messages,
            stream: false,
            additional_config: self.additional_config.clone(),
        };

        let response: ChatCompletionResponse = self.client.send_request(body, &self.url).await?;
        let choices: Vec<ChatCompletionChoices> = response.choices;
        let messages: Vec<PromptMessage> = choices
            .into_iter()
            .map(|x| PromptMessage::from(x.message))
            .collect();

        Ok(messages[0].clone())
    }
}

impl AsyncStreamedChatClient for OpenAIChatCompletionClient {
    type ErrorType = OpenAIError;
    type Item = OpenAICompletionStream;

    /// # [`OpenAIChatCompletionClient::invoke_stream`]
    ///
    /// function to execute the ChatCompletion given a list of prompt messages.
    ///
    /// # Arguments
    /// * `prompt_messages`: [`PromptMessage`] - the list of prompt messages that will be sent to the LLM.
    ///
    /// # Errors
    /// * [`OpenAIError`] - if the chat client invocation fails.
    ///
    /// # Returns
    /// impl [`ChatCompletionStream`] - the response from the chat client.
    async fn invoke_stream(
        &self,
        prompt_messages: Vec<PromptMessage>,
    ) -> Result<Self::Item, Self::ErrorType> {
        let mapped_messages: Vec<ChatMessage> =
            prompt_messages.into_iter().map(ChatMessage::from).collect();

        let body: ChatCompletionRequest = ChatCompletionRequest {
            model: self.model,
            messages: mapped_messages,
            stream: true,
            additional_config: self.additional_config.clone(),
        };

        let event_source: EventSource = self.client.send_stream_request(body, &self.url).await?;
        Ok(OpenAICompletionStream::new(event_source))
    }
}

/// [`OpenAICompletionStream`]
///
/// This structs wraps the EventSource and parses returned
/// messages into prompt messages on demand.
pub struct OpenAICompletionStream {
    event_source: EventSource,
}

/// [`CompletionStreamValue`]
///
/// Value returned from each iteration of the stream.
/// Given we wanted to represent connecting as a non-failure
/// state we had to create a new enum to represent this.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CompletionStreamValue {
    Connecting,
    Message(PromptMessage),
}

impl OpenAICompletionStream {
    const STOP_MESSAGE: &'static str = "[DONE]";

    /// # [`OpenAICompletionStream::new`]
    ///
    /// This struct just wraps the EventSource when from the
    /// context of streaming chat completions.
    pub fn new(event_source: EventSource) -> Self {
        Self { event_source }
    }

    /// # [`ChatCompletionStream::parse_message`]
    ///
    /// Helper method to deserialize the raw response message from the event source.
    ///
    /// # Arguments
    /// * `msg`: &[`str`] - the raw response from the event source.
    ///
    /// # Errors
    /// * [`OpenAIError`] - if the chat client invocation fails.
    ///
    /// # Returns
    /// * [`Option<Result<CompletionStreamValue, OpenAIError>`] - the response from the chat client.
    ///         None represents the stream is finished. and Some(Err) represents an error.
    ///
    fn parse_message(msg: &str) -> Option<Result<CompletionStreamValue, OpenAIError>> {
        let response: ChatCompletionStreamedResponse = match serde_json::from_str(msg) {
            Ok(response) => response,
            Err(e) => {
                return Some(Err(OpenAIError::ErrorDeserializingResponseBody(
                    200,
                    e.to_string(),
                )));
            }
        };
        let chat_message: ChatCompletionDelta = response.choices.first().unwrap().delta.clone();
        match chat_message.content {
            Some(msg) => {
                let prompt_message: PromptMessage = PromptMessage::AIMessage(msg);
                Some(Ok(CompletionStreamValue::Message(prompt_message)))
            }
            None => None,
        }
    }
}

impl ChatCompletionStream for OpenAICompletionStream {
    type ErrorType = OpenAIError;
    type Item = CompletionStreamValue;

    /// # [`ChatCompletionStream::next`]
    ///
    /// Method to iterate over the completion stream. Note it blocks
    /// until the next message is received. At which point it will
    /// parse the response into a [`CompletionStreamValue`].
    ///
    /// # Examples
    /// ```
    /// use rag_toolchain::clients::*;
    ///
    /// async fn stream_chat_completions(client: OpenAIChatCompletionClient) {
    ///     let user_message: PromptMessage = PromptMessage::HumanMessage("Please ask me a question".into());
    ///     let mut stream: OpenAICompletionStream = client.invoke_stream(vec![user_message]).await.unwrap();
    ///     while let Some(response) = stream.next().await {
    ///         match response {
    ///            Ok(CompletionStreamValue::Connecting) => {},
    ///            Ok(CompletionStreamValue::Message(msg)) => {
    ///                 println!("{:?}", msg.content());
    ///            }
    ///            Err(e) => {
    ///                 println!("{:?}", e);
    ///                 break;
    ///            }
    ///         }
    ///     }
    /// }
    /// ```
    ///
    /// # Errors
    /// * [`OpenAIError::ErrorReadingStream`] - if there was an error reading from the stream.
    /// * [`OpenAIError::ErrorDeserializingResponseBody`] - if there was an error deserializing the response body.
    ///
    /// # Returns
    /// * [`Option<Result<CompletionStreamValue, OpenAIError>>`] - the response from the chat client.
    ///         None represents the stream is finished..
    async fn next(&mut self) -> Option<Result<Self::Item, Self::ErrorType>> {
        let event: Result<Event, reqwest_eventsource::Error> = self.event_source.next().await?;

        let event: Event = match event {
            Ok(event) => event,
            Err(e) => {
                self.event_source.close();
                return Some(Err(OpenAIError::ErrorReadingStream(e.to_string())));
            }
        };

        match event {
            Event::Message(msg) => {
                if msg.data == Self::STOP_MESSAGE {
                    self.event_source.close();
                    None
                } else {
                    Self::parse_message(&msg.data)
                }
            }
            Event::Open => Some(Ok(CompletionStreamValue::Connecting)),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use mockito::{Mock, Server, ServerGuard};

    const CHAT_COMPLETION_RESPONSE: &'static str = r#"
    {
        "id": "chatcmpl-123",
        "object": "chat.completion",
        "created": 1677652288,
        "model": "gpt-3.5-turbo-0613",
        "system_fingerprint": "fp_44709d6fcb",
        "choices": [{
          "index": 0,
          "message": {
            "role": "assistant",
            "content": "Hello there, how may I assist you today?"
          },
          "logprobs": null,
          "finish_reason": "stop"
        }],
        "usage": {
          "prompt_tokens": 9,
          "completion_tokens": 12,
          "total_tokens": 21
        }
    }
    "#;

    const ERROR_RESPONSE: &'static str = r#"
    {
        "error": {
            "message": "Incorrect API key provided: fdas. You can find your API key at https://platform.openai.com/account/api-keys.",
            "type": "invalid_request_error",
            "param": null,
            "code": "invalid_api_key"
        }
    }
    "#;

    const STREAMED_CHAT_COMPLETION_RESPONSE: &'static str = "id:1\ndata:{\"id\":\"chatcmpl-9BRO0Nnca1ZtfMkFc5tOpQNSJ2Eo0\",\"object\":\"chat.completion.chunk\",\"created\":1712513908,\"model\":\"gpt-3.5-turbo-0125\",\"system_fingerprint\":\"fp_b28b39ffa8\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":\"Hello\"},\"logprobs\":null,\"finish_reason\":null}]}\n\ndata:[DONE]\n\n";

    #[test]
    fn try_new_with_env_var_succeeds() {
        std::env::set_var("OPENAI_API_KEY", "test");
        OpenAIChatCompletionClient::try_new(OpenAIModel::Gpt4o)
            .expect("Failed to create OpenAIChatCompletionClient");
    }

    #[test]
    fn try_new_with_additional_config_succeeds() {
        OpenAIChatCompletionClient::try_new_with_additional_config(OpenAIModel::Gpt4o, Map::new())
            .expect("Failed to create OpenAIChatCompletionClient");
    }

    #[tokio::test]
    async fn invoke_correct_response_succeeds() {
        let (client, mut server) = with_mocked_client(None).await;
        let mock = with_mocked_request(&mut server, 200, CHAT_COMPLETION_RESPONSE);
        let prompt = PromptMessage::HumanMessage("Please ask me a question".into());
        let response = client.invoke(vec![prompt]).await.unwrap();
        let expected_response =
            PromptMessage::AIMessage("Hello there, how may I assist you today?".into());
        mock.assert();
        assert_eq!(expected_response, response);
    }

    #[tokio::test]
    async fn invoke_error_response_maps_correctly() {
        let (client, mut server) = with_mocked_client(Some(Map::new())).await;
        let mock = with_mocked_request(&mut server, 401, ERROR_RESPONSE);
        let prompt = PromptMessage::HumanMessage("Please ask me a question".into());
        let response = client.invoke(vec![prompt]).await.unwrap_err();
        let error_body = serde_json::from_str(ERROR_RESPONSE).unwrap();
        let expected_response = OpenAIError::CODE401(error_body);
        mock.assert();
        assert_eq!(expected_response, response);
    }

    #[tokio::test]
    async fn invoke_stream_correct_response_succeeds() {
        let (client, mut server) = with_mocked_client(None).await;
        let mock = server
            .mock("POST", "/")
            .with_status(200)
            .with_header("Content-Type", "text/event-stream")
            .with_body(STREAMED_CHAT_COMPLETION_RESPONSE)
            .create();
        let prompt = PromptMessage::HumanMessage("Please ask me a question".into());
        let mut stream = client.invoke_stream(vec![prompt]).await.unwrap();
        let value1 = stream.next().await.unwrap().unwrap();
        assert_eq!(value1, CompletionStreamValue::Connecting);
        let value2 = stream.next().await.unwrap().unwrap();
        assert_eq!(
            value2,
            CompletionStreamValue::Message(PromptMessage::AIMessage("Hello".into()))
        );
        let value3 = stream.next().await;
        assert_eq!(value3, None);
        mock.assert();
    }

    // Method which mocks the response the server will give. this
    // allows us to stub the requests instead of sending them to OpenAI
    fn with_mocked_request(
        server: &mut ServerGuard,
        status_code: usize,
        response_body: &str,
    ) -> Mock {
        server
            .mock("POST", "/")
            .with_status(status_code)
            .with_header("Content-Type", "application/json")
            .with_body(response_body)
            .create()
    }

    // This methods returns a client which is pointing at the mocked url
    // and the mock server which we can orchestrate the stubbings on.
    async fn with_mocked_client(
        config: Option<Map<String, Value>>,
    ) -> (OpenAIChatCompletionClient, ServerGuard) {
        std::env::set_var("OPENAI_API_KEY", "fake key");
        let server = Server::new_async().await;
        let url = server.url();
        let model = OpenAIModel::Gpt3Point5Turbo;
        let client = match config {
            Some(config) => OpenAIChatCompletionClient::try_new_with_url_and_additional_config(
                model, url, config,
            )
            .unwrap(),
            None => OpenAIChatCompletionClient::try_new_with_url(model, url).unwrap(),
        };
        (client, server)
    }
}