Integrating Fikra API with LangChain: Custom OpenAI Endpoint Guide
Integrating Fikra API with LangChain
This guide demonstrates how to use the Fikra API as a custom OpenAI-compatible endpoint within the LangChain framework. By configuring the base_url, you can seamlessly swap out the default OpenAI provider for Fikra's hosted models.
Prerequisites
- Python 3.8+ installed.
- LangChain and OpenAI packages installed:
bash pip install langchain openai - A valid Fikra API Key.
Step 1: Install Dependencies
Ensure you have the latest versions of the required libraries:
pip install --upgrade langchain openai
Step 2: Configure the Custom Base URL
The key to using Fikra API with LangChain is overriding the base_url parameter in the ChatOpenAI class. The Fikra API base URL is https://api.fikraapi.co.ke/v1.
Python Implementation
Below is the exact code block to initialize the LLM with the Fikra API endpoint:
from langchain_openai import ChatOpenAI
# Initialize the LLM with Fikra API as the custom OpenAI endpoint
llm = ChatOpenAI(
model="fikra-model-name", # Replace with the specific Fikra model ID
api_key="your-fikra-api-key", # Replace with your actual Fikra API key
base_url="https://api.fikraapi.co.ke/v1" # Critical: Point to Fikra API
)
# Test the connection
response = llm.invoke("Hello, Fikra! How are you?")
print(response.content)
JavaScript Implementation
If you are using LangChain.js, the configuration is similar:
import { ChatOpenAI } from "@langchain/openai";
// Initialize the LLM with Fikra API as the custom OpenAI endpoint
const llm = new ChatOpenAI({
model: "fikra-model-name", // Replace with the specific Fikra model ID
apiKey: "your-fikra-api-key", // Replace with your actual Fikra API key
configuration: {
baseURL: "https://api.fikraapi.co.ke/v1" // Critical: Point to Fikra API
}
});
// Test the connection
const response = await llm.invoke("Hello, Fikra! How are you?");
console.log(response.content);
Step 3: Verify the Integration
Run the script. If configured correctly, you should receive a response from the Fikra model. If you encounter a 401 Unauthorized error, double-check your API key. If you receive a 404 Not Found error, ensure the base_url is exactly https://api.fikraapi.co.ke/v1.
Troubleshooting
- Connection Errors: Ensure your network allows outbound HTTPS traffic to
api.fikraapi.co.ke. - Model Not Found: Verify that the
modelparameter matches an available model on the Fikra platform. - Timeouts: For large prompts, consider increasing the
timeoutparameter in theChatOpenAIconstructor.
Conclusion
By simply changing the base_url to https://api.fikraapi.co.ke/v1, you can leverage the power of Fikra's models within any LangChain application designed for OpenAI. This approach allows for easy switching between providers without refactoring your core logic.
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.