0.6.1 • Published 6 months ago

@api2ai/core v0.6.1

Weekly downloads
-
License
MIT
Repository
-
Last release
6 months ago

☁️⇨🤖🧠 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

0.6.1

6 months ago

0.6.0

9 months ago

0.5.0

9 months ago

0.4.0

9 months ago

0.3.1

9 months ago

0.3.0

10 months ago

0.2.1

10 months ago

0.2.0

10 months ago

0.1.2

10 months ago

0.1.1

10 months ago