Analysis Of Form 10 K Using Cohere And RAG
Analysis of Form 10-K/10-Q Using Cohere and RAG
Getting Started
You may use this script to jumpstart financial analysis of 10-Ks or 10-Qs with Cohere's Command model.
This cookbook relies on helpful tooling from LlamaIndex, as well as our Cohere SDK. If you're familiar with LlamaIndex, it should be easy to slot this process into your own productivity flows.
Enter your Cohere API key: ··········
Step 1: Loading a 10-K
You may run the following cells to load a 10-K that has already been preprocessed with OCR.
💡 If you'd like to run the OCR pipeline yourself, you can find more info in the section titled PDF to Text using OCR and
pdf2image.
[nltk_data] Downloading package averaged_perceptron_tagger to [nltk_data] /root/nltk_data... [nltk_data] Unzipping taggers/averaged_perceptron_tagger.zip.
We'll need to convert the text into chunks of a certain size in order for the Cohere embedding model to properly ingest them down the line.
We choose to use LlamaIndex's SentenceSplitter in this case in order to get these chunks. We must pass a tokenization callable, which we can do using the transformers library.
You may also apply further transformations from the LlamaIndex repo if you so choose. Take a look at the docs for inspiration on what is possible with transformations.
/usr/local/lib/python3.10/dist-packages/huggingface_hub/utils/_token.py:88: UserWarning: The secret `HF_TOKEN` does not exist in your Colab secrets. To authenticate with the Hugging Face Hub, create a token in your settings tab (https://huggingface.co/settings/tokens), set it as secret in your Google Colab and restart your session. You will be able to reuse this secret in all of your notebooks. Please note that authentication is recommended but still optional to access public models or datasets. warnings.warn(
tokenizer_config.json: 0%| | 0.00/7.92k [00:00<?, ?B/s]
tokenization_cohere_fast.py: 0%| | 0.00/43.7k [00:00<?, ?B/s]
configuration_cohere.py: 0%| | 0.00/7.37k [00:00<?, ?B/s]
A new version of the following files was downloaded from https://huggingface.co/CohereForAI/c4ai-command-r-v01: - configuration_cohere.py . Make sure to double-check they do not contain any added malicious code. To avoid downloading new versions of the code file, you can pin a revision. A new version of the following files was downloaded from https://huggingface.co/CohereForAI/c4ai-command-r-v01: - tokenization_cohere_fast.py - configuration_cohere.py . Make sure to double-check they do not contain any added malicious code. To avoid downloading new versions of the code file, you can pin a revision.
tokenizer.json: 0%| | 0.00/12.8M [00:00<?, ?B/s]
special_tokens_map.json: 0%| | 0.00/429 [00:00<?, ?B/s]
Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.
Step 2: Load document into a LlamaIndex vector store
Loading the document into a LlamaIndex vector store will allow us to use the Cohere embedding model and rerank model to retrieve the relevant parts of the form to pass into Command.
Step 3: Query generation and retrieval
In order to do RAG, we need a query or a set of queries to actually do the retrieval step. As is standard in RAG settings, we'll use Command to generate those queries for us. Then, we'll use those queries along with the LlamaIndex retriever we built earlier to retrieve the most relevant pieces of the 10-K.
To learn more about document mode and query generation, check out our documentation.
Now, with the queries in hand, we search against our vector index.
Step 4: Make a RAG request to Command using document mode
Now that we have our nicely formatted chunks from the 10-K, we can pass them directly into Command using the Cohere SDK. By passing the chunks into the documents kwarg, we enable document mode, which will perform grounded inference on the documents you pass in.
You can see this for yourself by inspecting the response.citations field to check where the model is citing from.
You can learn more about the chat endpoint by checking out the API reference here.
Here are the overall revenue numbers for the years 2021, 2022, and 2023 as bullet points: - 2021: $5,992 million - 2022: $8,399 million - 2023: $9,917 million Revenue increased by 18% in 2023 compared to 2022, primarily due to a 14% increase in Nights and Experiences Booked, which reached 54.5 million. This, combined with higher average daily rates, resulted in a 16% increase in Gross Booking Value, which reached $10.0 billion. The revenue growth trend demonstrates sustained strong travel demand. On a constant-currency basis, revenue increased by 17% in 2023 compared to the previous year. Other factors influencing the company's financial performance are described outside of the revenue growth trends.
Here are the overall revenue numbers for the years 2021, 2022, and 2023 as bullet points: - 2021: $5,992 million [13] - 2022: $8,399 million [13] - 2023: $9,917 million [13] Revenue increased by 18% in 2023 [11] compared to 2022, primarily due to a 14% increase in Nights and Experiences Booked [11], which reached 54.5 million. [11] This, combined with higher average daily rates [11], resulted in a 16% increase in Gross Booking Value [11], which reached $10.0 billion. [11] The revenue growth trend demonstrates sustained strong travel demand. [11] On a constant-currency basis [11], revenue increased by 17% in 2023 [11] compared to the previous year. Other factors [8, 14] influencing the company's financial performance are described outside of the revenue growth trends. [8, 14]
Appendix
PDF to Text using OCR and pdf2image
This method will be required for any PDFs you have that need to be converted to text.
WARNING: this process can take a long time without the proper optimizations. We have provided a snippet for your use below, but use at your own risk.
To go from PDF to text with PyTesseract, there is an intermediary step of converting the PDF to an image first, then passing that image into the OCR package, as OCR is usually only available for images.
To do this, we use pdf2image, which uses poppler behind the scenes to convert the PDF into a PNG. From there, we can pass the image (which is a PIL Image object) directly into the OCR tool.
Token count / price comparison and latency
RAG is 93% cheaper than full context
14.9 s ± 1.4 s per loop (mean ± std. dev. of 7 runs, 1 loop each)
22.7 s ± 7.43 s per loop (mean ± std. dev. of 7 runs, 1 loop each)