Skip to content

Commit

Permalink
add http solution (#101)
Browse files Browse the repository at this point in the history
* add http solution

* edit session id
  • Loading branch information
daviddai1998 authored Feb 22, 2024
1 parent 3251c90 commit 5896e73
Show file tree
Hide file tree
Showing 30 changed files with 1,261 additions and 0 deletions.
23 changes: 23 additions & 0 deletions solidgpt/src/api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import uuid
from pydantic import BaseModel
from solidgpt.src.api.api_response import *
from solidgpt.src.configuration.configreader import ConfigReader
from solidgpt.src.manager.initializer import Initializer
from fastapi import FastAPI, UploadFile, File, HTTPException, Body, BackgroundTasks
from fastapi.responses import JSONResponse
Expand Down Expand Up @@ -357,3 +358,25 @@ async def get_graph_status_impl(body: dict = Body(...)):
message=f"Graph in unknown state.",
status=3,
), status_code=200)


@app.post("/httpsolution/v1")
async def http_solution_v1(body: dict = Body(...)):
logging.info("celery task: http solution graph")
graph_id = body['graph_id']
openai_key = body['openai_key']
requirement = body['requirement']
logging.info("celery task: http solution v1")
openai.api_key = openai_key

result = celery_task_http_solution.apply_async(args=[openai_key, requirement, graph_id])
graph_result = GraphResult(result, "HTTP Solution Graph")
graph_result_map[graph_id] = graph_result

return JSONResponse(content={
"message": f"Running HTTP Solution graph...",
"graph_id": graph_id,
"is_final": True,
"path": result,
"current_work_name": "HTTP Solution"
}, status_code=200)
20 changes: 20 additions & 0 deletions solidgpt/src/api/celery_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ def celery_task_autogen_analysis_graph(self, openai_key, requirement, onboarding
state='PROGRESS',
meta={'result': "", 'state_id': ""}
)

def autogen_message_input_callback():
data = redis_instance.blpop(self.request.id)
if data:
Expand All @@ -151,3 +152,22 @@ def autogen_update_result_callback(result):
g.execute()

return ""


@app.task(bind=True)
def celery_task_http_solution(self, openai_key, requirement, graph_id):
logging.info("celery task: http solution")
openai.api_key = openai_key
g = build_http_solution_graph(requirement, graph_id)

def update_progress(current_content):
self.update_state(
state='PROGRESS',
meta={'current_content': current_content}
)

