Skip to content

Commit

Permalink
Merge pull request #81 from openchatai/feat/unify_flows_and_openapi_c…
Browse files Browse the repository at this point in the history
…hain

Unifying openapi chain with api flows
  • Loading branch information
gharbat authored Sep 21, 2023
2 parents 5410786 + 14b0ae9 commit fdbde64
Show file tree
Hide file tree
Showing 9 changed files with 531 additions and 22 deletions.
10 changes: 7 additions & 3 deletions llm-server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
from langchain.utilities.openapi import OpenAPISpec
from utils.base import try_to_match_and_call_api_endpoint
from models.models import AiResponseFormat
from flask_pymongo import PyMongo
import os
from routes.workflow.workflow_controller import workflow
import json
import logging
from typing import Any, Tuple
from prompts.base import api_base_prompt, non_api_base_prompt
from routes.workflow.workflow_service import run_workflow
from routes.workflow.typings.run_workflow_input import WorkflowData
from utils.detect_multiple_intents import hasMultipleIntents, hasSingleIntent

app = Flask(__name__)

Expand All @@ -30,13 +30,17 @@ def handle():
swagger_url = data.get("swagger_url")
base_prompt = data.get("base_prompt")
headers = data.get("headers", {})
server_base_url = data.get("server_base_url")

if not text:
return json.dumps({"error": "text is required"}), 400

if not swagger_url:
return json.dumps({"error": "swagger_url is required"}), 400

if not hasSingleIntent(swagger_url, text):
return run_workflow(WorkflowData(text, swagger_url, headers, server_base_url))

if not base_prompt:
return json.dumps({"error": "base_prompt is required"}), 400

Expand Down
161 changes: 161 additions & 0 deletions llm-server/docs/flows_apis/1.flows.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
## Create a workflow: This contains one workflow, that places an order for an available pet
```json
{
"_id" : ObjectId("65057ed7353fae69e7ddf896"),
"opencopilot" : "0.1",
"info" : {
"title" : "This contains multiple worksflows defined for the petstore app",
"version" : "1.0.0"
},
"flows" : [
{
"name" : "Add and retrieve the newly added pet to petstore",
"description" : "Add and retrieve a new pet",
"requires_confirmation" : false,
"steps" : [
{
"stepId" : "1",
"operation" : "call",
"open_api_operation_id" : "addPet",
"description" : "Add a pet"
},
{
"stepId" : "2",
"operation" : "call",
"open_api_operation_id" : "getPetById",
"parameters" : {
"user_verification" : "xx.response.some_key"
}
},
{
"operation" : "call",
"open_api_operation_id" : "placeOrder"
}
],
"on_success" : [
{
"handler" : "plotOutcomeJsFunction"
}
],
"on_failure" : [
{
"handler" : "plotOutcomeJsFunction"
}
]
}
]
}
```


## Run the workflow

```sh
curl --location 'http://127.0.0.1:5000/workflow/run_workflow' \
--header 'Content-Type: application/json' \
--data '{
"text": "Add and retrieve a pet her name is luna, id: 1101, she is a labrador, then set the ship date for 1 dog for 11th September 2023",
"swagger_src": "https://petstore3.swagger.io/api/v3/openapi.yaml",
"namespace": "workflows",
"server_base_url": "https://petstore.swagger.io/v2"
}'
```

Output
```json
{
"Workflow Name": null,
"addPet": {
"id": 1101,
"category": {
"id": 1,
"name": "Labrador"
},
"name": "Luna",
"photoUrls": [
"recently"
],
"tags": [
{
"id": 5726773248436745889,
"name": "music"
}
]
},
"getPetById": {
"id": 1101,
"category": {
"id": 1,
"name": "Labrador"
},
"name": "Luna",
"photoUrls": [
"recently"
],
"tags": [
{
"id": 5726773248436745889,
"name": "music"
}
]
},
"placeOrder": {
"id": 1101,
"petId": 1101,
"quantity": 1,
"shipDate": "2023-09-11T00:00:00.000+0000",
"status": "placed",
"complete": false
}
}
```

