Private
apiPrivate
urlChat generates chat completions based on a conversation history.
input represents the entire set of possible input for the Chat call.
import * as pg from 'predictionguard';
const client = new pg.Client('https://api.predictionguard.com', process.env.PGKEY);
async function Chat() {
const input = {
model: 'Neural-Chat-7B',
messages: 'How do you feel about the world in general',
maxTokens: 1000,
temperature: 0.1,
topP: 0.1,
topK: 50,
inputExtension: {
pii: pg.PIIs.Replace,
piiReplaceMethod: pg.ReplaceMethods.Random,
},
outputExtension: {
factuality: true,
toxicity: true,
},
};
var [result, err] = await client.Chat(input);
if (err != null) {
console.log('ERROR:' + err.error);
return;
}
console.log('RESULT:' + result.createdDate() + ': ' + result.model + ': ' + result.choices[0].message.content);
}
async function ChatMulti() {
const input = {
model: 'Neural-Chat-7B',
messages: [
{
role: pg.Roles.User,
content: 'How do you feel about the world in general',
},
],
maxTokens: 1000,
temperature: 0.1,
topP: 0.1,
topK: 50,
inputExtension: {
pii: pg.PIIs.Replace,
piiReplaceMethod: pg.ReplaceMethods.Random,
},
outputExtension: {
factuality: true,
toxicity: true,
},
};
var [result, err] = await client.Chat(input);
if (err != null) {
console.log('ERROR:' + err.error);
return;
}
console.log('RESULT:' + result.createdDate() + ': ' + result.model + ': ' + result.choices[0].message.content);
}
Chat();
ChatMulti();
ChatSSE generates a stream of chat completions based on a conversation history.
input represents the entire set of possible input for the SSE Chat call.
import * as pg from 'predictiongaurd';
const client = new pg.Client('https://api.predictionguard.com', process.env.PGKEY);
async function ChatSSE() {
const input = {
model: 'Neural-Chat-7B',
messages: [
{
role: pg.Roles.User,
content: 'How do you feel about the world in general',
},
],
maxTokens: 1000,
temperature: 0.1,
topP: 0.1,
topK: 50,
inputExtension: {
pii: pg.PIIs.Replace,
piiReplaceMethod: pg.ReplaceMethods.Random,
},
onMessage: function (event, err) {
if (err != null) {
if (err.error == 'EOF') {
return;
}
console.log('ERROR 1:' + err.error);
return;
}
if (event.error != '') {
console.log('ERROR 2:' + err.error);
return;
}
for (const choice of event.choices) {
if (choice.delta.hasOwnProperty('content')) {
process.stdout.write(choice.delta.content);
}
}
},
};
var err = await client.ChatSSE(input);
if (err != null) {
console.log('ERROR:' + err.error);
return;
}
}
ChatSSE();
ChatVision generates answers a question about an image.
input represents the entire set of possible input for the Vision Chat call.
import * as pg from 'predictionguard';
const client = new pg.Client('https://api.predictionguard.com', process.env.PGKEY);
async function ChatVision() {
const image = new pg.ImageNetwork('https://pbs.twimg.com/profile_images/1571574401107169282/ylAgz_f5_400x400.jpg');
const input = {
role: pg.Roles.User,
question: 'is there a deer in this picture',
image: image,
maxTokens: 1000,
temperature: 0.1,
topP: 0.1,
topK: 50,
inputExtension: {
pii: pg.PIIs.Replace,
piiReplaceMethod: pg.ReplaceMethods.Random,
},
outputExtension: {
factuality: true,
toxicity: true,
},
};
var [result, err] = await client.ChatVision(input);
if (err != null) {
console.log('ERROR:' + err.error);
return;
}
console.log('RESULT:' + result.createdDate() + ': ' + result.model + ': ' + result.choices[0].message.content);
}
ChatVision();
Completion generates text completions based on the provided input.
input represents the entire set of possible input for the Completion call.
import * as pg from 'predictionguard';
const client = new pg.Client('https://api.predictionguard.com', process.env.PGKEY);
async function Completions() {
const input = {
model: 'Neural-Chat-7B',
prompt: 'Will I lose my hair',
maxTokens: 1000,
temperature: 0.1,
topP: 0.1,
topK: 50,
inputExtension: {
pii: pg.PIIs.Replace,
piiReplaceMethod: pg.ReplaceMethods.Random,
},
outputExtension: {
factuality: true,
toxicity: true,
},
};
var [result, err] = await client.Completion(input);
if (err != null) {
console.log('ERROR:' + err.error);
return;
}
console.log('RESULT:' + result.choices[0].text);
}
Completions();
Embedding generates chat completions based on a conversation history.
model to use.
input represents a collection of text and images, numbers, or a slice of numbers to vectorize.
Optional
truncate: booleantruncate represents whether to truncate the input if it's too long. Not all models support this.
Optional
truncateDir: DirectionstruncateDir represents the direction to truncate, either Right or Left.
import * as pg from 'predictiongaurd';
const client = new pg.Client('https://api.predictionguard.com', process.env.PGKEY);
async function Embedding() {
const image = new pg.ImageNetwork('https://pbs.twimg.com/profile_images/1571574401107169282/ylAgz_f5_400x400.jpg');
const input1 = [
{
text: 'This is Bill Kennedy, a decent Go developer.',
image: image,
},
];
var [result1, err] = await client.Embedding('bridgetower-large-itm-mlm-itc', input1);
if (err != null) {
console.log('ERROR:' + err.error);
return;
}
for (const dt of result1.data) {
process.stdout.write(dt.embedding.toString());
}
const input2 = [
{
text: 'This is Bill Kennedy, a decent Go developer.',
},
];
var [result2, err] = await client.Embedding('multilingual-e5-large-instruct', input2, true, pg.Directions.Right);
if (err != null) {
console.log('ERROR:' + err.error);
return;
}
for (const dt of result2.data) {
process.stdout.write(dt.embedding.toString());
}
}
Embedding();
Factuality checks the factuality of a given text compared to a reference.
reference represents the reference text for comparison.
text represents the text to be checked for factuality.
import * as pg from 'predictionguard';
const client = new pg.Client('https://api.predictionguard.com', process.env.PGKEY);
async function Factuality() {
const 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.`;
const text = `The president of the united states can take a salary of one
million dollars`;
var [result, err] = await client.Factuality(fact, text);
if (err != null) {
console.log('ERROR:' + err.error);
return;
}
console.log('RESULT:' + JSON.stringify(result.checks[0]));
}
Factuality();
HealthCheck validates the PG API Service is available.
import * as pg from 'predictionguard';
const client = new pg.Client('https://api.predictionguard.com', process.env.PGKEY);
async function HealthCheck() {
var [result, err] = await client.HealthCheck();
if (err != null) {
console.log('ERROR:' + err.error);
return;
}
console.log(result);
}
HealthCheck();
Injection detects potential prompt injection attacks in a given prompt.
prompt represents the text to detect injection attacks against.
import * as pg from 'predictionguard';
const client = new pg.Client('https://api.predictionguard.com', process.env.PGKEY);
async function Injection() {
const 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.`;
var [result, err] = await client.Injection(prompt);
if (err != null) {
console.log('ERROR:' + err.error);
return;
}
console.log('RESULT:' + result.checks[0].probability);
}
Injection();
Protected
RawRawDoGet performs a raw GET call.
endpoint represents endpoint to call and does not include the transport or domain.
Protected
RawRawDoPost performs a raw POST call.
endpoint represents endpoint to call and does not include the transport or domain.
body represents an input object.
Protected
RawRawDoSSEPost performs a raw POST call with SSE support.
endpoint represents endpoint to call and does not include the transport or domain.
body represents an input object.
onMessage represents a function that will receive the stream of chat results.
ReplacePII replaces personal information such as names, SSNs, and emails in a given text.
replaceMethod represents the method to use for replacing personal information.
prompt represents the text to detect injection attacks against.
import * as pg from 'predictionguard';
const client = new pg.Client('https://api.predictionguard.com', process.env.PGKEY);
async function ReplacePII() {
const replaceMethod = pg.ReplaceMethods.Mask;
const prompt = `My email is bill@ardanlabs.com and my number is 954-123-4567.`;
var [result, err] = await client.ReplacePII(replaceMethod, prompt);
if (err != null) {
console.log('ERROR:' + err.error);
return;
}
console.log('RESULT:' + result.checks[0].new_prompt);
}
ReplacePII();
Rerank sorts text inputs by semantic relevance to a specified query.
input represents the entire set of possible input for the Rerank call.
import * as pg from '../dist/index.js';
const client = new pg.Client('https://api.predictionguard.com', process.env.PREDICTIONGUARD_API_KEY);
async function Rerank() {
const input = {
model: 'bge-reranker-v2-m3',
query: 'What is Deep Learning?',
documents: ['Deep Learning is not pizza.', 'Deep Learning is pizza.'],
returnDocuments: true,
};
var [result, err] = await client.Rerank(input);
if (err != null) {
console.log('ERROR:' + err.error);
return;
}
console.log('RESULT:' + result.results[0].relevance_score);
}
Rerank();
Toxicity checks the toxicity of a given text.
text represents the text to be scored for toxicity.
import * as pg from 'predictionguard';
const client = new pg.Client('https://api.predictionguard.com', process.env.PGKEY);
async function Toxicity() {
const text = `Every flight I have is late and I am very angry. I want to
hurt someone.`;
var [result, err] = await client.Toxicity(text);
if (err != null) {
console.log('ERROR:' + err.error);
return;
}
console.log('RESULT:' + result.checks[0].score);
}
Toxicity();
Translate converts text from one language to another.
text represents the text to be translated.
sourceLang represents the source language of the text.
targetLang represents the target language of the text.
Use third-party translation engines such as OpenAI, DeepL, and Google. Defaults to false.
import * as pg from 'predictionguard';
const client = new pg.Client('https://api.predictionguard.com', process.env.PGKEY);
async function Translate() {
const text = `The rain in Spain stays mainly in the plain`;
const sourceLang = pg.Languages.English;
const targetLang = pg.Languages.Spanish;
const useThirdPartyEngine = false;
var [result, err] = await client.Translate(text, sourceLang, targetLang, useThirdPartyEngine);
if (err != null) {
console.log('ERROR:' + err.error);
return;
}
console.log('RESULT:' + result.best_translation);
}
Translate();
Private
embed
Client provides access the PredictionGuard API.