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
517
518
519
520
521
522
523
524
525
526
//! Used to connect to the Prediction Guard API.
use std::{env, fmt, sync::Arc, time::Duration};
use dotenvy;
use eventsource_client::Client as EventClient;
use eventsource_client::SSE;
use futures::TryStreamExt;
use reqwest::{
ClientBuilder,
header::{HeaderMap, HeaderValue}, Response, StatusCode,
};
use serde::{Deserialize, Serialize};
use crate::{chat, completion, embedding, factuality, injection, pii, Result, toxicity, translate};
use crate::built_info;
const USER_AGENT: &str = "Prediction Guard Rust Client";
/// The base error that is returned from the API calls.
#[derive(Debug, Serialize, Deserialize, Clone, Default)]
pub struct ApiError {
#[serde(default)]
http_status: u16,
error: String,
}
impl fmt::Display for ApiError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_fmt(format_args!(
"http_status {}, error {}",
self.http_status, self.error
))
}
}
impl std::error::Error for ApiError {}
/// Prediction Guard Configuration
pub struct PgEnvironment {
pub key: String,
pub host: String,
}
impl PgEnvironment {
/// Specify Prediction Guard API configuration manually
///
/// ## Arguments:
///
/// * `key` - the Prediction Guard API key
/// * `host` - the Prediction Guard URL
pub fn new(key: String, host: String) -> Self {
Self { key, host }
}
/// Loads Prediction Guard API configuration from either
/// a `.env` file or environment variables. Expects to find
/// the `PGKEY` and `PGHOST` environment variables.
///
/// Returns an error if the environment variables are not found.
pub fn from_env() -> Result<Self> {
let _ = dotenvy::dotenv(); // Ignoring error - it's ok to not have .env files
Ok(Self {
key: env::var("PGKEY")?,
host: env::var("PGHOST")?,
})
}
}
/// Handles the connectivity to the Prediction Guard API. It is safe to be
/// used across threads.
#[derive(Debug, Clone)]
pub struct Client {
inner: Arc<ClientInner>,
}
#[derive(Debug)]
struct ClientInner {
server: String,
http_client: reqwest::Client,
headers: HeaderMap,
api_key: String,
}
impl Client {
/// Creates a new instance of client to be used with a particular Prediction Guard environment.
///
/// ## Arguments:
///
/// * `pg_env` - the prediction guard environment to connect to.
pub fn new(pg_env: PgEnvironment) -> Result<Self> {
let user_agent = format!("{} v{}", USER_AGENT, built_info::PKG_VERSION);
let http = ClientBuilder::new()
.connect_timeout(Duration::new(15, 0))
.read_timeout(Duration::new(30, 0))
.timeout(Duration::new(45, 0))
.user_agent(user_agent)
.build()?;
let header_key = match HeaderValue::from_str(&pg_env.key) {
Ok(x) => x,
Err(e) => {
return Err(Box::new(e));
}
};
let mut header_map = HeaderMap::new();
let _ = header_map
.insert("x-api-key", header_key)
.ok_or("invalid api key");
let inner = Arc::new(ClientInner {
server: pg_env.host.to_string(),
http_client: http,
headers: header_map,
api_key: pg_env.key,
});
Ok(Self { inner })
}
/// Calls the health endpoint.
///
/// Returns the text response from the server. A 200 (Ok) status code is expected from
/// Prediction Guard api. Any other status code is considered an error.
pub async fn check_health(&self) -> Result<Option<String>> {
let result = self
.inner
.http_client
.get(&self.inner.server)
.headers(self.inner.headers.clone())
.send()
.await?;
if result.status() != StatusCode::OK {
return Err(retrieve_error(result).await);
}
let txt = result.text().await?;
Ok(Some(txt))
}
/// Calls the embedding endpoint.
///
/// ## Arguments:
///
/// * `req` - An instance of [`embedding::Request`]
///
/// Returns a [`embedding::Response`]. A 200 (Ok) status code is expected from the Prediction Guard api. Any other status code
/// is considered an error.
pub async fn embedding(&self, req: &embedding::Request) -> Result<Option<embedding::Response>> {
let url = format!("{}{}", &self.inner.server, embedding::PATH);
let result = self
.inner
.http_client
.post(url)
.headers(self.inner.headers.clone())
.json(req)
.send()
.await?;
if result.status() != StatusCode::OK {
return Err(retrieve_error(result).await);
}
let embed_response = result.json::<embedding::Response>().await?;
Ok(Some(embed_response))
}
/// Calls the generate completion endpoint.
///
/// ## Arguments:
///
/// * `req` - An instance of [`completion::Request`]
///
/// Returns a [`completion::Response`]. A 200 (Ok) status code is expected from the Prediction Guard api. Any other status code
/// is considered an error.
pub async fn generate_completion(
&self,
req: &completion::Request,
) -> Result<Option<completion::Response>> {
let url = format!("{}{}", &self.inner.server, completion::PATH);
let result = self
.inner
.http_client
.post(url)
.headers(self.inner.headers.clone())
.json(req)
.send()
.await?;
if result.status() != StatusCode::OK {
return Err(retrieve_error(result).await);
}
let comp_response = result.json::<completion::Response>().await?;
Ok(Some(comp_response))
}
/// Calls the generate chat completion endpoint.
///
/// ## Arguments:
///
/// * `req` - An instance of [`chat::Request::<Message>`]
///
/// Returns an instance of [`chat::Response`]. A 200 (Ok) status code is expected from the Prediction Guard api. Any other status code
/// is considered an error.
pub async fn generate_chat_completion(
&self,
req: &chat::Request<chat::Message>,
) -> Result<Option<chat::Response>> {
let url = format!("{}{}", &self.inner.server, chat::PATH);
let result = self
.inner
.http_client
.post(url)
.headers(self.inner.headers.clone())
.json(req)
.send()
.await?;
if result.status() != StatusCode::OK {
return Err(retrieve_error(result).await);
}
let chat_response = result.json::<chat::Response>().await?;
Ok(Some(chat_response))
}
/// Calls the generate chat completion endpoint.
///
/// ## Arguments:
///
/// * `req` - An instance of [`chat::Request::<Message>`]
/// * `event_handler` - Event handler function that is called when a server side event is raised.
///
/// Returns an instance of [`chat::Response`].
///
/// The generated text is returned via events from the server. The event handler function gets called
/// every time the client receives an event response with data. Once the server terminates the events the call returns.
/// The entire [`chat::Response`] response is then returned to the caller.
///
/// A 200 (Ok) status code is expected from the Prediction Guard api. Any other status code
/// is considered an error.
pub async fn generate_chat_completion_events<F>(
&self,
req: &mut chat::Request<chat::Message>,
event_handler: &mut F,
) -> Result<Option<chat::ResponseEvents>>
where
F: FnMut(&String),
{
let url = format!("{}{}", &self.inner.server, chat::PATH);
req.stream = true;
let body = serde_json::to_string(&req)?;
let user_agent = format!("{} v{}", USER_AGENT, built_info::PKG_VERSION);
let client = eventsource_client::ClientBuilder::for_url(&url)?
.header("user-agent", user_agent.as_str())?
.header("x-api-key", &self.inner.api_key)?
.method("POST".to_string())
.body(body)
.build();
// TODO: Add Timeouts
let mut stream = Box::pin(client.stream());
loop {
match stream.try_next().await {
Ok(Some(event)) => {
match event {
SSE::Comment(_) => continue,
SSE::Event(evt) => {
// Check for [DONE]
if evt.data == "[DONE]" {
return Ok(None);
}
// JSON Response
let resp: chat::ResponseEvents = match serde_json::from_str(&evt.data) {
Ok(v) => v,
Err(e) => {
return Err(Box::from(ApiError {
http_status: 500,
error: format!("error parsing stream response: {}", e),
}));
}
};
if resp.choices.is_empty() {
// No data to stream or Done
continue;
}
// Finish Reason == Stop That is the final Response.
if resp.choices[0].finish_reason == Some("stop".to_string()) {
return Ok(Some(resp));
}
let msg = resp.choices[0].delta.clone().content;
event_handler(&msg);
}
}
}
Ok(None) => continue,
Err(e) => match e {
eventsource_client::Error::StreamClosed => break,
_ => return Err(stream_error_into_api_err(e).await),
},
}
}
Ok(None)
}
/// Calls the generate chat completion endpoint for chat vision.
///
/// ## Arguments:
///
/// * `req` - An instance of [`chat::Request::<MessageVision>`]
///
/// Returns an instance of [`chat::Response`]. A 200 (Ok) status code is expected from the Prediction Guard api. Any other status code
/// is considered an error.
pub async fn generate_chat_vision(
&self,
req: &chat::Request<chat::MessageVision>,
) -> Result<Option<chat::Response>> {
let url = format!("{}{}", &self.inner.server, chat::PATH);
let result = self
.inner
.http_client
.post(url)
.headers(self.inner.headers.clone())
.json(req)
.send()
.await?;
if result.status() != StatusCode::OK {
return Err(retrieve_error(result).await);
}
let chat_response = result.json::<chat::Response>().await?;
Ok(Some(chat_response))
}
/// Calls the factuality check endpoint.
///
/// ## Arguments:
///
/// * `req` - An instance of [`factuality::Request`]
///
/// Returns am instance of [`factuality::Response`]. A 200 (Ok) status code is expected from the Prediction Guard api. Any other status code
/// is considered an error.
pub async fn check_factuality(
&self,
req: &factuality::Request,
) -> Result<Option<factuality::Response>> {
let url = format!("{}{}", &self.inner.server, factuality::PATH);
let result = self
.inner
.http_client
.post(url)
.headers(self.inner.headers.clone())
.json(req)
.send()
.await?;
if result.status() != StatusCode::OK {
return Err(retrieve_error(result).await);
}
let fact_response = result.json::<factuality::Response>().await?;
Ok(Some(fact_response))
}
/// Calls the translate endpoint.
///
/// ## Arguments:
///
/// `req` - Instance of [`translate::Request`]
///
/// Returns a [`translate::Response`]. A 200 (Ok) status code is expected from the Prediction Guard api. Any other status code
/// is considered an error.
pub async fn translate(&self, req: &translate::Request) -> Result<Option<translate::Response>> {
let url = format!("{}{}", &self.inner.server, translate::PATH);
let result = self
.inner
.http_client
.post(url)
.headers(self.inner.headers.clone())
.json(req)
.send()
.await?;
if result.status() != StatusCode::OK {
return Err(retrieve_error(result).await);
}
let translate_response = result.json::<translate::Response>().await?;
Ok(Some(translate_response))
}
/// Calls the PII endpoint that is used to remove/detect PII information in the request.
///
/// ## Arguments:
///
/// `req` - An instance of [`pii::Request`]
///
/// Returns an instance of [`pii::Response`]. A 200 (Ok) status code is expected from the Prediction Guard api.
/// Any other status code is considered an error.
pub async fn pii(&self, req: &pii::Request) -> Result<Option<pii::Response>> {
let url = format!("{}{}", &self.inner.server, pii::PATH);
let result = self
.inner
.http_client
.post(url)
.headers(self.inner.headers.clone())
.json(req)
.send()
.await?;
if result.status() != StatusCode::OK {
return Err(retrieve_error(result).await);
}
let pii_response = result.json::<pii::Response>().await?;
Ok(Some(pii_response))
}
/// Calls the injection check endpoint.
///
/// ## Arguments:
///
/// `req` - Instance of [`injection::Request`]
///
/// Returns an instance of [`injection::Response`]. A 200 (Ok) status code is expected from the Prediction Guard api. Any other status code
/// is considered an error.
pub async fn injection(&self, req: &injection::Request) -> Result<Option<injection::Response>> {
let url = format!("{}{}", &self.inner.server, injection::PATH);
let result = self
.inner
.http_client
.post(url)
.headers(self.inner.headers.clone())
.json(req)
.send()
.await?;
if result.status() != StatusCode::OK {
return Err(retrieve_error(result).await);
}
let injection_response = result.json::<injection::Response>().await?;
Ok(Some(injection_response))
}
/// Calls the injection check endpoint.
///
/// ## Arguments:
///
/// `req` - An instance of [`toxicity::Request`]
///
/// Returns an instance of [`toxicity::Response`]. A 200 (Ok) status code is expected from the Prediction Guard api. Any other status code
/// is considered an error.
pub async fn toxicity(&self, req: &toxicity::Request) -> Result<Option<toxicity::Response>> {
let url = format!("{}{}", &self.inner.server, toxicity::PATH);
let result = self
.inner
.http_client
.post(url)
.headers(self.inner.headers.clone())
.json(req)
.send()
.await?;
if result.status() != StatusCode::OK {
return Err(retrieve_error(result).await);
}
let toxicity_response = result.json::<toxicity::Response>().await?;
Ok(Some(toxicity_response))
}
}
async fn retrieve_error(resp: Response) -> Box<dyn std::error::Error> {
let code = resp.status().as_u16();
let mut err = match resp.json::<ApiError>().await {
Ok(x) => x,
Err(e) => return Box::from(format!("error parsing error response, {}", e)),
};
err.http_status = code;
err.into()
}
async fn stream_error_into_api_err(err: eventsource_client::Error) -> Box<dyn std::error::Error> {
let msg = format!("{}", err);
Box::from(ApiError {
http_status: 500,
error: msg.to_string(),
})
}