🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

privategpt-sdk-web

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

privategpt-sdk-web

The PrivateGPT TypeScript SDK is a powerful open-source library that allows developers to work with AI in a private and secure manner. This SDK provides a set of tools and utilities to interact with the PrivateGPT API and leverage its capabilities.

0.1.1
latest
Source
npm
Version published
Maintainers
1
Created
Source

PrivateGPT TypeScript SDK

The PrivateGPT TypeScript SDK is a powerful open-source library that allows developers to work with AI in a private and secure manner. This SDK provides a set of tools and utilities to interact with the PrivateGPT API and leverage its capabilities.

Live demo

Source demo

Installation

npm install privategpt-ts

Usage (plain typescript)

Initialize client

First you need to initalize the PrivategptApiClient:

const pgptApiClient = new PrivategptApiClient({ environment: 'http://localhost:8001' });

Ingest file

import { pgptApiClient } from 'your/path';

const file = getFileFromSomewhere();
const ingestResponse = await pgptApiClient.ingestion.ingestFile(file);

Get ingested files

import { pgptApiClient } from 'your/path';

const ingestResponse = await pgptApiClient.ingestion.listIngested();

Delete ingested file

import { pgptApiClient } from 'your/path';

await pgptApiClient.ingestion.deleteIngested(docId);

Chat completion

import { pgptApiClient } from 'your/path';

// stream way
const stream = await pgptApiClient.completion.chatCompletionStream({
  messages: [
    {
      content: 'How are you',
      role: 'user'
    }
  ],
  includeSources: true,
  useContext: true
});
const readableStream = streamToReadableStream(stream);
const reader = readableStream.getReader();
const decoder = createChunkDecoder();
const loopRunner = true;
let result = '';

while (loopRunner) {
  const { value, done } = await reader.read();
  if (done) {
    break;
  }
  const decodedChunk = decoder(value);
  if (!decodedChunk) continue;
  result += decoder(value);
  onNewMessage?.(result);
}
return result;

// async way
const openAiCompletionResponse = await pgptApiClient.completion.chatCompletionStream({
  messages: [
    {
      content: 'How are you',
      role: 'user'
    }
  ],
  includeSources: true,
  useContext: true
});

Prompt completion

import { pgptApiClient } from 'your/path';

// stream way
const stream = await pgptApiClient.completion.promptCompletionStream({
  promt: 'Hello! Make a joke',
  includeSources: true,
  useContext: true
});
const readableStream = streamToReadableStream(stream);
const reader = readableStream.getReader();
const decoder = createChunkDecoder();
const loopRunner = true;
let result = '';

while (loopRunner) {
  const { value, done } = await reader.read();
  if (done) {
    break;
  }
  const decodedChunk = decoder(value);
  if (!decodedChunk) continue;
  result += decoder(value);
  onNewMessage?.(result);
}
return result;

// async way
const openAiCompletionResponse = await pgptApiClient.completion.promptCompletion({
  promt: 'Hello! Make a joke',
  includeSources: true,
  useContext: true
});

Get ingested files

import { pgptApiClient } from 'your/path';

const ingestResponse = await pgptApiClient.ingestion.listIngested();

Delete ingested file

import { pgptApiClient } from 'your/path';

await pgptApiClient.ingestion.deleteIngested(docId);

Chat completion

import { pgptApiClient } from 'your/path';

// stream way
const stream = await pgptApiClient.completion.chatCompletionStream({
  messages: [
    {
      content: 'How are you',
      role: 'user'
    }
  ],
  includeSources: true,
  useContext: true
});
const readableStream = streamToReadableStream(stream);
const reader = readableStream.getReader();
const decoder = createChunkDecoder();
const loopRunner = true;
let result = '';

while (loopRunner) {
  const { value, done } = await reader.read();
  if (done) {
    break;
  }
  const decodedChunk = decoder(value);
  if (!decodedChunk) continue;
  result += decoder(value);
  onNewMessage?.(result);
}
return result;

// async way
const openAiCompletionResponse = await pgptApiClient.completion.chatCompletionStream({
  messages: [
    {
      content: 'How are you',
      role: 'user'
    }
  ],
  includeSources: true,
  useContext: true
});

Prompt completion

import { pgptApiClient } from 'your/path';

// stream way
const stream = await pgptApiClient.completion.promptCompletionStream({
  promt: 'Hello! Make a joke',
  includeSources: true,
  useContext: true
});
const readableStream = streamToReadableStream(stream);
const reader = readableStream.getReader();
const decoder = createChunkDecoder();
const loopRunner = true;
let result = '';

while (loopRunner) {
  const { value, done } = await reader.read();
  if (done) {
    break;
  }
  const decodedChunk = decoder(value);
  if (!decodedChunk) continue;
  result += decoder(value);
  onNewMessage?.(result);
}
return result;

// async way
const openAiCompletionResponse = await pgptApiClient.completion.promptCompletion({
  promt: 'Hello! Make a joke',
  includeSources: true,
  useContext: true
});

Usage (react adapter)

Initialize client

First you need to initalize the PrivategptApiClient:

const pgptApiClient = new PrivategptApiClient({ environment: 'http://localhost:8001' });

useFiles (ingest file, delete file, get ingested files)

import { pgptApiClient } from 'your/path';

const YourComponent = () => {
  const {
    addFile,
    isUploadingFile,
    isDeletingFile,
    files,
    deleteFile,
    isFetchingFiles,
    errorDeletingFile,
    errorFetchingFiles,
    errorUploadingFile,
  } = useFiles({
    client: pgptApiClient,
    fetchFiles: true // in case you don't want to fetch files automatically when using this hook
  });
}

useChat (chat completion)

import { pgptApiClient } from 'your/path';

const YourComponent = () => {
  const {
    stop,
    isLoading,
    completion,
    setCompletion
  } = useChat({
    messages: [
      {
        content: 'Hello, how are you?',
        role: 'user'
      }
    ],
    includeSources: true,
    useContext: true,
    onFinish: () => {
      console.log('finished streaming');
    },
    client: pgptApiClient,
    systemPrompt: 'You are a character from Battlestar Galactica'
  });
}

usePrompt (prompt completion)

import { pgptApiClient } from 'your/path';

const YourComponent = () => {
  const {
    stop,
    isLoading,
    completion,
    setCompletion
  } = useChat({
    prompt: 'Hello, make a joke in japanese',
    includeSources: true,
    useContext: true,
    onFinish: () => {
      console.log('finished streaming');
    },
    client: pgptApiClient,
    systemPrompt: 'You are a japanese chef'
  });
}

FAQs

Package last updated on 13 May 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts