Skip to main content

Model Providers

Model Providers are interfaces for interacting with different model providers. They allow you to easily switch between providers without having to change your code.

You can use Model providers in the same way you would use the OpenAI API.

Creating a Model Provider

import { OpenAI } from "@promptable/promptable";
const openai = new OpenAI(apiKey);
//or
const openai = new OpenAI(apiKey, {
// Provide OpenAI Configuration here
});

Using a Model Provider

Text Generation

You can use the model provider to complete text generation.

import { OpenAI } from "@promptable/promptable";

const text = "This is a test";
const tokensUsed = openai.countTokens(text);
const response = await openai.generate(text);

console.log("Tokens: ", tokensUsed);
console.log(response);

Streaming

With OpenAI, you can also stream completions like this:

import { OpenAI } from "@promptable/promptable";

const text = "This is a test";
const tokensUsed = openai.countTokens(text);
await openai.stream(
promptText,
(chunk: string) => {
console.log(chunk);
},
() => {
console.log("Done");
}
);