g.callback_map[SKILL_NAME_HTTP_SOLUTION] = update_progress
g.init_node_dependencies()
g.execute()
text_result = g.display_result.get_result()
return text_result
1 change: 1 addition & 0 deletions solidgpt/src/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
SKILL_NAME_PROVIDE_TECH_SOLUTION = "Provide Tech Solution"
SKILL_NAME_REPO_CHAT = "Repo Chat"
SKILL_NAME_AUTOGEN_ANALYSIS = "AutoGen analysis"
SKILL_NAME_HTTP_SOLUTION = "HTTP Solution"
21 changes: 21 additions & 0 deletions solidgpt/src/manager/promptresource.py
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,27 @@
Relatived code files, and Background I provide below and your professional relatived knowledge
to response to the Requirements. The requirements as follow:"""

SCHEMA_DESIGN_PRINCIPLE = """
NOTICE
Role: You are a professional database engineer; the main goal is to follow user requirement to design a NoSQL collection schema.
ATTENTION: Output format carefully referenced "Format example".
ATTENTION: Only design ONE collection. Be concise, only includes the necessary property fields in this collection.
ATTENTION: DO NOT MODIFY id part. Only change name in the curly brackets. The output must not contain curly brackets.
ATTENTION: Only select the property types from 'timestamp', 'True', 'False' or data['{Property_name}']. Do not improvise field types.
***Format example***
-----
```
##Collection##: {name}
'id': str(uuid.uuid1()),
'{Property_name1}': data['{Property_name1}'],
'{Property_name2}': data['{Property_name2}'],
'{Property_name3}': False,
'createdAt': timestamp,
'updatedAt': timestamp,
```
-----
"""

def get_custom_skills_assumption_role_prompt(question_subject):
return f"""Assume you are the expert of {question_subject}.
I want to know the list of top 5 essential actual hard skills (no softskill) for the {question_subject}. Can you please list them for me and use && sign to seperate them?"""
Expand Down
167 changes: 167 additions & 0 deletions solidgpt/src/tools/templates/aws-python-flask-dynamodb-api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
<!--
title: 'Serverless Framework Python Flask API backed by DynamoDB on AWS'
description: 'This template demonstrates how to develop and deploy a simple Python Flask API service backed by DynamoDB running on AWS Lambda using the traditional Serverless Framework.'
layout: Doc
framework: v3
platform: AWS
language: Python
priority: 2
authorLink: 'https://github.com/serverless'
authorName: 'Serverless, inc.'
authorAvatar: 'https://avatars1.githubusercontent.com/u/13742415?s=200&v=4'
-->

# Serverless Framework Python Flask API service backed by DynamoDB on AWS

This template demonstrates how to develop and deploy a simple Python Flask API service, backed by DynamoDB, running on AWS Lambda using the traditional Serverless Framework.


## Anatomy of the template

This template configures a single function, `api`, which is responsible for handling all incoming requests thanks to configured `httpApi` events. To learn more about `httpApi` event configuration options, please refer to [httpApi event docs](https://www.serverless.com/framework/docs/providers/aws/events/http-api/). As the events are configured in a way to accept all incoming requests, `Flask` framework is responsible for routing and handling requests internally. The implementation takes advantage of `serverless-wsgi`, which allows you to wrap WSGI applications such as Flask apps. To learn more about `serverless-wsgi`, please refer to corresponding [GitHub repository](https://github.com/logandk/serverless-wsgi). The template also relies on `serverless-python-requirements` plugin for packaging dependencies from `requirements.txt` file. For more details about `serverless-python-requirements` configuration, please refer to corresponding [GitHub repository](https://github.com/UnitedIncome/serverless-python-requirements).

Additionally, the template also handles provisioning of a DynamoDB database that is used for storing data about users. The Flask application exposes two endpoints, `POST /users` and `GET /user/{userId}`, which allow to create and retrieve users.

## Usage

### Prerequisites

In order to package your dependencies locally with `serverless-python-requirements`, you need to have `Python3.9` installed locally. You can create and activate a dedicated virtual environment with the following command:

```bash
python3.9 -m venv ./venv
source ./venv/bin/activate
```

Alternatively, you can also use `dockerizePip` configuration from `serverless-python-requirements`. For details on that, please refer to corresponding [GitHub repository](https://github.com/UnitedIncome/serverless-python-requirements).

### Deployment

This example is made to work with the Serverless Framework dashboard, which includes advanced features such as CI/CD, monitoring, metrics, etc.

In order to deploy with dashboard, you need to first login with:

```
serverless login
```

install dependencies with:

```
npm install
```

and then perform deployment with:

```
serverless deploy
```

After running deploy, you should see output similar to:

```bash
Deploying aws-python-flask-dynamodb-api-project to stage dev (us-east-1)

✔ Service deployed to stack aws-python-flask-dynamodb-api-project-dev (182s)

endpoint: ANY - https://xxxxxxxx.execute-api.us-east-1.amazonaws.com
functions:
api: aws-python-flask-dynamodb-api-project-dev-api (1.5 MB)
```

_Note_: In current form, after deployment, your API is public and can be invoked by anyone. For production deployments, you might want to configure an authorizer. For details on how to do that, refer to [httpApi event docs](https://www.serverless.com/framework/docs/providers/aws/events/http-api/).

### Invocation

After successful deployment, you can create a new user by calling the corresponding endpoint:

```bash
curl --request POST 'https://xxxxxx.execute-api.us-east-1.amazonaws.com/users' --header 'Content-Type: application/json' --data-raw '{"name": "John", "userId": "someUserId"}'
```

Which should result in the following response:

```bash
{"userId":"someUserId","name":"John"}
```

You can later retrieve the user by `userId` by calling the following endpoint:

```bash
curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/users/someUserId
```

Which should result in the following response:

```bash
{"userId":"someUserId","name":"John"}
```

If you try to retrieve user that does not exist, you should receive the following response:

```bash
{"error":"Could not find user with provided \"userId\""}
```

### Local development

Thanks to capabilities of `serverless-wsgi`, it is also possible to run your application locally, however, in order to do that, you will need to first install `werkzeug`, `boto3` dependencies, as well as all other dependencies listed in `requirements.txt`. It is recommended to use a dedicated virtual environment for that purpose. You can install all needed dependencies with the following commands:

```bash
pip install werkzeug boto3
pip install -r requirements.txt
```

Additionally, you will need to emulate DynamoDB locally, which can be done by using `serverless-dynamodb-local` plugin. In order to do that, execute the following commands:

```bash
serverless plugin install -n serverless-dynamodb-local
serverless dynamodb install
```

It will add the plugin to `devDependencies` in `package.json` file as well as to `plugins` section in `serverless.yml`. Additionally, it will also install DynamoDB locally.

You should also add the following config to `custom` section in `serverless.yml`:


```yml
custom:
(...)
dynamodb:
start:
migrate: true
stages:
- dev
```
Additionally, we need to reconfigure DynamoDB Client to connect to our local instance of DynamoDB. We can take advantage of `IS_OFFLINE` environment variable set by `serverless-wsgi` plugin and replace:


```python
dynamodb_client = boto3.client('dynamodb')
```

with

```python
dynamodb_client = boto3.client('dynamodb')
if os.environ.get('IS_OFFLINE'):
dynamodb_client = boto3.client('dynamodb', region_name='localhost', endpoint_url='http://localhost:8000')
```

Now you can start DynamoDB local with the following command:

```bash
serverless dynamodb start
```

At this point, you can run your application locally with the following command:

```bash
serverless wsgi serve
```

For additional local development capabilities of `serverless-wsgi` and `serverless-dynamodb-local` plugins, please refer to corresponding GitHub repositories:
- https://github.com/logandk/serverless-wsgi
- https://github.com/99x/serverless-dynamodb-local
50 changes: 50 additions & 0 deletions solidgpt/src/tools/templates/aws-python-flask-dynamodb-api/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import os

import boto3
from flask import Flask, jsonify, make_response, request

app = Flask(__name__)


dynamodb_client = boto3.client('dynamodb')

if os.environ.get('IS_OFFLINE'):
dynamodb_client = boto3.client(
'dynamodb', region_name='localhost', endpoint_url='http://localhost:8000'
)


USERS_TABLE = os.environ['USERS_TABLE']


@app.route('/users/<string:user_id>')
def get_user(user_id):
result = dynamodb_client.get_item(
TableName=USERS_TABLE, Key={'userId': {'S': user_id}}
)
item = result.get('Item')
if not item:
return jsonify({'error': 'Could not find user with provided "userId"'}), 404

return jsonify(
{'userId': item.get('userId').get('S'), 'name': item.get('name').get('S')}
)


@app.route('/users', methods=['POST'])
def create_user():
user_id = request.json.get('userId')
name = request.json.get('name')
if not user_id or not name:
return jsonify({'error': 'Please provide both "userId" and "name"'}), 400

dynamodb_client.put_item(
TableName=USERS_TABLE, Item={'userId': {'S': user_id}, 'name': {'S': name}}
)

return jsonify({'userId': user_id, 'name': name})


@app.errorhandler(404)
def resource_not_found(e):
return make_response(jsonify(error='Not found!'), 404)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "aws-python-flask-dynamodb-api",
"version": "1.0.0",
"description": "Example of a Python Flask API service backed by DynamoDB with traditional Serverless Framework",
"author": "",
"devDependencies": {
"serverless-python-requirements": "^6.0.0",
"serverless-wsgi": "^3.0.1"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Flask==1.1.4
Werkzeug==1.0.1
markupsafe==2.0.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: aws-python-flask-dynamodb-api
org: serverlessinc
description: Deploys a Python Flask API service, backed by DynamoDB, with traditional Serverless Framework
keywords: aws, serverless, faas, lambda, python, flask, dynamodb
repo: https://github.com/serverless/examples/aws-python-flask-dynamodb-api
license: MIT
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
service: aws-python-flask-dynamodb-api

frameworkVersion: '3'

custom:
tableName: 'users-table-${sls:stage}'
wsgi:
app: app.app

provider:
name: aws
runtime: python3.9
iam:
role:
statements:
- Effect: Allow
Action:
- dynamodb:Query
- dynamodb:Scan
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:UpdateItem
- dynamodb:DeleteItem
Resource:
- Fn::GetAtt: [ UsersTable, Arn ]
environment:
USERS_TABLE: ${self:custom.tableName}

functions:
api:
handler: wsgi_handler.handler
events:
- httpApi: '*'

plugins:
- serverless-wsgi
- serverless-python-requirements

resources:
Resources:
UsersTable:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
- AttributeName: userId
AttributeType: S
KeySchema:
- AttributeName: userId
KeyType: HASH
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
TableName: ${self:custom.tableName}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.serverless
*.pyc
*.pyo
Loading

0 comments on commit 5896e73

Please sign in to comment.