Getting Started

SDKs

We provide official open-source SDKs (client libraries) for your favorite platforms. These clients make connecting to our API faster and help avoid errors.

Official SDK Documentation

Request a New SDK

If you’d like to request an SDK for a language that we don’t currently support, please reach out to us on Discord. We prioritize languages based on demand.

Access Tokens

To access the API, contact us here to get an access token.

SDK 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,
7# or when creating the client object
8os.environ["PREDICTIONGUARD_API_KEY"]
9
10client = PredictionGuard(
11 api_key="<your Prediction Guard API Key>"
12)
13
14messages = [
15 {
16 "role": "system",
17 "content": "You are a helpful chatbot that helps people learn."
18 },
19 {
20 "role": "user",
21 "content": "What is a good way to learn to code?"
22 }
23]
24
25result = client.chat.completions.create(
26 model="Hermes-2-Pro-Llama-3-8B",
27 messages=messages,
28 max_tokens=100
29)
30
31print(json.dumps(
32 result,
33 sort_keys=True,
34 indent=4,
35 separators=(',', ': ')
36))

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('https://api.predictionguard.com', process.env.PGKEY);
4
5async function Chat() {
6 const input = {
7 model: pg.Models.NeuralChat7B,
8 messages: [
9 {
10 role: pg.Roles.User,
11 content: 'How do you feel about the world in general',
12 },
13 ],
14 maxTokens: 1000,
15 temperature: 0.1,
16 topP: 0.1,
17 topK: 50.0,
18 options: {
19 factuality: true,
20 toxicity: true,
21 pii: pg.PIIs.Replace,
22 piiReplaceMethod: pg.ReplaceMethods.Random,
23 },
24 };
25
26 var [result, err] = await client.Chat(input);
27 if (err != null) {
28 console.log('ERROR:' + err.error);
29 return;
30 }
31
32 console.log('RESULT:' + result.createdDate() + ': ' + result.model + ': ' + result.choices[0].message.content);
33}
34
35Chat();

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 "os"
8 "time"
9
10 "github.com/predictionguard/go-client"
11)
12
13func main() {
14 if err := run(); err != nil {
15 log.Fatalln(err)
16 }
17}
18
19func run() error {
20 host := "https://api.predictionguard.com"
21 apiKey := os.Getenv("PGKEY")
22
23 logger := func(ctx context.Context, msg string, v ...any) {
24 s := fmt.Sprintf("msg: %s", msg)
25 for i := 0; i < len(v); i = i + 2 {
26 s = s + fmt.Sprintf(", %s: %v", v[i], v[i+1])
27 }
28 log.Println(s)
29 }
30
31 cln := client.New(logger, host, apiKey)
32
33 ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
34 defer cancel()
35
36 input := client.ChatInput{
37 Model: client.Models.NeuralChat7B,
38 Messages: []client.ChatInputMessage{
39 {
40 Role: client.Roles.User,
41 Content: "How do you feel about the world in general",
42 },
43 },
44 MaxTokens: 1000,
45 Temperature: 0.1,
46 TopP: 0.1,
47 TopK: 50.0,
48 Options: &client.ChatInputOptions{
49 Factuality: true,
50 Toxicity: true,
51 PII: client.PIIs.Replace,
52 PIIReplaceMethod: client.ReplaceMethods.Random,
53 },
54 }
55
56 resp, err := cln.Chat(ctx, input)
57 if err != nil {
58 return fmt.Errorf("ERROR: %w", err)
59 }
60
61 fmt.Println(resp.Choices[0].Message.Content)
62
63 return nil
64}

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

More Rust Examples

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