Skip to content

Commit

Permalink
update workshop with new titan embeddings
Browse files Browse the repository at this point in the history
  • Loading branch information
lauerarnaud committed Sep 13, 2023
1 parent 77abc34 commit 685dbf7
Show file tree
Hide file tree
Showing 5 changed files with 1,406 additions and 227 deletions.
25 changes: 17 additions & 8 deletions 00_Intro/bedrock_boto3_setup.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -632,13 +632,15 @@
"\n",
"Use text embeddings to convert text into meaningful vector representations. You input a body of text \n",
"and the output is a (1 x n) vector. You can use embedding vectors for a wide variety of applications. \n",
"Bedrock currently offers one model for text embedding that supports text similarity (finding the \n",
"Bedrock currently offers Titan Embeddings for text embedding that supports text similarity (finding the \n",
"semantic similarity between bodies of text) and text retrieval (such as search).\n",
"For the text embeddings model, the input text size is 512 tokens and the output vector length is 4096.\n",
"\n",
"At the time of writing you can use `amazon.titan-embed-g1-text-02` as embedding model via the API. The input text size is 512 tokens and the output vector length is 4096.\n",
"\n",
"To use a text embeddings model, use the InvokeModel API operation or the Python SDK.\n",
"Use InvokeModel to retrieve the vector representation of the input text from the specified model.\n",
"\n",
"At the time of writing you can only use `amazon.titan-e1t-medium` as embedding model via the API.\n",
"\n",
"\n",
"#### Input\n",
"\n",
Expand Down Expand Up @@ -685,7 +687,7 @@
"outputs": [],
"source": [
"body = json.dumps({\"inputText\": prompt_data})\n",
"modelId = \"amazon.titan-e1t-medium\" # (Change this to try different embedding models)\n",
"modelId = \"amazon.titan-embed-g1-text-02\" # (Change this to try different embedding models)\n",
"accept = \"application/json\"\n",
"contentType = \"application/json\"\n",
"\n",
Expand All @@ -707,6 +709,14 @@
"\n",
"In this notebook we showed some basic examples of invoking Amazon Bedrock models using the AWS Python SDK. You're now ready to explore the other labs to dive deeper on different use-cases and patterns."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f8bb76df-4e99-4ebe-a954-53992ad317dc",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down Expand Up @@ -1285,11 +1295,10 @@
"vcpuNum": 96
}
],
"instance_type": "ml.t3.medium",
"kernelspec": {
"display_name": "Python 3 (Data Science 3.0)",
"display_name": "Python 3 (Data Science 2.0)",
"language": "python",
"name": "python3__SAGEMAKER_INTERNAL__arn:aws:sagemaker:us-west-2:236514542706:image/sagemaker-data-science-310-v1"
"name": "python3__SAGEMAKER_INTERNAL__arn:aws:sagemaker:us-east-1:081325390199:image/sagemaker-data-science-38"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -1301,7 +1310,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.6"
"version": "3.8.13"
}
},
"nbformat": 4,
Expand Down
92 changes: 71 additions & 21 deletions 03_QuestionAnswering/01_qa_w_rag_claude.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# Make sure you ran `download-dependencies.sh` from the root of the repository first!\n",
Expand All @@ -124,7 +126,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import json\n",
Expand Down Expand Up @@ -171,13 +175,19 @@
"- `ai21.j2-grande-instruct`\n",
"- `ai21.j2-jumbo-instruct`\n",
"- `anthropic.claude-instant-v1`\n",
"- `anthropic.claude-v1`"
"- `anthropic.claude-v1`\n",
"\n",
"Similarly for Embeddings:\n",
"\n",
"`berock_embeddings = BedrockEmbeddings(model_id=\"amazon.titan-embed-g1-text-02\")`\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# We will be using the Titan Embeddings Model to generate our Embeddings.\n",
Expand All @@ -186,7 +196,7 @@
"\n",
"# - create the Anthropic Model\n",
"llm = Bedrock(model_id=\"anthropic.claude-v1\", client=boto3_bedrock, model_kwargs={'max_tokens_to_sample':200})\n",
"bedrock_embeddings = BedrockEmbeddings(client=boto3_bedrock)"
"bedrock_embeddings = BedrockEmbeddings(model_id=\"amazon.titan-embed-g1-text-02\", client=boto3_bedrock)"
]
},
{
Expand All @@ -200,7 +210,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from urllib.request import urlretrieve\n",
Expand Down Expand Up @@ -228,7 +240,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import numpy as np\n",
Expand All @@ -250,7 +264,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"avg_doc_length = lambda documents: sum([len(doc.page_content) for doc in documents])//len(documents)\n",
Expand All @@ -273,7 +289,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"sample_embedding = np.array(bedrock_embeddings.embed_query(docs[0].page_content))\n",
Expand All @@ -295,7 +313,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from langchain.chains.question_answering import load_qa_chain\n",
Expand Down Expand Up @@ -323,7 +343,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"query = \"Is it possible that I get sentenced to jail due to failure in filings?\""
Expand All @@ -339,7 +361,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"query_embedding = vectorstore_faiss.embedding_function(query)\n",
Expand All @@ -357,7 +381,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"relevant_documents = vectorstore_faiss.similarity_search_by_vector(query_embedding)\n",
Expand Down Expand Up @@ -396,7 +422,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"answer = wrapper_store_faiss.query(question=query, llm=llm)\n",
Expand All @@ -413,7 +441,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"query_2 = \"What is the difference between market discount and qualified stated interest\""
Expand All @@ -422,7 +452,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"answer_2 = wrapper_store_faiss.query(question=query_2, llm=llm)\n",
Expand All @@ -442,7 +474,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from langchain.chains import RetrievalQA\n",
Expand Down Expand Up @@ -476,7 +510,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"result['source_documents']"
Expand Down Expand Up @@ -504,6 +540,20 @@
"\n",
"# Thank You"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down Expand Up @@ -1084,9 +1134,9 @@
],
"instance_type": "ml.t3.medium",
"kernelspec": {
"display_name": "Python 3 (Data Science 3.0)",
"display_name": "Python 3 (Data Science 2.0)",
"language": "python",
"name": "python3__SAGEMAKER_INTERNAL__arn:aws:sagemaker:us-west-2:236514542706:image/sagemaker-data-science-310-v1"
"name": "python3__SAGEMAKER_INTERNAL__arn:aws:sagemaker:us-east-1:081325390199:image/sagemaker-data-science-38"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -1098,7 +1148,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.6"
"version": "3.8.13"
}
},
"nbformat": 4,
Expand Down
Loading

0 comments on commit 685dbf7

Please sign in to comment.