You might be in the situation I was the other day: I wanted to develop a small AI feature for learning purposes on my side project, but I didn’t want to pay for an API key. So I did some research and found 4 free methods to use LLM APIs, all compatible with the OpenAI SDK.
Code Setup
All four methods work with the same code, requiring only changes to environment variables. Here's a summary:
import OpenAI from 'openai';
const token = process.env.LLM_TOKEN!;
const endpoint = process.env.LLM_ENDPOINT!;
const model = process.env.LLM_MODEL!;
export async function prompt(userPrompt: string) {
const client = new OpenAI({ baseURL: endpoint, apiKey: token });
const response = await client.chat.completions.create({
messages: [
{ role: 'system', content: 'You are a helpful assistant.' },
{ role: 'user', content: userPrompt },
],
model: model,
});
console.log(response.choices[0].message.content);
}
For the full code, see GitHub.