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

@api2ai/core

v0.6.1

Published

⚡ Create an API assistant from any OpenAPI Spec ⚡

Downloads

18

Readme

☁️⇨🤖🧠 api2ai

⚡ Create an API assistant from any OpenAPI Spec ⚡

Features

api2ai lets you interface with any API using plain English or any natural language.

  • Automatically parses OpenAPI spec and auth schemes
  • Selects endpoint and parses arguments provided in user prompt into query and body params.
  • Invokes the API call and return the response
  • Comes with a local API

Installation

npm install --save @api2ai/core

yarn add --save @api2ai/core

Quickstart

The following example uses OpenAI API, essentially creating a single interface for all OpenAI endpoints. Please check out the api code for more details.

import { ApiAgent } from "@api2ai/core";

const OPEN_AI_KEY = "sk-...";

const agent = new ApiAgent({
  apiKey: OPEN_AI_KEY,
  model: "gpt-3.5-turbo-1106", // "gpt-4-1106-preview" also works
  apis: [
    {
      filename: "path/to/open-api-spec.yaml",
      auth: { token: "sk-...." },
    },
    {
      filename: "url/to/another-open-api-spec.yaml",
      auth: { username: "u$er", password: "pa$$word" },
    },
  ],
});

const result = await agent.execute({
  userPrompt: "Create an image of Waikiki beach",
  verbose: true, // default: false
});

// Sanitized output of result
{
  "userPrompt": "Create an image of Waikiki beach",
  "selectedOperation": "createImage",
  "request": {
    "url": "https://api.openai.com/v1/images/generations",
    "method": "post",
    "headers": {
      "Content-Type": "application/json",
      "Authorization": "Bearer sk-..."
    },
    "body": "{\"prompt\":\"Waikiki beach\"}"
  },
  "response": {
    "headers": {},
    "status": 200,
    "body": {
      "created": 1691253354,
      "data": [
        {
          "url": "https://oaidalleapiprodscus.blob.core.windows.net/private/org-mSgbuBJYTxIWjjopcJpDnkwh/user-.../img-ZsEtynyCxFIYTlDfWor0mTJP.png?st=2023-08-05T15%3A35%3A54Z&se=2023-08-05T17%3A35%3A54Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2023-08-04T18%3A09%3A06Z&ske=2023-08-05T18%3A09%3A06Z&sks=b&skv=2021-08-06&sig=ZYKOP%2BGlz60di2sCiHMWL5ssruXyGMlAUFmQx/aXmqA%3D"
        }
      ]
    }
  }
}

Using the agent via the API

To run the server in your machine, please clone the repo and follow the instruction in Development & Contributing section.

We use dotenv to store environment variables. Please create an .env file in the project's root directory and add your openai key

OPEN_AI_KEY=sk-...

Start the server

yarn dev

Make an api call

fetch("http://localhost:5555/api/run", {
  headers: { "Content-Type": "application/json" },
  method: "POST",
  body: JSON.stringify({
    userPrompt:
      "Create an image of an astronaut swimming with dolphins in clear water ocean",
  }),
});

Configure the server/pages/api/api2ai.config.ts file to add your own APIs. Follow the existing template in this file. You may add as many files as you want.

OpenAPI Spec

api2ai parses valid OAS files to determine which endpoint and parameters to use. Please ensure your OAS contains descriptive parameters and requestBody schema definition. We currently support OAS version 3.0.0 and above.

Tips: We leverage the summary fields to determine which endpoint to use. You can tweak your prompt according to the summary text for better result.

Authentication

Configure your API auth credentials under the auth key for applicable APIs:

// server/pages/api/api2ai.config.ts
export const configs = {
  model: "gpt-3.5-turbo-1106",
  token: process.env["OPEN_AI_KEY"],
  apis: [
    {
      file: "path/to/your-open-api-spec.yaml",
      auth: { token: process.env["MY_API_KEY"] },
    },
  ],
};

Currently, we support the following auth schemes:

Please ensure securitySchemes fields are properly defined. Refer to the Swagger doc for more details.

Development & Contributing

We use yarn and turbo. Please clone the repo and install both in order to run the demo and build packages in your machine.

yarn install
yarn build

To run the server

yarn dev

Access the app from http://localhost:5555/

To run all tests

yarn test

Run a single test file

turbo run test -- core/src/api/__tests__/operation.test.ts