AIInterface is a flexible AI interface class that supports multiple AI providers, including OpenAI, Azure, Google Gemini, G4F, GPT4JS, other APIs, and Deepseek.
First, ensure you have installed all necessary dependencies:
npm install ai-interface
- 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...
};
// or
const config = {
provider: "moonshot",
moonshot: {
apiKey: "xxxxxx-xxxxxx",
},
};
- 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();
- OpenAI
- Azure OpenAI
- Google Gemini
- G4F
- GPT4JS
- Deepseek
- Moonshot
- Other API
Each provider requires specific configuration. Ensure that you include the necessary API keys, model names, and base URLs in the configuration object.
openai: {
apiKey: "your-openai-api-key",
modelName: "gpt-3.5-turbo",
baseUrl: "https://api.openai.com/v1",
}
azure: {
apiKey: "your-azure-api-key",
modelName: "your-azure-model",
baseUrl: "your-azure-endpoint",
}
gemini: {
apiKey: "your-gemini-api-key",
modelName: "gemini-pro",
}
otherAI: {
baseUrl: "your-other-api-endpoint",
data: {
// Additional data to send with the request
},
requestConfig: {
// Axios request configuration
},
responsePath: "data.choices[0].message.content",
}
The AIInterface class throws errors when configuration is missing or API calls fail. Ensure you handle these errors appropriately in your application.
You can switch between providers by updating the provider
field in your configuration:
config.provider = "azure";
const azureAI = new AIInterface(config);
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
}
}
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
This project is licensed under the MIT License.