The AI framework for building declarative and composable AI-powered LLM products.
First, install the @baseai/core
package using npm or yarn:
npm install @baseai/core
or
pnpm add @baseai/core
or
yarn add @baseai/core
To use the generate
function from the @baseai/core
package, follow these steps:
-
Import the
generate
function:import {generate} from '@baseai/core';
-
Set up environment variables:
Ensure you have the following environment variables set in your
.env
file:OPENAI_API_KEY=your_openai_api_key
-
Generate a response using a prompt:
import {generate} from '@baseai/core'; async function exampleWithPrompt() { const response = await generate({ model: 'gpt-3.5-turbo-0125', prompt: '1+1', }); console.log(response); // Output: '2' } exampleWithPrompt();
-
Generate a response using messages array:
import {generate} from '@baseai/core'; async function exampleWithMessages() { const response = await generate({ model: 'gpt-3.5-turbo-0125', messages: [ {role: 'system', content: 'You are a helpful assistant.'}, {role: 'user', content: 'Give me 5 title ideas'}, {role: 'assistant', content: 'Sure, here you go … …'}, ], }); console.log(response); } exampleWithMessages();
Generates a response using the specified model, prompt, or messages array.
async function generate(params: GenerateParams): Promise<string>;
-
params
: An object containing the following properties:-
model
(string): The model to use for generating the response. -
prompt
(optional string): The prompt to use for generating the response. Eitherprompt
ormessages
must be provided. -
messages
(optionalMessage[]
): An array of message objects. Each message object should containrole
andcontent
properties. Eitherprompt
ormessages
must be provided.
-
- A promise that resolves to a string containing the generated response.
import {generate} from '@baseai/core';
const responseFromPrompt = await generate({
model: 'gpt-3.5-turbo-0125',
prompt: '1+1',
});
console.log(responseFromPrompt);
const responseFromMessages = await generate({
model: 'gpt-3.5-turbo-0125',
messages: [
{role: 'system', content: 'You are a helpful assistant.'},
{role: 'user', content: 'Give me 5 title ideas'},
{role: 'assistant', content: 'Sure, here you go … …'},
],
});
console.log(responseFromMessages);
Validates the input parameters and environment variables.
function validateInput(params: GenerateParams): ValidatedParams;
-
params
: An object containing the following properties:-
model
(string): The model to use for generating the response. -
prompt
(optional string): The prompt to use for generating the response. -
messages
(optionalMessage[]
): An array of message objects.
-
- An object containing the validated parameters and environment variables.
const validatedParams = validateInput({
model: 'gpt-3.5-turbo-0125',
prompt: 'Hi',
});
Constructs the messages array using the provided prompt or messages array.
function buildMessages({
prompt,
messages,
}: {
prompt?: string;
messages?: Message[];
}): Message[];
-
prompt
(optional string): The prompt to use for generating the response. -
messages
(optionalMessage[]
): An array of message objects.
- An array of message objects.
const messages = buildMessages({prompt: 'Hi'});
Constructs the headers for the API request using the provided API key.
function buildHeaders(API_KEY: string): Record<string, string>;
-
API_KEY
(string): The API key to use for the request.
- An object containing the headers for the API request.
const headers = buildHeaders('your-api-key');
Processes the API response and extracts the generated message content.
async function handleResponse(response: Response): Promise<string>;
-
response
(Response): The response object from the API request.
- A promise that resolves to a string containing the generated message content.
const content = await handleResponse(response);
Type definition for the parameters of the generate
function.
interface GenerateParams {
model: string;
prompt?: string;
messages?: Message[];
}
Type definition for a message object.
interface Message {
role: 'system' | 'user' | 'assistant';
content: string;
}
Type definition for the validated environment variables.
interface ValidatedEnv {
API_KEY: string;
API_URL_CHAT: string;
}
The API key for authenticating requests to the OpenAI API.
The URL for the OpenAI API chat endpoint.
import {generate} from '@baseai/core';
const responseFromPrompt = await generate({
model: 'gpt-3.5-turbo-0125',
prompt: '1+1',
});
console.log(responseFromPrompt);
const responseFromMessages = await generate({
model: 'gpt-3.5-turbo-0125',
messages: [
{role: 'system', content: 'You are a helpful assistant.'},
{role: 'user', content: 'Give me 5 title ideas'},
{role: 'assistant', content: 'Sure, here you go … …'},
],
});
console.log(responseFromMessages);
This documentation provides a comprehensive guide for getting started with the @baseai/core
package, as well as a detailed API reference for the generate
function and its related components.