Git Product home page Git Product logo

bedrock-claude-chat's Introduction

Bedrock Claude Chat

Warning

If you are using old version (e.g. v0.4.x) and wish to use the latest version, refer migration guide. Without any care, ALL DATA IN Aurora cluster WILL BE DESTROYED, and NO LONGER USERS CANNOT USE EXISTING BOTS WITH KNOWLEDGE AND CREATE NEW BOTS.

This repository is a sample chatbot using the Anthropic company's LLM Claude, one of the foundational models provided by Amazon Bedrock for generative AI.

Basic Conversation

Not only text but also images are available with Anthropic's Claude 3. Currently we support Haiku, Sonnet and Opus.

Bot Personalization

Add your own instruction and give external knowledge as URL or files (a.k.a RAG). The bot can be shared among application users. The customized bot also can be published as stand-alone API (See the detail).

Important

For governance reasons, only allowed users are able to create customized bots. To allow the creation of customized bots, the user must be a member of group called CreatingBotAllowed, which can be set up via the management console > Amazon Cognito User pools or aws cli. Note that the user pool id can be referred by accessing CloudFormation > BedrockChatStack > Outputs > AuthUserPoolIdxxxx.

Administrator dashboard

Analyze usage for each user / bot on administrator dashboard. detail

LLM-powered Agent

By using the Agent functionality, your chatbot can automatically handle more complex tasks. For example, to answer a user's question, the Agent can retrieve necessary information from external tools or break down the task into multiple steps for processing.

📚 Supported Languages

  • English 💬
  • 日本語 💬 (ドキュメントはこちら)
  • 한국어 💬
  • 中文 💬
  • Français 💬
  • Deutsch 💬
  • Español 💬
  • Italian 💬

🚀 Super-easy Deployment

  • In the us-east-1 region, open Bedrock Model access > Manage model access > Check Anthropic / Claude 3 Haiku, Anthropic / Claude 3 Sonnet and Cohere / Embed Multilingual then Save changes.
Screenshot

  • Open CloudShell at the region where you want to deploy
  • Run deployment via following commands
git clone https://github.com/aws-samples/bedrock-claude-chat.git
cd bedrock-claude-chat
chmod +x bin.sh
./bin.sh
  • You will be asked if a new user or using v1. If so, enter y.
  • After about 30 minutes, you will get the following output, which you can access from your browser

Optional Parameters

You can now specify the following parameters during deployment to enhance security and customization:

  • --disable-self-register: Disable self-registration (default: enabled). If this flag is set, you will need to create all users on cognito and it will not allow users to self register their accounts.
  • --ipv4-ranges: Comma-separated list of allowed IPv4 ranges. (default: allow all ipv4 addresses)
  • --ipv6-ranges: Comma-separated list of allowed IPv6 ranges. (default: allow all ipv6 addresses)
  • --allowed-signup-email-domains: Comma-separated list of allowed email domains for sign-up. (default: no domain restriction)
  • --region: Define the region where bedrock is available. (default: us-east-1)

Example command with parameters:

./bin.sh --disable-self-register --ipv4-ranges "192.0.2.0/25,192.0.2.128/25" --ipv6-ranges "2001:db8:1:2::/64,2001:db8:1:3::/64" --allowed-signup-email-domains "example.com,anotherexample.com" --region "us-west-2"
  • After about 35 minutes, you will get the following output, which you can access from your browser
Frontend URL: https://xxxxxxxxx.cloudfront.net

The sign-up screen will appear as shown above, where you can register your email and log in.

Important

This deployment method allows anyone with the URL to sign up if optional parameters are not configured. For production use, we strongly recommend adding IP address restrictions and disabling self-signup to mitigate security risks (Defining the allowed-signup-email-domains to allow only your emails from your company domain to be able to sing-up to restrict the users). For ip address restriction use both ipv4-ranges and ipv6-ranges and to disable self-signup use disable-self-register when executing ./bin.

Architecture

It's an architecture built on AWS managed services, eliminating the need for infrastructure management. Utilizing Amazon Bedrock, there's no need to communicate with APIs outside of AWS. This enables deploying scalable, reliable, and secure applications.

Deploy using CDK

Super-easy Deployment uses AWS CodeBuild to perform deployment by CDK internally. This section describes the procedure for deploying directly with CDK.

  • Please have UNIX, Docker and a Node.js runtime environment. If not, you can also use Cloud9

