Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 69 additions & 57 deletions financial_document_analysis/bedrock_10k_analysis.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
"id": "26397bea-29e7-4185-b03f-0ae80685de9b",
"metadata": {},
"source": [
"# Using Jamba Instruct on Bedrock for Analyzing Financial Documents \n",
"Jamba-Instruct can be used to analyze financial documents. In this notebook, we will first load the Amazon's 10K filing (downloaded from [here](https://d18rn0p25nwr6d.cloudfront.net) and can then ask questions about it. This report has about 40,000 words."
"# Using Jamba on Amazon Bedrock for Analyzing Financial Documents \n",
"Jamba can be used to analyze financial documents. In this notebook, we will first load the Amazon's 10K filing (downloaded from [here](https://d18rn0p25nwr6d.cloudfront.net) and can then ask questions about it. This report has about 40,000 words."
]
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 11,
"id": "2469f0ac-becf-4c94-84c8-d68c9cb15f3c",
"metadata": {},
"outputs": [],
Expand All @@ -20,6 +20,18 @@
"import os\n",
"import json\n",
"import requests\n",
"import time\n",
"\n",
"import boto3\n"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "f639a59a",
"metadata": {},
"outputs": [],
"source": [
"# Function to read the contents of the files\n",
"def read_file(file_path):\n",
" with open(file_path, 'r') as file:\n",
Expand All @@ -28,29 +40,30 @@
"# Paths to the text files\n",
"document = '10k.txt'\n",
"\n",
"# Required imports\n",
"import os\n",
"import json\n",
"import requests\n",
"import time\n",
"import boto3\n",
"# Function to read the contents of the files\n",
"def read_file(file_path):\n",
" with open(file_path, 'r') as file:\n",
" return file.read()\n",
"\n",
"document_content=read_file(document)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "979f2bcf",
"metadata": {},
"outputs": [],
"source": [
"def _call_bedrock_jamba(prompt,**kwargs):\n",
" bedrock = boto3.client(service_name=\"bedrock-runtime\")\n",
" \n",
" body={\n",
" \"messages\":[{\"role\":\"user\",\"content\":prompt}],\n",
" \"max_tokens\": 1024,\n",
" \"top_p\": 0.8,\n",
" \"temperature\": 0.7,\n",
" }\n",
" \n",
" body.update(kwargs)\n",
" body = json.dumps(body)\n",
" \n",
" modelId = \"ai21.jamba-instruct-v1:0\"\n",
" modelId = \"ai21.jamba-1-5-large-v1:0\"\n",
" \n",
" accept = \"application/json\"\n",
" contentType = \"application/json\"\n",
Expand All @@ -61,10 +74,22 @@
" accept=accept,\n",
" contentType=contentType\n",
" )\n",
"\n",
" print(\"response: \", response)\n",
" \n",
" result=json.loads(response.get('body').read())\n",
" return result['choices'][0]['message']['content']\n",
" return result['choices'][0]['message']['content']"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "c41f1cf8",
"metadata": {},
"outputs": [],
"source": [
"\n",
"def call_bedrock_jamba(prompt, **kwargs):\n",
"def call_jamba(prompt, **kwargs):\n",
" attempts = 0\n",
" while attempts < 5:\n",
" try:\n",
Expand All @@ -88,37 +113,22 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 15,
"id": "cffbd621",
"metadata": {},
"outputs": [],
"source": [
"question=\"What was Amazon's revenue generating activity in 2019?\""
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "23d22a19-6f7b-476e-b01c-3cdf219c30d7",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Answer:\n",
" In 2019, Amazon's primary revenue-generating activities included:\n",
"\n",
"1. **Online stores**: This includes product sales and digital media content where Amazon records revenue gross. They leverage their retail infrastructure to offer a wide selection of consumable and durable goods that includes media products available in both a physical and digital format, such as books, music, videos, games, and software.\n",
"\n",
"\n",
"2. **Physical stores**: This includes product sales where customers physically select items in a store. Sales from customers who order goods online for delivery or pickup at Amazon's physical stores are included in “Online stores.”\n",
"\n",
"\n",
"3. **Third-party seller services**: Amazon offers programs that enable sellers to sell their products in Amazon's stores and fulfill orders through Amazon. Amazon is not the seller of record in these transactions. The commissions and any related fulfillment and shipping fees Amazon earns from these arrangements are recognized when the services are rendered.\n",
"\n",
"\n",
"4. **Subscription services**: This includes annual and monthly fees associated with Amazon Prime memberships, as well as audiobook, digital video, digital music, e-book, and other non-AWS subscription services.\n",
"\n",
"\n",
"5. **Other (includes sales of advertising services, as well as sales related to Amazon's other service offerings).**\n"
]
}
],
"outputs": [],
"source": [
"\n",
"question=\"What was Amazon's revenue generating activity in 2019?\"\n",
"document_content=read_file(document)\n",
"q_a_prompt = f\"\"\"\n",
"You are an excellent research assistant. Based the following \"Document Content\" do your best to answer the question posed.\n",
"Keep your answer strictly grounded in the document, and if the answer cannot be found in the document, just say \"I do not know\"\n",
Expand All @@ -131,19 +141,21 @@
"{question}\n",
"\n",
"Answer to Question:\n",
"\"\"\"\n",
"\n",
"#print(comparison_prompt)\n",
"# Call the API\n",
"\n",
"#print(q_a_prompt)\n",
"\n",
"\n",
"answer = call_bedrock_jamba(q_a_prompt,temperature=.7)\n",
"\"\"\"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "507486fc",
"metadata": {},
"outputs": [],
"source": [
"answer = call_jamba(q_a_prompt,temperature=.7)\n",
"\n",
"# Print the comparison result\n",
"# Print the result\n",
"print(\"Answer:\")\n",
"print(answer)\n"
"print(answer)"
]
},
{
Expand All @@ -157,9 +169,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.12.2",
"display_name": ".venv",
"language": "python",
"name": "python-3.12.2"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
Expand Down