client

package module
v2.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 11, 2025 License: Apache-2.0 Imports: 15 Imported by: 0

README

Prediction Guard Go Client

CircleCI Go Report Card go.mod Go version

Copyright 2024 Prediction Guard bill@predictionguard.com

Description

This Module provides functionality developed to simplify interfacing with Prediction Guard API in Go.

Requirements

To access the API, contact us here to get an enterprise access token. You will need this access token to continue.

Usage
package main

import (
	"context"
	"fmt"
	"log"
	"net/http"
	"os"
	"time"

	"github.com/predictionguard/go-client/v2"
)

func main() {
	if err := run(); err != nil {
		log.Fatalln(err)
	}
}

func run() error {
	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()

	logger := func(ctx context.Context, msg string, v ...any) {
		s := fmt.Sprintf("msg: %s", msg)
		for i := 0; i < len(v); i = i + 2 {
			s = s + fmt.Sprintf(", %s: %v", v[i], v[i+1])
		}
		log.Println(s)
	}

	cln := client.New(logger, os.Getenv("PREDICTIONGUARD_API_KEY"))

	// -------------------------------------------------------------------------

	d := client.D{
		"model":       "neural-chat-7b-v3-3",
		"messages":    "How do you feel about the world in general",
		"max_tokens":  1000,
		"temperature": 0.1,
		"top_p":       0.1,
		"top_k":       50,
		"input": client.D{
			"pii":                client.PIIs.Replace,
			"pii_replace_method": client.ReplaceMethods.Random,
		},
		"output": client.D{
			"factuality": true,
			"toxicity":   true,
		},
	}

	// -------------------------------------------------------------------------

	const url = "https://api.predictionguard.com/chat/completions"

	var resp client.Chat
	if err := cln.Do(ctx, http.MethodPost, url, d, &resp); err != nil {
		return fmt.Errorf("do: %w", err)
	}

	fmt.Println(resp.Choices[0].Message)

	return nil
}

Take a look at the examples directory for more examples.

Docs

You can find the SDK and Prediction Guard docs using these links.

SDK Docs

PG API Docs

Getting started

Once you have your api key you can use the makefile to run curl commands for the different api endpoints. For example, make curl-injection will connect to the injection endpoint and return the injection response. The makefile also allows you to run the different examples such as make go-injection to run the Go injection example.

Licensing
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Copyright 2024 Prediction Guard

Documentation

Overview

Package client provides support to access the Prediction Guard API service.

Index

Examples

Constants

This section is empty.

Variables

View Source
var Capabilities = capabilitySet{
	ChatCompletion:     newCapability("chat-completion"),
	ChatWithImage:      newCapability("chat-with-image"),
	Completion:         newCapability("completion"),
	Embedding:          newCapability("embedding"),
	EmbeddingWithImage: newCapability("embedding-with-image"),
	Tokenize:           newCapability("tokenize"),
}

Capabilities represents the set of model capabilities.