Important

If there is insufficient storage space in the local environment during deployment, CDK bootstrapping may result in an error. If you are running in Cloud9 etc., we recommend expanding the volume size of the instance before deploying.

  • Clone this repository
git clone https://github.com/aws-samples/bedrock-claude-chat
  • Install npm packages
cd bedrock-claude-chat
cd cdk
npm ci
npm i -g aws-cdk
  • Before deploying the CDK, you will need to work with Bootstrap once for the region you are deploying to. In this example, we will deploy to the us-east-1 region. Please replace your account id into <account id>.
cdk bootstrap aws://<account id>/us-east-1
  • If necessary, edit the following entries in cdk.json if necessary.

    • bedrockRegion: Region where Bedrock is available. NOTE: Bedrock does NOT support all regions for now.
    • allowedIpV4AddressRanges, allowedIpV6AddressRanges: Allowed IP Address range.
  • Deploy this sample project

cdk deploy --require-approval never --all
  • You will get output similar to the following. The URL of the web app will be output in BedrockChatStack.FrontendURL, so please access it from your browser.
 ✅  BedrockChatStack

✨  Deployment time: 78.57s

Outputs:
BedrockChatStack.AuthUserPoolClientIdXXXXX = xxxxxxx
BedrockChatStack.AuthUserPoolIdXXXXXX = ap-northeast-1_XXXX
BedrockChatStack.BackendApiBackendApiUrlXXXXX = https://xxxxx.execute-api.ap-northeast-1.amazonaws.com
BedrockChatStack.FrontendURL = https://xxxxx.cloudfront.net

Others

Configure Mistral models support

Update enableMistral to true in cdk.json, and run cdk deploy.

...
  "enableMistral": true,

Important

This project focus on Anthropic Claude models, the Mistral models are limited supported. For example, prompt examples are based on Claude models. This is a Mistral-only option, once you toggled to enable Mistral models, you can only use Mistral models for all the chat features, NOT both Claude and Mistral models.

Configure default text generation

Users can adjust the text generation parameters from the custom bot creation screen. If the bot is not used, the default parameters set in config.py will be used.

DEFAULT_GENERATION_CONFIG = {
    "max_tokens": 2000,
    "top_k": 250,
    "top_p": 0.999,
    "temperature": 0.6,
    "stop_sequences": ["Human: ", "Assistant: "],
}

Remove resources

If using cli and CDK, please cdk destroy. If not, access CloudFormation and then delete BedrockChatStack and FrontendWafStack manually. Please note that FrontendWafStack is in us-east-1 region.

Stopping Vector DB for RAG

By setting cdk.json in the following CRON format, you can stop and restart Aurora Serverless resources created by the VectorStore construct. Applying this setting can reduce operating costs. By default, Aurora Serverless is always running. Note that it will be executed in UTC time.

...
"rdbSchedules": {
  "stop": {
    "minute": "50",
    "hour": "10",
    "day": "*",
    "month": "*",
    "year": "*"
  },
  "start": {
    "minute": "40",
    "hour": "2",
    "day": "*",
    "month": "*",
    "year": "*"
  }
}

Language Settings

This asset automatically detects the language using i18next-browser-languageDetector. You can switch languages from the application menu. Alternatively, you can use Query String to set the language as shown below.

https://example.com?lng=ja

Disable self sign up

This sample has self sign up enabled by default. To disable self sign up, open cdk.json and switch selfSignUpEnabled as false. If you configure external identity provider, the value will be ignored and automatically disabled.

Restrict Domains for Sign-Up Email Addresses

By default, this sample does not restrict the domains for sign-up email addresses. To allow sign-ups only from specific domains, open cdk.json and specify the domains as a list in allowedSignUpEmailDomains.

"allowedSignUpEmailDomains": ["example.com"],

External Identity Provider

This sample supports external identity provider. Currently we support Google and custom OIDC provider.

Add new users to groups automatically

This sample has the following groups to give permissions to users:

If you want newly created users to automatically join groups, you can specify them in cdk.json.

"autoJoinUserGroups": ["CreatingBotAllowed"],

By default, newly created users will be joined to the CreatingBotAllowed group.

Local Development

See LOCAL DEVELOPMENT.

Contribution

