Quick Start

Python Client

You can find the SDK docs and package information using this link.

Python SDK Docs

Python Code Example

copy
1import json
2import os
3
4from predictionguard import PredictionGuard
5
6# You can set you Prediction Guard API Key as an env variable named "PREDICTIONGUARD_API_KEY",
7# or when creating the client object
8
9client = PredictionGuard(
10 api_key="<api key>"
11)
12
13messages = [
14 {
15 "role": "system",
16 "content": "You are a helpful chatbot that helps people learn."
17 },
18 {
19 "role": "user",
20 "content": "What is a good way to learn to code?"
21 }
22]
23
24result = client.chat.completions.create(
25 model="Hermes-3-Llama-3.1-70B",
26 messages=messages,
27 max_completion_tokens=100
28)
29
30print(json.dumps(
31 result,
32 sort_keys=True,
33 indent=4,
34 separators=(',', ': ')
35))

More Python Examples

Take a look at the examples directory for more Python examples.


JS Client

You can find the SDK docs and package information using this link.

JS SDK Docs

JS Code Example

copy
1import * as pg from "predictionguard";
2
3const client = new pg.Client(
4 "https://api.predictionguard.com",
5 process.env.PREDICTIONGUARD_API_KEY
6);
7
8async function Chat() {
9 const input = {
10 model: pg.Models.Hermes3Llama3170B,
11 messages: [
12 {
13 role: pg.Roles.User,
14 content: "How do you feel about the world in general",
15 },
16 ],
17 maxTokens: 1000,
18 temperature: 0.1,
19 topP: 0.1,
20 topK: 50.0,
21 options: {
22 factuality: true,
23 toxicity: true,
24 pii: pg.PIIs.Replace,
25 piiReplaceMethod: pg.ReplaceMethods.Random,
26 },
27 };
28
29 var [result, err] = await client.Chat(input);
30 if (err != null) {
31 console.log("ERROR:" + err.error);
32 return;
33 }
34
35 console.log(
36 "RESULT:" +
37 result.createdDate() +
38 ": " +
39 result.model +
40 ": " +
41 result.choices[0].message.content
42 );
43}
44
45Chat();

More JS Examples

Take a look at the examples directory for more JS examples.


Go Client

You can find the SDK docs and package information using this link.

Go SDK Docs

Go Code Example

copy
1package main
2
3import (
4 "context"
5 "fmt"
6 "log"
7 "net/http"
8 "os"
9 "time"
10
11 "github.com/predictionguard/go-client/v2"
12)
13
14func main() {
15 if err := run(); err != nil {
16 log.Fatalln(err)
17 }
18}
19
20func run() error {
21 ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
22 defer cancel()
23
24 logger := func(ctx context.Context, msg string, v ...any) {
25 s := fmt.Sprintf("msg: %s", msg)
26 for i := 0; i < len(v); i = i + 2 {
27 s = s + fmt.Sprintf(", %s: %v", v[i], v[i+1])
28 }
29 log.Println(s)
30 }
31
32 cln := client.New(logger, os.Getenv("PREDICTIONGUARD_API_KEY"))
33
34 // -------------------------------------------------------------------------
35
36 d := client.D{
37 "model": "Hermes-3-Llama-3.1-70B",
38 "messages": "How do you feel about the world in general",
39 "max_tokens": 1000,
40 "temperature": 0.1,
41 "top_p": 0.1,
42 "top_k": 50,
43 "input": client.D{
44 "pii": client.PIIs.Replace,
45 "pii_replace_method": client.ReplaceMethods.Random,
46 },
47 "output": client.D{
48 "factuality": true,
49 "toxicity": true,
50 },
51 }
52
53 // -------------------------------------------------------------------------
54
55 const url = "https://api.predictionguard.com/chat/completions"
56
57 var resp client.Chat
58 if err := cln.Do(ctx, http.MethodPost, url, d, &resp); err != nil {
59 return fmt.Errorf("do: %w", err)
60 }
61
62 fmt.Println(resp.Choices[0].Message)
63
64 return nil
65}

More Go Examples

Take a look at the examples directory for more Go examples.


Rust Client

You can find the SDK docs and package information using this link.

Rust SDK Docs

Rust Code Example

copy
1extern crate prediction_guard as pg_client;
2
3use pg_client::{chat, client, models};
4
5#[tokio::main]
6async fn main() {
7 let pg_env = client::PgEnvironment::from_env().expect("env keys");
8
9 let clt = client::Client::new(pg_env).expect("client value");
10
11 let req = chat::Request::<chat::Message>::new(models::Model::Hermes3Llama3170B)
12 .add_message(
13 chat::Roles::User,
14 "How do you feel about the world in general?".to_string(),
15 )
16 .max_completion_tokens(1000)
17 .temperature(0.85);
18
19 let result = clt
20 .generate_chat_completion(&req)
21 .await
22 .expect("error from generate chat completion");
23
24 println!("\nchat completion response:\n\n {:?}", result);
25}

More Rust Examples

Take a look at the examples directory for more Rust examples.