View Source
var Directions = directionSet{
	Right: newDirection("Right"),
	Left:  newDirection("Left"),
}
View Source
var ErrUnauthorized = errors.New("api understands the request but refuses to authorize it")
View Source
var Languages = languageSet{
	Afrikanns:  newLanguage("afr"),
	Amharic:    newLanguage("amh"),
	Arabic:     newLanguage("ara"),
	Armenian:   newLanguage("hye"),
	Azerbaijan: newLanguage("aze"),
	Basque:     newLanguage("eus"),
	Belarusian: newLanguage("bel"),
	Bengali:    newLanguage("ben"),
	Bosnian:    newLanguage("bos"),
	Catalan:    newLanguage("cat"),
	Chechen:    newLanguage("che"),
	Cherokee:   newLanguage("chr"),
	Chinese:    newLanguage("zho"),
	Croatian:   newLanguage("hrv"),
	Czech:      newLanguage("ces"),
	Danish:     newLanguage("dan"),
	Dutch:      newLanguage("nld"),
	English:    newLanguage("eng"),
	Estonian:   newLanguage("est"),
	Fijian:     newLanguage("fij"),
	Filipino:   newLanguage("fil"),
	Finnish:    newLanguage("fin"),
	French:     newLanguage("fra"),
	Galician:   newLanguage("glg"),
	Georgian:   newLanguage("kat"),
	German:     newLanguage("deu"),
	Greek:      newLanguage("ell"),
	Gujarati:   newLanguage("guj"),
	Haitian:    newLanguage("hat"),
	Hebrew:     newLanguage("heb"),
	Hindi:      newLanguage("hin"),
	Hungarian:  newLanguage("hun"),
	Icelandic:  newLanguage("isl"),
	Indonesian: newLanguage("ind"),
	Irish:      newLanguage("gle"),
	Italian:    newLanguage("ita"),
	Japanese:   newLanguage("jpn"),
	Kannada:    newLanguage("kan"),
	Kazakh:     newLanguage("kaz"),
	Korean:     newLanguage("kor"),
	Latvian:    newLanguage("lav"),
	Lithuanian: newLanguage("lit"),
	Macedonian: newLanguage("mkd"),
	Malay1:     newLanguage("msa"),
	Malay2:     newLanguage("zlm"),
	Malayalam:  newLanguage("mal"),
	Maltese:    newLanguage("mlt"),
	Marathi:    newLanguage("mar"),
	Nepali:     newLanguage("nep"),
	Norwegian:  newLanguage("nor"),
	Persian:    newLanguage("fas"),
	Polish:     newLanguage("pol"),
	Portuguese: newLanguage("por"),
	Romanian:   newLanguage("ron"),
	Russian:    newLanguage("rus"),
	Samoan:     newLanguage("smo"),
	Serbian:    newLanguage("srp"),
	Slovak:     newLanguage("slk"),
	Slovenian:  newLanguage("slv"),
	Slavonic:   newLanguage("chu"),
	Spanish:    newLanguage("spa"),
	Swahili:    newLanguage("swh"),
	Swedish:    newLanguage("swe"),
	Tamil:      newLanguage("tam"),
	Telugu:     newLanguage("tel"),
	Thai:       newLanguage("tha"),
	Turkish:    newLanguage("tur"),
	Ukrainian:  newLanguage("ukr"),
	Urdu:       newLanguage("urd"),
	Welsh:      newLanguage("cym"),
	Vietnamese: newLanguage("vie"),
}
View Source
var PIIs = piiSet{
	Block:   newPII("block"),
	Replace: newPII("replace"),
}
View Source
var ReplaceMethods = replaceMethodSet{
	Random:   newReplaceMethod("random"),
	Fake:     newReplaceMethod("fake"),
	Category: newReplaceMethod("category"),
	Mask:     newReplaceMethod("mask"),
}
View Source
var Roles = roleSet{
	Assistant: newRole("assistant"),
	User:      newRole("user"),
	System:    newRole("system"),
}

Functions

func WithClient

func WithClient(http *http.Client) func(cln *Client)

Types

type Capability

type Capability struct {
	// contains filtered or unexported fields
}

func (Capability) Equal

func (c Capability) Equal(c2 Capability) bool

func (Capability) MarshalText

func (c Capability) MarshalText() ([]byte, error)

func (Capability) String

func (c Capability) String() string

func (*Capability) UnmarshalText

func (c *Capability) UnmarshalText(data []byte) error

type Chat

type Chat struct {
	ID      string       `json:"id"`
	Object  string       `json:"object"`
	Created Time         `json:"created"`
	Model   string       `json:"model"`
	Choices []ChatChoice `json:"choices"`
}

type ChatChoice

type ChatChoice struct {
	Index   int         `json:"index"`
	Message ChatMessage `json:"message"`
}

type ChatMessage

type ChatMessage struct {
	Role    string `json:"role"`
	Content string `json:"content"`
}

type ChatSSE

type ChatSSE struct {
	ID      string          `json:"id"`
	Object  string          `json:"object"`
	Created Time            `json:"created"`
	Model   string          `json:"model"`
	Choices []ChatSSEChoice `json:"choices"`
	Error   string          `json:"error"`
}

type ChatSSEChoice

type ChatSSEChoice struct {
	Index        int          `json:"index"`
	Delta        ChatSSEDelta `json:"delta"`
	Text         string       `json:"generated_text"`
	Probs        float32      `json:"logprobs"`
	FinishReason string       `json:"finish_reason"`
}

