Skip to content

AIInterface is a flexible AI interface class that supports multiple AI providers

Notifications You must be signed in to change notification settings

ai-pulse-matrix/ai-interface

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

  1. Import the AIInterface class:
const AIInterface = require("ai-interface");
  1. 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",
  },
};
  1. Initialize AIInterface:
const ai = new AIInterface(config);
  1. 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.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

This project is licensed under the MIT License.

About

AIInterface is a flexible AI interface class that supports multiple AI providers

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published