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-2-Pro-Llama-3-8B",
26 messages=messages,
27 max_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.NeuralChat7B,
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 "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("PREDICTIONGUARD_API_KEY")
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
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::NeuralChat7B)
12 .add_message(
13 chat::Roles::User,
14 "How do you feel about the world in general?".to_string(),
15 )
16 .max_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.