type ChatSSEDelta

type ChatSSEDelta struct {
	Content string `json:"content"`
}

type ChatVision

type ChatVision struct {
	ID      string             `json:"id"`
	Object  string             `json:"object"`
	Created Time               `json:"created"`
	Model   string             `json:"model"`
	Choices []ChatVisionChoice `json:"choices"`
}

ChatVision represents the result for the vision call.

type ChatVisionChoice

type ChatVisionChoice struct {
	Index   int               `json:"index"`
	Message ChatVisionMessage `json:"message"`
}

ChatVisionChoice represents a choice for the vision call.

type ChatVisionMessage

type ChatVisionMessage struct {
	Role    string `json:"role"`
	Content string `json:"content"`
}

type Client

type Client struct {
	// contains filtered or unexported fields
}

func New

func New(log Logger, apiKey string, options ...func(cln *Client)) *Client

func (*Client) Do

func (cln *Client) Do(ctx context.Context, method string, endpoint string, body D, v any) error
Example (Capability)
// examples/capability/main.go

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

logger := func(ctx context.Context, msg string, v ...any) {
	s := fmt.Sprintf("msg: %s", msg)
	for i := 0; i < len(v); i = i + 2 {
		s = s + fmt.Sprintf(", %s: %v", v[i], v[i+1])
	}
	log.Println(s)
}

cln := client.New(logger, os.Getenv("PREDICTIONGUARD_API_KEY"))

// -------------------------------------------------------------------------

url := "https://api.predictionguard.com/models/" + client.Capabilities.ChatCompletion.String()

var resp client.ModelResponse
if err := cln.Do(ctx, http.MethodGet, url, nil, &resp); err != nil {
	log.Fatalf("do: %s", err)
}

fmt.Println(resp)
Example (Chat)
// examples/chat/basic/main.go

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

logger := func(ctx context.Context, msg string, v ...any) {
	s := fmt.Sprintf("msg: %s", msg)
	for i := 0; i < len(v); i = i + 2 {
		s = s + fmt.Sprintf(", %s: %v", v[i], v[i+1])
	}
	log.Println(s)
}

cln := client.New(logger, os.Getenv("PREDICTIONGUARD_API_KEY"))

// -------------------------------------------------------------------------

d := client.D{
	"model":       "neural-chat-7b-v3-3",
	"messages":    "How do you feel about the world in general",
	"max_tokens":  1000,
	"temperature": 0.1,
	"top_p":       0.1,
	"top_k":       50,
	"input": client.D{
		"pii":                client.PIIs.Replace,
		"pii_replace_method": client.ReplaceMethods.Random,
	},
	"output": client.D{
		"factuality": true,
		"toxicity":   true,
	},
}

// -------------------------------------------------------------------------

const url = "https://api.predictionguard.com/chat/completions"

var resp client.Chat
if err := cln.Do(ctx, http.MethodPost, url, d, &resp); err != nil {
	log.Fatalf("do: %s", err)
}

fmt.Println(resp.Choices[0].Message)
Example (ChatSSE)
// examples/chat/sse/main.go

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

logger := func(ctx context.Context, msg string, v ...any) {
	s := fmt.Sprintf("msg: %s", msg)
	for i := 0; i < len(v); i = i + 2 {
		s = s + fmt.Sprintf(", %s: %v", v[i], v[i+1])
	}
	log.Println(s)
}

cln := client.NewSSE[client.ChatSSE](logger, os.Getenv("PREDICTIONGUARD_API_KEY"))

// -------------------------------------------------------------------------

d := client.D{
	"model": "neural-chat-7b-v3-3",
	"messages": []client.D{
		{
			"role":    "user",
			"content": "How do you feel about the world in general",
		},
	},
	"stream":      true,
	"max_tokens":  1000,
	"temperature": 0.1,
	"top_p":       0.1,
	"top_k":       50,
	"input": client.D{
		"pii":                client.PIIs.Replace,
		"pii_replace_method": client.ReplaceMethods.Random,
	},
}

// -------------------------------------------------------------------------

const url = "https://api.predictionguard.com/chat/completions"

ch := make(chan client.ChatSSE, 100)
if err := cln.Do(ctx, http.MethodPost, url, d, ch); err != nil {
	log.Fatalf("do: %s", err)
}

