give me free API of open AI to make chatbot for our soft and our soft is related to renewable energy and solar, wind, coal, power etc so give me free API model to give correct response for our requirement but free and respose should be fast and realible -- tell me which model free best for this
I can help you build a chatbot for your renewable energy software. While OpenAI's models aren't free, there are several open-source alternatives you can use with the AI SDK 1.
Let me show you how to create a simple chatbot for renewable energy topics using the AI SDK with an open-source model:
For a free solution, you have several options:
To use these with the AI SDK, you'd need to create a custom model adapter. Here's how you could modify the chat route to use Ollama:
// Example of using Ollama with AI SDK
import { generateText } from 'ai'
// Create a custom model adapter for Ollama
const ollama = (modelName: string) => {
return {
id: `ollama:${modelName}`,
generate: async ({ prompt, system }) => {
const response = await fetch('http://localhost:11434/api/generate', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
model: modelName,
prompt: system ? `${system}\n\n${prompt}` : prompt,
stream: false
}),
})
const data = await response.json()
return { text: data.response }
}
}
}
export async function POST(req: Request) {
const { message } = await req.json()
const systemPrompt = `You are an expert in renewable energy...`
// Use Llama 3 or another model you've pulled to Ollama
const { text } = await generateText({
model: ollama('llama3'),
system: systemPrompt,
prompt: message,
})
return Response.json({ text })
}For the best free experience, I recommend:
These options will give you reliable responses for renewable energy topics without any cost 1.
Sources
The code returns the following error:
Error in chat API: OpenAI API key is missing. Pass it using the 'apiKey' parameter or the OPENAI_API_KEY environment variable.
at Module.POST (/app/api/chat/route)
at async handleSubmit (/app/page)
Revise the code to address the error.
I was previewing: /
I'll revise the code to address the error by implementing a mock response system that doesn't require an API key. This will allow you to test the UI while you decide which free model to use in production.
This revised code removes the dependency on OpenAI and implements a simple keyword-based response system. It's just for demonstration purposes, but it will work without any API keys.
For a production-ready free solution, here are your options:
Ollama lets you run open-source models locally. Here's how to integrate it:
// app/api/chat/route.ts
export async function POST(req: Request) {
const { message } = await req.json();
try {
// Call your locally running Ollama instance
const response = await fetch('http://localhost:11434/api/generate', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
model: 'llama3', // or any model you've pulled
prompt: `You are a renewable energy expert. Answer this question: ${message}`,
stream: false
}),
});
const data = await response.json();
return Response.json({ text: data.response });
} catch (error) {
console.error("Error calling Ollama:", error);
return Response.json({ text: "Error connecting to the model. Make sure Ollama is running." });
}
}// app/api/chat/route.ts
export async function POST(req: Request) {
const { message } = await req.json();
try {
const response = await fetch(
"https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2",
{
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer hf_..." // Free Hugging Face token
},
body: JSON.stringify({
inputs: `<s>[INST] You are a renewable energy expert. ${message} [/INST]`,
}),
}
);
const result = await response.json();
return Response.json({ text: result[0].generated_text });
} catch (error) {
console.error("Error:", error);
return Response.json({ text: "Error connecting to the model." });
}
}LocalAI is another option for self-hosting models that provides an OpenAI-compatible API, making it easy to use with the AI SDK 1.
The mock implementation I provided will work immediately without any API keys, while you set up one of these free alternatives for production use.