-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from DennisTraub/main
Python Foundation Model (FM) Playground
- Loading branch information
Showing
61 changed files
with
3,162 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.idea/ | ||
.vscode/ | ||
__pycache__/ | ||
|
||
**/venv/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,102 @@ | ||
## My Project | ||
# 🐍 Python FM Playground | ||
|
||
TODO: Fill this README out! | ||
Welcome to the Python Foundation Model (FM) Playground, an example app to explore how to use **Amazon Bedrock** with boto3, the AWS SDK for Python. | ||
|
||
Be sure to: | ||
> 🚨 **Important:** This application is for educational purposes and not intended for production use. | ||
* Change the title in this README | ||
* Edit your repository description on GitHub | ||
## Overview | ||
|
||
## Security | ||
This repository includes a **FastAPI** application and a **Next.js** frontend, both executable locally. Below is a screenshot of the app in action. | ||
|
||
See [CONTRIBUTING](CONTRIBUTING.md#security-issue-notifications) for more information. | ||
|
||
<img src="screenshot.png" alt="Screenshot of the Python FM Playground" width="720" /> | ||
|
||
## Prerequisites | ||
|
||
Ensure you have the following installed: | ||
|
||
- [Python 3.8+](https://www.python.org/downloads/) with pip | ||
- [Node.js (v18.17+)](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) with npm (for the Next.js frontend) | ||
- An [AWS account](https://aws.amazon.com/free/) with permissions to access Amazon Bedrock | ||
- To use Bedrock, you must [enable access](https://docs.aws.amazon.com/bedrock/latest/userguide/model-access.html#add-model-access) to at least the following models in `us-east-1`: | ||
1. Anthropic: Claude | ||
2. Stability AI: Stable Diffusion XL | ||
|
||
## Running the Application | ||
|
||
After verifying the prerequisites, follow these steps: | ||
|
||
### Clone the repository | ||
|
||
Open a terminal, navigate to a directory of your choice, and execute the following command: | ||
|
||
```shell | ||
git clone https://github.com/build-on-aws/python-fm-playground.git | ||
``` | ||
|
||
### Backend Setup | ||
|
||
**Note:** It's a good practice to use a virtual environment for your Python projects to manage dependencies separately for each project. If you're not already in a virtual environment, [you can create one and then install your packages there](https://packaging.python.org/en/latest/guides/installing-using-pip-and-virtual-environments/). | ||
|
||
In the `python-fm-playground/backend` directory, run: | ||
|
||
```shell | ||
pip install -r requirements.txt | ||
python main.py | ||
``` | ||
|
||
> 🛠 The backend runs on port 55500 by default. See below for port changes. | ||
### Frontent Setup | ||
|
||
In a new terminal window, navigate to `python-fm-playground/frontend` and execute: | ||
|
||
```shell | ||
npm install | ||
npm run dev | ||
``` | ||
|
||
> 🛠 The frontend runs on port 3000 by default. See below for port changes. | ||
## Accessing the Application | ||
|
||
Open `http://localhost:3000` in your web browser to interact with the application. | ||
|
||
## Stopping the Application | ||
|
||
To halt the application, you will need to stop both the backend and frontend processes. | ||
|
||
### Stopping the Frontend | ||
|
||
In the terminal where the frontend is running, press `Ctrl + C` to terminate the process. | ||
|
||
### Stopping the Backend | ||
|
||
Similarly, in the backend terminal, use the `Ctrl + C` shortcut to stop the server. | ||
|
||
If you encounter any issues, you can forcefully terminate the processes by finding the process ID (`PID`) and using the `kill` command on Unix-based systems or Task Manager on Windows. | ||
|
||
|
||
## Using Different Ports | ||
|
||
Change the backend port in the following files: | ||
- `backend/config.py` | ||
- `frontend/app/app.config.js` | ||
|
||
To run the frontend on a different port: | ||
|
||
```shell | ||
npm run dev -- --port NEW_PORT | ||
``` | ||
|
||
## Troubleshooting | ||
|
||
The AWS Region is hard-coded in the application. However, if your local `AWS_REGION` environment variable is set to a different region, the application may fail. In this case, please make sure to either unset `AWS_REGION`, or set it to `us-east-1`. | ||
|
||
## License | ||
|
||
This library is licensed under the MIT-0 License. See the LICENSE file. | ||
This library is licensed under the MIT-0 License. See the [LICENSE](LICENSE) file. | ||
|
||
## Security | ||
|
||
See [CONTRIBUTING](CONTRIBUTING.md#security-issue-notifications) for more information. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
*$py.class | ||
|
||
# C extensions | ||
*.so | ||
|
||
# Distribution / packaging | ||
.Python | ||
build/ | ||
develop-eggs/ | ||
dist/ | ||
downloads/ | ||
eggs/ | ||
.eggs/ | ||
lib/ | ||
lib64/ | ||
parts/ | ||
sdist/ | ||
var/ | ||
wheels/ | ||
share/python-wheels/ | ||
*.egg-info/ | ||
.installed.cfg | ||
*.egg | ||
MANIFEST | ||
|
||
# PyInstaller | ||
# Usually these files are written by a python script from a template | ||
# before PyInstaller builds the exe, so as to inject date/other infos into it. | ||
*.manifest | ||
*.spec | ||
|
||
# Installer logs | ||
pip-log.txt | ||
pip-delete-this-directory.txt | ||
|
||
# Unit test / coverage reports | ||
htmlcov/ | ||
.tox/ | ||
.nox/ | ||
.coverage | ||
.coverage.* | ||
.cache | ||
nosetests.xml | ||
coverage.xml | ||
*.cover | ||
*.py,cover | ||
.hypothesis/ | ||
.pytest_cache/ | ||
cover/ | ||
|
||
# Translations | ||
*.mo | ||
*.pot | ||
|
||
# Django stuff: | ||
*.log | ||
local_settings.py | ||
db.sqlite3 | ||
db.sqlite3-journal | ||
|
||
# Flask stuff: | ||
instance/ | ||
.webassets-cache | ||
|
||
# Scrapy stuff: | ||
.scrapy | ||
|
||
# Sphinx documentation | ||
docs/_build/ | ||
|
||
# PyBuilder | ||
.pybuilder/ | ||
target/ | ||
|
||
# Jupyter Notebook | ||
.ipynb_checkpoints | ||
|
||
# IPython | ||
profile_default/ | ||
ipython_config.py | ||
|
||
# pyenv | ||
# For a library or package, you might want to ignore these files since the code is | ||
# intended to run in multiple environments; otherwise, check them in: | ||
# .python-version | ||
|
||
# pipenv | ||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. | ||
# However, in case of collaboration, if having platform-specific dependencies or dependencies | ||
# having no cross-platform support, pipenv may install dependencies that don't work, or not | ||
# install all needed dependencies. | ||
#Pipfile.lock | ||
|
||
# poetry | ||
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. | ||
# This is especially recommended for binary packages to ensure reproducibility, and is more | ||
# commonly ignored for libraries. | ||
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control | ||
#poetry.lock | ||
|
||
# pdm | ||
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. | ||
#pdm.lock | ||
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it | ||
# in version control. | ||
# https://pdm.fming.dev/#use-with-ide | ||
.pdm.toml | ||
|
||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm | ||
__pypackages__/ | ||
|
||
# Celery stuff | ||
celerybeat-schedule | ||
celerybeat.pid | ||
|
||
# SageMath parsed files | ||
*.sage.py | ||
|
||
# Environments | ||
.env | ||
.venv | ||
env/ | ||
venv/ | ||
ENV/ | ||
env.bak/ | ||
venv.bak/ | ||
|
||
# Spyder project settings | ||
.spyderproject | ||
.spyproject | ||
|
||
# Rope project settings | ||
.ropeproject | ||
|
||
# mkdocs documentation | ||
/site | ||
|
||
# mypy | ||
.mypy_cache/ | ||
.dmypy.json | ||
dmypy.json | ||
|
||
# Pyre type checker | ||
.pyre/ | ||
|
||
# pytype static type analyzer | ||
.pytype/ | ||
|
||
# Cython debug symbols | ||
cython_debug/ | ||
|
||
.idea/ |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from pydantic import BaseModel | ||
|
||
class ChatRequest(BaseModel): | ||
prompt: str | ||
|
||
class ChatResponse(BaseModel): | ||
completion: str |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from fastapi import APIRouter | ||
from . import models | ||
from . import services | ||
|
||
router = APIRouter() | ||
|
||
|
||
@router.post("/foundation-models/model/chat/anthropic.claude-v2/invoke") | ||
def invoke(body: models.ChatRequest): | ||
completion = services.invoke(body.prompt) | ||
|
||
return models.ChatResponse( | ||
completion=completion | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import boto3 | ||
import json | ||
|
||
bedrock_runtime = boto3.client( | ||
service_name="bedrock-runtime", | ||
region_name="us-east-1", | ||
) | ||
|
||
def invoke(prompt): | ||
|
||
systemPrompt = """ | ||
Take the role of a friendly chat bot. Your responses are brief. | ||
You sometimes use emojis where appropriate, but you don't overdo it. | ||
You engage human in a dialog by regularly asking questions, | ||
except when Human indicates that the conversation is over. | ||
"""; | ||
|
||
prompt_config = { | ||
"prompt": f'\n\nHuman: {systemPrompt}\n\n{prompt}\n\nAssistant:', | ||
"max_tokens_to_sample": 1024, | ||
"temperature": 0.8 | ||
} | ||
|
||
response = bedrock_runtime.invoke_model( | ||
body=json.dumps(prompt_config), | ||
modelId="anthropic.claude-v2" | ||
) | ||
|
||
response_body = json.loads(response.get("body").read()) | ||
|
||
return response_body.get("completion") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
HOST = "0.0.0.0" | ||
PORT = 55500 | ||
|
||
# Enable CORS for all domains, remember to change this for production purposes | ||
ALLOWED_CORS_ORIGINS = [ "*" ] | ||
ALLOWED_CORS_HEADERS = [ "*" ] | ||
ALLOWED_CORS_METHODS = [ "GET", "POST" ] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from fastapi import APIRouter | ||
from . import service | ||
|
||
router = APIRouter() | ||
|
||
@router.get("/foundation-models") | ||
def list_foundation_models(): | ||
return service.list_foundation_models() | ||
|
||
@router.get("/foundation-models/model/{model_id}") | ||
def get_foundation_model_details(model_id: str): | ||
return service.get_foundation_model(model_id) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import boto3 | ||
|
||
|
||
bedrock_client = boto3.client( | ||
service_name="bedrock", | ||
region_name="us-east-1" | ||
) | ||
|
||
|
||
def list_foundation_models(): | ||
response = bedrock_client.list_foundation_models() | ||
|
||
return response['modelSummaries'] | ||
|
||
|
||
def get_foundation_model(model_id): | ||
response = bedrock_client.get_foundation_model( | ||
modelIdentifier=model_id | ||
) | ||
|
||
return response['modelDetails'] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from pydantic import BaseModel | ||
|
||
class ImageRequest(BaseModel): | ||
prompt: str | ||
stylePreset: str | ||
|
||
# class ImageResponse(BaseModel): | ||
# imageByteArray: bytes |
Oops, something went wrong.