for resp := range ch {
	if resp.Error != "" {
		log.Fatalf(resp.Error)
	}

	for _, choice := range resp.Choices {
		fmt.Print(choice.Delta.Content)
	}
}
Example (ChatVision)
// examples/chat/vision/main.go

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

logger := func(ctx context.Context, msg string, v ...any) {
	s := fmt.Sprintf("msg: %s", msg)
	for i := 0; i < len(v); i = i + 2 {
		s = s + fmt.Sprintf(", %s: %v", v[i], v[i+1])
	}
	log.Println(s)
}

cln := client.New(logger, os.Getenv("PREDICTIONGUARD_API_KEY"))

// -------------------------------------------------------------------------

image, err := client.NewImageNetwork("https://static.wixstatic.com/media/f54603_b7882b876e2b47d3a38843a58a9829f1~mv2.png")
if err != nil {
	log.Fatalf("newimage: %s", err)
}

base64, err := image.EncodeBase64(ctx)
if err != nil {
	log.Fatalf("base64: %s", err)
}

d := client.D{
	"model": "llava-1.5-7b-hf",
	"messages": []client.D{
		{
			"role": client.Roles.User,
			"content": []client.D{
				{
					"type": "text",
					"text": "Is this a picture of a rose?",
				},
				{
					"type": "image_url",
					"image_url": client.D{
						"url": fmt.Sprintf("data:image/png;base64,%s", base64),
					},
				},
			},
		},
	},
	"max_tokens":  1000,
	"temperature": 0.1,
	"top_p":       0.1,
	"top_k":       50,
	"input": client.D{
		"pii":                client.PIIs.Replace,
		"pii_replace_method": client.ReplaceMethods.Random,
	},
	"output": client.D{
		"factuality": false,
		"toxicity":   true,
	},
}

// -------------------------------------------------------------------------

const url = "https://api.predictionguard.com/chat/completions"

var resp client.ChatVision
if err := cln.Do(ctx, http.MethodPost, url, d, &resp); err != nil {
	log.Fatalf("do: %s", err)
}

for i, choice := range resp.Choices {
	fmt.Printf("choice %d: %s\n", i, choice.Message.Content)
}
Example (Completions)
// examples/completion/main.go

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

logger := func(ctx context.Context, msg string, v ...any) {
	s := fmt.Sprintf("msg: %s", msg)
	for i := 0; i < len(v); i = i + 2 {
		s = s + fmt.Sprintf(", %s: %v", v[i], v[i+1])
	}
	log.Println(s)
}

cln := client.New(logger, os.Getenv("PREDICTIONGUARD_API_KEY"))

// -------------------------------------------------------------------------

d := client.D{
	"model":       "neural-chat-7b-v3-3",
	"prompt":      "Will I lose my hair",
	"max_tokens":  1000,
	"temperature": 0.1,
	"top_p":       0.1,
	"top_k":       50,
}

// -------------------------------------------------------------------------

const url = "https://api.predictionguard.com/completions"

var resp client.Completion
if err := cln.Do(ctx, http.MethodPost, url, d, &resp); err != nil {
	log.Fatalf("do: %s", err)
}

fmt.Println(resp.Choices[0].Text)
Example (Embedding)
// examples/embedding/basic/main.go

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

logger := func(ctx context.Context, msg string, v ...any) {
	s := fmt.Sprintf("msg: %s", msg)
	for i := 0; i < len(v); i = i + 2 {
		s = s + fmt.Sprintf(", %s: %v", v[i], v[i+1])
	}
	log.Println(s)
}

cln := client.New(logger, os.Getenv("PREDICTIONGUARD_API_KEY"))

// -------------------------------------------------------------------------

image, err := client.NewImageNetwork("https://static.wixstatic.com/media/f54603_b7882b876e2b47d3a38843a58a9829f1~mv2.png")
if err != nil {
	log.Fatalf("newimage: %s", err)
}

base64, err := image.EncodeBase64(ctx)
if err != nil {
	log.Fatalf("base64: %s", err)
}

// -------------------------------------------------------------------------

