From 8af8c3e3a188211e05a4ac13cc75ea40e7a60aed Mon Sep 17 00:00:00 2001 From: George Burton Date: Tue, 3 Sep 2024 10:43:13 +0100 Subject: [PATCH] added aws bedrock --- .../redbox_app/redbox_core/consumers.py | 4 +- redbox-core/poetry.lock | 39 ++++++++++--------- redbox-core/pyproject.toml | 1 + redbox-core/redbox/chains/components.py | 5 +++ redbox-core/redbox/models/chain.py | 5 ++- 5 files changed, 32 insertions(+), 22 deletions(-) diff --git a/django_app/redbox_app/redbox_core/consumers.py b/django_app/redbox_app/redbox_core/consumers.py index d4e5dd221..fdd40a0da 100644 --- a/django_app/redbox_app/redbox_core/consumers.py +++ b/django_app/redbox_app/redbox_core/consumers.py @@ -242,10 +242,12 @@ def file_save(file): @staticmethod @database_sync_to_async def get_ai_settings(user: User) -> AISettings: - return model_to_dict( + ai_settings = model_to_dict( user.ai_settings, fields=[field.name for field in user.ai_settings._meta.fields if field.name != "label"], # noqa: SLF001 ) + ai_settings["chat_backend"] = "anthropic.claude-3-sonnet-20240229-v1:0" + return ai_settings class CoreError(Exception): diff --git a/redbox-core/poetry.lock b/redbox-core/poetry.lock index 6168eccbf..0f30d0f4b 100644 --- a/redbox-core/poetry.lock +++ b/redbox-core/poetry.lock @@ -1375,6 +1375,22 @@ requests = ">=2,<3" SQLAlchemy = ">=1.4,<3" tenacity = ">=8.1.0,<8.4.0 || >8.4.0,<9.0.0" +[[package]] +name = "langchain-aws" +version = "0.1.17" +description = "An integration package connecting AWS and LangChain" +optional = false +python-versions = "<4.0,>=3.8.1" +files = [ + {file = "langchain_aws-0.1.17-py3-none-any.whl", hash = "sha256:192a94eb3fa510ed7850cb7ab599fef47776ca872545dd37a0d7dc6a2887ae46"}, + {file = "langchain_aws-0.1.17.tar.gz", hash = "sha256:829cbcd8b3ffde6c2d7afd423da51c4bd03423db29b25ae7bc224979876d853e"}, +] + +[package.dependencies] +boto3 = ">=1.34.131,<1.35.0" +langchain-core = ">=0.2.33,<0.3" +numpy = {version = ">=1.26.0,<2.0.0", markers = "python_version >= \"3.12\""} + [[package]] name = "langchain-community" version = "0.2.12" @@ -1400,13 +1416,13 @@ tenacity = ">=8.1.0,<8.4.0 || >8.4.0,<9.0.0" [[package]] name = "langchain-core" -version = "0.2.30" +version = "0.2.37" description = "Building applications with LLMs through composability" optional = false python-versions = "<4.0,>=3.8.1" files = [ - {file = "langchain_core-0.2.30-py3-none-any.whl", hash = "sha256:ea7eccb9566dd51b2b74bd292c4239d843a77cdba8ffae2b5edf7000d70d4194"}, - {file = "langchain_core-0.2.30.tar.gz", hash = "sha256:552ec586698140062cd299a83bad7e308f925b496d306b62529579c6fb122f7a"}, + {file = "langchain_core-0.2.37-py3-none-any.whl", hash = "sha256:bf0f39ccb653931eeb0a4dd248591ef239895620e04da9ec928446cd9ddb47f0"}, + {file = "langchain_core-0.2.37.tar.gz", hash = "sha256:944de6cfaad72de5641f6765d5310a022fcd9943f10735507ea040cc5fb95011"}, ] [package.dependencies] @@ -2073,7 +2089,6 @@ optional = false python-versions = ">=3.9" files = [ {file = "pandas-2.2.2-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:90c6fca2acf139569e74e8781709dccb6fe25940488755716d1d354d6bc58bce"}, - {file = "pandas-2.2.2-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:c7adfc142dac335d8c1e0dcbd37eb8617eac386596eb9e1a1b77791cf2498238"}, {file = "pandas-2.2.2-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:4abfe0be0d7221be4f12552995e58723c7422c80a659da13ca382697de830c08"}, {file = "pandas-2.2.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:8635c16bf3d99040fdf3ca3db669a7250ddf49c55dc4aa8fe0ae0fa8d6dcc1f0"}, {file = "pandas-2.2.2-cp310-cp310-musllinux_1_1_aarch64.whl", hash = "sha256:40ae1dffb3967a52203105a077415a86044a2bea011b5f321c6aa64b379a3f51"}, @@ -2094,7 +2109,6 @@ files = [ {file = "pandas-2.2.2-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:43498c0bdb43d55cb162cdc8c06fac328ccb5d2eabe3cadeb3529ae6f0517c32"}, {file = "pandas-2.2.2-cp312-cp312-win_amd64.whl", hash = "sha256:d187d355ecec3629624fccb01d104da7d7f391db0311145817525281e2804d23"}, {file = "pandas-2.2.2-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:0ca6377b8fca51815f382bd0b697a0814c8bda55115678cbc94c30aacbb6eff2"}, - {file = "pandas-2.2.2-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:9057e6aa78a584bc93a13f0a9bf7e753a5e9770a30b4d758b8d5f2a62a9433cd"}, {file = "pandas-2.2.2-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:001910ad31abc7bf06f49dcc903755d2f7f3a9186c0c040b827e522e9cef0863"}, {file = "pandas-2.2.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:66b479b0bd07204e37583c191535505410daa8df638fd8e75ae1b383851fe921"}, {file = "pandas-2.2.2-cp39-cp39-musllinux_1_1_aarch64.whl", hash = "sha256:a77e9d1c386196879aa5eb712e77461aaee433e54c68cf253053a73b7e49c33a"}, @@ -2218,19 +2232,6 @@ files = [ {file = "pyarrow-17.0.0-cp312-cp312-win_amd64.whl", hash = "sha256:392bc9feabc647338e6c89267635e111d71edad5fcffba204425a7c8d13610d7"}, {file = "pyarrow-17.0.0-cp38-cp38-macosx_10_15_x86_64.whl", hash = "sha256:af5ff82a04b2171415f1410cff7ebb79861afc5dae50be73ce06d6e870615204"}, {file = "pyarrow-17.0.0-cp38-cp38-macosx_11_0_arm64.whl", hash = "sha256:edca18eaca89cd6382dfbcff3dd2d87633433043650c07375d095cd3517561d8"}, - {file = "pyarrow-17.0.0-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:7c7916bff914ac5d4a8fe25b7a25e432ff921e72f6f2b7547d1e325c1ad9d155"}, - {file = "pyarrow-17.0.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:f553ca691b9e94b202ff741bdd40f6ccb70cdd5fbf65c187af132f1317de6145"}, - {file = "pyarrow-17.0.0-cp38-cp38-manylinux_2_28_aarch64.whl", hash = "sha256:0cdb0e627c86c373205a2f94a510ac4376fdc523f8bb36beab2e7f204416163c"}, - {file = "pyarrow-17.0.0-cp38-cp38-manylinux_2_28_x86_64.whl", hash = "sha256:d7d192305d9d8bc9082d10f361fc70a73590a4c65cf31c3e6926cd72b76bc35c"}, - {file = "pyarrow-17.0.0-cp38-cp38-win_amd64.whl", hash = "sha256:02dae06ce212d8b3244dd3e7d12d9c4d3046945a5933d28026598e9dbbda1fca"}, - {file = "pyarrow-17.0.0-cp39-cp39-macosx_10_15_x86_64.whl", hash = "sha256:13d7a460b412f31e4c0efa1148e1d29bdf18ad1411eb6757d38f8fbdcc8645fb"}, - {file = "pyarrow-17.0.0-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:9b564a51fbccfab5a04a80453e5ac6c9954a9c5ef2890d1bcf63741909c3f8df"}, - {file = "pyarrow-17.0.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:32503827abbc5aadedfa235f5ece8c4f8f8b0a3cf01066bc8d29de7539532687"}, - {file = "pyarrow-17.0.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:a155acc7f154b9ffcc85497509bcd0d43efb80d6f733b0dc3bb14e281f131c8b"}, - {file = "pyarrow-17.0.0-cp39-cp39-manylinux_2_28_aarch64.whl", hash = "sha256:dec8d129254d0188a49f8a1fc99e0560dc1b85f60af729f47de4046015f9b0a5"}, - {file = "pyarrow-17.0.0-cp39-cp39-manylinux_2_28_x86_64.whl", hash = "sha256:a48ddf5c3c6a6c505904545c25a4ae13646ae1f8ba703c4df4a1bfe4f4006bda"}, - {file = "pyarrow-17.0.0-cp39-cp39-win_amd64.whl", hash = "sha256:42bf93249a083aca230ba7e2786c5f673507fa97bbd9725a1e2754715151a204"}, - {file = "pyarrow-17.0.0.tar.gz", hash = "sha256:4beca9521ed2c0921c1023e68d097d0299b62c362639ea315572a58f3f50fd28"}, ] [package.dependencies] @@ -3695,4 +3696,4 @@ test = ["big-O", "importlib-resources", "jaraco.functools", "jaraco.itertools", [metadata] lock-version = "2.0" python-versions = ">=3.12,<3.13" -content-hash = "f0c19792be78366896cb7df3adcbb95848b673ba05d11c99c1458e958f63d038" +content-hash = "96fb252fbd141dd576e41538ea79dda171b8e050cda8c1e0a941465d2e571f71" diff --git a/redbox-core/pyproject.toml b/redbox-core/pyproject.toml index 4839ce05a..227768de6 100644 --- a/redbox-core/pyproject.toml +++ b/redbox-core/pyproject.toml @@ -24,6 +24,7 @@ langchain-elasticsearch = "^0.2.2" pytest-dotenv = "^0.5.2" kneed = "^0.8.5" langgraph = "^0.2.3" +langchain-aws = "^0.1.17" [tool.poetry.group.dev.dependencies] diff --git a/redbox-core/redbox/chains/components.py b/redbox-core/redbox/chains/components.py index dad6dcf03..f1229de5a 100644 --- a/redbox-core/redbox/chains/components.py +++ b/redbox-core/redbox/chains/components.py @@ -6,6 +6,7 @@ from langchain_openai.embeddings import AzureOpenAIEmbeddings, OpenAIEmbeddings from langchain_core.utils import convert_to_secret_str import tiktoken +from langchain_aws import ChatBedrock from redbox.models.chain import AISettings from redbox.models.settings import Settings @@ -71,6 +72,10 @@ def get_chat_llm(env: Settings, ai_settings: AISettings): ) ] ) + elif ai_settings.chat_backend == "anthropic.claude-3-sonnet-20240229-v1:0": + chat_model = ChatBedrock( + model_id="anthropic.claude-3-sonnet-20240229-v1:0", + ) if chat_model is None: raise Exception("%s not recognised", ai_settings.chat_backend) else: diff --git a/redbox-core/redbox/models/chain.py b/redbox-core/redbox/models/chain.py index 665557bd2..942ff79f2 100644 --- a/redbox-core/redbox/models/chain.py +++ b/redbox-core/redbox/models/chain.py @@ -116,8 +116,9 @@ class AISettings(BaseModel): knn_boost: int = 1 similarity_threshold: int = 0 - # this is also the azure_openai_model - chat_backend: Literal["gpt-35-turbo-16k", "gpt-4-turbo-2024-04-09", "gpt-4o"] = "gpt-4o" + chat_backend: Literal[ + "gpt-35-turbo-16k", "gpt-4-turbo-2024-04-09", "gpt-4o", "anthropic.claude-3-sonnet-20240229-v1:0" + ] = "gpt-4o" class DocumentState(TypedDict):