Thank you for considering contributing to this repository! We welcome bug fixes, language translations (i18n), feature enhancements, agent tools, and other improvements.

For feature enhancements and other improvements, before creating a Pull Request, we would greatly appreciate it if you could create a Feature Request Issue to discuss the implementation approach and details. For bug fixes and language translations (i18n), proceed with creating a Pull Request directly.

Please also take a look at the following guidelines before contributing:

RAG (Retrieval Augmented Generation)

See here.

Contacts

Contributors

bedrock claude chat contributors

License

This library is licensed under the MIT-0 License. See the LICENSE file.

bedrock-claude-chat's People

Contributors

alandao avatar amaynez avatar dependabot[bot] avatar eltociear avatar fsatsuki avatar gitsumito avatar heyitsnoah avatar hustshawn avatar inagaki-keisuke-lvgs avatar jeremylatorre avatar josephedward avatar k70suk3-k06a7ash1 avatar kennell avatar kyosuke-kobayashi-lvgs avatar mats16 avatar msalheb avatar sajee avatar skeppeler avatar statefb avatar sugi275 avatar syobochim avatar taikono-himazin avatar tatshima avatar tmokmss avatar wadabee avatar wildgeece96 avatar wzr1337 avatar xdelacour avatar yoyoyo-pg avatar yukinobu-mine avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

bedrock-claude-chat's Issues

Files as well

Hello,

The sample looks nice!

Is there any chance of adding function like Creation, storage, and deletion of files to be used along with the internet info. in chat?

Thanks!

v0.4.0 でデプロイ環境のストレージ容量が小さいとcdk bootstrapに失敗する

Readmeに記載のCodeBuildデプロイ(CloudShell利用)や、CDKデプロイをCloud9で実施すると発生。

権限ないのでPRは出せませんでしたが、 Readme_ja.md のWarningに以下文言を記載しておくと良さそうです。

また、デプロイ時にローカル環境のストレージ容量が不足するとCDKのブートストラップがエラーとなってしまう可能性があります。Cloud9等で実行される場合は、インスタンスのボリュームサイズを拡張のうえデプロイ実施されることをお勧めします。

[Feature Request] Download conversations for use case discovery

As a leader who is proceeding with generative AI usage in a company, I would like to discover major use cases from conversations with active users.

For example, I can detect major use case by counting keywords in prompts (such as "please summarize", "please translate", etc). In addition, I could identify heavy users by analyzing frequency. Interviewing them would be the the first step to implement an internal prompt guide or specialized application for their business. We can find sample implementations from generative-ai-use-cases-jp.

As a minimum feature, downloading all the conversation data with the following items will be sufficient (bracket item is optional).

  • user id
  • date
  • inputted prompt
  • model name
  • outputted prompt
  • (instruction)
  • (hyper parameter)

API call and Streaming chunks

Thanks for sharing this awesome project to give us more ideas to develop AWS Bedrock!

I would like to know how to modify the frontend code to call api (invoke()) instead of streaming via web socket (invoke_model_with_response_stream()).

Please give me some guidance to use your code more properly.

Best Regards,
Webber

[Easy Deployment] Specify region by argument

背景

Easy Deploymentの場合、CodeBuild内でgit cloneしたものをそのままcdk deployしていると思うので、cdk.jsonを編集するすべがなくBedrockのAPI自体は必ずus-east-1を見る必要

方針

引数でデプロイ先のリージョン・bedrockのリージョンを指定可能にする

Dark theme

ライトテーマとダークテーマを切り替えられるようにする

Refactor backend directory structure

Purpose

Enable FastAPI hot reload on local development

Solution

Instead of the idea of sharing portions of the code, write it as a single application, and switch the behavior through CMD (command) or handlers at runtime

スタックの削除手順

README.md にデプロイ後のスタック削除手順がなかったため、追記いただけるとありがたいです。

新規会話登録時、フロント側でIDを付与する仕様に変更

背景

  • 現在はバックエンドで登録してからIDが確定するため、チャットの送信 -> IDの取得の間にラグがある
  • そのラグの間に次の会話を入力すると会話が正常に登録されないので、初回登録時にフロントエンド側でIDを即時確定させたい

方針

  • conversation idは登録必須(今はnull許容だが、変更後は必ず指定するようにする)
  • バックエンド側でDBの存在チェック