LLM Debug
---
```sh
> Entering new LLMChain chain...
Prompt after formatting:
To enable a substantially intelligent language model to execute a series of APIs sequentially, the following essential details are necessary to gather information needed for the next API call:
1. Initial input when starting the flow: `Add and retrieve a pet her name is luna, id: 1101, she is a labrador, then set the ship date for 1 dog for 11th September 2023`
2. Previous API responses: ``
3. A JSON response schema that defines the expected format: `{'id': {'type': 'integer', 'format': 'int64', 'example': 10}, 'name': {'type': 'string', 'example': 'doggie'}, 'category': {'type': 'object', 'properties': {'id': {'type': 'integer', 'format': 'int64', 'example': 1}, 'name': {'type': 'string', 'example': 'Dogs'}}, 'xml': {'name': 'category'}}, 'photoUrls': {'type': 'array', 'xml': {'wrapped': True}, 'items': {'type': 'string', 'xml': {'name': 'photoUrl'}}}, 'tags': {'type': 'array', 'xml': {'wrapped': True}, 'items': {'type': 'object', 'properties': {'id': {'type': 'integer', 'format': 'int64'}, 'name': {'type': 'string'}}, 'xml': {'name': 'tag'}}}, 'status': {'type': 'string', 'description': 'pet status in the store', 'enum': ['available', 'pending', 'sold']}}`

Here is a dummy example for expected output: ```{"id": 10, "name": "doggie", "category": {"id": 1, "name": "Dogs"}, "photoUrls": ["deep"], "tags": [{"id": 345171825301067173, "name": "simply"}], "status": "available"}```
The JSON payload, enclosed within triple backticks on both sides, strictly conforming to the specified "type/format" as outlined in the schema is as follows:

Failed to post https://api.smith.langchain.com/runs in LangSmith API. {"detail":"Invalid API key"}

> Finished chain.
Failed to patch https://api.smith.langchain.com/runs/6153d9ee-0a6b-4c0f-a41f-9a887fcf0429 in LangSmith API. {"detail":"Invalid API key"}


> Entering new LLMChain chain...
Prompt after formatting:
In order to facilitate the sequential execution of a highly intelligent language model with a series of APIs, we furnish the vital information required for executing the next API call.

The initial input at the onset of the process: Add and retrieve a pet her name is luna, id: 1101, she is a labrador, then set the ship date for 1 dog for 11th September 2023
The responses obtained from previous API calls: {"id":1101,"category":{"id":1,"name":"Labrador"},"name":"Luna","photoUrls":[],"tags":[],"status":"pending"}
A schema for request parameters that defines the expected format: [{'name': 'petId', 'in': 'path', 'description': 'ID of pet to return', 'required': True, 'schema': {'type': 'integer', 'format': 'int64'}}]

The JSON payload, which is used to represent the query parameters and is constructed using the initial input and previous API responses, must be enclosed within triple backticks on both sides. It must strictly adhere to the specified "type/format" guidelines laid out in the schema, and the structure is as follows:

> Finished chain.
Query params: {'petId': 1101, 'shipDate': '2023-09-11'}
Some key is not present in the requestBody dictionary.
Generator function output {"id": 10, "petId": 198772, "quantity": 7, "shipDate": "2012-01-06T14:09:10", "status": "approved", "complete": true}


> Entering new LLMChain chain...
Prompt after formatting:
To enable a substantially intelligent language model to execute a series of APIs sequentially, the following essential details are necessary to gather information needed for the next API call:
1. Initial input when starting the flow: `Add and retrieve a pet her name is luna, id: 1101, she is a labrador, then set the ship date for 1 dog for 11th September 2023`
2. Previous API responses: `{"id":1101,"category":{"id":1,"name":"Labrador"},"name":"Luna","photoUrls":[],"tags":[],"status":"pending"}`
3. A JSON response schema that defines the expected format: `{'id': {'type': 'integer', 'format': 'int64', 'example': 10}, 'petId': {'type': 'integer', 'format': 'int64', 'example': 198772}, 'quantity': {'type': 'integer', 'format': 'int32', 'example': 7}, 'shipDate': {'type': 'string', 'format': 'date-time'}, 'status': {'type': 'string', 'description': 'Order Status', 'example': 'approved', 'enum': ['placed', 'approved', 'delivered']}, 'complete': {'type': 'boolean'}}`

Here is a dummy example for expected output: ```{"id": 10, "petId": 198772, "quantity": 7, "shipDate": "2012-01-06T14:09:10", "status": "approved", "complete": true}```
The JSON payload, enclosed within triple backticks on both sides, strictly conforming to the specified "type/format" as outlined in the schema is as follows:


> Finished chain.
127.0.0.1 - - [20/Sep/2023 22:04:36] "POST /workflow/run_workflow HTTP/1.1" 200 -
```
175 changes: 175 additions & 0 deletions llm-server/docs/flows_apis/2.flows.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
## Create a workflow: This contains one workflow, that places an order for an available pet
```json
{
"opencopilot": "0.1",
"info": {
"title": "This contains one workflow, that places an order for an available pet",
"version": "1.0.0"
},
"flows": [
{
"name": "Gets a list of available pets and places an order for the first available pet in that list",
"description": "Gets a list of available pets and places an order for the first available pet in that list",
"requires_confirmation": false,
"steps": [
{
"stepId": "1",
"operation": "call",
"open_api_operation_id": "findPetsByStatus",
"description": "Gets a list of available pets by status"
},
{
"stepId": "2",
"operation": "call",
"open_api_operation_id": "placeOrder",
"description": "Places an order for a pet",
"parameters": {
"user_verification": "xx.response.some_key"
}
}
],
"on_success": [
{
"handler": "plotOutcomeJsFunction"
}
],
"on_failure": [
{
"handler": "plotOutcomeJsFunction"
}
]
}
]
}
```


## Run the workflow

```sh
curl --location 'http://127.0.0.1:5000/handle' \
--header 'Content-Type: application/json' \
--data '{
"text": "Get the list of pets and add the first available pet in that list",
"swagger_url": "https://petstore3.swagger.io/api/v3/openapi.yaml",
"namespace": "workflows",
"server_base_url": "https://petstore3.swagger.io/api/v3"
}'
```

