Git Product home page Git Product logo

api2ai's Introduction

☁️⇨🤖🧠 api2ai

⚡ Create an API assistant from any OpenAPI Spec ⚡

api2ai demo with multiple APIs

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
api2ai demo with multiple languages

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

api2ai's People

Contributors

mquan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.