Stop streaming

ストリーミングで生成されるレスポンスを中断する

[ERROR] ProtocolError: ("Connection broken: InvalidChunkLength(got length b'', 0 bytes read)", InvalidChunkLength(got length b'', 0 bytes read))

下記のエラーが発生することがある

[ERROR] ProtocolError: ("Connection broken: InvalidChunkLength(got length b'', 0 bytes read)", InvalidChunkLength(got length b'', 0 bytes read))
Traceback (most recent call last):
File "/var/task/index.py", line 67, in handler
for chunk in generate_chunk(stream):
File "/var/task/index.py", line 19, in generate_chunk
for event in stream:
File "/var/lang/lib/python3.11/site-packages/botocore/eventstream.py", line 602, in iter
for event in self._event_generator:
File "/var/lang/lib/python3.11/site-packages/botocore/eventstream.py", line 609, in _create_raw_event_generator
for chunk in self._raw_stream.stream():
File "/var/lang/lib/python3.11/site-packages/urllib3/response.py", line 624, in stream
for line in self.read_chunked(amt, decode_content=decode_content):
File "/var/lang/lib/python3.11/site-packages/urllib3/response.py", line 816, in read_chunked
with self._error_catcher():
File "/var/lang/lib/python3.11/contextlib.py", line 155, in exit
self.gen.throw(typ, value, traceback)
File "/var/lang/lib/python3.11/site-packages/urllib3/response.py", line 461, in _error_catcher
raise ProtocolError("Connection broken: %r" % e, e)

Could not resolve the foundation model from the provided model identifier

When sending a chat message the frontend hangs forever. Looking at the CloudWatch BedrockChatStack-WebSocketHandler log stream, I see the following:

Failed to invoke bedrock: An error occurred (ResourceNotFoundException) when calling the InvokeModelWithResponseStream operation: Could not resolve the foundation model from the provided model identifier.

In the log stream request I see "model":"claude-v2", however digging into the backend bedrock.py I see get_model_id() is providing the correct modelID (in theory) before calling invoke. I looked at the docs and Bedrock in the console and the model identifiers do not appear to have changed (other than the addition of Claude v2.1).

I would also note I've had all models enabled in Bedrock for some time, and the app was deployed in the same region (us-west-2).

I haven't had time to dig in any further. It would be nice to add CloudWatch logging to bedrock.py as well as improve exception handling on the front end by passing the error message.

The install otherwise went as advertised.

Does deployment require Docker?

Thank you very much for contributing this!
But when trying to deploy with cdk deploy, whether from my PC or CloudShell, I ran into an error indicating "Cannot connect to the Docker daemon".
Is Docker really required for deployment? Or any guideline to bypass this?

Error on switching languages

When the browser default language is configured to languages other than en,ja,ko,zh, the frontend throws an error when trying to switching languages.

Reproduction

Set the browser default language to en-us (or whatever unsupported languages):

image

Clear local storage for the page to reset i18nextLng, then open the page, login, and try to switch language.

The error I get:

Unexpected Application Error!
props.options.filter(...)[0] is undefined

Select/selectedLabel<@http://localhost:5173/src/components/Select.tsx?t=1698727269604:25:52
mountMemo@http://localhost:5173/node_modules/.vite/deps/chunk-GYWC62UC.js?v=f26ec844:12817:27
useMemo@http://localhost:5173/node_modules/.vite/deps/chunk-GYWC62UC.js?v=f26ec844:13141:24
useMemo@http://localhost:5173/node_modules/.vite/deps/chunk-HS5T2ZWL.js?v=f26ec844:1094:29
Select@http://localhost:5173/src/components/Select.tsx?t=1698727269604:24:32
...

Possible solution

Something like this will fix the issue (check if the find result is undefined):

const selectedLabel = useMemo(() => {
return props.value === ''
? ''
: props.options.filter((o) => o.value === props.value)[0].label;
}, [props.options, props.value]);

-    return props.value === ''
-      ? ''
-      : props.options.filter((o) => o.value === props.value)[0].label;
+    return props.options.find((o) => o.value === props.value)?.label ?? '';

Feature request: 外部IdentityProviderのサポート

