From 0153ab970abaf931f5ab54a305c7b9cbefc27d6f Mon Sep 17 00:00:00 2001 From: IsmaelKP2 Date: Mon, 24 Feb 2025 13:59:04 +0000 Subject: [PATCH 1/4] gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 5c4816f..2565c32 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +mynotes.md # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] From 830216b253c690a7de9d4c23cba695760225ee63 Mon Sep 17 00:00:00 2001 From: IsmaelKP2 Date: Mon, 24 Feb 2025 13:59:37 +0000 Subject: [PATCH 2/4] added langchain_community package to complete langchain replaced langchain_community.embeddings with langchain_aws to remove deprecation comment updated bedrock model to v2 change --- bedrock-chat-with-pdf/Admin/admin.py | 8 ++++---- bedrock-chat-with-pdf/Admin/requirements.txt | 2 ++ 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/bedrock-chat-with-pdf/Admin/admin.py b/bedrock-chat-with-pdf/Admin/admin.py index 9dc6087..d9c7d1c 100644 --- a/bedrock-chat-with-pdf/Admin/admin.py +++ b/bedrock-chat-with-pdf/Admin/admin.py @@ -8,8 +8,8 @@ BUCKET_NAME = os.getenv("BUCKET_NAME") ## Bedrock -from langchain_community.embeddings import BedrockEmbeddings - +#from langchain_community.embeddings import BedrockEmbeddings +from langchain_aws import BedrockEmbeddings ## Text Splitter from langchain.text_splitter import RecursiveCharacterTextSplitter @@ -19,8 +19,8 @@ ## import FAISS from langchain_community.vectorstores import FAISS -bedrock_client = boto3.client(service_name="bedrock-runtime") -bedrock_embeddings = BedrockEmbeddings(model_id="amazon.titan-embed-text-v1", client=bedrock_client) +bedrock_client = boto3.client(service_name="bedrock-runtime", region_name="eu-west-2") +bedrock_embeddings = BedrockEmbeddings(model_id="amazon.titan-embed-text-v2:0", client=bedrock_client) def get_unique_id(): return str(uuid.uuid4()) diff --git a/bedrock-chat-with-pdf/Admin/requirements.txt b/bedrock-chat-with-pdf/Admin/requirements.txt index 3637850..0d9975e 100644 --- a/bedrock-chat-with-pdf/Admin/requirements.txt +++ b/bedrock-chat-with-pdf/Admin/requirements.txt @@ -1,5 +1,7 @@ streamlit pypdf langchain +langchain_community +langchain-aws faiss-cpu boto3 \ No newline at end of file From 94463d0e05ac6b83b84af8de2a74334301c4773b Mon Sep 17 00:00:00 2001 From: IsmaelKP2 Date: Mon, 24 Feb 2025 14:08:08 +0000 Subject: [PATCH 3/4] added region as a environment variable --- bedrock-chat-with-pdf/Admin/admin.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bedrock-chat-with-pdf/Admin/admin.py b/bedrock-chat-with-pdf/Admin/admin.py index d9c7d1c..cee199a 100644 --- a/bedrock-chat-with-pdf/Admin/admin.py +++ b/bedrock-chat-with-pdf/Admin/admin.py @@ -6,6 +6,7 @@ ## s3_client s3_client = boto3.client("s3") BUCKET_NAME = os.getenv("BUCKET_NAME") +REGION = os.getenv("REGION") ## Bedrock #from langchain_community.embeddings import BedrockEmbeddings @@ -19,7 +20,7 @@ ## import FAISS from langchain_community.vectorstores import FAISS -bedrock_client = boto3.client(service_name="bedrock-runtime", region_name="eu-west-2") +bedrock_client = boto3.client(service_name="bedrock-runtime", region_name=REGION) bedrock_embeddings = BedrockEmbeddings(model_id="amazon.titan-embed-text-v2:0", client=bedrock_client) def get_unique_id(): From b2dc474d958b6369b532e5082b59c332a1812a71 Mon Sep 17 00:00:00 2001 From: IsmaelKP2 Date: Mon, 24 Feb 2025 18:08:02 +0000 Subject: [PATCH 4/4] added langchain_community package to complete langchain replaced langchain_community.embeddings with langchain_aws to remove deprecation comment llm uses ChatBedrock instead of Bedrock updated bedrock amazon.titan model to v2 updated claude to claude-3 environment variable update --- bedrock-chat-with-pdf/User/app.py | 26 ++++++++++++++------- bedrock-chat-with-pdf/User/requirements.txt | 2 ++ 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/bedrock-chat-with-pdf/User/app.py b/bedrock-chat-with-pdf/User/app.py index 26abfee..ac217c5 100644 --- a/bedrock-chat-with-pdf/User/app.py +++ b/bedrock-chat-with-pdf/User/app.py @@ -6,11 +6,13 @@ ## s3_client s3_client = boto3.client("s3") BUCKET_NAME = os.getenv("BUCKET_NAME") +REGION = os.getenv("REGION") ## Bedrock -from langchain_community.embeddings import BedrockEmbeddings -from langchain.llms.bedrock import Bedrock - +#from langchain_community.embeddings import BedrockEmbeddings +from langchain_aws import BedrockEmbeddings +#from langchain.llms.bedrock import Bedrock +from langchain_aws import ChatBedrock ## prompt and chain from langchain.prompts import PromptTemplate from langchain.chains import RetrievalQA @@ -24,8 +26,9 @@ ## import FAISS from langchain_community.vectorstores import FAISS -bedrock_client = boto3.client(service_name="bedrock-runtime") -bedrock_embeddings = BedrockEmbeddings(model_id="amazon.titan-embed-text-v1", client=bedrock_client) + +bedrock_client = boto3.client(service_name="bedrock-runtime", region_name=REGION) +bedrock_embeddings = BedrockEmbeddings(model_id="amazon.titan-embed-text-v2:0", client=bedrock_client) folder_path="/tmp/" @@ -38,8 +41,14 @@ def load_index(): s3_client.download_file(Bucket=BUCKET_NAME, Key="my_faiss.pkl", Filename=f"{folder_path}my_faiss.pkl") def get_llm(): - llm=Bedrock(model_id="anthropic.claude-v2:1", client=bedrock_client, - model_kwargs={'max_tokens_to_sample': 512}) + llm = ChatBedrock( + client=bedrock_client, + model_id="anthropic.claude-3-haiku-20240307-v1:0", + ) +# llm = BedrockChat( +# client=bedrock_client, +# model_id="anthropic.claude-3-haiku-20240307-v1:0", +# ) return llm # get_response() @@ -70,7 +79,8 @@ def get_response(llm,vectorstore, question ): return_source_documents=True, chain_type_kwargs={"prompt": PROMPT} ) - answer=qa({"query":question}) + #answer=qa({"query":question}) + answer = qa.invoke({"query": question}) return answer['result'] diff --git a/bedrock-chat-with-pdf/User/requirements.txt b/bedrock-chat-with-pdf/User/requirements.txt index c4cbbc8..3e877f2 100644 --- a/bedrock-chat-with-pdf/User/requirements.txt +++ b/bedrock-chat-with-pdf/User/requirements.txt @@ -1,4 +1,6 @@ streamlit langchain +langchain_community +langchain-aws faiss-cpu boto3 \ No newline at end of file