npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ai-embedapi

v1.0.1

Published

A powerful JavaScript SDK to interact with multiple AI models (OpenAI, Anthropic, VertexAI, XAI) for text generation and other AI capabilities using the EmbedAPI service.

Downloads

126

Readme

AI Embed API - JavaScript SDK

A JavaScript SDK for interacting with the AI Embed API platform.

Installation

To install the SDK, run:

npm install ai-embedapi

Getting Started

Here's a quick example to get you started with the One AI API.

Import the SDK

const OneAIAPI = require('ai-embedapi');

Initialize the SDK

You need to instantiate the OneAIAPI class by providing your API key. You can obtain your API key from EmbedAPI.

const oneAI = new OneAIAPI('YOUR_API_KEY_HERE');

Example Usage

Text Generation Example (OpenAI)

(async () => {
  try {
    const response = await oneAI.generateText({
      service: 'openai',
      model: 'gpt-4o',
      messages: [
        { role: 'user', content: 'Hello, can you tell me about AI trends?' }
      ],
      maxTokens: 500,
      temperature: 0.7
    });

    console.log('Generated Response:', response);
  } catch (error) {
    console.error('Failed to generate text:', error);
  }
})();

Parameters

  • service (required): The AI service provider (e.g., openai, anthropic, vertexai, xai).
  • model (required): The model to use for text generation (e.g., gpt-4o, claude-3-5-sonnet-20241022).
  • messages (required): The array of messages to send to the model, typically for chat completion.
  • maxTokens (optional): The maximum number of tokens to generate.
  • temperature (optional): Controls randomness. Higher values (e.g., 0.9) will make the output more random, while lower values (0.2) will make it more focused and deterministic.
  • topP (optional): An alternative to sampling with temperature, called nucleus sampling.
  • frequencyPenalty (optional): Number between -2.0 and 2.0. Positive values penalize new tokens based on their frequency so far.
  • presencePenalty (optional): Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far.
  • stopSequences (optional): Up to 4 sequences where the API will stop generating further tokens.

Models Supported

The SDK allows you to use the following AI models:

OpenAI

  • gpt-4o: Advanced model with support for multiple modalities.
  • gpt-3.5-turbo: Fast and efficient, suitable for lightweight tasks.
  • gpt-3.5-turbo-16k: Extended context window version of GPT-3.5 Turbo.
  • o1-preview: Preview model designed for complex reasoning and problem-solving tasks.
  • o1-mini: Cost-efficient reasoning model for coding and tasks requiring limited world knowledge.

Anthropic

  • claude-3-5-sonnet-20241022: Balanced performance model.
  • claude-3-haiku-20240307: Fast response model for simple queries.
  • claude-3-opus-20240229: Powerful AI model for complex tasks.

VertexAI

  • gemini-1.5-pro: Advanced model from Google for a wide range of tasks.
  • gemini-1.5-flash: Faster variant of Gemini 1.5 Pro.
  • Other Vertex models from vertexModels list.

XAI

  • grok-beta: Latest model from XAI focused on conversational AI.

Error Handling

The SDK provides detailed error messages to help you understand any issues that arise during text generation. Errors are thrown with details such as status codes and messages to help debug effectively.

Example with Different AI Models

Using Anthropic

(async () => {
  try {
    const response = await oneAI.generateText({
      service: 'anthropic',
      model: 'claude-3-5-sonnet-20241022',
      messages: [
        { role: 'user', content: 'Explain quantum mechanics in simple terms.' }
      ],
      maxTokens: 400,
      temperature: 0.5
    });

    console.log('Generated Response:', response);
  } catch (error) {
    console.error('Failed to generate text:', error);
  }
})();

Using VertexAI

(async () => {
  try {
    const response = await oneAI.generateText({
      service: 'vertexai',
      model: 'gemini-1.5-pro',
      messages: [
        { role: 'user', content: 'What are the benefits of renewable energy?' }
      ],
      maxTokens: 300,
      temperature: 0.6
    });

    console.log('Generated Response:', response);
  } catch (error) {
    console.error('Failed to generate text:', error);
  }
})();

API Cost Calculation

The API calculates the cost of each request based on the model and the number of tokens used.

  • The SDK provides a calculateCost method internally to estimate the cost based on token usage.
  • Costs vary depending on the service and model used.
  • OpenAI tokens cost 0.03 per thousand tokens for GPT-4o, for example.

Testing

To run the tests, use:

npm test

License

This SDK is released under the MIT License.

Contributing

We welcome contributions! Please read our Contributing Guide for more details on how to contribute to the project.

Issues

If you encounter any issues, please report them on our GitHub Issues page.

Contact

For questions or support, please reach out to [email protected].