
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
Fully typed chat APIs for OpenAI and Azure's chat models - with token checking and retries
Fully typed chat APIs for OpenAI, Anthropic, and Azure's chat models for browser, edge, and node environments.
import { OpenAIChatApi } from 'llm-api';
const openai = new OpenAIChatApi({ apiKey: 'YOUR_OPENAI_KEY' });
const resText = await openai.textCompletion('Hello');
const resChat = await openai.chatCompletion({
role: 'user',
content: 'Hello world',
});
This package is hosted on npm:
npm i llm-api
yarn add llm-api
To configure a new model endpoint:
const openai = new OpenAIChatApi(params: OpenAIConfig, config: ModelConfig);
These model config map to OpenAI's config directly, see doc: https://platform.openai.com/docs/api-reference/chat/create
interface ModelConfig {
model?: string;
contextSize?: number;
maxTokens?: number;
temperature?: number;
topP?: number;
stop?: string | string[];
presencePenalty?: number;
frequencyPenalty?: number;
logitBias?: Record<string, number>;
user?: string;
// use stream mode for API response, the streamed tokens will be sent to `events in `ModelRequestOptions`
stream?: boolean;
}
To send a completion request to a model:
const text: ModelResponse = await openai.textCompletion(api: CompletionApi, prompt: string, options: ModelRequestOptions);
const completion: ModelResponse = await openai.chatCompletion(api: CompletionApi, messages: ChatCompletionRequestMessage, options: ModelRequestOptions);
// respond to existing chat session, preserving the past messages
const response: ModelResponse = await completion.respond(message: ChatCompletionRequestMessage, options: ModelRequestOptions);
options You can override the default request options via this parameter. A request will automatically be retried if there is a ratelimit or server error.
type ModelRequestOptions = {
// set to automatically add system message (only relevant when using textCompletion)
systemMessage?: string | (() => string);
// send a prefix to the model response so the model can continue generating from there, useful for steering the model towards certain output structures.
// the response prefix WILL be appended to the model response.
// for Anthropic's models ONLY
responsePrefix?: string;
// function related parameters are for OpenAI's models ONLY
functions?: ModelFunction[];
// force the model to call the following function
callFunction?: string;
// default: 3
retries?: number;
// default: 30s
retryInterval?: number;
// default: 60s
timeout?: number;
// the minimum amount of tokens to allocate for the response. if the request is predicted to not have enough tokens, it will automatically throw a 'TokenError' without sending the request
// default: 200
minimumResponseTokens?: number;
// the maximum amount of tokens to use for response
// NOTE: in OpenAI models, setting this option also requires contextSize in ModelConfig to be set
maximumResponseTokens?: number;
};
Completion responses are in the following format:
interface ModelResponse {
content?: string;
// used to parse function responses
name?: string;
arguments?: JsonValue;
usage?: {
promptTokens: number;
completionTokens: number;
totalTokens: number;
};
// function to send another message in the same 'chat', this will automatically append a new message to the messages array
respond: (
message: ChatCompletionRequestMessage,
opt?: ModelRequestOptions,
) => Promise<ModelResponse>;
}
A common error with LLM APIs is token usage - you are only allowed to fit a certain amount of data in the context window.
If you set a contextSize key, llm-api will automatically determine if the request will breach the token limit BEFORE sending the actual request to the model provider (e.g. OpenAI). This will save one network round-trip call and let you handle these type of errors in a responsive manner.
const openai = new OpenAIChatApi(
{ apiKey: 'YOUR_OPENAI_KEY' },
{ model: 'gpt-4-0613', contextSize: 8129 },
);
try {
const res = await openai.textCompletion(...);
} catch (e) {
if (e instanceof TokenError) {
// handle token errors...
}
}
llm-api also comes with support for Azure's OpenAI models. The Azure version is usually much faster and more reliable than OpenAI's own API endpoints. In order to use the Azure endpoints, you must include 2 Azure specific options when initializing the OpenAI model, azureDeployment and azureEndpoint. The apiKey field will also now be used for the Azure API key.
You can find the Azure API key and endpoint in the Azure Portal. The Azure Deployment must be created under the Azure AI Portal.
Note that the model parameter in ModelConfig will be ignored when using Azure. This is because in the Azure system, the model is selected on deployment creation, not on run time.
const openai = new OpenAIChatApi({
apiKey: 'AZURE_OPENAI_KEY',
azureDeployment: 'AZURE_DEPLOYMENT_NAME',
azureEndpoint: 'AZURE_ENDPOINT',
// optional, defaults to 2023-06-01-preview
azureApiVersion: 'YYYY-MM-DD',
});
Anthropic's models have the unique advantage of a large 100k context window and extremely fast performance. If no explicit model is specified, llm-api will default to the Claude Sonnet model.
const anthropic = new AnthropicChatApi(params: AnthropicConfig, config: ModelConfig);
Groq is a new LLM inference provider that provides the fastest inference speed on the market. They currently support Meta's Llama 2 and Mistral's Mixtral models.
const groq = new GroqChatApi(params: GroqConfig, config: ModelConfig);
const conf = {
accessKeyId: 'AWS_ACCESS_KEY',
secretAccessKey: 'AWS_SECRET_KEY',
};
const bedrock = new AnthropicBedrockChatApi(params: BedrockConfig, config: ModelConfig);
llm-api usese the debug module for logging & error messages. To run in debug mode, set the DEBUG env variable:
DEBUG=llm-api:* yarn playground
You can also specify different logging types via:
DEBUG=llm-api:error yarn playground
DEBUG=llm-api:log yarn playground
FAQs
Fully typed chat APIs for OpenAI and Azure's chat models - with token checking and retries
The npm package llm-api receives a total of 2,087 weekly downloads. As such, llm-api popularity was classified as popular.
We found that llm-api demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

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.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.