Output
```json
{
"Workflow Name": null,
"findPetsByStatus": [
{
"id": 9,
"category": {
"id": 4,
"name": "Lions"
},
"name": "Lion 3",
"photoUrls": [
"url1",
"url2"
],
"tags": [
{
"id": 1,
"name": "tag3"
},
{
"id": 2,
"name": "tag4"
}
],
"status": "available"
},
{
"id": 1,
"name": "Dogs",
"photoUrls": [
"adipisicing laboris"
],
"tags": [
{
"id": -24265804,
"name": "ex incididunt"
}
],
"status": "available"
},
{
"id": 1101,
"category": {
"id": 1,
"name": "Labrador"
},
"name": "Luna",
"photoUrls": [
""
],
"tags": [],
"status": "available"
},
{
"id": 11,
"category": {
"id": 1,
"name": "Dogs"
},
"name": "doggieMK",
"photoUrls": [
"string"
],
"tags": [
{
"id": 0,
"name": "string"
}
],
"status": "available"
}
],
"placeOrder": {
"id": 9,
"petId": 4,
"quantity": 1,
"shipDate": "2020-07-01T12:00:00.000+00:00",
"status": "placed",
"complete": false
}
}
```

LLM Debug
---
```sh
> Entering new LLMChain chain...
Prompt after formatting:
In order to facilitate the sequential execution of a highly intelligent language model with a series of APIs, we furnish the vital information required for executing the next API call.

The initial input at the onset of the process: Get the list of pets and add the first available pet in that list
The responses obtained from previous API calls:
A schema for request parameters that defines the expected format: [{'name': 'status', 'in': 'query', 'description': 'Status values that need to be considered for filter', 'required': False, 'explode': True, 'schema': {'type': 'string', 'default': 'available', 'enum': ['available', 'pending', 'sold']}}]

The JSON payload, which is used to represent the query parameters and is constructed using the initial input and previous API responses, must be enclosed within triple backticks on both sides. It must strictly adhere to the specified "type/format" guidelines laid out in the schema, and the structure is as follows:
Failed to post https://api.smith.langchain.com/runs in LangSmith API. {"detail":"Invalid API key"}

> Finished chain.
Query params: {'status': 'available'}
Some key is not present in the requestBody dictionary.
Failed to patch https://api.smith.langchain.com/runs/5c2d36c9-091c-4ae4-a3bc-79f4b8205705 in LangSmith API. {"detail":"Invalid API key"}
Generator function output {"id": 10, "petId": 198772, "quantity": 7, "shipDate": "2008-04-03T18:41:32", "status": "approved", "complete": true}


> Entering new LLMChain chain...
Prompt after formatting:
To enable a substantially intelligent language model to execute a series of APIs sequentially, the following essential details are necessary to gather information needed for the next API call:
1. Initial input when starting the flow: `Get the list of pets and add the first available pet in that list`
2. Previous API responses: `[{"id":9,"category":{"id":4,"name":"Lions"},"name":"Lion 3","photoUrls":["url1","url2"],"tags":[{"id":1,"name":"tag3"},{"id":2,"name":"tag4"}],"status":"available"},{"id":1,"name":"Dogs","photoUrls":["adipisicing laboris"],"tags":[{"id":-24265804,"name":"ex incididunt"}],"status":"available"},{"id":1101,"category":{"id":1,"name":"Labrador"},"name":"Luna","photoUrls":[""],"tags":[],"status":"available"},{"id":11,"category":{"id":1,"name":"Dogs"},"name":"doggieMK","photoUrls":["string"],"tags":[{"id":0,"name":"string"}],"status":"available"}]`
3. A JSON response schema that defines the expected format: `{'id': {'type': 'integer', 'format': 'int64', 'example': 10}, 'petId': {'type': 'integer', 'format': 'int64', 'example': 198772}, 'quantity': {'type': 'integer', 'format': 'int32', 'example': 7}, 'shipDate': {'type': 'string', 'format': 'date-time'}, 'status': {'type': 'string', 'description': 'Order Status', 'example': 'approved', 'enum': ['placed', 'approved', 'delivered']}, 'complete': {'type': 'boolean'}}`

Here is a dummy example for expected output: ```{"id": 10, "petId": 198772, "quantity": 7, "shipDate": "2008-04-03T18:41:32", "status": "approved", "complete": true}```
The JSON payload, enclosed within triple backticks on both sides, strictly conforming to the specified "type/format" as outlined in the schema is as follows:
```
Empty file.
13 changes: 13 additions & 0 deletions llm-server/routes/workflow/typings/run_workflow_input.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Headers:
def __init__(self) -> None:
self.data: dict[str, str] = {}


class WorkflowData:
def __init__(
self, text: str, swagger_url: str, headers: Headers, server_base_url: str
) -> None:
self.text = text
self.swagger_url = swagger_url
self.headers = headers
self.server_base_url = server_base_url
Loading

0 comments on commit fdbde64

Please sign in to comment.