初めまして。
現在Bedrockの組織適応を行なっており、このリポジトリのAppは我々の求めているものに近しく感じています。
こちらはCognitoによるユーザー管理を軸としていますが、外部のIdP連携を実装する予定はありますでしょうか。
(我々はKeycloakを使っており、完全サポートではなくとも実装の余地があると嬉しく思います。)

現時点の実装においてはAuthがcognitoに依存しており、外部IdPによる実装はfrontend, backend, cdkと改修範囲が広く見えます。
ロードマップにない場合は諦めて実装を行いますが、計画にある場合は本リポジトリの改修の流れに乗れると嬉しく思っています。

また外部IdPをサポートしないとしても、cognitoのAuthを必須としない状態へとパラメータ等で制御可能になるだけでも、サンプルプロジェクトとして使いやすくなると感じています。

Is the SNS necessary in your design?

Hi there. I'm just wondering if the SNS as the "decoupling" mechanism is necessary and/or if there was a specific requirement or reasoning behind it.

Your flow of getting a response from a model is as follows:

  1. front-end connects to a websocket - which, under the hood, invokes the publisher Lambda - but it doesn't do much due to route_key being equal to $connect
  2. then, front-end sends the user prompt to the open websocket (
    ws.send(JSON.stringify({ ...input, token }));
    ) - it also triggers the same publisher Lambda. However, this time, it actually pushes the prompt forward to the SNS because the route_key is different.
  3. now, the SNS invokes the websocket Lambda, which streams the response back to the websocket (https://github.com/aws-samples/bedrock-claude-chat/blob/main/backend/app/websocket.py#L77)

However, if (instead of being SNS consumer) your websocket Lambda was just a different route on the WS API gateway (lets call it /invoke), then the flow could look as follows:

  1. front-end connects to a websocket - which, under the hood, invokes the publisher Lambda - but it doesn't do much due to route_key being equal to $connect
  2. front-end sends the user prompt to the open websocket as an action /invoke - it triggers the websocket Lambda

Voila, you no longer need the SNS and it still works the same way.

Am I missing something (could be totally wrong above of course)? Is the SNS here necessary? Does it provide any additional value over this alternative method that I've described which would simplify the design?

Other than that - great work. Thanks for providing this excellent blueprint!

Input is too long for requested model

Getting this error on one of my chats.

[ERROR] EventStreamError: An error occurred (validationException) when calling the InvokeModelWithResponseStream operation: Input is too long for requested model.
Traceback (most recent call last):
File "/var/task/app/websocket.py", line 84, in handler
for chunk in generate_chunk(stream):
File "/var/task/app/websocket.py", line 23, in generate_chunk
for event in stream:
File "/var/lang/lib/python3.11/site-packages/botocore/eventstream.py", line 603, in iter
parsed_event = self._parse_event(event)
File "/var/lang/lib/python3.11/site-packages/botocore/eventstream.py", line 619, in _parse_event
raise EventStreamError(parsed_response, self._operation_name)

Section & sort conversation history

It'd be great if we can sort history, create a section, and expand/collapse sections on conversation history panel. Just like the Slack channel side bar.

[feature request] 続きを出力ボタン (continue button)

概要

「続きを出力」ボタンの実装をしてほしい

背景

出力に 30 秒以上かかるリクエストがユーザーから入力された場合、30秒経過後 「回答中にエラーが発生しました。」と出力が消えてしまいます。

再現手順

入力はながければなんでもいいのですが、例えば: https://qiita.com/kei31/items/82356c4130b3ab627d32 を入力します。

下記の図のように途中まで出力されます。
Screenshot 2023-10-10 at 19 57 36

29秒後画面出力がクリアされ、「回答中にエラーが発生しました。」と表示されます。
Screenshot 2023-10-10 at 19 54 48

Screenshot 2023-10-10 at 19 55 45

根本原因は api-gw と lambda間の統合が max 29秒であることだと考えられます。
https://stackoverflow.com/questions/61617193/aws-api-gateway-websocket-timeout でも同じ議論が見えます。
Screenshot 2023-10-10 at 19 56 13

期待する機能

ユーザー入力+途中まで出力された結果を記録し、再度推論させれば、続きが出力されると思うので、「続きを出力」させる機能がほしい。

{message: "Endpoint request timed out" をキャッチした場合は、以下の図のように「続きを出力」をさせるボタンを出してほしい
Screenshot 2023-10-10 at 19 52 10

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.