Client provides access the PredictionGuard API.

Constructors

  • constructor constructs a Client API for use.

    Parameters

    • url: string

      url represents the transport and domain:port.

    • apiKey: string

      apiKey represents PG api key.

    Returns Client

Methods

  • Chat generates chat completions based on a conversation history.

    Parameters

    • input: ChatInput

      input represents the entire set of possible input for the Chat call.

    Returns Promise<[Chat, null | Error]>

    • A Promise with a Chat object and an Error object if the error is not null.
    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: [
    {
    role: pg.Roles.User,
    content: 'How do you feel about the world in general',
    },
    ],
    maxTokens: 1000,
    temperature: 0.1,
    topP: 0.1,
    topK: 50,
    options: {
    factuality: true,
    toxicity: true,
    pii: pg.PIIs.Replace,
    piiReplaceMethod: pg.ReplaceMethods.Random,
    },
    };

    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();
  • ChatSSE generates a stream of chat completions based on a conversation history.

    Parameters

    • input: ChatSSEInput

      input represents the entire set of possible input for the SSE Chat call.

    Returns Promise<null | Error>

    • A Promise with an Error object if the error is not null.
    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,
    onMessage: function (event, err) {
    if (err != null) {
    if (err.error == 'EOF') {
    return;
    }
    console.log(err);
    }

    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.

    Parameters

    • input: ChatVisionInput

      input represents the entire set of possible input for the Vision Chat call.

    Returns Promise<[ChatVision, null | Error]>

    • A Promise with a ChatVision object and a Error object if the error is not null.
    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,
    };

    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.

    Parameters

    • input: CompletionInput

      input represents the entire set of possible input for the Completion call.

    Returns Promise<[Completion, null | Error]>

    • A Promise with a Completion object and a Error object if the error is not null.
    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,
    };

    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.

    Parameters

    • model: string

      model to use.

    • input: EmbeddingInput[]

      input represents a collection of text and images to vectorize.

    Returns Promise<[Embedding, null | Error]>

    • A Promise with a Embedding object and an Error object if the error is not null.
    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 input = [
    {
    text: 'This is Bill Kennedy, a decent Go developer.',
    image: image,
    },
    ];

    var [result, err] = await client.Embedding('bridgetower-large-itm-mlm-itc', input);
    if (err != null) {
    console.log('ERROR:' + err.error);
    return;
    }

    for (const dt of result.data) {
    process.stdout.write(dt.embedding.toString());
    }
    }

    Embedding();
  • Factuality checks the factuality of a given text compared to a reference.

    Parameters

    • reference: string

      reference represents the reference text for comparison.

    • text: string

      text represents the text to be checked for factuality.

    Returns Promise<[Factuality, null | Error]>

    • A Promise with a Factuality object and a Error object if the error is not null.
    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.

    Returns Promise<[string, null | Error]>

    • A Promise with a string and an Error object if the error is not null.
    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.

    Parameters

    • prompt: string

      prompt represents the text to detect injection attacks against.

    Returns Promise<[Injection, null | Error]>

    • A Promise with a Injection object and a Error object if the error is not null.
    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();
  • RawDoGet performs a raw GET call.

    Parameters

    • endpoint: string

      endpoint represents endpoint to call and does not include the transport or domain.

    Returns Promise<[any, null | Error]>

    • A Promise with a respose object and an error object if the error is not null.
  • RawDoPost performs a raw POST call.

    Parameters

    • endpoint: string

      endpoint represents endpoint to call and does not include the transport or domain.

    • body: any

      body represents an input object.

    Returns Promise<[any, null | Error]>

    • A Promise with a respose object and an error object if the error is not null.
  • RawDoSSEPost performs a raw POST call with SSE support.

    Parameters

    • endpoint: string

      endpoint represents endpoint to call and does not include the transport or domain.

    • body: any

      body represents an input object.

    • onMessage: ((event: null | ServerSentEvent, err: null | Error) => void)

      onMessage represents a function that will receive the stream of chat results.

        • (event, err): void
        • Parameters

          • event: null | ServerSentEvent
          • err: null | Error

          Returns void

    Returns Promise<null | Error>

    • A Promise with an error object if the error is not null.
  • ReplacePII replaces personal information such as names, SSNs, and emails in a given text.

    Parameters

    • replaceMethod: ReplaceMethods

      replaceMethod represents the method to use for replacing personal information.

    • prompt: string

      prompt represents the text to detect injection attacks against.

    Returns Promise<[ReplacePII, null | Error]>

    • A Promise with a ReplacePII object and a Error object if the error is not null.
    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();
  • Toxicity checks the toxicity of a given text.

    Parameters

    • text: string

      text represents the text to be scored for toxicity.

    Returns Promise<[Toxicity, null | Error]>

    • A Promise with a Toxicity object and a Error object if the error is not null.
    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.

    Parameters

    • text: string

      text represents the text to be translated.

    • sourceLang: Languages

      sourceLang represents the source language of the text.

    • targetLang: Languages

      targetLang represents the target language of the text.

    • useThirdPartyEngine: boolean

      Use third-party translation engines such as OpenAI, DeepL, and Google. Defaults to false.

    Returns Promise<[Translate, null | Error]>

    • A Promise with a Translate object and a Error object if the error is not null.
    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();