ai-interface v1.0.6
AIInterface
AIInterface is a flexible AI interface class that supports multiple AI providers, including OpenAI, Azure, Google Gemini, G4F, GPT4JS, other APIs, and Deepseek.
Installation
First, ensure you have installed all necessary dependencies:
npm install ai-interface
Usage
- Import the AIInterface class:
const AIInterface = require("ai-interface");
- Create a configuration object:
const config = {
provider: "openai", // or 'azure', 'gemini', 'g4f', 'gpt4js', 'other', 'deepseek'
default: {
// Default configuration
},
openai: {
apiKey: "your-openai-api-key",
modelName: "gpt-3.5-turbo",
baseUrl: "https://api.openai.com/v1",
},
// Configuration for other providers...
};
- Initialize AIInterface:
const ai = new AIInterface(config);
- Call the AI interface:
async function main() {
const prompt = "Hello, can you tell me about today's weather?";
try {
const response = await ai.callAIInterface(prompt);
console.log(response);
} catch (error) {
console.error("Error:", error);
}
}
main();
Supported Providers
- OpenAI
- Azure OpenAI
- Google Gemini
- G4F
- GPT4JS
- Deepseek
- Moonshot
- Other API
Configuration
Each provider requires specific configuration. Ensure that you include the necessary API keys, model names, and base URLs in the configuration object.
OpenAI Configuration
openai: {
apiKey: "your-openai-api-key",
modelName: "gpt-3.5-turbo",
baseUrl: "https://api.openai.com/v1",
}
Azure Configuration
azure: {
apiKey: "your-azure-api-key",
modelName: "your-azure-model",
baseUrl: "your-azure-endpoint",
}
Google Gemini Configuration
gemini: {
apiKey: "your-gemini-api-key",
modelName: "gemini-pro",
}
Other API Configuration
otherAI: {
baseUrl: "your-other-api-endpoint",
data: {
// Additional data to send with the request
},
requestConfig: {
// Axios request configuration
},
responsePath: "data.choices[0].message.content",
}
Error Handling
The AIInterface class throws errors when configuration is missing or API calls fail. Ensure you handle these errors appropriately in your application.
Advanced Usage
Switching Providers
You can switch between providers by updating the provider
field in your configuration:
config.provider = "azure";
const azureAI = new AIInterface(config);
Other Provider Implementation
To add a new provider, extend the AIInterface
class and implement a new method for your provider:
class ExtendedAIInterface extends AIInterface {
async callNewProvider(prompt) {
// Implement the API call for the new provider
}
}
Contributing
Contributions are welcome! Please feel free to submit issues and pull requests to improve this project.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature
) - Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
License
This project is licensed under the MIT License.