Integrating Fikra API with Vercel AI SDK
Integrating Fikra API with Vercel AI SDK
This guide demonstrates how to use the Vercel AI SDK with Fikra API by configuring it as a custom OpenAI-compatible endpoint. This allows you to leverage Fikra's models while utilizing the robust features of the Vercel AI SDK.
Prerequisites
-
Install the necessary packages:
bash npm install ai @ai-sdk/openai -
Ensure you have a valid Fikra API key.
Step 1: Configure the OpenAI Provider
To use Fikra API, you need to create a custom OpenAI provider instance that points to the Fikra base URL. The Fikra API is OpenAI-compatible, so we can use the createOpenAI function from @ai-sdk/openai.
import { createOpenAI } from '@ai-sdk/openai';
// Create a custom OpenAI provider for Fikra API
const fikra = createOpenAI({
baseURL: 'https://api.fikraapi.co.ke/v1',
apiKey: process.env.FIKRA_API_KEY,
});
Step 2: Use the Fikra Model
Now you can use the Fikra model with the Vercel AI SDK. Replace fikra-model-name with the specific model identifier provided by Fikra.
import { generateText } from 'ai';
const { data } = await generateText({
model: fikra('fikra-model-name'),
prompt: 'Hello, how are you?',
});
console.log(data);
Step 3: Streaming Responses
You can also stream responses from the Fikra API using the streamText function.
import { streamText } from 'ai';
const { stream } = streamText({
model: fikra('fikra-model-name'),
prompt: 'Tell me a story.',
});
// Consume the stream
for await (const chunk of stream) {
process.stdout.write(chunk);
}
Environment Variables
Make sure to set the FIKRA_API_KEY environment variable in your .env file:
FIKRA_API_KEY=your_fikra_api_key_here
Conclusion
By following these steps, you can seamlessly integrate Fikra API with the Vercel AI SDK, enabling you to build powerful AI applications with minimal configuration.
James Miano
CTO & ML Engineer at Lacesse Ventures. James specializes in low-overhead LLM quantization processes, custom ternary weights architectures, and localized server optimization.
Stop paying for overpriced round-trip latency
Why route queries over Western servers when you can use low-overhead hardware located in Nairobi? Save 87% on your monthly inference spend. No minimum credit limits. M-Pesa ready.