Skip to content

Commit

Permalink
[add] application code & cdk
Browse files Browse the repository at this point in the history
  • Loading branch information
kashinoki38 committed Sep 16, 2024
1 parent 1300310 commit 767e318
Show file tree
Hide file tree
Showing 122 changed files with 12,860 additions and 5 deletions.
175 changes: 175 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
.DS_Store
.venv
__pycache__
.env

# 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/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

# Poetry
# poetry.lock

cdk/default.json

src/static/**
!src/static/**/
!src/static/**/.gitkeep
src/static.zip
112 changes: 107 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,113 @@
## My Project
# Eコマースにおける生成AI 4大ユースケースデモ

TODO: Fill this README out!
本デモでは、以下の コマースにおいて代表的な生成AIの 4 つのユースケースについて、 Amazon Bedrock とその上で利用可能なモデルを利用した実装をお試しいただけます。

Be sure to:
紹介ブログ:https://aws.amazon.com/jp/blogs/news/aws-summit-2024-retail-cpg-ec-genai-bedrock-demo-architecture/

* Change the title in this README
* Edit your repository description on GitHub
![alt text](./img/streamlit-image.png)


## 機能紹介
次の 4 つのユースケースについての Amazon Bedrock によるデモ実装が含まれます。
![alt text](img/usecase.png)

機能の細かい解説はブログを参照ください。
紹介ブログ:https://aws.amazon.com/jp/blogs/news/aws-summit-2024-retail-cpg-ec-genai-bedrock-demo-architecture/

## アーキテクチャ
アーキテクチャは非常にシンプルで、Amazon ECS 上に Steamlit で実装された簡易的な Web アプリケーションがデプロイされており、HTTP/S にてアクセスが可能です。

Steamlit 上の Python スクリプトにより、Amazon Bedrock の API を呼び出し、各種生成 AI のプロンプト実行と結果の取得を行います。
モデルは、画像生成には Amazon Titan Image Generator G1、テキスト処理の LLM には Anthropic Claude 3 Haiku、検索時のベクトル化(Embedding)には、 Amazon Titan Multimodal Embeddings G1 を Amazon Bedrock の API から利用しています。

![picture 1](./img/Architecture.png)

## デプロイ
> [!IMPORTANT]
> このリポジトリでは、デフォルトのモデルとしてオレゴンリージョン (us-west-2) の以下モデルを利用する設定になっています。
> - Amazon
> - Titan Image Generator G1
> - Titan Multimodal Embeddings G1
> - Anthropic
> - Claude 3.5 Sonnet
> - Claude 3 Sonnet
> - Claude 3 Haiku
>
> [Model access 画面 (us-east-1)](https://us-west-2.console.aws.amazon.com/bedrock/home?region=us-west-2#/modelaccess)を開き、モデルアクセスにチェックして Save changes してください。

デプロイには [AWS Cloud Development Kit](https://aws.amazon.com/jp/cdk/)(以降 CDK)と[Projen](https://github.com/projen/projen)をを利用します。

> [!IMPORTANT]
> Projen と CDK については以下 AWS ブログも参考にしてください。
> https://aws.amazon.com/jp/blogs/news/getting-started-with-projen-and-aws-cdk/
CDK を利用したことがない場合、初回のみ [Bootstrap](https://docs.aws.amazon.com/ja_jp/cdk/v2/guide/bootstrapping.html) 作業が必要です。すでに Bootstrap された環境では以下のコマンドは不要です。

```bash
npx cdk bootstrap
```

続いて、以下のコマンドで`cdk`ディレクトリに移動し、セットアップします。

```bash
cd cdk

npx projen install
```

AWS リソースをデプロイします。デプロイが完了するまで、お待ちください。

```bash
npx projen build
npx projen deploy
```

## 開発

### ローカルのコンテナで動かす場合

環境変数でアクセスキー, region を設定

```bash
export AWS_ACCESS_KEY_ID=xxx
export AWS_SECRET_ACCESS_KEY=xxx
export AWS_SESSION_TOKEN=xxx
```

Docker composeにて起動
```bash
cd src
docker compose up
```

### ローカルで動かす場合

> [!IMPORTANT]
>pythonバージョンは `">=3.11,<3.12"` である必要があります
>Amazon Bedrock や Amazon Translate を呼び出せる権限を持っている必要があります
poetry で環境構築が可能です。
```
poetry config virtualenvs.in-project true
poetry install
# .venvで仮想環境が利用可能です。
source .venv/bin/activate
```

`.env.example` ファイルを `.env` にリネームし、AWS の認証情報を設定します。
```
# EC2 や Cloud9 の IAM ロールを利用する場合は .env の設定は不要です
cp .env.example .env
```

環境構築後、以下のコマンドで Streamlit を起動します。

```
source .venv/bin/activate
region=us-west-2 poetry run streamlit run app.py
```

## Security

Expand Down
Loading

0 comments on commit 767e318

Please sign in to comment.