⚡ Create an API assistant from any OpenAPI Spec ⚡
![api2ai demo with multiple APIs](https://private-user-images.githubusercontent.com/138784/255413185-6719fdb2-6687-4768-a599-d61d7ab454a6.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQ0MzI1MjIsIm5iZiI6MTcyNDQzMjIyMiwicGF0aCI6Ii8xMzg3ODQvMjU1NDEzMTg1LTY3MTlmZGIyLTY2ODctNDc2OC1hNTk5LWQ2MWQ3YWI0NTRhNi5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwODIzJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDgyM1QxNjU3MDJaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT0xMTA2NGFlNTM4MzczZGY4NDk1ODVkM2NkODE5ZTgxN2M0ODNkZjcyODk0YmNlODE1YTgzOWU5NTZkNjgxYjZhJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.-05fZNBJuOJMUK8eLNMBJM41oOZSBJpnfCt6Q1GhUrw)
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](https://private-user-images.githubusercontent.com/138784/255413549-aead4548-7d61-4ec6-8228-7c999e182cf0.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQ0MzI1MjIsIm5iZiI6MTcyNDQzMjIyMiwicGF0aCI6Ii8xMzg3ODQvMjU1NDEzNTQ5LWFlYWQ0NTQ4LTdkNjEtNGVjNi04MjI4LTdjOTk5ZTE4MmNmMC5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwODIzJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDgyM1QxNjU3MDJaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT0zMTA2YjkzMDcxYTBhMzI4Y2RiODJhZTZjNDg4MDJlMTkyNzhiYTRiMjBlYjMwMjQ5Y2JlNGJiMmE0MDNmNDQwJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.EM-zCVoo4uR0tneycmY7lwR41v8PmQz2aoLfzgKNKF0)
npm install --save @api2ai/core
yarn add --save @api2ai/core
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"
}
]
}
}
}
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.
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.
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.
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