Add rest-api example (#889)

This commit is contained in:
Sidharth Mohanty
2023-11-03 13:02:51 +05:30
committed by GitHub
parent a054f7be9c
commit 8dd5cb9602
44 changed files with 1217 additions and 118 deletions

View File

@@ -0,0 +1,3 @@
---
openapi: post /{app_id}/add
---

View File

@@ -0,0 +1,3 @@
---
openapi: post /{app_id}/chat
---

View File

@@ -0,0 +1,3 @@
---
openapi: get /ping
---

View File

@@ -0,0 +1,3 @@
---
openapi: post /create
---

View File

@@ -0,0 +1,3 @@
---
openapi: delete /{app_id}/delete
---

View File

@@ -0,0 +1,3 @@
---
openapi: post /{app_id}/deploy
---

View File

@@ -0,0 +1,3 @@
---
openapi: get /apps
---

View File

@@ -0,0 +1,3 @@
---
openapi: get /{app_id}/data
---

View File

@@ -0,0 +1,102 @@
---
title: "🌍 Getting Started"
---
## Quick Start
To run Embedchain as a REST API server use,
```bash
docker run -d --name embedchain -p 8000:8000 embedchain/app:rest-api-latest
```
Open up your browser and navigate to http://0.0.0.0:8000/docs to interact with the API. There is a full-fledged Swagger docs playground with all the information
about the API endpoints.
![Swagger Docs Screenshot](https://github.com/embedchain/embedchain/assets/73601258/299d81e5-a0df-407c-afc2-6fa2c4286844)
## Creating your first App
App requires an `app_id` to be created. The `app_id` is a unique identifier for your app.
By default we will use the opensource **gpt4all** model to perform operations. You can also specify your own config by uploading a config YAML file.
For example, create a `config.yaml` file (adjust according to your requirements):
```yaml
app:
config:
id: "default-app"
llm:
provider: openai
config:
model: "gpt-3.5-turbo"
temperature: 0.5
max_tokens: 1000
top_p: 1
stream: false
template: |
Use the following pieces of context to answer the query at the end.
If you don't know the answer, just say that you don't know, don't try to make up an answer.
$context
Query: $query
Helpful Answer:
vectordb:
provider: chroma
config:
collection_name: "rest-api-app"
dir: db
allow_reset: true
embedder:
provider: openai
config:
model: "text-embedding-ada-002"
```
To learn more about custom configurations, check out the [Custom configurations](https://docs.embedchain.ai/advanced/configuration).
To explore more examples of config YAMLs for Embedchain, visit [embedchain/configs](https://github.com/embedchain/embedchain/tree/main/configs).
Now, you can upload this config file in the request body.
**Note:** To use custom models, an **API key** might be required. Refer to the table below to determine the necessary API key for your provider.
| Keys | Providers |
| -------------------------- | ------------------------------ |
| `OPENAI_API_KEY ` | OpenAI, Azure OpenAI, Jina etc |
| `OPENAI_API_TYPE` | Azure OpenAI |
| `OPENAI_API_BASE` | Azure OpenAI |
| `OPENAI_API_VERSION` | Azure OpenAI |
| `COHERE_API_KEY` | Cohere |
| `ANTHROPIC_API_KEY` | Anthropic |
| `JINACHAT_API_KEY` | Jina |
| `HUGGINGFACE_ACCESS_TOKEN` | Huggingface |
| `REPLICATE_API_TOKEN` | LLAMA2 |
To provide them, you can simply run the docker command with the `-e` flag.
For example,
```bash
docker run -d --name embedchain -p 8000:8000 -e OPENAI_API_KEY=YOUR_API_KEY embedchain/app:rest-api-latest
```
Cool! This will create a new Embedchain App with the given `app_id`.
## Deploying your App to Embedchain Platform
This feature is very powerful as it allows the creation of a public API endpoint for your app, enabling queries from anywhere. This creates a _pipeline_
for your app that can sync the data time to time and provide you with the best results.
![My first Pipeline](https://github.com/embedchain/embedchain/assets/73601258/266a66e0-330e-4bb9-aa97-687d826cd3fa)
To utilize this functionality, visit app.embedchain.ai and create an account. Subsequently, generate a new [API KEY](https://app.embedchain.ai/settings/keys/).
![Create Embedchain API Key](https://github.com/embedchain/embedchain/assets/73601258/791e92cc-4a6f-4740-94c2-f11cce13e93b)
Using this API key, you can deploy your app to the platform.

View File

@@ -0,0 +1,3 @@
---
openapi: post /{app_id}/query
---

View File

@@ -1,93 +0,0 @@
---
title: '🌍 API Server'
---
The API server example can be found [here](https://github.com/embedchain/embedchain/tree/main/examples/api_server).
It is a Flask based server that integrates the `embedchain` package, offering endpoints to add, query, and chat to engage in conversations with a chatbot using JSON requests.
### 🐳 Docker Setup
- Open variables.env, and edit it to add your 🔑 `OPENAI_API_KEY`.
- To setup your api server using docker, run the following command inside this folder using your terminal.
```bash
docker-compose up --build
```
📝 Note: The build command might take a while to install all the packages depending on your system resources.
### 🚀 Usage Instructions
- Your api server is running on [http://localhost:5000/](http://localhost:5000/)
- To use the api server, make an api call to the endpoints `/add`, `/query` and `/chat` using the json formats discussed below.
- To add data sources to the bot (/add):
```json
// Request
{
"data_type": "your_data_type_here",
"url_or_text": "your_url_or_text_here"
}
// Response
{
"data": "Added data_type: url_or_text"
}
```
- To ask queries from the bot (/query):
```json
// Request
{
"question": "your_question_here"
}
// Response
{
"data": "your_answer_here"
}
```
- To chat with the bot (/chat):
```json
// Request
{
"question": "your_question_here"
}
// Response
{
"data": "your_answer_here"
}
```
### 📡 Curl Call Formats
- To add data sources to the bot (/add):
```bash
curl -X POST \
-H "Content-Type: application/json" \
-d '{
"data_type": "your_data_type_here",
"url_or_text": "your_url_or_text_here"
}' \
http://localhost:5000/add
```
- To ask queries from the bot (/query):
```bash
curl -X POST \
-H "Content-Type: application/json" \
-d '{
"question": "your_question_here"
}' \
http://localhost:5000/query
```
- To chat with the bot (/chat):
```bash
curl -X POST \
-H "Content-Type: application/json" \
-d '{
"question": "your_question_here"
}' \
http://localhost:5000/chat
```
🎉 Happy Chatting! 🎉

View File

@@ -14,6 +14,7 @@
"dark": "#020415"
}
},
"openapi": ["/rest-api.json"],
"metadata": {
"og:image": "/images/og.png",
"twitter:site": "@embedchain"
@@ -28,8 +29,8 @@
"url": "https://twitter.com/embedchain"
},
{
"name":"Slack",
"url":"https://join.slack.com/t/embedchain/shared_invite/zt-22uwz3c46-Zg7cIh5rOBteT_xe1jwLDw"
"name": "Slack",
"url": "https://join.slack.com/t/embedchain/shared_invite/zt-22uwz3c46-Zg7cIh5rOBteT_xe1jwLDw"
},
{
"name": "Discord",
@@ -46,11 +47,20 @@
"navigation": [
{
"group": "Get started",
"pages": ["get-started/quickstart", "get-started/introduction", "get-started/faq", "get-started/examples"]
"pages": [
"get-started/quickstart",
"get-started/introduction",
"get-started/faq",
"get-started/examples"
]
},
{
"group": "Components",
"pages": ["components/llms", "components/embedding-models", "components/vector-databases"]
"pages": [
"components/llms",
"components/embedding-models",
"components/vector-databases"
]
},
{
"group": "Data sources",
@@ -81,16 +91,34 @@
"group": "Advanced",
"pages": ["advanced/configuration"]
},
{
"group": "Rest API",
"pages": [
"api-reference/getting-started",
"api-reference/check-status",
"api-reference/get-all-apps",
"api-reference/create-app",
"api-reference/query-an-app",
"api-reference/add-datasource-to-an-app",
"api-reference/get-datasources-associated-with-app-id",
"api-reference/deploy-app",
"api-reference/delete-app"
]
},
{
"group": "Examples",
"pages": ["examples/full_stack", "examples/api_server", "examples/discord_bot", "examples/slack_bot", "examples/telegram_bot", "examples/whatsapp_bot", "examples/poe_bot"]
"pages": [
"examples/full_stack",
"examples/discord_bot",
"examples/slack_bot",
"examples/telegram_bot",
"examples/whatsapp_bot",
"examples/poe_bot"
]
},
{
"group": "Community",
"pages": [
"community/connect-with-us",
"community/showcase"
]
"pages": ["community/connect-with-us", "community/showcase"]
},
{
"group": "Integrations",
@@ -108,16 +136,13 @@
},
{
"group": "Product",
"pages": [
"product/release-notes"
]
"pages": ["product/release-notes"]
}
],
"footerSocials": {
"website": "https://embedchain.ai",
"github": "https://github.com/embedchain/embedchain",
"slack":"https://join.slack.com/t/embedchain/shared_invite/zt-22uwz3c46-Zg7cIh5rOBteT_xe1jwLDw",
"slack": "https://join.slack.com/t/embedchain/shared_invite/zt-22uwz3c46-Zg7cIh5rOBteT_xe1jwLDw",
"discord": "https://discord.gg/6PzXDgEjG5",
"twitter": "https://twitter.com/embedchain",
"linkedin": "https://www.linkedin.com/company/embedchain"

428
docs/rest-api.json Normal file
View File

@@ -0,0 +1,428 @@
{
"openapi": "3.1.0",
"info": {
"title": "Embedchain REST API",
"description": "This is the REST API for Embedchain.",
"license": {
"name": "Apache 2.0",
"url": "https://github.com/embedchain/embedchain/blob/main/LICENSE"
},
"version": "0.0.1"
},
"paths": {
"/ping": {
"get": {
"tags": ["Utility"],
"summary": "Check Status",
"description": "Endpoint to check the status of the API.",
"operationId": "check_status_ping_get",
"responses": {
"200": {
"description": "Successful Response",
"content": { "application/json": { "schema": {} } }
}
}
}
},
"/apps": {
"get": {
"tags": ["Apps"],
"summary": "Get All Apps",
"description": "Get all apps.",
"operationId": "get_all_apps_apps_get",
"responses": {
"200": {
"description": "Successful Response",
"content": { "application/json": { "schema": {} } }
}
}
}
},
"/create": {
"post": {
"tags": ["Apps"],
"summary": "Create App",
"description": "Create a new app using App ID.",
"operationId": "create_app_using_default_config_create_post",
"parameters": [
{
"name": "app_id",
"in": "query",
"required": true,
"schema": { "type": "string", "title": "App Id" }
}
],
"requestBody": {
"content": {
"multipart/form-data": {
"schema": {
"allOf": [
{
"$ref": "#/components/schemas/Body_create_app_using_default_config_create_post"
}
],
"title": "Body"
}
}
}
},
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/DefaultResponse" }
}
}
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/HTTPValidationError" }
}
}
}
}
}
},
"/{app_id}/data": {
"get": {
"tags": ["Apps"],
"summary": "Get Datasources Associated With App Id",
"description": "Get all datasources for an app.",
"operationId": "get_datasources_associated_with_app_id__app_id__data_get",
"parameters": [
{
"name": "app_id",
"in": "path",
"required": true,
"schema": { "type": "string", "title": "App Id" }
}
],
"responses": {
"200": {
"description": "Successful Response",
"content": { "application/json": { "schema": {} } }
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/HTTPValidationError" }
}
}
}
}
}
},
"/{app_id}/add": {
"post": {
"tags": ["Apps"],
"summary": "Add Datasource To An App",
"description": "Add a source to an existing app.",
"operationId": "add_datasource_to_an_app__app_id__add_post",
"parameters": [
{
"name": "app_id",
"in": "path",
"required": true,
"schema": { "type": "string", "title": "App Id" }
}
],
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/SourceApp" }
}
}
},
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/DefaultResponse" }
}
}
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/HTTPValidationError" }
}
}
}
}
}
},
"/{app_id}/query": {
"post": {
"tags": ["Apps"],
"summary": "Query An App",
"description": "Query an existing app.",
"operationId": "query_an_app__app_id__query_post",
"parameters": [
{
"name": "app_id",
"in": "path",
"required": true,
"schema": { "type": "string", "title": "App Id" }
}
],
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/QueryApp" }
}
}
},
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/DefaultResponse" }
}
}
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/HTTPValidationError" }
}
}
}
}
}
},
"/{app_id}/chat": {
"post": {
"tags": ["Apps"],
"summary": "Chat With An App",
"description": "Query an existing app.\n\napp_id: The ID of the app. Use \"default\" for the default app.\n\nmessage: The message that you want to send to the App.",
"operationId": "chat_with_an_app__app_id__chat_post",
"parameters": [
{
"name": "app_id",
"in": "path",
"required": true,
"schema": { "type": "string", "title": "App Id" }
}
],
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/MessageApp" }
}
}
},
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/DefaultResponse" }
}
}
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/HTTPValidationError" }
}
}
}
}
}
},
"/{app_id}/deploy": {
"post": {
"tags": ["Apps"],
"summary": "Deploy App",
"description": "Deploy an existing app.",
"operationId": "deploy_app__app_id__deploy_post",
"parameters": [
{
"name": "app_id",
"in": "path",
"required": true,
"schema": { "type": "string", "title": "App Id" }
}
],
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/DeployAppRequest" }
}
}
},
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/DefaultResponse" }
}
}
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/HTTPValidationError" }
}
}
}
}
}
},
"/{app_id}/delete": {
"delete": {
"tags": ["Apps"],
"summary": "Delete App",
"description": "Delete an existing app.",
"operationId": "delete_app__app_id__delete_delete",
"parameters": [
{
"name": "app_id",
"in": "path",
"required": true,
"schema": { "type": "string", "title": "App Id" }
}
],
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/DefaultResponse" }
}
}
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/HTTPValidationError" }
}
}
}
}
}
}
},
"components": {
"schemas": {
"Body_create_app_using_default_config_create_post": {
"properties": {
"config": { "type": "string", "format": "binary", "title": "Config" }
},
"type": "object",
"title": "Body_create_app_using_default_config_create_post"
},
"DefaultResponse": {
"properties": { "response": { "type": "string", "title": "Response" } },
"type": "object",
"required": ["response"],
"title": "DefaultResponse"
},
"DeployAppRequest": {
"properties": {
"api_key": {
"type": "string",
"title": "Api Key",
"description": "The Embedchain API key for App deployments.",
"default": ""
}
},
"type": "object",
"title": "DeployAppRequest",
"example":{
"api_key":"ec-xxx"
}
},
"HTTPValidationError": {
"properties": {
"detail": {
"items": { "$ref": "#/components/schemas/ValidationError" },
"type": "array",
"title": "Detail"
}
},
"type": "object",
"title": "HTTPValidationError"
},
"MessageApp": {
"properties": {
"message": {
"type": "string",
"title": "Message",
"description": "The message that you want to send to the App.",
"default": ""
}
},
"type": "object",
"title": "MessageApp"
},
"QueryApp": {
"properties": {
"query": {
"type": "string",
"title": "Query",
"description": "The query that you want to ask the App.",
"default": ""
}
},
"type": "object",
"title": "QueryApp",
"example":{
"query":"Who is Elon Musk?"
}
},
"SourceApp": {
"properties": {
"source": {
"type": "string",
"title": "Source",
"description": "The source that you want to add to the App.",
"default": ""
},
"data_type": {
"anyOf": [{ "type": "string" }, { "type": "null" }],
"title": "Data Type",
"description": "The type of data to add, remove it if you want Embedchain to detect it automatically.",
"default": ""
}
},
"type": "object",
"title": "SourceApp",
"example":{
"source":"https://en.wikipedia.org/wiki/Elon_Musk"
}
},
"ValidationError": {
"properties": {
"loc": {
"items": { "anyOf": [{ "type": "string" }, { "type": "integer" }] },
"type": "array",
"title": "Location"
},
"msg": { "type": "string", "title": "Message" },
"type": { "type": "string", "title": "Error Type" }
},
"type": "object",
"required": ["loc", "msg", "type"],
"title": "ValidationError"
}
}
}
}