d := client.D{
	"model":              "bridgetower-large-itm-mlm-itc",
	"truncate":           true,
	"truncate_direction": client.Directions.Right,
	"input": []client.D{
		{
			"text":  "A picture of a rose",
			"image": base64,
		},
	},
}

// -------------------------------------------------------------------------

const url = "https://api.predictionguard.com/embeddings"

var resp client.Embedding
if err := cln.Do(ctx, http.MethodPost, url, d, &resp); err != nil {
	log.Fatalf("do: %s", err)
}

for _, data := range resp.Data {
	fmt.Print(data.Embedding)
}
Example (EmbeddingInts)
// examples/embedding/ints/main.go

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

logger := func(ctx context.Context, msg string, v ...any) {
	s := fmt.Sprintf("msg: %s", msg)
	for i := 0; i < len(v); i = i + 2 {
		s = s + fmt.Sprintf(", %s: %v", v[i], v[i+1])
	}
	log.Println(s)
}

cln := client.New(logger, os.Getenv("PREDICTIONGUARD_API_KEY"))

// -------------------------------------------------------------------------

d := client.D{
	"model":    "bridgetower-large-itm-mlm-itc",
	"truncate": false,
	"input": [][]int{
		{0, 3293, 83, 19893, 118963, 25, 7, 3034, 5, 2},
	},
}

// -------------------------------------------------------------------------

const url = "https://api.predictionguard.com/embeddings"

var resp client.Embedding
if err := cln.Do(ctx, http.MethodPost, url, d, &resp); err != nil {
	log.Fatalf("do: %s", err)
}

for _, data := range resp.Data {
	fmt.Print(data.Embedding)
}
Example (Factuality)
// examples/factuality/main.go

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

logger := func(ctx context.Context, msg string, v ...any) {
	s := fmt.Sprintf("msg: %s", msg)
	for i := 0; i < len(v); i = i + 2 {
		s = s + fmt.Sprintf(", %s: %v", v[i], v[i+1])
	}
	log.Println(s)
}

cln := client.New(logger, os.Getenv("PREDICTIONGUARD_API_KEY"))

// -------------------------------------------------------------------------

fact := "The President shall receive in full for his services during the term for which he shall have been elected compensation in the aggregate amount of 400,000 a year, to be paid monthly, and in addition an expense allowance of 50,000 to assist in defraying expenses relating to or resulting from the discharge of his official duties. Any unused amount of such expense allowance shall revert to the Treasury pursuant to section 1552 of title 31, United States Code. No amount of such expense allowance shall be included in the gross income of the President. He shall be entitled also to the use of the furniture and other effects belonging to the United States and kept in the Executive Residence at the White House."
text := "The president of the united states can take a salary of one million dollars"

d := client.D{
	"reference": fact,
	"text":      text,
}

// -------------------------------------------------------------------------

const url = "https://api.predictionguard.com/factuality"

var resp client.Factuality
if err := cln.Do(ctx, http.MethodPost, url, d, &resp); err != nil {
	log.Fatalf("do: %s", err)
}

fmt.Println(resp.Checks[0])
Example (Injection)
// examples/injection/main.go

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

logger := func(ctx context.Context, msg string, v ...any) {
	s := fmt.Sprintf("msg: %s", msg)
	for i := 0; i < len(v); i = i + 2 {
		s = s + fmt.Sprintf(", %s: %v", v[i], v[i+1])
	}
	log.Println(s)
}

cln := client.New(logger, os.Getenv("PREDICTIONGUARD_API_KEY"))

// -------------------------------------------------------------------------

prompt := "A short poem may be a stylistic choice or it may be that you have said what you intended to say in a more concise way."

d := client.D{
	"prompt": prompt,
	"detect": true,
}

// -------------------------------------------------------------------------

const url = "https://api.predictionguard.com/injection"

var resp client.Injection
if err := cln.Do(ctx, http.MethodPost, url, d, &resp); err != nil {
	log.Fatalf("do: %s", err)
}

fmt.Println(resp.Checks[0].Probability)
Example (ReplacePII)
// examples/ReplacePII/main.go

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

logger := func(ctx context.Context, msg string, v ...any) {
	s := fmt.Sprintf("msg: %s", msg)
	for i := 0; i < len(v); i = i + 2 {
		s = s + fmt.Sprintf(", %s: %v", v[i], v[i+1])
	}
	log.Println(s)
}

cln := client.New(logger, os.Getenv("PREDICTIONGUARD_API_KEY"))

// -------------------------------------------------------------------------

prompt := "My email is bill@ardanlabs.com and my number is 954-123-4567."

d := client.D{
	"prompt":         prompt,
	"replace":        true,
	"replace_method": "mask",
}

// -------------------------------------------------------------------------

const url = "https://api.predictionguard.com/PII"

var resp client.ReplacePII
if err := cln.Do(ctx, http.MethodPost, url, d, &resp); err != nil {
	log.Fatalf("do: %s", err)
}

fmt.Println(resp.Checks[0].NewPrompt)
Example (Rerank)
// examples/rerank/main.go

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

logger := func(ctx context.Context, msg string, v ...any) {
	s := fmt.Sprintf("msg: %s", msg)
	for i := 0; i < len(v); i = i + 2 {
		s = s + fmt.Sprintf(", %s: %v", v[i], v[i+1])
	}
	log.Println(s)
}

cln := client.New(logger, os.Getenv("PREDICTIONGUARD_API_KEY"))

// -------------------------------------------------------------------------

d := client.D{
	"model":            "bge-reranker-v2-m3",
	"query":            "What is Deep Learning?",
	"documents":        []string{"Deep Learning is not pizza.", "Deep Learning is pizza."},
	"return_documents": true,
}

// -------------------------------------------------------------------------

const url = "https://api.predictionguard.com/rerank"

var resp client.Rerank
if err := cln.Do(ctx, http.MethodPost, url, d, &resp); err != nil {
	log.Fatalf("do: %s", err)
}

fmt.Println(resp.Results)
Example (Tokenize)
// examples/tokenize/main.go

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

logger := func(ctx context.Context, msg string, v ...any) {
	s := fmt.Sprintf("msg: %s", msg)
	for i := 0; i < len(v); i = i + 2 {
		s = s + fmt.Sprintf(", %s: %v", v[i], v[i+1])
	}
	log.Println(s)
}

cln := client.New(logger, os.Getenv("PREDICTIONGUARD_API_KEY"))

// -------------------------------------------------------------------------

d := client.D{
	"model": "neural-chat-7b-v3-3",
	"input": "how many tokens exist for this sentence.",
}

// -------------------------------------------------------------------------

const url = "https://api.predictionguard.com/tokenize"

var resp client.Tokenize
if err := cln.Do(ctx, http.MethodPost, url, d, &resp); err != nil {
	log.Fatalf("do: %s", err)
}

fmt.Println(resp)
Example (Toxicity)
// examples/toxicity/main.go

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

logger := func(ctx context.Context, msg string, v ...any) {
	s := fmt.Sprintf("msg: %s", msg)
	for i := 0; i < len(v); i = i + 2 {
		s = s + fmt.Sprintf(", %s: %v", v[i], v[i+1])
	}
	log.Println(s)
}

cln := client.New(logger, os.Getenv("PREDICTIONGUARD_API_KEY"))

// -------------------------------------------------------------------------

d := client.D{
	"text": "Every flight I have is late and I am very angry. I want to hurt someone.",
}

// -------------------------------------------------------------------------

const url = "https://api.predictionguard.com/toxicity"

var resp client.Toxicity
if err := cln.Do(ctx, http.MethodPost, url, d, &resp); err != nil {
	log.Fatalf("do: %s", err)
}

fmt.Println(resp.Checks[0].Score)
Example (Translate)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

logger := func(ctx context.Context, msg string, v ...any) {
	s := fmt.Sprintf("msg: %s", msg)
	for i := 0; i < len(v); i = i + 2 {
		s = s + fmt.Sprintf(", %s: %v", v[i], v[i+1])
	}
	log.Println(s)
}

cln := client.New(logger, os.Getenv("PREDICTIONGUARD_API_KEY"))

// -------------------------------------------------------------------------

d := client.D{
	"text":                   "The rain in Spain stays mainly in the plain",
	"source_lang":            client.Languages.English,
	"target_lang":            client.Languages.Spanish,
	"use_third_party_engine": false,
}

// -------------------------------------------------------------------------

const url = "https://api.predictionguard.com/translate"

var resp client.Translate
if err := cln.Do(ctx, http.MethodPost, url, d, &resp); err != nil {
	log.Fatalf("do: %s", err)
}

fmt.Println(resp.BestTranslation)

type Completion

type Completion struct {
	ID      string             `json:"id"`
	Object  string             `json:"object"`
	Created Time               `json:"created"`
	Model   string             `json:"model"`
	Choices []CompletionChoice `json:"choices"`
}

type CompletionChoice

type CompletionChoice struct {
	Index int    `json:"index"`
	Text  string `json:"text"`
}

type D

type D map[string]any

type Direction

type Direction struct {
	// contains filtered or unexported fields
}

func (Direction) Equal

func (d Direction) Equal(d2 Direction) bool

func (Direction) MarshalText

func (d Direction) MarshalText() ([]byte, error)

func (Direction) String

func (d Direction) String() string

func (*Direction) UnmarshalText

func (d *Direction) UnmarshalText(data []byte) error

type Embedding

type Embedding struct {
	ID      string          `json:"id"`
	Object  string          `json:"object"`
	Created Time            `json:"created"`
	Model   string          `json:"model"`
	Data    []EmbeddingData `json:"data"`
}

type EmbeddingData

type EmbeddingData struct {
	Index     int       `json:"index"`
	Object    string    `json:"object"`
	Embedding []float64 `json:"embedding"`
}

type Error

type Error struct {
	Message string `json:"error"`
}

func (*Error) Error

func (err *Error) Error() string

type Factuality

type Factuality struct {
	ID      string            `json:"id"`
	Object  string            `json:"object"`
	Created Time              `json:"created"`
	Checks  []FactualityCheck `json:"checks"`
}

type FactualityCheck

type FactualityCheck struct {
	Score float64 `json:"score"`
	Index int     `json:"index"`
}

type ImageBase64

type ImageBase64 struct {
	// contains filtered or unexported fields
}

func NewImageBase64

func NewImageBase64(base64 string) ImageBase64

func (ImageBase64) EncodeBase64

func (img ImageBase64) EncodeBase64(ctx context.Context) (string, error)

type ImageFile

type ImageFile struct {
	// contains filtered or unexported fields
}

func NewImageFile

func NewImageFile(imagePath string) (ImageFile, error)

func (ImageFile) EncodeBase64

func (img ImageFile) EncodeBase64(ctx context.Context) (string, error)

type ImageNetwork

type ImageNetwork struct {
	// contains filtered or unexported fields
}

func NewImageNetwork

func NewImageNetwork(imageURL string) (ImageNetwork, error)

func (ImageNetwork) EncodeBase64

func (img ImageNetwork) EncodeBase64(ctx context.Context) (string, error)

type Injection

type Injection struct {
	ID      string           `json:"id"`
	Object  string           `json:"object"`
	Created Time             `json:"created"`
	Checks  []InjectionCheck `json:"checks"`
}

type InjectionCheck

type InjectionCheck struct {
	Probability float64 `json:"probability"`
	Index       int     `json:"index"`
	Status      string  `json:"status"`
}

type Language

type Language struct {
	// contains filtered or unexported fields
}

func (Language) Equal

func (l Language) Equal(l2 Language) bool

func (Language) MarshalText

func (l Language) MarshalText() ([]byte, error)

func (Language) String

func (l Language) String() string

func (*Language) UnmarshalText

func (l *Language) UnmarshalText(data []byte) error

type Logger

type Logger func(context.Context, string, ...any)

type ModelCapabilities

type ModelCapabilities struct {
	ChatCompletion     bool `json:"chat_completion"`
	ChatWithImage      bool `json:"chat_with_image"`
	Completion         bool `json:"completion"`
	Embedding          bool `json:"embedding"`
	EmbeddingWithImage bool `json:"embedding_with_image"`
	Tokenize           bool `json:"tokenize"`
}

type ModelData

type ModelData struct {
	ID               string            `json:"id"`
	Object           string            `json:"object"`
	Created          time.Time         `json:"created"`
	OwnedBy          string            `json:"owned_by"`
	Description      string            `json:"description"`
	MaxContextLength int               `json:"max_context_length"`
	PromptFormat     string            `json:"prompt_format"`
	Capabilities     ModelCapabilities `json:"capabilities"`
}

type ModelResponse

type ModelResponse struct {
	Object string      `json:"object"`
	Data   []ModelData `json:"data"`
}

type PII

type PII struct {
	// contains filtered or unexported fields
}

func (PII) Equal

func (p PII) Equal(p2 PII) bool

func (PII) MarshalText

func (p PII) MarshalText() ([]byte, error)

func (PII) String

func (p PII) String() string

func (*PII) UnmarshalText

func (p *PII) UnmarshalText(data []byte) error

type ReplaceMethod

type ReplaceMethod struct {
	// contains filtered or unexported fields
}

func (ReplaceMethod) Equal

func (rm ReplaceMethod) Equal(rm2 ReplaceMethod) bool

func (ReplaceMethod) MarshalText

func (rm ReplaceMethod) MarshalText() ([]byte, error)

func (ReplaceMethod) String

func (rm ReplaceMethod) String() string

func (*ReplaceMethod) UnmarshalText

func (rm *ReplaceMethod) UnmarshalText(data []byte) error

type ReplacePII

type ReplacePII struct {
	ID      string            `json:"id"`
	Object  string            `json:"object"`
	Created Time              `json:"created"`
	Checks  []ReplacePIICheck `json:"checks"`
}

type ReplacePIICheck

type ReplacePIICheck struct {
	NewPrompt string `json:"new_prompt"`
	Index     int    `json:"index"`
	Status    string `json:"status"`
}

type Rerank

type Rerank struct {
	ID      string         `json:"id"`
	Object  string         `json:"object"`
	Created Time           `json:"created"`
	Model   string         `json:"model"`
	Results []RerankResult `json:"results"`
}

type RerankResult

type RerankResult struct {
	Index          int     `json:"index"`
	RelevanceScore float64 `json:"relevance_score"`
	Text           string  `json:"text"`
}

type Role

type Role struct {
	// contains filtered or unexported fields
}

func (Role) Equal

func (r Role) Equal(r2 Role) bool

func (Role) MarshalText

func (r Role) MarshalText() ([]byte, error)

func (Role) String

func (r Role) String() string

func (*Role) UnmarshalText

func (r *Role) UnmarshalText(data []byte) error

type SSEClient

type SSEClient[T any] struct {
	*Client
}

func NewSSE

func NewSSE[T any](log Logger, apiKey string, options ...func(cln *Client)) *SSEClient[T]

func (*SSEClient[T]) Do

func (cln *SSEClient[T]) Do(ctx context.Context, method string, endpoint string, body D, ch chan T) error

type Time

type Time struct {
	time.Time
}

func ToTime

func ToTime(sec int64) Time

func (Time) MarshalJSON

func (t Time) MarshalJSON() ([]byte, error)

func (*Time) UnmarshalJSON

func (t *Time) UnmarshalJSON(data []byte) error

type TokenData

type TokenData struct {
	ID    int    `json:"id"`
	Start int    `json:"start"`
	Stop  int    `json:"stop"`
	Text  string `json:"text"`
}

type Tokenize

type Tokenize struct {
	ID      string      `json:"id"`
	Object  string      `json:"object"`
	Created Time        `json:"created"`
	Data    []TokenData `json:"data"`
}

type Toxicity

type Toxicity struct {
	ID      string          `json:"id"`
	Object  string          `json:"object"`
	Created Time            `json:"created"`
	Checks  []ToxicityCheck `json:"checks"`
}

type ToxicityCheck

type ToxicityCheck struct {
	Score float64 `json:"score"`
	Index int     `json:"index"`
}

type Translate

type Translate struct {
	ID                   string        `json:"id"`
	Object               string        `json:"object"`
	Created              Time          `json:"created"`
	BestTranslation      string        `json:"best_translation"`
	BestTranslationModel string        `json:"best_translation_model"`
	Score                float64       `json:"best_score"`
	Translations         []Translation `json:"translations"`
}

type Translation

type Translation struct {
	Score       float64 `json:"score"`
	Translation string  `json:"translation"`
	Model       string  `json:"model"`
	Status      string  `json:"status"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL