Compare commits

..

1 Commits

Author SHA1 Message Date
Pierre-Loic Doulcet 2e6c064682 add more supported format 2024-04-23 11:18:38 +08:00
33 changed files with 2243 additions and 3286 deletions
-48
View File
@@ -1,48 +0,0 @@
name: Build Package
# Build package on its own without additional pip install
on:
push:
branches:
- main
pull_request:
env:
POETRY_VERSION: "1.6.1"
jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
# You can use PyPy versions in python-version.
# For example, pypy-2.7 and pypy-3.8
matrix:
os: [ubuntu-latest, windows-latest]
python-version: ["3.9"]
steps:
- uses: actions/checkout@v3
- name: Set up python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install Poetry
uses: snok/install-poetry@v1
with:
version: ${{ env.POETRY_VERSION }}
- name: Install deps
shell: bash
run: poetry install
- name: Ensure lock works
shell: bash
run: poetry lock
- name: Build
shell: bash
run: poetry build
- name: Test installing built package
shell: bash
run: python -m pip install .
- name: Test import
shell: bash
working-directory: ${{ vars.RUNNER_TEMP }}
run: python -c "import llama_parse"
-81
View File
@@ -1,81 +0,0 @@
# For most projects, this workflow file will not need changing; you simply need
# to commit it to your repository.
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.
#
# ******** NOTE ********
# We have attempted to detect the languages in your repository. Please check
# the `language` matrix defined below to confirm you have the correct set of
# supported CodeQL languages.
#
name: "CodeQL"
on:
push:
branches: ["main"]
pull_request:
# The branches below must be a subset of the branches above
branches: ["main"]
schedule:
- cron: "30 16 * * 4"
jobs:
analyze:
name: Analyze
# Runner size impacts CodeQL analysis time. To learn more, please see:
# - https://gh.io/recommended-hardware-resources-for-running-codeql
# - https://gh.io/supported-runners-and-hardware-resources
# - https://gh.io/using-larger-runners
# Consider using larger runners for possible analysis time improvements.
runs-on: ${{ (matrix.language == 'swift' && 'macos-latest') || 'ubuntu-latest' }}
timeout-minutes: ${{ (matrix.language == 'swift' && 120) || 360 }}
permissions:
actions: read
contents: read
security-events: write
strategy:
fail-fast: false
matrix:
language: ["python"]
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby', 'swift' ]
# Use only 'java' to analyze code written in Java, Kotlin or both
# Use only 'javascript' to analyze code written in JavaScript, TypeScript or both
# Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support
steps:
- name: Checkout repository
uses: actions/checkout@v3
# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.
# For more details on CodeQL's query packs, refer to: https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
# queries: security-extended,security-and-quality
# Autobuild attempts to build any compiled languages (C/C++, C#, Go, Java, or Swift).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v2
# ️ Command-line programs to run using the OS shell.
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun
# If the Autobuild fails above, remove it and uncomment the following three lines.
# modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance.
# - run: |
# echo "Run, Build Application using script"
# ./location_of_script_within_repo/buildscript.sh
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
with:
category: "/language:${{matrix.language}}"
-37
View File
@@ -1,37 +0,0 @@
name: Linting
on:
push:
branches:
- main
pull_request:
env:
POETRY_VERSION: "1.6.1"
jobs:
build:
runs-on: ubuntu-latest
strategy:
# You can use PyPy versions in python-version.
# For example, pypy-2.7 and pypy-3.8
matrix:
python-version: ["3.9"]
steps:
- uses: actions/checkout@v3
with:
fetch-depth: ${{ github.event_name == 'pull_request' && 2 || 0 }}
- name: Set up python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install Poetry
uses: snok/install-poetry@v1
with:
version: ${{ env.POETRY_VERSION }}
- name: Install pre-commit
shell: bash
run: poetry run pip install pre-commit
- name: Run linter
shell: bash
run: poetry run make lint
-64
View File
@@ -1,64 +0,0 @@
name: Publish llama-parse to PyPI / GitHub
on:
push:
tags:
- "v*"
workflow_dispatch:
env:
POETRY_VERSION: "1.6.1"
PYTHON_VERSION: "3.9"
jobs:
build-n-publish:
name: Build and publish to PyPI
if: github.repository == 'run-llama/llama_parse'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up python ${{ env.PYTHON_VERSION }}
uses: actions/setup-python@v4
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: Install Poetry
uses: snok/install-poetry@v1
with:
version: ${{ env.POETRY_VERSION }}
- name: Install deps
shell: bash
run: pip install -e .
- name: Build and publish to pypi
uses: JRubics/poetry-publish@v1.17
with:
pypi_token: ${{ secrets.LLAMA_PARSE_PYPI_TOKEN }}
ignore_dev_requirements: "yes"
- name: Create GitHub Release
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # This token is provided by Actions, you do not need to create your own token
with:
tag_name: ${{ github.ref }}
release_name: ${{ github.ref }}
draft: false
prerelease: false
- name: Get Asset name
run: |
export PKG=$(ls dist/ | grep tar)
set -- $PKG
echo "name=$1" >> $GITHUB_ENV
- name: Upload Release Asset (sdist) to GitHub
id: upload-release-asset
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: dist/${{ env.name }}
asset_name: ${{ env.name }}
asset_content_type: application/zip
-40
View File
@@ -1,40 +0,0 @@
name: Unit Testing
on:
push:
branches:
- main
pull_request:
env:
POETRY_VERSION: "1.6.1"
LLAMA_CLOUD_API_KEY: ${{ secrets.LLAMA_CLOUD_API_KEY }}
jobs:
test:
runs-on: ubuntu-latest
strategy:
# You can use PyPy versions in python-version.
# For example, pypy-2.7 and pypy-3.8
matrix:
python-version: ["3.8", "3.10", "3.11"]
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Set up python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install Poetry
uses: snok/install-poetry@v1
with:
version: ${{ env.POETRY_VERSION }}
- name: Install deps
shell: bash
run: poetry install --with dev
- name: Run testing
env:
CI: true
shell: bash
run: poetry run pytest tests
+1 -1
View File
@@ -1,4 +1,4 @@
.git
__pycache__/
*.pyc
.DS_Store
.DS_Store
-86
View File
@@ -1,86 +0,0 @@
---
default_language_version:
python: python3
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: check-byte-order-marker
- id: check-merge-conflict
- id: check-symlinks
- id: check-toml
- id: check-yaml
- id: detect-private-key
- id: end-of-file-fixer
- id: mixed-line-ending
- id: trailing-whitespace
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.1.5
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
exclude: ".*poetry.lock"
- repo: https://github.com/psf/black-pre-commit-mirror
rev: 23.10.1
hooks:
- id: black-jupyter
name: black-src
alias: black
exclude: ".*poetry.lock"
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.0.1
hooks:
- id: mypy
additional_dependencies:
[
"types-requests",
"types-Deprecated",
"types-redis",
"types-setuptools",
"types-PyYAML",
"types-protobuf==4.24.0.4",
]
args:
[
--disallow-untyped-defs,
--ignore-missing-imports,
--python-version=3.8,
]
- repo: https://github.com/adamchainz/blacken-docs
rev: 1.16.0
hooks:
- id: blacken-docs
name: black-docs-text
alias: black
types_or: [rst, markdown, tex]
additional_dependencies: [black==23.10.1]
# Using PEP 8's line length in docs prevents excess left/right scrolling
args: [--line-length=79]
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v3.0.3
hooks:
- id: prettier
exclude: poetry.lock
- repo: https://github.com/codespell-project/codespell
rev: v2.2.6
hooks:
- id: codespell
additional_dependencies: [tomli]
exclude: ^(poetry.lock|examples)
args:
[
"--ignore-words-list",
"astroid,gallary,momento,narl,ot,rouge,nin,gere,te,inh",
]
- repo: https://github.com/srstevenson/nb-clean
rev: 3.1.0
hooks:
- id: nb-clean
args: [--preserve-cell-outputs, --remove-empty-cells]
- repo: https://github.com/pappasam/toml-sort
rev: v0.23.1
hooks:
- id: toml-sort-fix
exclude: ".*poetry.lock"
-14
View File
@@ -1,14 +0,0 @@
GIT_ROOT ?= $(shell git rev-parse --show-toplevel)
help: ## Show all Makefile targets.
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[33m%-30s\033[0m %s\n", $$1, $$2}'
format: ## Run code autoformatters (black).
pre-commit install
git ls-files | xargs pre-commit run black --files
lint: ## Run linters: pre-commit (black, ruff, codespell) and mypy
pre-commit install && git ls-files | xargs pre-commit run --show-diff-on-failure --files
test: ## Run tests via pytest
pytest tests
+6 -9
View File
@@ -4,11 +4,12 @@ LlamaParse is an API created by LlamaIndex to efficiently parse and represent fi
LlamaParse directly integrates with [LlamaIndex](https://github.com/run-llama/llama_index).
Free plan is up to 1000 pages a day. Paid plan is free 7k pages per week + 0.3c per additional page.
## Getting Started
First, login and get an api-key from [**https://cloud.llamaindex.ai ↗**](https://cloud.llamaindex.ai).
First, login and get an api-key from `https://cloud.llamaindex.ai`.
Then, make sure you have the latest LlamaIndex version installed.
@@ -27,7 +28,6 @@ Now you can run the following to parse your first PDF file:
```python
import nest_asyncio
nest_asyncio.apply()
from llama_parse import LlamaParse
@@ -35,9 +35,9 @@ from llama_parse import LlamaParse
parser = LlamaParse(
api_key="llx-...", # can also be set in your env as LLAMA_CLOUD_API_KEY
result_type="markdown", # "markdown" and "text" are available
num_workers=4, # if multiple files passed, split in `num_workers` API calls
num_workers=4, # if multiple files passed, split in `num_workers` API calls
verbose=True,
language="en", # Optionally you can define a language, default=en
language="en" # Optionaly you can define a language, default=en
)
# sync
@@ -59,7 +59,6 @@ You can also integrate the parser as the default PDF loader in `SimpleDirectoryR
```python
import nest_asyncio
nest_asyncio.apply()
from llama_parse import LlamaParse
@@ -68,13 +67,11 @@ from llama_index.core import SimpleDirectoryReader
parser = LlamaParse(
api_key="llx-...", # can also be set in your env as LLAMA_CLOUD_API_KEY
result_type="markdown", # "markdown" and "text" are available
verbose=True,
verbose=True
)
file_extractor = {".pdf": parser}
documents = SimpleDirectoryReader(
"./data", file_extractor=file_extractor
).load_data()
documents = SimpleDirectoryReader("./data", file_extractor=file_extractor).load_data()
```
Full documentation for `SimpleDirectoryReader` can be found on the [LlamaIndex Documentation](https://docs.llamaindex.ai/en/stable/module_guides/loading/simpledirectoryreader.html).
+22 -22
View File
@@ -27,7 +27,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
@@ -78,7 +78,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
@@ -91,7 +91,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {},
"outputs": [
{
@@ -108,7 +108,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"metadata": {},
"outputs": [
{
@@ -129,13 +129,9 @@
],
"source": [
"import nest_asyncio\n",
"\n",
"nest_asyncio.apply()\n",
"\n",
"from llama_index.core.node_parser import (\n",
" MarkdownElementNodeParser,\n",
" SentenceSplitter,\n",
")\n",
"from llama_index.core.node_parser import MarkdownElementNodeParser, SentenceSplitter\n",
"\n",
"# explicitly extract tables with the MarkdownElementNodeParser\n",
"node_parser = MarkdownElementNodeParser(num_workers=8)\n",
@@ -143,9 +139,7 @@
"nodes, objects = node_parser.get_nodes_and_objects(nodes)\n",
"\n",
"# Chain splitters to ensure chunk size requirements are met\n",
"nodes = SentenceSplitter(chunk_size=512, chunk_overlap=20).get_nodes_from_documents(\n",
" nodes\n",
")"
"nodes = SentenceSplitter(chunk_size=512, chunk_overlap=20).get_nodes_from_documents(nodes)"
]
},
{
@@ -157,7 +151,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
@@ -169,7 +163,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
@@ -180,7 +174,8 @@
"tools = [\n",
" QueryEngineTool(\n",
" vector_index.as_query_engine(\n",
" similarity_top_k=8, node_postprocessors=[ColbertRerank(top_n=3)]\n",
" similarity_top_k=8,\n",
" node_postprocessors=[ColbertRerank(top_n=3)]\n",
" ),\n",
" metadata=ToolMetadata(\n",
" name=\"search\",\n",
@@ -196,12 +191,15 @@
" ),\n",
"]\n",
"\n",
"agent = OpenAIAgent.from_tools(tools=tools, verbose=True)"
"agent = OpenAIAgent.from_tools(\n",
" tools=tools, \n",
" verbose=True\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"metadata": {},
"outputs": [
{
@@ -224,7 +222,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"metadata": {},
"outputs": [
{
@@ -241,7 +239,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 10,
"metadata": {},
"outputs": [
{
@@ -263,7 +261,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 11,
"metadata": {},
"outputs": [
{
@@ -294,8 +292,10 @@
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3"
}
"pygments_lexer": "ipython3",
"version": "3.10.12"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
-529
View File
@@ -1,529 +0,0 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "c148b65e-e8a6-476e-86ba-bf6a73d479c7",
"metadata": {},
"source": [
"# RAG over the Caltrain Weekend Schedule \n",
"\n",
"<a href=\"https://colab.research.google.com/github/run-llama/llama_parse/blob/main/examples/caltrain_text_mode.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>\n",
"\n",
"This example shows off LlamaParse parsing capabilities to build a functioning query pipeline over the Caltrain weekend schedule, a big timetable containing all trains northbound and southbound and their stops in various cities.\n",
"\n",
"Naive parsing solutions mess up in representing this tabular representation, leading to LLM hallucinations. In contrast, LlamaParse text-mode spatially lays out the table in a neat format, enabling more sophisticated LLMs like gpt-4-turbo to understand the spacing and reason over all the numbers.\n",
"\n",
"**NOTE**: LlamaParse markdown mode doesn't quite work yet - it's in development!"
]
},
{
"cell_type": "markdown",
"id": "ef115dbe-b834-4639-828e-e2c11aef710b",
"metadata": {},
"source": [
"## Setup\n",
"\n",
"Download the data."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e6ae2e38-30c9-4865-aa13-47780bc3848f",
"metadata": {},
"outputs": [],
"source": [
"import nest_asyncio\n",
"\n",
"nest_asyncio.apply()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "335ce1d0-757a-4f09-846e-21c409768871",
"metadata": {},
"outputs": [],
"source": [
"!wget \"https://www.caltrain.com/media/31602/download?inline?inline\" -O caltrain_schedule_weekend.pdf"
]
},
{
"cell_type": "markdown",
"id": "45fa9120-65bb-4772-9db7-53e7cecf9adc",
"metadata": {},
"source": [
"## Initialize LlamaParse\n",
"\n",
"Initialize LlamaParse in `text` mode which will represent complex documents incl. text, tables, and figures as nicely formatted text."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "54aa9579-84d4-49bc-ab54-5474e69c1188",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/jerryliu/Programming/llama_parse/.venv/lib/python3.10/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
" from .autonotebook import tqdm as notebook_tqdm\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Started parsing the file under job_id 5f73353a-1f4b-480d-9eea-58d1d22b75f6\n"
]
}
],
"source": [
"from llama_parse import LlamaParse\n",
"\n",
"docs = LlamaParse(result_type=\"text\").load_data(\"./caltrain_schedule_weekend.pdf\")"
]
},
{
"cell_type": "markdown",
"id": "602756b2-9ea1-4519-a8e3-c773ec624205",
"metadata": {},
"source": [
"Take a look at the below text (and zoom out from the browser to really get the effect!). You'll see that the entire table is nicely laid out."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4928281a-591a-4653-b451-b2b8112a7101",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ZONE 2ZONE 3ZONE 4ZONE 4 ZONE 3ZONE 2ZONE 1ZONE 1\n",
" Printer-Friendly Caltrain Schedule\n",
" Northbound WEEKEND SERVICE to SAN FRANCISCO 2XX Local\n",
"\n",
"\n",
" Train No. 221 225 229 233 237 241 245 249 253 257 261 265 269 273 *277 *281\n",
" Service Types L2 L2 L2 L2 L2 L2 L2 L2 L2 L2 L2 L2 L2 L2 L2 L2\n",
" Tamien 7:12a 9:05a 10:05a 11:05a 1:05p 3:05p 5:05p 7:05p 9:05p 11:05p\n",
" San Jose Diridon 7:19a 9:12a 10:12a 11:12a 12:12p 1:12p 2:12p 3:12p 4:12p 5:12p 6:12p 7:12p 8:12p 9:12p 10:19p 11:12p\n",
" Santa Clara 7:25a 9:18a 10:18a 11:18a 12:18p 1:18p 2:18p 3:18p 4:18p 5:18p 6:18p 7:18p 8:18p 9:18p 10:25p 11:18p\n",
" Lawrence 7:31a 9:24a 10:24a 11:24a 12:24p 1:24p 2:24p 3:24p 4:24p 5:24p 6:24p 7:24p 8:24p 9:24p 10:31p 11:24p\n",
" Sunnyvale 7:35a 9:28a 10:28a 11:28a 12:28p 1:28p 2:28p 3:28p 4:28p 5:28p 6:28p 7:28p 8:28p 9:28p 10:35p 11:28p\n",
" Mountain View 7:40a 9:34a 10:34a 11:34a 12:34p 1:34p 2:34p 3:34p 4:34p 5:34p 6:34p 7:34p 8:34p 9:34p 10:40p 11:34p\n",
" San Antonio 7:43a 9:37a 10:37a 11:37a 12:37p 1:37p 2:37p 3:37p 4:37p 5:37p 6:37p 7:37p 8:37p 9:37p 10:44p 11:37p\n",
" California Ave 7:48a 9:42a 10:42a 11:42a 12:42p 1:42p 2:42p 3:42p 4:42p 5:42p 6:42p 7:42p 8:42p 9:42p 10:48p 11:42p\n",
" Palo Alto 7:52a 9:46a 10:46a 11:46a 12:46p 1:46p 2:46p 3:46p 4:46p 5:46p 6:46p 7:46p 8:46p 9:46p 10:53p 11:46p\n",
" Menlo Park 7:55a 9:50a 10:50a 11:50a 12:50p 1:50p 2:50p 3:50p 4:50p 5:50p 6:50p 7:50p 8:50p 9:50p 10:56p 11:50p\n",
" Redwood City 8:01a 9:56a 10:56a 11:56a 12:56p 1:56p 2:56p 3:56p 4:56p 5:56p 6:56p 7:56p 8:56p 9:56p 11:02p 11:56p\n",
" San Carlos 8:05a 10:01a 11:01a 12:01p 1:01p 2:01p 3:01p 4:01p 5:01p 6:01p 7:01p 8:01p 9:01p 10:01p 11:07p 12:01a\n",
" Belmont 8:09a 10:04a 11:04a 12:04p 1:04p 2:04p 3:04p 4:04p 5:04p 6:04p 7:04p 8:04p 9:04p 10:04p 11:10p 12:04a\n",
" Hillsdale 8:12a 10:08a 11:08a 12:08p 1:08p 2:08p 3:08p 4:08p 5:08p 6:08p 7:08p 8:08p 9:08p 10:08p 11:14p 12:08a\n",
" Hayward Park 8:15a 10:11a 11:11a 12:11p 1:11p 2:11p 3:11p 4:11p 5:11p 6:11p 7:11p 8:11p 9:11p 10:11p 11:17p 12:11a\n",
" San Mateo 8:19a 10:15a 11:15a 12:15p 1:15p 2:15p 3:15p 4:15p 5:15p 6:15p 7:15p 8:15p 9:15p 10:15p 11:21p 12:15a\n",
" Burlingame 8:22a 10:19a 11:19a 12:19p 1:19p 2:19p 3:19p 4:19p 5:19p 6:19p 7:19p 8:19p 9:19p 10:19p 11:25p 12:19a\n",
" Broadway 8:25a 10:22a 11:22a 12:22p 1:22p 2:22p 3:22p 4:22p 5:22p 6:22p 7:22p 8:22p 9:22p 10:22p 11:28p 12:22a\n",
" Millbrae 8:29a 10:26a 11:26a 12:26p 1:26p 2:26p 3:26p 4:26p 5:26p 6:26p 7:26p 8:26p 9:26p 10:26p 11:32p 12:26a\n",
" San Bruno 8:34a 10:30a 11:30a 12:30p 1:30p 2:30p 3:30p 4:30p 5:30p 6:30p 7:30p 8:30p 9:30p 10:30p 11:37p 12:30a\n",
" S. San Francisco 8:38a 10:34a 11:34a 12:34p 1:34p 2:34p 3:34p 4:34p 5:34p 6:34p 7:34p 8:34p 9:34p 10:34p 11:41p 12:34a\n",
" Bayshore 8:44a 10:41a 11:41a 12:41p 1:41p 2:41p 3:41p 4:41p 5:41p 6:41p 7:41p 8:41p 9:41p 10:41p 11:47p 12:41a\n",
" 22 ndStreet 8:50a 10:46a 11:46a 12:46p 1:46p 2:46p 3:46p 4:46p 5:46p 6:46p 7:46p 8:46p 9:46p 10:46p 11:53p 12:46a\n",
" San Francisco 8:56a 10:52a 11:53a 12:53p 1:52p 2:52p 3:52p 4:52p 5:52p 6:52p 7:52p 8:52p 9:52p 10:52p 11:59p 12:52a\n",
" *On SAP Center event days, Train 277 or Train 281departure from San Jose Diridon station may be delayed and will depart no later than 10:30p or 11:30p respectively.\n",
"\n",
"\n",
" Southbound WEEKEND SERVICE to SAN JOSE 2XX Local\n",
" Train No. 224 228 232 236 240 244 248 252 256 260 264 268 272 276 280 284\n",
" Service Types L2 L2 L2 L2 L2 L2 L2 L2 L2 L2 L2 L2 L2 L2 L2 L2\n",
" San Francisco 8:28a 9:58a 10:58a 11:58a 12:58p 1:58p 2:58p 3:58p 4:58p 5:58p 6:58p 7:58p 8:58p 9:58p 10:58p 12:05a\n",
" 22 ndStreet 8:33a 10:03a 11:03a 12:03p 1:03p 2:03p 3:03p 4:03p 5:03p 6:03p 7:03p 8:03p 9:03p 10:03p 11:03p 12:10a\n",
" Bayshore 8:38a 10:08a 11:08a 12:08p 1:08p 2:08p 3:08p 4:08p 5:08p 6:08p 7:08p 8:08p 9:08p 10:08p 11:08p 12:15a\n",
" S. San Francisco 8:45a 10:15a 11:15a 12:15p 1:15p 2:15p 3:15p 4:15p 5:15p 6:15p 7:15p 8:15p 9:15p 10:15p 11:15p 12:22a\n",
" San Bruno 8:49a 10:19a 11:19a 12:19p 1:19p 2:19p 3:19p 4:19p 5:19p 6:19p 7:19p 8:19p 9:19p 10:19p 11:19p 12:26a\n",
" Millbrae 8:53a 10:24a 11:24a 12:24p 1:24p 2:24p 3:24p 4:24p 5:24p 6:24p 7:24p 8:24p 9:24p 10:24p 11:24p 12:31a\n",
" Broadway 8:57a 10:27a 11:27a 12:27p 1:27p 2:27p 3:27p 4:27p 5:27p 6:27p 7:27p 8:27p 9:27p 10:27p 11:27p 12:35a\n",
" Burlingame 9:00a 10:31a 11:31a 12:31p 1:31p 2:31p 3:31p 4:31p 5:31p 6:31p 7:31p 8:31p 9:31p 10:31p 11:31p 12:38a\n",
" San Mateo 9:04a 10:34a 11:34a 12:34p 1:34p 2:34p 3:34p 4:34p 5:34p 6:34p 7:34p 8:34p 9:34p 10:34p 11:34p 12:41a\n",
" Hayward Park 9:07a 10:37a 11:37a 12:37p 1:37p 2:37p 3:37p 4:37p 5:37p 6:37p 7:37p 8:37p 9:37p 10:37p 11:37p 12:45a\n",
" Hillsdale 9:10a 10:41a 11:41a 12:41p 1:41p 2:41p 3:41p 4:41p 5:41p 6:41p 7:41p 8:41p 9:41p 10:41p 11:41p 12:48a\n",
" Belmont 9:14a 10:44a 11:44a 12:44p 1:44p 2:44p 3:44p 4:44p 5:44p 6:44p 7:44p 8:44p 9:44p 10:44p 11:44p 12:52a\n",
" San Carlos 9:17a 10:48a 11:48a 12:48p 1:48p 2:48p 3:48p 4:48p 5:48p 6:48p 7:48p 8:48p 9:48p 10:48p 11:48p 12:55a\n",
" Redwood City 9:21a 10:52a 11:52a 12:52p 1:52p 2:52p 3:52p 4:52p 5:52p 6:52p 7:52p 8:52p 9:52p 10:52p 11:52p 12:59a\n",
" Menlo Park 9:28a 10:58a 11:58a 12:58p 1:58p 2:58p 3:58p 4:58p 5:58p 6:58p 7:58p 8:58p 9:58p 10:58p 11:58p 1:05a\n",
" Palo Alto 9:32a 11:02a 12:02p 1:02p 2:02p 3:02p 4:02p 5:02p 6:02p 7:02p 8:02p 9:02p 10:02p 11:02p 12:02a 1:09a\n",
" California Avenue 9:36a 11:06a 12:06p 1:06p 2:06p 3:06p 4:06p 5:06p 6:06p 7:06p 8:06p 9:06p 10:06p 11:06p 12:06a 1:12a\n",
" San Antonio 9:41a 11:11a 12:11p 1:11p 2:11p 3:11p 4:11p 5:11p 6:11p 7:11p 8:11p 9:11p 10:11p 11:11p 12:10a 1:17a\n",
" Mountain View 9:45a 11:16a 12:16p 1:16p 2:16p 3:16p 4:16p 5:16p 6:16p 7:16p 8:16p 9:16p 10:16p 11:16p 12:15a 1:21a\n",
" Sunnyvale 9:51a 11:21a 12:21p 1:21p 2:21p 3:21p 4:21p 5:21p 6:21p 7:21p 8:21p 9:21p 10:21p 11:21p 12:20a 1:26a\n",
" Lawrence 9:55a 11:26a 12:26p 1:26p 2:26p 3:26p 4:26p 5:26p 6:26p 7:26p 8:26p 9:26p 10:26p 11:26p 12:25a 1:31a\n",
" Santa Clara 10:01a 11:32a 12:32p 1:32p 2:32p 3:32p 4:32p 5:32p 6:32p 7:32p 8:32p 9:32p 10:32p 11:32p 12:31a 1:37a\n",
" San Jose Diridon 10:10a 11:40a 12:40p 1:38p 2:40p 3:38p 4:40p 5:38p 6:40p 7:38p 8:40p 9:38p 10:40p 11:38p 12:39a 1:44a\n",
" Tamien 10:15a 11:45a 12:45p 2:45p 4:45p 6:45p 8:45p 10:45p 12:44a 1:49a\n",
" EFFECTIVE September 12, 2022 Timetable subject to change without notice.\n"
]
}
],
"source": [
"print(docs[0].get_content())"
]
},
{
"cell_type": "markdown",
"id": "8f5064d4-3e33-4f67-9b2e-46787161538f",
"metadata": {},
"source": [
"## Initialize Query Engine\n",
"\n",
"We now initialize a query engine over this data. Here we use a baseline summary index, which doesn't do vector indexing/chunking and instead dumps the entire text into the prompt.\n",
"\n",
"We see that the LLM (gpt-4-turbo) is able to provide all the stops for train no 225 northbound."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b3e985b6-9d38-449f-9cf9-aae166824eed",
"metadata": {},
"outputs": [],
"source": [
"from llama_index.core import SummaryIndex\n",
"from llama_index.llms.openai import OpenAI\n",
"\n",
"llm = OpenAI(model=\"gpt-4o\")\n",
"index = SummaryIndex.from_documents(docs)\n",
"query_engine = index.as_query_engine(llm=llm)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "66eb0976-2cd6-4b14-9083-124baae9ed5d",
"metadata": {},
"outputs": [],
"source": [
"response = query_engine.query(\n",
" \"What are the stops (and times) for train no 237 northbound?\"\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7dc6f275-07f4-429e-9335-f50982fe974c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The stops and times for train no. 237 northbound are as follows:\n",
"\n",
"- San Jose Diridon: 12:12 PM\n",
"- Santa Clara: 12:18 PM\n",
"- Lawrence: 12:24 PM\n",
"- Sunnyvale: 12:28 PM\n",
"- Mountain View: 12:34 PM\n",
"- San Antonio: 12:37 PM\n",
"- California Ave: 12:42 PM\n",
"- Palo Alto: 12:46 PM\n",
"- Menlo Park: 12:50 PM\n",
"- Redwood City: 12:56 PM\n",
"- San Carlos: 1:01 PM\n",
"- Belmont: 1:04 PM\n",
"- Hillsdale: 1:08 PM\n",
"- Hayward Park: 1:11 PM\n",
"- San Mateo: 1:15 PM\n",
"- Burlingame: 1:19 PM\n",
"- Broadway: 1:22 PM\n",
"- Millbrae: 1:26 PM\n",
"- San Bruno: 1:30 PM\n",
"- S. San Francisco: 1:34 PM\n",
"- Bayshore: 1:41 PM\n",
"- 22nd Street: 1:46 PM\n",
"- San Francisco: 1:52 PM\n"
]
}
],
"source": [
"print(str(response))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "229c4cb0-cf94-4a9f-bc7c-590388f50c1f",
"metadata": {},
"outputs": [],
"source": [
"response = query_engine.query(\n",
" \"What are all the trains (and times) that end at Tamien going Southbound?\"\n",
")"
]
},
{
"cell_type": "markdown",
"id": "6cf9fce0-5067-48f6-a7ef-62aa9e2edc3d",
"metadata": {},
"source": [
"It gets most of the answers correct (to be fair it misses two trains)."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "51cf03ff-7728-4815-ab72-3bf54fc4a2c0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The trains that end at Tamien going Southbound are:\n",
"\n",
"- Train 224 at 10:15a\n",
"- Train 228 at 11:45a\n",
"- Train 240 at 2:45p\n",
"- Train 248 at 4:45p\n",
"- Train 256 at 6:45p\n",
"- Train 264 at 8:45p\n",
"- Train 272 at 10:45p\n",
"- Train 284 at 1:49a\n"
]
}
],
"source": [
"print(str(response))"
]
},
{
"cell_type": "markdown",
"id": "e51e7feb-b74f-4101-8963-933ac7ec9763",
"metadata": {},
"source": [
"## Try Baseline\n",
"\n",
"In contrast, we try a baseline approach with the default PDF reader (PyPDF) in `SimpleDirectoryReader`."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "364e5155-cc75-4302-a754-9444ae28e6b1",
"metadata": {},
"outputs": [],
"source": [
"from llama_index.core import SimpleDirectoryReader\n",
"from llama_index.core import SummaryIndex\n",
"from llama_index.llms.openai import OpenAI\n",
"\n",
"llm = OpenAI(model=\"gpt-4o\")\n",
"input_file = \"caltrain_schedule_weekend.pdf\"\n",
"reader = SimpleDirectoryReader(input_files=[input_file])\n",
"base_docs = reader.load_data()\n",
"index = SummaryIndex.from_documents(base_docs)\n",
"base_query_engine = index.as_query_engine(llm=llm)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a4011389-2d27-4a1a-bf8d-7309da28ab15",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Southbound WEEKEND SERVICE to SAN JOSE\n",
"Train No. 224 228 232 236 240 244 248 252 256 260 264 268 272 276 280 284\n",
"Service Types L2 L2 L2 L2 L2 L2 L2 L2 L2 L2 L2 L2 L2 L2 L2 L2\n",
"San Francisco 8:28a 9:58a 10:58a 11:58a 12:58p 1:58p 2:58p 3:58p 4:58p 5:58p 6:58p 7:58p 8:58p 9:58p 10:58p 12:05a\n",
"22nd Street 8:33a 10:03a 11:03a 12:03p 1:03p 2:03p 3:03p 4:03p 5:03p 6:03p 7:03p 8:03p 9:03p 10:03p 11:03p 12:10a\n",
"Bayshore 8:38a 10:08a 11:08a 12:08p 1:08p 2:08p 3:08p 4:08p 5:08p 6:08p 7:08p 8:08p 9:08p 10:08p 11:08p 12:15a\n",
"S. San Francisco 8:45a 10:15a 11:15a 12:15p 1:15p 2:15p 3:15p 4:15p 5:15p 6:15p 7:15p 8:15p 9:15p 10:15p 11:15p 12:22a\n",
"San Bruno 8:49a 10:19a 11:19a 12:19p 1:19p 2:19p 3:19p 4:19p 5:19p 6:19p 7:19p 8:19p 9:19p 10:19p 11:19p 12:26a\n",
"Millbrae 8:53a 10:24a 11:24a 12:24p 1:24p 2:24p 3:24p 4:24p 5:24p 6:24p 7:24p 8:24p 9:24p 10:24p 11:24p 12:31a\n",
"Broadway 8:57a 10:27a 11:27a 12:27p 1:27p 2:27p 3:27p 4:27p 5:27p 6:27p 7:27p 8:27p 9:27p 10:27p 11:27p 12:35a\n",
"Burlingame 9:00a 10:31a 11:31a 12:31p 1:31p 2:31p 3:31p 4:31p 5:31p 6:31p 7:31p 8:31p 9:31p 10:31p 11:31p 12:38a\n",
"San Mateo 9:04a 10:34a 11:34a 12:34p 1:34p 2:34p 3:34p 4:34p 5:34p 6:34p 7:34p 8:34p 9:34p 10:34p 11:34p 12:41a\n",
"Hayward Park 9:07a 10:37a 11:37a 12:37p 1:37p 2:37p 3:37p 4:37p 5:37p 6:37p 7:37p 8:37p 9:37p 10:37p 11:37p 12:45a\n",
"Hillsdale 9:10a 10:41a 11:41a 12:41p 1:41p 2:41p 3:41p 4:41p 5:41p 6:41p 7:41p 8:41p 9:41p 10:41p 11:41p 12:48a\n",
"Belmont 9:14a 10:44a 11:44a 12:44p 1:44p 2:44p 3:44p 4:44p 5:44p 6:44p 7:44p 8:44p 9:44p 10:44p 11:44p 12:52a\n",
"San Carlos 9:17a 10:48a 11:48a 12:48p 1:48p 2:48p 3:48p 4:48p 5:48p 6:48p 7:48p 8:48p 9:48p 10:48p 11:48p 12:55a\n",
"Redwood City 9:21a 10:52a 11:52a 12:52p 1:52p 2:52p 3:52p 4:52p 5:52p 6:52p 7:52p 8:52p 9:52p 10:52p 11:52p 12:59a\n",
"Menlo Park 9:28a 10:58a 11:58a 12:58p 1:58p 2:58p 3:58p 4:58p 5:58p 6:58p 7:58p 8:58p 9:58p 10:58p 11:58p 1:05a\n",
"Palo Alto 9:32a 11:02a 12:02p 1:02p 2:02p 3:02p 4:02p 5:02p 6:02p 7:02p 8:02p 9:02p 10:02p 11:02p 12:02a 1:09a\n",
"California Avenue 9:36a 11:06a 12:06p 1:06p 2:06p 3:06p 4:06p 5:06p 6:06p 7:06p 8:06p 9:06p 10:06p 11:06p 12:06a 1:12a\n",
"San Antonio 9:41a 11:11a 12:11p 1:11p 2:11p 3:11p 4:11p 5:11p 6:11p 7:11p 8:11p 9:11p 10:11p 11:11p 12:10a 1:17a\n",
"Mountain View 9:45a 11:16a 12:16p 1:16p 2:16p 3:16p 4:16p 5:16p 6:16p 7:16p 8:16p 9:16p 10:16p 11:16p 12:15a 1:21a\n",
"Sunnyvale 9:51a 11:21a 12:21p 1:21p 2:21p 3:21p 4:21p 5:21p 6:21p 7:21p 8:21p 9:21p 10:21p 11:21p 12:20a 1:26a\n",
"Lawrence 9:55a 11:26a 12:26p 1:26p 2:26p 3:26p 4:26p 5:26p 6:26p 7:26p 8:26p 9:26p 10:26p 11:26p 12:25a 1:31a\n",
"Santa Clara 10:01a 11:32a 12:32p 1:32p 2:32p 3:32p 4:32p 5:32p 6:32p 7:32p 8:32p 9:32p 10:32p 11:32p 12:31a 1:37a\n",
"San Jose Diridon 10:10a 11:40a 12:40p 1:38p 2:40p 3:38p 4:40p 5:38p 6:40p 7:38p 8:40p 9:38p 10:40p 11:38p 12:39a 1:44a\n",
"Tamien 10:15a 11:45a 12:45p 2:45p 4:45p 6:45p 8:45p 10:45p 12:44a 1:49aPrinter-Friendly Caltrain Schedule\n",
"Northbound WEEKEND SERVICE to SAN FRANCISCO\n",
"Train No. 221 225 229 233 237 241 245 249 253 257 261 265 269 273 *277 *281\n",
"Service Types L2 L2 L2 L2 L2 L2 L2 L2 L2 L2 L2 L2 L2 L2 L2 L2\n",
"Tamien 7:12a 9:05a 10:05a 11:05a 1:05p 3:05p 5:05p 7:05p 9:05p 11:05p\n",
"San Jose Diridon 7:19a 9:12a 10:12a 11:12a 12:12p 1:12p 2:12p 3:12p 4:12p 5:12p 6:12p 7:12p 8:12p 9:12p 10:19p 11:12p\n",
"Santa Clara 7:25a 9:18a 10:18a 11:18a 12:18p 1:18p 2:18p 3:18p 4:18p 5:18p 6:18p 7:18p 8:18p 9:18p 10:25p 11:18p\n",
"Lawrence 7:31a 9:24a 10:24a 11:24a 12:24p 1:24p 2:24p 3:24p 4:24p 5:24p 6:24p 7:24p 8:24p 9:24p 10:31p 11:24p\n",
"Sunnyvale 7:35a 9:28a 10:28a 11:28a 12:28p 1:28p 2:28p 3:28p 4:28p 5:28p 6:28p 7:28p 8:28p 9:28p 10:35p 11:28p\n",
"Mountain View 7:40a 9:34a 10:34a 11:34a 12:34p 1:34p 2:34p 3:34p 4:34p 5:34p 6:34p 7:34p 8:34p 9:34p 10:40p 11:34p\n",
"San Antonio 7:43a 9:37a 10:37a 11:37a 12:37p 1:37p 2:37p 3:37p 4:37p 5:37p 6:37p 7:37p 8:37p 9:37p 10:44p 11:37p\n",
"California Ave 7:48a 9:42a 10:42a 11:42a 12:42p 1:42p 2:42p 3:42p 4:42p 5:42p 6:42p 7:42p 8:42p 9:42p 10:48p 11:42p\n",
"Palo Alto 7:52a 9:46a 10:46a 11:46a 12:46p 1:46p 2:46p 3:46p 4:46p 5:46p 6:46p 7:46p 8:46p 9:46p 10:53p 11:46p\n",
"Menlo Park 7:55a 9:50a 10:50a 11:50a 12:50p 1:50p 2:50p 3:50p 4:50p 5:50p 6:50p 7:50p 8:50p 9:50p 10:56p 11:50p\n",
"Redwood City 8:01a 9:56a 10:56a 11:56a 12:56p 1:56p 2:56p 3:56p 4:56p 5:56p 6:56p 7:56p 8:56p 9:56p 11:02p 11:56p\n",
"San Carlos 8:05a 10:01a 11:01a 12:01p 1:01p 2:01p 3:01p 4:01p 5:01p 6:01p 7:01p 8:01p 9:01p 10:01p 11:07p 12:01a\n",
"Belmont 8:09a 10:04a 11:04a 12:04p 1:04p 2:04p 3:04p 4:04p 5:04p 6:04p 7:04p 8:04p 9:04p 10:04p 11:10p 12:04a\n",
"Hillsdale 8:12a 10:08a 11:08a 12:08p 1:08p 2:08p 3:08p 4:08p 5:08p 6:08p 7:08p 8:08p 9:08p 10:08p 11:14p 12:08a\n",
"Hayward Park 8:15a 10:11a 11:11a 12:11p 1:11p 2:11p 3:11p 4:11p 5:11p 6:11p 7:11p 8:11p 9:11p 10:11p 11:17p 12:11a\n",
"San Mateo 8:19a 10:15a 11:15a 12:15p 1:15p 2:15p 3:15p 4:15p 5:15p 6:15p 7:15p 8:15p 9:15p 10:15p 11:21p 12:15a\n",
"Burlingame 8:22a 10:19a 11:19a 12:19p 1:19p 2:19p 3:19p 4:19p 5:19p 6:19p 7:19p 8:19p 9:19p 10:19p 11:25p 12:19a\n",
"Broadway 8:25a 10:22a 11:22a 12:22p 1:22p 2:22p 3:22p 4:22p 5:22p 6:22p 7:22p 8:22p 9:22p 10:22p 11:28p 12:22a\n",
"Millbrae 8:29a 10:26a 11:26a 12:26p 1:26p 2:26p 3:26p 4:26p 5:26p 6:26p 7:26p 8:26p 9:26p 10:26p 11:32p 12:26a\n",
"San Bruno 8:34a 10:30a 11:30a 12:30p 1:30p 2:30p 3:30p 4:30p 5:30p 6:30p 7:30p 8:30p 9:30p 10:30p 11:37p 12:30a\n",
"S. San Francisco 8:38a 10:34a 11:34a 12:34p 1:34p 2:34p 3:34p 4:34p 5:34p 6:34p 7:34p 8:34p 9:34p 10:34p 11:41p 12:34a\n",
"Bayshore 8:44a 10:41a 11:41a 12:41p 1:41p 2:41p 3:41p 4:41p 5:41p 6:41p 7:41p 8:41p 9:41p 10:41p 11:47p 12:41a\n",
"22nd Street 8:50a 10:46a 11:46a 12:46p 1:46p 2:46p 3:46p 4:46p 5:46p 6:46p 7:46p 8:46p 9:46p 10:46p 11:53p 12:46a\n",
"San Francisco 8:56a 10:52a 11:53a 12:53p 1:52p 2:52p 3:52p 4:52p 5:52p 6:52p 7:52p 8:52p 9:52p 10:52p 11:59p 12:52aZONE 2 ZONE 3 ZONE 4 ZONE 4 ZONE 3 ZONE 2 ZONE 1 ZONE 12XX Local\n",
"2XX Local\n",
"EFFECTIVE September 12, 2022 Timetable subject to change without notice. *On SAP Center event days, Train 277 or Train 281departure from San Jose Diridon station may be delayed and will depart no later than 10:30p or 11:30p respectively.\n"
]
}
],
"source": [
"print(base_docs[0].get_content())"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "42203c70-7ca7-4200-bf47-6282eefca3bf",
"metadata": {},
"outputs": [],
"source": [
"base_response = base_query_engine.query(\n",
" \"What are the stops (and times) for train no 237 northbound?\"\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "06aa47b6-0f31-4b2d-90f0-bf6c74befd38",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train No. 237 northbound stops at the following stations and times:\n",
"\n",
"- Tamien: 1:05p\n",
"- San Jose Diridon: 1:12p\n",
"- Santa Clara: 1:18p\n",
"- Lawrence: 1:24p\n",
"- Sunnyvale: 1:28p\n",
"- Mountain View: 1:34p\n",
"- San Antonio: 1:37p\n",
"- California Ave: 1:42p\n",
"- Palo Alto: 1:46p\n",
"- Menlo Park: 1:50p\n",
"- Redwood City: 1:56p\n",
"- San Carlos: 2:01p\n",
"- Belmont: 2:04p\n",
"- Hillsdale: 2:08p\n",
"- Hayward Park: 2:11p\n",
"- San Mateo: 2:15p\n",
"- Burlingame: 2:19p\n",
"- Broadway: 2:22p\n",
"- Millbrae: 2:26p\n",
"- San Bruno: 2:30p\n",
"- S. San Francisco: 2:34p\n",
"- Bayshore: 2:41p\n",
"- 22nd Street: 2:46p\n",
"- San Francisco: 2:52p\n"
]
}
],
"source": [
"print(str(base_response))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4f3c1de7-3351-4cd8-991c-34a777952194",
"metadata": {},
"outputs": [],
"source": [
"base_response = base_query_engine.query(\n",
" \"What are all the trains (and times) that end at Tamien going Southbound?\"\n",
")"
]
},
{
"cell_type": "markdown",
"id": "513b1007-7508-4fb1-836c-de9353433a67",
"metadata": {},
"source": [
"Note that the trains don't line up with the times!"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "108edb92-76af-406b-a139-8b9e7c6528f2",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The trains that end at Tamien going Southbound are:\n",
"\n",
"- Train 224 at 10:15a\n",
"- Train 228 at 11:45a\n",
"- Train 240 at 2:45p\n",
"- Train 252 at 4:45p\n",
"- Train 264 at 6:45p\n",
"- Train 276 at 8:45p\n",
"- Train 284 at 10:45p\n",
"- Train 284 at 12:44a\n"
]
}
],
"source": [
"print(str(base_response))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "llama_parse",
"language": "python",
"name": "llama_parse"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
+35 -40
View File
@@ -19,12 +19,12 @@
"metadata": {},
"outputs": [],
"source": [
"%pip install llama-index\n",
"%pip install llama-index-core\n",
"%pip install llama-index-embeddings-openai\n",
"%pip install llama-index-postprocessor-flag-embedding-reranker\n",
"%pip install git+https://github.com/FlagOpen/FlagEmbedding.git\n",
"%pip install llama-parse"
"!pip install llama-index\n",
"!pip install llama-index-core\n",
"!pip install llama-index-embeddings-openai\n",
"!pip install llama-index-postprocessor-flag-embedding-reranker\n",
"!pip install git+https://github.com/FlagOpen/FlagEmbedding.git\n",
"!pip install llama-parse"
]
},
{
@@ -33,7 +33,7 @@
"metadata": {},
"outputs": [],
"source": [
"!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/10q/uber_10q_march_2022.pdf' -O './uber_10q_march_2022.pdf'"
"!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/examples/data/10q/uber_10q_march_2022.pdf' -O './uber_10q_march_2022.pdf'"
]
},
{
@@ -45,17 +45,15 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"# llama-parse is async-first, running the async code in a notebook requires the use of nest_asyncio\n",
"import nest_asyncio\n",
"\n",
"nest_asyncio.apply()\n",
"\n",
"import os\n",
"\n",
"# API access to llama-cloud\n",
"os.environ[\"LLAMA_CLOUD_API_KEY\"] = \"llx-\"\n",
"\n",
@@ -65,7 +63,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
@@ -74,11 +72,11 @@
"from llama_index.core import VectorStoreIndex\n",
"from llama_index.core import Settings\n",
"\n",
"embed_model = OpenAIEmbedding(model=\"text-embedding-3-small\")\n",
"embed_model=OpenAIEmbedding(model=\"text-embedding-3-small\")\n",
"llm = OpenAI(model=\"gpt-3.5-turbo-0125\")\n",
"\n",
"Settings.llm = llm\n",
"Settings.embed_model = embed_model"
"Settings.embed_model = embed_model\n"
]
},
{
@@ -94,7 +92,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"metadata": {},
"outputs": [
{
@@ -108,12 +106,12 @@
"source": [
"from llama_parse import LlamaParse\n",
"\n",
"documents = LlamaParse(result_type=\"markdown\").load_data(\"./uber_10q_march_2022.pdf\")"
"documents = LlamaParse(result_type=\"markdown\").load_data('./uber_10q_march_2022.pdf')"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"metadata": {},
"outputs": [
{
@@ -148,25 +146,23 @@
}
],
"source": [
"print(documents[0].text[:1000] + \"...\")"
"print(documents[0].text[:1000] + '...')"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"from llama_index.core.node_parser import MarkdownElementNodeParser\n",
"\n",
"node_parser = MarkdownElementNodeParser(\n",
" llm=OpenAI(model=\"gpt-3.5-turbo-0125\"), num_workers=8\n",
")"
"node_parser = MarkdownElementNodeParser(llm=OpenAI(model=\"gpt-3.5-turbo-0125\"), num_workers=8)"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"metadata": {},
"outputs": [
{
@@ -191,7 +187,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
@@ -200,23 +196,21 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"recursive_index = VectorStoreIndex(nodes=base_nodes + objects)\n",
"recursive_index = VectorStoreIndex(nodes=base_nodes+objects)\n",
"raw_index = VectorStoreIndex.from_documents(documents)"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"from llama_index.postprocessor.flag_embedding_reranker import (\n",
" FlagEmbeddingReranker,\n",
")\n",
"from llama_index.postprocessor.flag_embedding_reranker import FlagEmbeddingReranker\n",
"\n",
"reranker = FlagEmbeddingReranker(\n",
" top_n=5,\n",
@@ -224,17 +218,17 @@
")\n",
"\n",
"recursive_query_engine = recursive_index.as_query_engine(\n",
" similarity_top_k=15, node_postprocessors=[reranker], verbose=True\n",
" similarity_top_k=15, \n",
" node_postprocessors=[reranker], \n",
" verbose=True\n",
")\n",
"\n",
"raw_query_engine = raw_index.as_query_engine(\n",
" similarity_top_k=15, node_postprocessors=[reranker]\n",
")"
"raw_query_engine = raw_index.as_query_engine(similarity_top_k=15, node_postprocessors=[reranker])"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 13,
"metadata": {},
"outputs": [
{
@@ -266,7 +260,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 14,
"metadata": {},
"outputs": [
{
@@ -303,7 +297,7 @@
"\n",
"response_2 = recursive_query_engine.query(query)\n",
"print(\"\\n***********New LlamaParse+ Recursive Retriever Query Engine***********\")\n",
"print(response_2)"
"print(response_2)\n"
]
},
{
@@ -320,7 +314,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 15,
"metadata": {},
"outputs": [
{
@@ -380,7 +374,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 16,
"metadata": {},
"outputs": [
{
@@ -436,7 +430,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 17,
"metadata": {},
"outputs": [
{
@@ -510,7 +504,8 @@
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3"
"pygments_lexer": "ipython3",
"version": "3.10.10"
}
},
"nbformat": 4,
+41 -50
View File
@@ -17,13 +17,13 @@
"metadata": {},
"outputs": [],
"source": [
"%pip install llama-index\n",
"%pip install llama-index-core==0.10.6.post1\n",
"%pip install llama-index-embeddings-openai\n",
"%pip install llama-index-postprocessor-flag-embedding-reranker\n",
"%pip install git+https://github.com/FlagOpen/FlagEmbedding.git\n",
"%pip install llama-parse\n",
"%pip install llama-index-vector-stores-astra-db"
"!pip install llama-index\n",
"!pip install llama-index-core==0.10.6.post1\n",
"!pip install llama-index-embeddings-openai\n",
"!pip install llama-index-postprocessor-flag-embedding-reranker\n",
"!pip install git+https://github.com/FlagOpen/FlagEmbedding.git\n",
"!pip install llama-parse\n",
"!pip install llama-index-vector-stores-astra-db"
]
},
{
@@ -32,7 +32,7 @@
"metadata": {},
"outputs": [],
"source": [
"!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/10q/uber_10q_march_2022.pdf' -O './uber_10q_march_2022.pdf'"
"!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/examples/data/10q/uber_10q_march_2022.pdf' -O './uber_10q_march_2022.pdf'"
]
},
{
@@ -44,17 +44,15 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# llama-parse is async-first, running the async code in a notebook requires the use of nest_asyncio\n",
"import nest_asyncio\n",
"\n",
"nest_asyncio.apply()\n",
"\n",
"import os\n",
"\n",
"# API access to llama-cloud\n",
"os.environ[\"LLAMA_CLOUD_API_KEY\"] = \"llx-\"\n",
"\n",
@@ -68,7 +66,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
@@ -77,7 +75,7 @@
"from llama_index.core import VectorStoreIndex\n",
"from llama_index.core import Settings\n",
"\n",
"embed_model = OpenAIEmbedding(model=\"text-embedding-3-small\")\n",
"embed_model=OpenAIEmbedding(model=\"text-embedding-3-small\")\n",
"llm = OpenAI(model=\"gpt-3.5-turbo-0125\")\n",
"\n",
"Settings.llm = llm\n",
@@ -95,7 +93,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [
{
@@ -109,12 +107,12 @@
"source": [
"from llama_parse import LlamaParse\n",
"\n",
"documents = LlamaParse(result_type=\"markdown\").load_data(\"./uber_10q_march_2022.pdf\")"
"documents = LlamaParse(result_type=\"markdown\").load_data('./uber_10q_march_2022.pdf')"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {},
"outputs": [
{
@@ -170,7 +168,7 @@
}
],
"source": [
"print(documents[0].text[:1000] + \"...\")"
"print(documents[0].text[:1000] + '...')"
]
},
{
@@ -182,27 +180,29 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"execution_count": 8,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from llama_index.vector_stores.astra_db import AstraDBVectorStore\n",
"\n",
"# define two storage classes representing two collections (to compare advanced approach vs. baseline)\n",
"# define two storage classes representing two collections (to compare advanced approach vs. baseline) \n",
"\n",
"astra_db_store_advanced = AstraDBVectorStore(\n",
" token=ASTRA_TOKEN,\n",
" api_endpoint=ASTRA_API_ENDPOINT,\n",
" namespace=ASTRA_NAMESPACE,\n",
" collection_name=\"astra_v_table_llamaparse_advanced\",\n",
" embedding_dimension=1536,\n",
" embedding_dimension=1536\n",
")\n",
"astra_db_store_base = AstraDBVectorStore(\n",
" token=ASTRA_TOKEN,\n",
" api_endpoint=ASTRA_API_ENDPOINT,\n",
" namespace=ASTRA_NAMESPACE,\n",
" collection_name=\"astra_v_table_llamaparse_base\",\n",
" embedding_dimension=1536,\n",
" embedding_dimension=1536\n",
")"
]
},
@@ -219,15 +219,13 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"from llama_index.core.node_parser import MarkdownElementNodeParser\n",
"\n",
"node_parser = MarkdownElementNodeParser(\n",
" llm=OpenAI(model=\"gpt-3.5-turbo-0125\"), num_workers=8\n",
")"
"node_parser = MarkdownElementNodeParser(llm=OpenAI(model=\"gpt-3.5-turbo-0125\"), num_workers=8)"
]
},
{
@@ -241,7 +239,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
@@ -250,23 +248,17 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"from llama_index.core import StorageContext\n",
"\n",
"storage_context_advanced = StorageContext.from_defaults(\n",
" vector_store=astra_db_store_advanced\n",
")\n",
"storage_context_advanced = StorageContext.from_defaults(vector_store=astra_db_store_advanced)\n",
"storage_context_base = StorageContext.from_defaults(vector_store=astra_db_store_base)\n",
"\n",
"recursive_index = VectorStoreIndex(\n",
" nodes=base_nodes + objects, storage_context=storage_context_advanced\n",
")\n",
"raw_index = VectorStoreIndex.from_documents(\n",
" documents, storage_context=storage_context_base\n",
")"
"recursive_index = VectorStoreIndex(nodes=base_nodes+objects, storage_context=storage_context_advanced)\n",
"raw_index = VectorStoreIndex.from_documents(documents, storage_context=storage_context_base)"
]
},
{
@@ -275,9 +267,7 @@
"metadata": {},
"outputs": [],
"source": [
"from llama_index.postprocessor.flag_embedding_reranker import (\n",
" FlagEmbeddingReranker,\n",
")\n",
"from llama_index.postprocessor.flag_embedding_reranker import FlagEmbeddingReranker\n",
"\n",
"reranker = FlagEmbeddingReranker(\n",
" top_n=5,\n",
@@ -285,12 +275,12 @@
")\n",
"\n",
"recursive_query_engine = recursive_index.as_query_engine(\n",
" similarity_top_k=15, node_postprocessors=[reranker], verbose=True\n",
" similarity_top_k=15, \n",
" node_postprocessors=[reranker], \n",
" verbose=True\n",
")\n",
"\n",
"raw_query_engine = raw_index.as_query_engine(\n",
" similarity_top_k=15, node_postprocessors=[reranker]\n",
")"
"raw_query_engine = raw_index.as_query_engine(similarity_top_k=15, node_postprocessors=[reranker])"
]
},
{
@@ -310,7 +300,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 12,
"metadata": {},
"outputs": [
{
@@ -351,7 +341,7 @@
"\n",
"response_2 = recursive_query_engine.query(query)\n",
"print(\"\\n***********New LlamaParse+ Recursive Retriever Query Engine***********\")\n",
"print(response_2)"
"print(response_2)\n"
]
},
{
@@ -368,7 +358,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 13,
"metadata": {},
"outputs": [
{
@@ -432,7 +422,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 14,
"metadata": {},
"outputs": [
{
@@ -488,7 +478,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 15,
"metadata": {},
"outputs": [
{
@@ -564,7 +554,8 @@
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3"
"pygments_lexer": "ipython3",
"version": "3.10.8"
}
},
"nbformat": 4,
+8 -6
View File
@@ -11,7 +11,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {},
"outputs": [
{
@@ -38,7 +38,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
@@ -47,7 +47,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
@@ -88,7 +88,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"metadata": {},
"outputs": [
{
@@ -128,8 +128,10 @@
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3"
}
"pygments_lexer": "ipython3",
"version": "3.11.5"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
+23 -22
View File
@@ -23,12 +23,12 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# First, install the required dependencies\n",
"%pip install --quiet llama-index llama-parse llama-index-vector-stores-astra-db llama-index-llms-openai"
"!pip install --quiet llama-index llama-parse llama-index-vector-stores-astra-db llama-index-llms-openai"
]
},
{
@@ -40,7 +40,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
@@ -53,9 +53,7 @@
"llama_cloud_api_key = getpass(\"Enter your Llama Index Cloud API Key: \")\n",
"api_endpoint = input(\"Enter your Astra DB API Endpoint: \")\n",
"token = getpass(\"Enter your Astra DB Token: \")\n",
"namespace = (\n",
" input(\"Enter your Astra DB namespace (optional, must exist on Astra): \") or None\n",
")\n",
"namespace = input(\"Enter your Astra DB namespace (optional, must exist on Astra): \") or None\n",
"openai_api_key = getpass(\"Enter your OpenAI API Key: \")\n",
"\n",
"os.environ[\"LLAMA_CLOUD_API_KEY\"] = llama_cloud_api_key\n",
@@ -64,7 +62,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
@@ -83,7 +81,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {},
"outputs": [
{
@@ -96,7 +94,7 @@
],
"source": [
"# Grab a PDF from Arxiv for indexing\n",
"import requests\n",
"import requests \n",
"\n",
"# The URL of the file you want to download\n",
"url = \"https://arxiv.org/pdf/1706.03762.pdf\"\n",
@@ -118,7 +116,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"metadata": {},
"outputs": [
{
@@ -137,7 +135,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"metadata": {},
"outputs": [
{
@@ -146,7 +144,7 @@
"'rmer - model architecture.\\nThe Transformer follows this overall architecture using stacked self-attention and point-wise, fully\\nconnected layers for both the encoder and decoder, shown in the left and right halves of Figure 1,\\nrespectively.\\n3.1 Encoder and Decoder Stacks\\nEncoder: The encoder is composed of a stack of N = 6 identical layers. Each layer has two\\nsub-layers. The first is a multi-head self-attention mechanism, and the second is a simple, position-\\nwise fully connected feed-forward network. We employ a residual connection [11] around each of\\nthe two sub-layers, followed by layer normalization [1]. That is, the output of each sub-layer is\\nLayerNorm(x + Sublayer(x)), where Sublayer(x) is the function implemented by the sub-layer\\nitself. To facilitate these residual connections, all sub-layers in the model, as well as the embedding\\nlayers, produce outputs of dimension dmodel = 512.\\nDecoder: The decoder is also composed of a stack of N = 6 identical layers. In addition '"
]
},
"execution_count": null,
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
@@ -165,7 +163,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
@@ -176,14 +174,16 @@
" api_endpoint=api_endpoint,\n",
" namespace=namespace,\n",
" collection_name=\"astra_v_table_llamaparse\",\n",
" embedding_dimension=1536,\n",
" embedding_dimension=1536\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"execution_count": 8,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from llama_index.core.node_parser import SimpleNodeParser\n",
@@ -195,7 +195,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
@@ -220,7 +220,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
@@ -229,7 +229,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 11,
"metadata": {},
"outputs": [
{
@@ -252,7 +252,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 12,
"metadata": {},
"outputs": [
{
@@ -261,7 +261,7 @@
"'We used beam search as described in the previous section, but no\\ncheckpoint averaging. We present these results in Table 3.\\nIn Table 3 rows (A), we vary the number of attention heads and the attention key and value dimensions,\\nkeeping the amount of computation constant, as described in Section 3.2.2. While single-head\\nattention is 0.9 BLEU worse than the best setting, quality also drops off with too many heads.\\nIn Table 3 rows (B), we observe that reducing the attention key size dk hurts model quality. This\\nsuggests that determining compatibility is not easy and that a more sophisticated compatibility\\nfunction than dot product may be beneficial. We further observe in rows (C) and (D) that, as expected,\\nbigger models are better, and dropout is very helpful in avoiding over-fitting. In row (E) we replace our\\nsinusoidal positional encoding with learned positional embeddings [9], and observe nearly identical\\nresults to the base model.\\n6.3 English Constituency Parsing\\nTo evaluate if the Transformer can generalize to other tasks we performed experiments on English\\nconstituency parsing. This task presents specific challenges: the output is subject to strong structural\\nconstraints and is significantly longer than the input. Furthermore, RNN sequence-to-sequence\\nmodels have not been able to attain state-of-the-art results in small-data regimes [37].\\nWe trained a 4-layer transformer with dmodel = 1024 on the Wall Street Journal (WSJ) portion of the\\nPenn Treebank [25], about 40K training sentences. We also trained it in a semi-supervised setting,\\nusing the larger high-confidence and BerkleyParser corpora from with approximately 17M sentences\\n[37]. We used a vocabulary of 16K tokens for the WSJ only setting and a vocabulary of 32K tokens\\nfor the semi-supervised setting.\\nWe performed only a small number of experiments to select the dropout, both attention and residual\\n(section 5.4), learning rates and beam size on the Section 22 development set, all other parameters\\nremained unchanged from the English-to-German base translation model. During inference, we\\n 9\\n---\\nTable 4: The Transformer generalizes well to English constituency parsing (Results are on Section 23\\nof WSJ)\\n Parser Training WSJ 23 F1\\n Vinyals & Kaiser el al. (2014) [37] WSJ only, discriminative 88.3\\n Petrov et al. (2006) [29] WSJ only, discriminative 90.4\\n Zhu et al. (2013) [40] WSJ only, discriminative 90.4\\n Dyer et al. (2016) [8] WSJ only, discriminative 91.7\\n Transformer (4 layers) WSJ only, discriminative 91.3\\n Zhu et al. (2013) [40] semi-supervised 91.3\\n Huang & Harper (2009) [14] semi-supervised 91.3\\n McClosky et al. (2006) [26] semi-supervised 92.1\\n Vinyals & Kaiser el al. (2014) [37] semi-supervised 92.1\\n Transformer (4 layers) semi-supervised 92.7\\n Luong et al. (2015) [23] multi-task 93.0\\n Dyer et al. (2016) [8] generative 93.3\\nincreased the maximum output length to input length + 300. We used a beam size of 21 and α = 0.3\\nfor both WSJ only and the semi-supervised setting.\\nOur results in Table 4 show that despite the lack of task-specific tuning our model performs sur-\\nprisingly well, yielding better results than all previously reported models with the exception of the\\nRecurrent Neural Network Grammar [8].\\nIn contrast to RNN sequence-to-sequence models [37], the Transformer outperforms the Berkeley-\\nParser [29] even when training only on the WSJ training set of 40K sentences.\\n7 Conclusion\\nIn this work, we presented the Transformer, the first sequence transduction model based entirely on\\nattention, replacing the recurrent layers most commonly used in encoder-decoder architectures with\\nmulti-headed self-attention.\\nFor translation tasks, the Transformer can be trained significantly faster than architectures based\\non recurrent or convolutional layers.'"
]
},
"execution_count": null,
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
@@ -287,7 +287,8 @@
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3"
"pygments_lexer": "ipython3",
"version": "3.11.6"
}
},
"nbformat": 4,
+16 -9
View File
@@ -13,12 +13,12 @@
"metadata": {},
"outputs": [],
"source": [
"%pip install llama-index llama-parse"
"!pip install llama-index llama-parse"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [
{
@@ -45,7 +45,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
@@ -55,13 +55,12 @@
"nest_asyncio.apply()\n",
"\n",
"import os\n",
"\n",
"os.environ[\"LLAMA_CLOUD_API_KEY\"] = \"llx-...\""
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [
{
@@ -80,7 +79,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {},
"outputs": [
{
@@ -108,7 +107,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"metadata": {},
"outputs": [
{
@@ -127,7 +126,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"metadata": {},
"outputs": [
{
@@ -158,6 +157,13 @@
"source": [
"print(documents[0].text[20000:21000] + \"...\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
@@ -175,7 +181,8 @@
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3"
"pygments_lexer": "ipython3",
"version": "3.10.10"
}
},
"nbformat": 4,
+81 -57
View File
@@ -24,7 +24,7 @@
"metadata": {},
"outputs": [],
"source": [
"%pip install llama-index llama-parse"
"!pip install llama-index llama-parse"
]
},
{
@@ -39,7 +39,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"!wget \"https://policyholder.gov.in/documents/37343/931203/NBHTGBP22011V012223.pdf/c392bcc1-f6a8-cadd-ab84-495b3273d2c3?version=1.0&t=1669350459879&download=true\" -O \"./policy.pdf\""
@@ -54,24 +56,24 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"execution_count": 1,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# llama-parse is async-first, running the sync code in a notebook requires the use of nest_asyncio\n",
"import nest_asyncio\n",
"\n",
"nest_asyncio.apply()"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"\n",
"os.environ[\"LLAMA_CLOUD_API_KEY\"] = \"llx-...\"\n",
"os.environ[\"OPENAI_API_KEY\"] = \"sk-...\""
]
@@ -79,7 +81,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from llama_index.llms.openai import OpenAI\n",
@@ -88,7 +92,7 @@
"from llama_index.core import Settings\n",
"\n",
"# for the purpose of this example, we will use the small model embedding and gpt3.5\n",
"embed_model = OpenAIEmbedding(model=\"text-embedding-3-small\")\n",
"embed_model=OpenAIEmbedding(model=\"text-embedding-3-small\")\n",
"llm = OpenAI(model=\"gpt-3.5-turbo-0125\")\n",
"\n",
"Settings.llm = llm"
@@ -103,8 +107,10 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"execution_count": 3,
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
@@ -123,8 +129,10 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"execution_count": 4,
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
@@ -160,21 +168,23 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"execution_count": 5,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from llama_index.core.node_parser import MarkdownElementNodeParser\n",
"\n",
"node_parser = MarkdownElementNodeParser(\n",
" llm=OpenAI(model=\"gpt-3.5-turbo-0125\"), num_workers=8\n",
")"
"node_parser = MarkdownElementNodeParser(llm=OpenAI(model=\"gpt-3.5-turbo-0125\"), num_workers=8)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"nodes = node_parser.get_nodes_from_documents(documents)"
@@ -182,19 +192,23 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"execution_count": 7,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"base_nodes, objects = node_parser.get_nodes_and_objects(nodes)\n",
"\n",
"recursive_index = VectorStoreIndex(nodes=base_nodes + objects)"
"recursive_index = VectorStoreIndex(nodes=base_nodes+objects)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"execution_count": 8,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"query_engine = recursive_index.as_query_engine(similarity_top_k=25)"
@@ -209,8 +223,10 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"execution_count": 9,
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
@@ -236,8 +252,10 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"execution_count": 10,
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
@@ -249,9 +267,7 @@
}
],
"source": [
"documents_with_instruction = LlamaParse(\n",
" result_type=\"markdown\",\n",
" parsing_instruction=\"\"\"\n",
"documents_with_instruction = LlamaParse(result_type=\"markdown\", parsing_instruction=\"\"\"\n",
"This document is an insurance policy.\n",
"When a benefits/coverage/exlusion is describe in the document ammend to it add a text in the follwing benefits string format (where coverage could be an exclusion).\n",
"\n",
@@ -259,8 +275,7 @@
" \n",
"If the document contain a benefits TABLE that describe coverage amounts, do not ouput it as a table, but instead as a list of benefits string.\n",
" \n",
"\"\"\",\n",
").load_data(\"./policy.pdf\")"
"\"\"\").load_data(\"./policy.pdf\")"
]
},
{
@@ -272,8 +287,10 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"execution_count": 11,
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
@@ -387,24 +404,17 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"node_parser_instruction = MarkdownElementNodeParser(\n",
" llm=OpenAI(model=\"gpt-3.5-turbo-0125\"), num_workers=8\n",
")\n",
"node_parser_instruction = MarkdownElementNodeParser(llm=OpenAI(model=\"gpt-3.5-turbo-0125\"), num_workers=8)\n",
"nodes_instruction = node_parser.get_nodes_from_documents(documents_with_instruction)\n",
"(\n",
" base_nodes_instruction,\n",
" objects_instruction,\n",
") = node_parser_instruction.get_nodes_and_objects(nodes_instruction)\n",
"base_nodes_instruction, objects_instruction = node_parser_instruction.get_nodes_and_objects(nodes_instruction)\n",
"\n",
"recursive_index_instruction = VectorStoreIndex(\n",
" nodes=base_nodes_instruction + objects_instruction\n",
")\n",
"query_engine_instruction = recursive_index_instruction.as_query_engine(\n",
" similarity_top_k=25\n",
")"
"recursive_index_instruction = VectorStoreIndex(nodes=base_nodes_instruction+objects_instruction)\n",
"query_engine_instruction = recursive_index_instruction.as_query_engine(similarity_top_k=25)"
]
},
{
@@ -418,8 +428,10 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"execution_count": 22,
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
@@ -441,7 +453,7 @@
"\n",
"print(\"With instructions:\")\n",
"response_1_i = query_engine_instruction.query(query_1)\n",
"print(response_1_i)"
"print(response_1_i)\n"
]
},
{
@@ -453,8 +465,10 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"execution_count": 16,
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
@@ -481,8 +495,10 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"execution_count": 18,
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
@@ -506,6 +522,13 @@
"response_3_i = query_engine_instruction.query(query_3)\n",
"print(response_3_i)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
@@ -523,7 +546,8 @@
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3"
"pygments_lexer": "ipython3",
"version": "3.10.8"
}
},
"nbformat": 4,
+75 -40
View File
@@ -40,18 +40,18 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "0879301c-ff91-4431-941a-6c0ef7cd8fe2",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# llama-parse is async-first, running the async code in a notebook requires the use of nest_asyncio\n",
"import nest_asyncio\n",
"\n",
"nest_asyncio.apply()\n",
"\n",
"import os\n",
"\n",
"# API access to llama-cloud\n",
"os.environ[\"LLAMA_CLOUD_API_KEY\"] = \"llx-\"\n",
"\n",
@@ -63,7 +63,9 @@
"cell_type": "code",
"execution_count": null,
"id": "391e2d95-5569-4d73-9f16-5b59d7326f8d",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from llama_index.llms.anthropic import Anthropic\n",
@@ -73,9 +75,11 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"id": "700f48e8-8b52-41f3-90f9-144d5fdd5c52",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from llama_index.core import Settings\n",
@@ -98,10 +102,12 @@
"cell_type": "code",
"execution_count": null,
"id": "c39d408f-e885-4940-85c7-b09ca3bc7cb7",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/10q/uber_10q_march_2022.pdf' -O './uber_10q_march_2022.pdf'"
"!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/examples/data/10q/uber_10q_march_2022.pdf' -O './uber_10q_march_2022.pdf'"
]
},
{
@@ -116,9 +122,11 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"id": "9c9cd670-8229-4ad6-99a9-845bd82b7ec1",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
@@ -138,9 +146,11 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"id": "b26d21d1-05b5-4f49-b937-c13106a84015",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from llama_index.core.schema import TextNode\n",
@@ -150,16 +160,23 @@
"def get_text_nodes(json_list: List[dict]):\n",
" text_nodes = []\n",
" for idx, page in enumerate(json_list):\n",
" text_node = TextNode(text=page[\"text\"], metadata={\"page\": page[\"page\"]})\n",
" text_node = TextNode(\n",
" text=page[\"text\"],\n",
" metadata={\n",
" \"page\": page[\"page\"]\n",
" }\n",
" )\n",
" text_nodes.append(text_node)\n",
" return text_nodes"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"id": "364a3276-d2db-4aee-9bc6-617ffd726d25",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"text_nodes = get_text_nodes(json_list)"
@@ -177,7 +194,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"id": "36012145-5521-4ddb-a53e-df9ebd1ca8dd",
"metadata": {},
"outputs": [
@@ -206,10 +223,13 @@
" for image_dict in image_dicts:\n",
" image_doc = ImageDocument(image_path=image_dict[\"path\"])\n",
" response = anthropic_mm_llm.complete(\n",
" prompt=\"Describe the images as alt text\",\n",
" prompt=\"Describe the images as an alternative text\",\n",
" image_documents=[image_doc],\n",
" )\n",
" text_node = TextNode(text=str(response), metadata={\"path\": image_dict[\"path\"]})\n",
" text_node = TextNode(\n",
" text=str(response),\n",
" metadata={\"path\": image_dict[\"path\"]}\n",
" )\n",
" img_text_nodes.append(text_node)\n",
" return img_text_nodes"
]
@@ -218,7 +238,9 @@
"cell_type": "code",
"execution_count": null,
"id": "38f25045-6102-4920-9cd0-42b0ae6c872f",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"image_text_nodes = get_image_text_nodes(json_objs)"
@@ -226,9 +248,11 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 60,
"id": "4683c97a-da06-408a-9fe9-7e3c0aceb77d",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
@@ -236,7 +260,7 @@
"'The image shows a bar graph titled \"Monthly Active Platform Consumers (in millions)\". The graph displays data from Q2 2020 to Q1 2022 over 8 quarters. The number of monthly active platform consumers starts at 55 million in Q2 2020 and steadily increases each quarter, reaching 115 million by Q1 2022. The graph illustrates consistent quarter-over-quarter growth in this metric over the nearly 2 year time period shown.'"
]
},
"execution_count": null,
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
}
@@ -257,7 +281,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 68,
"id": "939aec6c-064a-4319-b2dc-70cc4a304c06",
"metadata": {},
"outputs": [],
@@ -269,7 +293,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 69,
"id": "529340d5-9319-4cdf-8ee1-bbd01ed00226",
"metadata": {},
"outputs": [],
@@ -279,9 +303,11 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 71,
"id": "81d7ff30-5a87-44da-880d-4b1f41434d90",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
@@ -294,18 +320,18 @@
}
],
"source": [
"# ask question over image!\n",
"response = query_engine.query(\n",
" \"What does the bar graph titled 'Monthly Active Platform Consumers' show?\"\n",
")\n",
"print(str(response))"
"# ask question over image! \n",
"response = query_engine.query(\"What does the bar graph titled 'Monthly Active Platform Consumers' show?\") \n",
"print(str(response)) "
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 72,
"id": "c4f14ad8-6bfd-49d9-b3d5-7215cf0e4ac1",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
@@ -334,17 +360,25 @@
}
],
"source": [
"# ask question over text!\n",
"response = query_engine.query(\"What are the main risk factors for Uber?\")\n",
"print(str(response))"
"# ask question over text! \n",
"response = query_engine.query(\"What are the main risk factors for Uber?\") \n",
"print(str(response)) "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "82ea880b-a0c7-410c-94c7-8fb3ac96c30c",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "llama-parse-aNC435Vv-py3.10",
"display_name": "llama_parse",
"language": "python",
"name": "python3"
"name": "llama_parse"
},
"language_info": {
"codemirror_mode": {
@@ -355,7 +389,8 @@
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3"
"pygments_lexer": "ipython3",
"version": "3.10.8"
}
},
"nbformat": 4,
+30 -32
View File
@@ -33,27 +33,27 @@
"metadata": {},
"outputs": [],
"source": [
"%pip install llama-index\n",
"%pip install llama-index-core\n",
"%pip install llama-index-llms-anthropic llama-index-multi-modal-llms-anthropic\n",
"%pip install llama-index-embeddings-huggingface\n",
"%pip install llama-parse"
"!pip install llama-index\n",
"!pip install llama-index-core\n",
"!pip install llama-index-llms-anthropic llama-index-multi-modal-llms-anthropic\n",
"!pip install llama-index-embeddings-huggingface\n",
"!pip install llama-parse"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"id": "0879301c-ff91-4431-941a-6c0ef7cd8fe2",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# llama-parse is async-first, running the async code in a notebook requires the use of nest_asyncio\n",
"import nest_asyncio\n",
"\n",
"nest_asyncio.apply()\n",
"\n",
"import os\n",
"\n",
"# API access to llama-cloud\n",
"os.environ[\"LLAMA_CLOUD_API_KEY\"] = \"llx-\"\n",
"\n",
@@ -73,10 +73,12 @@
"cell_type": "code",
"execution_count": null,
"id": "391e2d95-5569-4d73-9f16-5b59d7326f8d",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/10q/uber_10q_march_2022.pdf' -O './uber_10q_march_2022.pdf'"
"!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/examples/data/10q/uber_10q_march_2022.pdf' -O './uber_10q_march_2022.pdf'"
]
},
{
@@ -89,7 +91,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"id": "33f2a06e",
"metadata": {},
"outputs": [
@@ -127,7 +129,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 28,
"id": "e1e9e955",
"metadata": {},
"outputs": [
@@ -155,7 +157,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"id": "3108ba82",
"metadata": {},
"outputs": [
@@ -186,7 +188,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"id": "07669c6a",
"metadata": {},
"outputs": [
@@ -218,12 +220,10 @@
"metadata": {},
"outputs": [],
"source": [
"from llama_index.core.node_parser import LlamaParseJsonNodeParser\n",
"from llama_index.core.node_parser import LlamaParseJsonNodeParser \n",
"from llama_index.llms.openai import OpenAI\n",
"\n",
"node_parser = LlamaParseJsonNodeParser(\n",
" llm=OpenAI(model=\"gpt-3.5-turbo\"), num_workers=16, include_metadata=True\n",
")\n",
"node_parser = LlamaParseJsonNodeParser(llm=OpenAI(model=\"gpt-3.5-turbo\"), num_workers=16, include_metadata=True)\n",
"nodes = node_parser.get_nodes_from_documents(documents)\n",
"base_nodes, objects = node_parser.get_nodes_and_objects(nodes)"
]
@@ -238,7 +238,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 15,
"id": "71b75550",
"metadata": {},
"outputs": [
@@ -260,18 +260,17 @@
"from llama_index.core import VectorStoreIndex\n",
"\n",
"\n",
"recursive_index = VectorStoreIndex(nodes=base_nodes + objects)\n",
"recursive_index = VectorStoreIndex(nodes=base_nodes+objects)\n",
"recursive_query_engine = recursive_index.as_query_engine(\n",
" similarity_top_k=5, verbose=True\n",
" similarity_top_k=5, \n",
" verbose=True\n",
")\n",
"res = recursive_query_engine.query(\n",
" \"what is UBER Short-term insurance reserves reported in 2022\"\n",
")"
"res = recursive_query_engine.query(\"what is UBER Short-term insurance reserves reported in 2022\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 18,
"id": "965fdd22",
"metadata": {},
"outputs": [
@@ -302,7 +301,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 23,
"id": "0f917e20",
"metadata": {},
"outputs": [
@@ -323,14 +322,12 @@
}
],
"source": [
"res = recursive_query_engine.query(\n",
" \"what is Comprehensive income (loss) attributable to Uber reported in 2021\"\n",
")"
"res = recursive_query_engine.query(\"what is Comprehensive income (loss) attributable to Uber reported in 2021\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 24,
"id": "85b5c025",
"metadata": {},
"outputs": [
@@ -375,7 +372,8 @@
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3"
"pygments_lexer": "ipython3",
"version": "3.11.6"
}
},
"nbformat": 4,
+60 -23
View File
@@ -21,14 +21,16 @@
"metadata": {},
"outputs": [],
"source": [
"%pip install llama-index llama-parse"
"!pip install llama-index llama-parse"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"id": "87322210-c21c-43d6-b459-2e8a828ac576",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# llama-parse is async-first, running the sync code in a notebook requires the use of nest_asyncio\n",
@@ -37,7 +39,6 @@
"nest_asyncio.apply()\n",
"\n",
"import os\n",
"\n",
"# os.environ[\"LLAMA_CLOUD_API_KEY\"] = \"llx-...\""
]
},
@@ -55,7 +56,9 @@
"cell_type": "code",
"execution_count": null,
"id": "e81e0a08-3a99-42e6-adcc-00bb4ce1c3d4",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"!wget \"https://www.dropbox.com/scl/fi/fxg17log5ydwoflhxmgrb/treasury_report.pdf?rlkey=mdintk0o2uuzkple26vc4v6fd&dl=1\" -O treasury_report.pdf"
@@ -63,9 +66,11 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"id": "ecfc578c-3c7f-4ec1-aa06-51565c28632b",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
@@ -79,15 +84,20 @@
"source": [
"from llama_parse import LlamaParse\n",
"\n",
"parser = LlamaParse(result_type=\"text\", language=\"fr\")\n",
"parser = LlamaParse(\n",
" result_type=\"text\",\n",
" language=\"fr\"\n",
")\n",
"documents = parser.load_data(\"./treasury_report.pdf\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"id": "0c37db27-3496-4a59-918b-701c9ad7706d",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
@@ -199,9 +209,11 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 24,
"id": "ac332ea3-cfff-4216-b292-62410a26c336",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
@@ -236,9 +248,11 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 25,
"id": "45235b17-08f0-48f1-92aa-06711225860b",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
@@ -252,15 +266,20 @@
"source": [
"from llama_parse import LlamaParse\n",
"\n",
"parser = LlamaParse(result_type=\"text\", language=\"ch_sim\")\n",
"parser = LlamaParse(\n",
" result_type=\"text\",\n",
" language=\"ch_sim\"\n",
")\n",
"documents = parser.load_data(\"./chinese_pdf.pdf\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 28,
"id": "f0d546cc-6549-4cf5-8b37-0896f4e8d43d",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
@@ -382,7 +401,9 @@
"cell_type": "code",
"execution_count": null,
"id": "640f0679-7f7e-4b0a-a46d-b099ae382fe2",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# download another copy with a different name to avoid hitting pdf cache\n",
@@ -391,9 +412,11 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 31,
"id": "bfcacf90-ca67-4bfd-b023-be0af2cb18c5",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
@@ -406,7 +429,10 @@
"source": [
"from llama_parse import LlamaParse\n",
"\n",
"base_parser = LlamaParse(result_type=\"text\", language=\"en\")\n",
"base_parser = LlamaParse(\n",
" result_type=\"text\",\n",
" language=\"en\"\n",
")\n",
"base_documents = parser.load_data(\"./chinese_pdf2.pdf\")"
]
},
@@ -414,11 +440,21 @@
"cell_type": "code",
"execution_count": null,
"id": "b264ed4e-647a-4f51-9f79-fdf82b76762a",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"print(base_documents[0].get_content()[1000:10000])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d9f02762-bb97-4e0e-8268-ccc00612a974",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
@@ -436,7 +472,8 @@
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3"
"pygments_lexer": "ipython3",
"version": "3.10.8"
}
},
"nbformat": 4,
+425 -359
View File
@@ -1,368 +1,434 @@
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# LlamaParse With MongoDB\n",
"\n",
"<a href=\"https://colab.research.google.com/github/run-llama/llama_parse/blob/main/examples/demo_mongodb.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>\n",
"\n",
"In this notebook, we provide a straightforward example of using LlamaParse with MongoDB Atlas VectorSearch.\n",
"\n",
"We illustrate the process of using llama-parse to parse a PDF document, then index the document with a MongoDB vector store, and subsequently perform basic queries against this store.\n",
"\n",
"This notebook is structured similarly to quick start guides, aiming to introduce users to utilizing llama-parse in conjunction with a MongoDB Atlas VectorSearch."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Installation"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%pip install llama-index llama-parse\n",
"%pip install llama-index-vector-stores-mongodb llama-index-llms-openai"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Setup API Keys"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"\n",
"os.environ[\n",
" \"LLAMA_CLOUD_API_KEY\"\n",
"] = \"\" # Get it from https://cloud.llamaindex.ai/api-key\n",
"os.environ[\"OPENAI_API_KEY\"] = \"\" # Get it from https://platform.openai.com/api-keys"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# llama-parse is async-first, running the sync code in a notebook requires the use of nest_asyncio\n",
"import nest_asyncio\n",
"\n",
"nest_asyncio.apply()\n",
"\n",
"import requests\n",
"import pymongo\n",
"\n",
"from llama_index.vector_stores.mongodb import MongoDBAtlasVectorSearch\n",
"from llama_parse import LlamaParse\n",
"from llama_index.embeddings.openai import OpenAIEmbedding\n",
"from llama_index.core import VectorStoreIndex, StorageContext\n",
"from llama_index.core.node_parser import SimpleNodeParser"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Download Document\n",
"\n",
"We will use `Attention is all you need` paper."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
"cells": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Download complete.\n"
]
}
],
"source": [
"# The URL of the file you want to download\n",
"url = \"https://arxiv.org/pdf/1706.03762.pdf\"\n",
"# The local path where you want to save the file\n",
"file_path = \"./attention.pdf\"\n",
"\n",
"# Perform the HTTP request\n",
"response = requests.get(url)\n",
"\n",
"# Check if the request was successful\n",
"if response.status_code == 200:\n",
" # Open the file in binary write mode and save the content\n",
" with open(file_path, \"wb\") as file:\n",
" file.write(response.content)\n",
" print(\"Download complete.\")\n",
"else:\n",
" print(\"Error downloading the file.\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Parse the document using `LlamaParse`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
"attachments": {},
"cell_type": "markdown",
"metadata": {
"id": "W6SX9VAnximx"
},
"source": [
"# LlamaParse With MongoDB\n",
"\n",
"<a href=\"https://colab.research.google.com/github/run-llama/llama_parse/blob/main/examples/demo_mongodb.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>\n",
"\n",
"In this notebook, we provide a straightforward example of using LlamaParse with MongoDB Atlas VectorSearch.\n",
"\n",
"We illustrate the process of using llama-parse to parse a PDF document, then index the document with a MongoDB vector store, and subsequently perform basic queries against this store.\n",
"\n",
"This notebook is structured similarly to quick start guides, aiming to introduce users to utilizing llama-parse in conjunction with a MongoDB Atlas VectorSearch."
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Started parsing the file under job_id 09a49745-9f21-4190-9de8-27e4e1a4bdf5\n"
]
}
],
"source": [
"documents = LlamaParse(result_type=\"text\").load_data(file_path)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
"cell_type": "markdown",
"metadata": {
"id": "rUJKhWDHxr_k"
},
"source": [
"### Installation"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"rmer - model architecture.\n",
"The Transformer follows this overall architecture using stacked self-attention and point-wise, fully\n",
"connected layers for both the encoder and decoder, shown in the left and right halves of Figure 1,\n",
"respectively.\n",
"3.1 Encoder and Decoder Stacks\n",
"Encoder: The encoder is composed of a stack of N = 6 identical layers. Each layer has two\n",
"sub-layers. The first is a multi-head self-attention mechanism, and the second is a simple, position-\n",
"wise fully connected feed-forward network. We employ a residual connection [11] around each of\n",
"the two sub-layers, followed by layer normalization [1]. That is, the output of each sub-layer is\n",
"LayerNorm(x + Sublayer(x)), where Sublayer(x) is the function implemented by the sub-layer\n",
"itself. To facilitate these residual connections, all sub-layers in the model, as well as the embedding\n",
"layers, produce outputs of dimension dmodel = 512.\n",
"Decoder: The decoder is also composed of a stack of N = 6 identical layers. In addition \n"
]
}
],
"source": [
"# Take a quick look at some of the parsed text from the document:\n",
"print(documents[0].get_content()[10000:11000])"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### Create `MongoDBAtlasVectorSearch`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"mongo_uri = os.environ[\"MONGO_URI\"]\n",
"\n",
"mongodb_client = pymongo.MongoClient(mongo_uri)\n",
"mongodb_vector_store = MongoDBAtlasVectorSearch(mongodb_client)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Create nodes."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"node_parser = SimpleNodeParser()\n",
"\n",
"nodes = node_parser.get_nodes_from_documents(documents)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Create Index and Query Engine."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"storage_context = StorageContext.from_defaults(vector_store=mongodb_vector_store)\n",
"\n",
"index = VectorStoreIndex(\n",
" nodes=nodes,\n",
" storage_context=storage_context,\n",
" embed_model=OpenAIEmbedding(),\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"query_engine = index.as_query_engine(similarity_top_k=2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Test Query"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "U6ZkIeBnxfRb"
},
"outputs": [],
"source": [
"!pip install llama-index llama-parse pip install llama-index-vector-stores-mongodb llama-index-llms-openai"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"***********New LlamaParse+ Basic Query Engine***********\n",
"The BLEU score on the WMT 2014 English-to-German translation task is 28.4.\n"
]
}
],
"source": [
"query = \"What is BLEU score on the WMT 2014 English-to-German translation task?\"\n",
"\n",
"response = query_engine.query(query)\n",
"print(\"\\n***********New LlamaParse+ Basic Query Engine***********\")\n",
"print(response)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
"cell_type": "markdown",
"metadata": {
"id": "wh1eeFJe1gkY"
},
"source": [
"### Setup API Keys"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"We varied the learning\n",
"rate over the course of training, according to the formula:\n",
" lrate = d0.5 (3)\n",
" model · min(step_num0.5, step_num · warmup_steps1.5)\n",
"This corresponds to increasing the learning rate linearly for the first warmup_steps training steps,\n",
"and decreasing it thereafter proportionally to the inverse square root of the step number. We used\n",
"warmup_steps = 4000.\n",
"5.4 Regularization\n",
"We employ three types of regularization during training:\n",
" 7\n",
"---\n",
"Table 2: The Transformer achieves better BLEU scores than previous state-of-the-art models on the\n",
"English-to-German and English-to-French newstest2014 tests at a fraction of the training cost.\n",
" Model BLEU Training Cost (FLOPs)\n",
" EN-DE EN-FR EN-DE EN-FR\n",
" ByteNet [18] 23.75\n",
" Deep-Att + PosUnk [39] 39.2 1.0 · 1020\n",
" GNMT + RL [38] 24.6 39.92 2.3 · 1019 1.4 · 1020\n",
" ConvS2S [9] 25.16 40.46 9.6 · 1018 1.5 · 1020\n",
" MoE [32] 26.03 40.56 2.0 · 1019 1.2 · 1020\n",
" Deep-Att + PosUnk Ensemble [39] 40.4 8.0 · 1020\n",
" GNMT + RL Ensemble [38] 26.30 41.16 1.8 · 1020 1.1 · 1021\n",
" ConvS2S Ensemble [9] 26.36 41.29 7.7 · 1019 1.2 · 1021\n",
" Transformer (base model) 27.3 38.1 3.3 · 1018\n",
" Transformer (big) 28.4 41.8 2.3 · 1019\n",
"Residual Dropout We apply dropout [33] to the output of each sub-layer, before it is added to the\n",
"sub-layer input and normalized. In addition, we apply dropout to the sums of the embeddings and the\n",
"positional encodings in both the encoder and decoder stacks. For the base model, we use a rate of\n",
"Pdrop = 0.1.\n",
"Label Smoothing During training, we employed label smoothing of value ϵls = 0.1 [36]. This\n",
"hurts perplexity, as the model learns to be more unsure, but improves accuracy and BLEU score.\n",
"6 Results\n",
"6.1 Machine Translation\n",
"On the WMT 2014 English-to-German translation task, the big transformer model (Transformer (big)\n",
"in Table 2) outperforms the best previously reported models (including ensembles) by more than 2.0\n",
"BLEU, establishing a new state-of-the-art BLEU score of 28.4. The configuration of this model is\n",
"listed in the bottom line of Table 3. Training took 3.5 days on 8 P100 GPUs. Even our base model\n",
"surpasses all previously published models and ensembles, at a fraction of the training cost of any of\n",
"the competitive models.\n",
"On the WMT 2014 English-to-French translation task, our big model achieves a BLEU score of 41.0,\n",
"outperforming all of the previously published single models, at less than 1/4 the training cost of the\n",
"previous state-of-the-art model. The Transformer (big) model trained for English-to-French used\n",
"dropout rate Pdrop = 0.1, instead of 0.3.\n",
"For the base models, we used a single model obtained by averaging the last 5 checkpoints, which\n",
"were written at 10-minute intervals. For the big models, we averaged the last 20 checkpoints. We\n",
"used beam search with a beam size of 4 and length penalty α = 0.6 [38]. These hyperparameters\n",
"were chosen after experimentation on the development set. We set the maximum output length during\n",
"inference to input length + 50, but terminate early when possible [38].\n",
"Table 2 summarizes our results and compares our translation quality and training costs to other model\n",
"architectures from the literature.\n"
]
"cell_type": "code",
"execution_count": 1,
"metadata": {
"id": "I5slpdnyxwIB"
},
"outputs": [],
"source": [
"import os\n",
"os.environ[\"LLAMA_CLOUD_API_KEY\"] = '' # Get it from https://cloud.llamaindex.ai/api-key\n",
"os.environ['OPENAI_API_KEY'] = '' # Get it from https://platform.openai.com/api-keys"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"id": "es2mz_OVyQw9"
},
"outputs": [],
"source": [
"# llama-parse is async-first, running the sync code in a notebook requires the use of nest_asyncio\n",
"import nest_asyncio\n",
"\n",
"nest_asyncio.apply()\n",
"\n",
"import requests\n",
"import pymongo\n",
"\n",
"from llama_index.vector_stores.mongodb import MongoDBAtlasVectorSearch\n",
"from llama_parse import LlamaParse\n",
"from llama_index.embeddings.openai import OpenAIEmbedding\n",
"from llama_index.core import VectorStoreIndex, StorageContext\n",
"from llama_index.core.node_parser import SimpleNodeParser"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Ou3bVdHQ10X5"
},
"source": [
"### Download Document\n",
"\n",
"We will use `Attention is all you need` paper."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "YO9lAk6bybV3",
"outputId": "5cee588a-bec5-482e-e8ef-fbb78e8a5967"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Download complete.\n"
]
}
],
"source": [
"# The URL of the file you want to download\n",
"url = \"https://arxiv.org/pdf/1706.03762.pdf\"\n",
"# The local path where you want to save the file\n",
"file_path = \"./attention.pdf\"\n",
"\n",
"# Perform the HTTP request\n",
"response = requests.get(url)\n",
"\n",
"# Check if the request was successful\n",
"if response.status_code == 200:\n",
" # Open the file in binary write mode and save the content\n",
" with open(file_path, \"wb\") as file:\n",
" file.write(response.content)\n",
" print(\"Download complete.\")\n",
"else:\n",
" print(\"Error downloading the file.\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "1NtR7PGo13Hh"
},
"source": [
"### Parse the document using `LlamaParse`."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "reeJsblfyeSd",
"outputId": "bb569e9f-fe31-47b9-a059-d7da369b3f94"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Started parsing the file under job_id 09a49745-9f21-4190-9de8-27e4e1a4bdf5\n"
]
}
],
"source": [
"documents = LlamaParse(result_type=\"text\").load_data(file_path)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "-NIXtCBwyiPp",
"outputId": "ad4b3cec-2c23-4858-81f0-994ae2c96b8f"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"rmer - model architecture.\n",
"The Transformer follows this overall architecture using stacked self-attention and point-wise, fully\n",
"connected layers for both the encoder and decoder, shown in the left and right halves of Figure 1,\n",
"respectively.\n",
"3.1 Encoder and Decoder Stacks\n",
"Encoder: The encoder is composed of a stack of N = 6 identical layers. Each layer has two\n",
"sub-layers. The first is a multi-head self-attention mechanism, and the second is a simple, position-\n",
"wise fully connected feed-forward network. We employ a residual connection [11] around each of\n",
"the two sub-layers, followed by layer normalization [1]. That is, the output of each sub-layer is\n",
"LayerNorm(x + Sublayer(x)), where Sublayer(x) is the function implemented by the sub-layer\n",
"itself. To facilitate these residual connections, all sub-layers in the model, as well as the embedding\n",
"layers, produce outputs of dimension dmodel = 512.\n",
"Decoder: The decoder is also composed of a stack of N = 6 identical layers. In addition \n"
]
}
],
"source": [
"# Take a quick look at some of the parsed text from the document:\n",
"print(documents[0].get_content()[10000:11000])"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"id": "wP9I5dhB1-w1"
},
"source": [
"### Create `MongoDBAtlasVectorSearch`."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"id": "-4Ek0oK-yp3L"
},
"outputs": [],
"source": [
"mongo_uri = os.environ[\"MONGO_URI\"]\n",
"\n",
"mongodb_client = pymongo.MongoClient(mongo_uri)\n",
"mongodb_vector_store = MongoDBAtlasVectorSearch(mongodb_client)\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "GYiVwFok2DNf"
},
"source": [
"### Create nodes."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"id": "aqdF6ZonytHF"
},
"outputs": [],
"source": [
"node_parser = SimpleNodeParser()\n",
"\n",
"nodes = node_parser.get_nodes_from_documents(documents)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "U5fMoGrA2GSH"
},
"source": [
"### Create Index and Query Engine."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"id": "gQUieIrAywSC"
},
"outputs": [],
"source": [
"storage_context = StorageContext.from_defaults(vector_store=mongodb_vector_store)\n",
"\n",
"index = VectorStoreIndex(\n",
" nodes=nodes,\n",
" storage_context=storage_context,\n",
" embed_model=OpenAIEmbedding(),\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"id": "snkZZss-zKDb"
},
"outputs": [],
"source": [
"query_engine = index.as_query_engine(similarity_top_k=2)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "rTKT34XO2LYk"
},
"source": [
"### Test Query"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "r66ciuPkzNv1",
"outputId": "919218e3-0884-4992-802c-ab1c4622ec4b"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"***********New LlamaParse+ Basic Query Engine***********\n",
"The BLEU score on the WMT 2014 English-to-German translation task is 28.4.\n"
]
}
],
"source": [
"query = \"What is BLEU score on the WMT 2014 English-to-German translation task?\"\n",
"\n",
"response = query_engine.query(query)\n",
"print(\"\\n***********New LlamaParse+ Basic Query Engine***********\")\n",
"print(response)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "K7RsivpwzQBo",
"outputId": "9bcbf62e-250c-46db-f247-e1f293c09bbe"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"We varied the learning\n",
"rate over the course of training, according to the formula:\n",
" lrate = d0.5 (3)\n",
" model · min(step_num0.5, step_num · warmup_steps1.5)\n",
"This corresponds to increasing the learning rate linearly for the first warmup_steps training steps,\n",
"and decreasing it thereafter proportionally to the inverse square root of the step number. We used\n",
"warmup_steps = 4000.\n",
"5.4 Regularization\n",
"We employ three types of regularization during training:\n",
" 7\n",
"---\n",
"Table 2: The Transformer achieves better BLEU scores than previous state-of-the-art models on the\n",
"English-to-German and English-to-French newstest2014 tests at a fraction of the training cost.\n",
" Model BLEU Training Cost (FLOPs)\n",
" EN-DE EN-FR EN-DE EN-FR\n",
" ByteNet [18] 23.75\n",
" Deep-Att + PosUnk [39] 39.2 1.0 · 1020\n",
" GNMT + RL [38] 24.6 39.92 2.3 · 1019 1.4 · 1020\n",
" ConvS2S [9] 25.16 40.46 9.6 · 1018 1.5 · 1020\n",
" MoE [32] 26.03 40.56 2.0 · 1019 1.2 · 1020\n",
" Deep-Att + PosUnk Ensemble [39] 40.4 8.0 · 1020\n",
" GNMT + RL Ensemble [38] 26.30 41.16 1.8 · 1020 1.1 · 1021\n",
" ConvS2S Ensemble [9] 26.36 41.29 7.7 · 1019 1.2 · 1021\n",
" Transformer (base model) 27.3 38.1 3.3 · 1018\n",
" Transformer (big) 28.4 41.8 2.3 · 1019\n",
"Residual Dropout We apply dropout [33] to the output of each sub-layer, before it is added to the\n",
"sub-layer input and normalized. In addition, we apply dropout to the sums of the embeddings and the\n",
"positional encodings in both the encoder and decoder stacks. For the base model, we use a rate of\n",
"Pdrop = 0.1.\n",
"Label Smoothing During training, we employed label smoothing of value ϵls = 0.1 [36]. This\n",
"hurts perplexity, as the model learns to be more unsure, but improves accuracy and BLEU score.\n",
"6 Results\n",
"6.1 Machine Translation\n",
"On the WMT 2014 English-to-German translation task, the big transformer model (Transformer (big)\n",
"in Table 2) outperforms the best previously reported models (including ensembles) by more than 2.0\n",
"BLEU, establishing a new state-of-the-art BLEU score of 28.4. The configuration of this model is\n",
"listed in the bottom line of Table 3. Training took 3.5 days on 8 P100 GPUs. Even our base model\n",
"surpasses all previously published models and ensembles, at a fraction of the training cost of any of\n",
"the competitive models.\n",
"On the WMT 2014 English-to-French translation task, our big model achieves a BLEU score of 41.0,\n",
"outperforming all of the previously published single models, at less than 1/4 the training cost of the\n",
"previous state-of-the-art model. The Transformer (big) model trained for English-to-French used\n",
"dropout rate Pdrop = 0.1, instead of 0.3.\n",
"For the base models, we used a single model obtained by averaging the last 5 checkpoints, which\n",
"were written at 10-minute intervals. For the big models, we averaged the last 20 checkpoints. We\n",
"used beam search with a beam size of 4 and length penalty α = 0.6 [38]. These hyperparameters\n",
"were chosen after experimentation on the development set. We set the maximum output length during\n",
"inference to input length + 50, but terminate early when possible [38].\n",
"Table 2 summarizes our results and compares our translation quality and training costs to other model\n",
"architectures from the literature.\n"
]
}
],
"source": [
"# Take a look at one of the source nodes from the response\n",
"print(response.source_nodes[0].get_content())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"display_name": "anthropic_env",
"language": "python",
"name": "anthropic_env"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.3"
},
"vscode": {
"interpreter": {
"hash": "b0fa6594d8f4cbf19f97940f81e996739fb7646882a419484c72d19e05852a7e"
}
}
],
"source": [
"# Take a look at one of the source nodes from the response\n",
"print(response.source_nodes[0].get_content())"
]
}
],
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"display_name": "anthropic_env",
"language": "python",
"name": "anthropic_env"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3"
},
"vscode": {
"interpreter": {
"hash": "b0fa6594d8f4cbf19f97940f81e996739fb7646882a419484c72d19e05852a7e"
}
}
},
"nbformat": 4,
"nbformat_minor": 0
"nbformat": 4,
"nbformat_minor": 0
}
File diff suppressed because it is too large Load Diff
+68 -44
View File
@@ -28,13 +28,13 @@
"metadata": {},
"outputs": [],
"source": [
"%pip install llama-index\n",
"%pip install llama-index-core\n",
"%pip install llama-index-embeddings-openai\n",
"%pip install llama-index-question-gen-openai\n",
"%pip install llama-index-postprocessor-flag-embedding-reranker\n",
"%pip install git+https://github.com/FlagOpen/FlagEmbedding.git\n",
"%pip install llama-parse"
"!pip install llama-index\n",
"!pip install llama-index-core\n",
"!pip install llama-index-embeddings-openai\n",
"!pip install llama-index-question-gen-openai\n",
"!pip install llama-index-postprocessor-flag-embedding-reranker\n",
"!pip install git+https://github.com/FlagOpen/FlagEmbedding.git\n",
"!pip install llama-parse"
]
},
{
@@ -56,17 +56,15 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# llama-parse is async-first, running the async code in a notebook requires the use of nest_asyncio\n",
"import nest_asyncio\n",
"\n",
"nest_asyncio.apply()\n",
"\n",
"import os\n",
"\n",
"# API access to llama-cloud\n",
"os.environ[\"LLAMA_CLOUD_API_KEY\"] = \"llx-\"\n",
"\n",
@@ -76,7 +74,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
@@ -85,11 +83,11 @@
"from llama_index.core import VectorStoreIndex\n",
"from llama_index.core import Settings\n",
"\n",
"embed_model = OpenAIEmbedding(model=\"text-embedding-3-small\")\n",
"embed_model=OpenAIEmbedding(model=\"text-embedding-3-small\")\n",
"llm = OpenAI(model=\"gpt-3.5-turbo-0125\")\n",
"\n",
"Settings.llm = llm\n",
"Settings.embed_model = embed_model"
"Settings.embed_model = embed_model\n"
]
},
{
@@ -111,8 +109,8 @@
"source": [
"from llama_parse import LlamaParse\n",
"\n",
"docs_2021 = LlamaParse(result_type=\"markdown\").load_data(\"./apple_2021_10k.pdf\")\n",
"docs_2020 = LlamaParse(result_type=\"markdown\").load_data(\"./apple_2020_10k.pdf\")"
"docs_2021 = LlamaParse(result_type=\"markdown\").load_data('./apple_2021_10k.pdf')\n",
"docs_2020 = LlamaParse(result_type=\"markdown\").load_data('./apple_2020_10k.pdf')"
]
},
{
@@ -129,34 +127,31 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 36,
"metadata": {},
"outputs": [],
"source": [
"from llama_index.core.node_parser import MarkdownElementNodeParser\n",
"\n",
"node_parser = MarkdownElementNodeParser(\n",
" llm=OpenAI(model=\"gpt-3.5-turbo-0125\"), num_workers=8\n",
")"
"node_parser = MarkdownElementNodeParser(llm=OpenAI(model=\"gpt-3.5-turbo-0125\"), num_workers=8)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"execution_count": 46,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import pickle\n",
"from llama_index.postprocessor.flag_embedding_reranker import (\n",
" FlagEmbeddingReranker,\n",
")\n",
"from llama_index.postprocessor.flag_embedding_reranker import FlagEmbeddingReranker\n",
"\n",
"reranker = FlagEmbeddingReranker(\n",
" top_n=5,\n",
" model=\"BAAI/bge-reranker-large\",\n",
")\n",
"\n",
"\n",
"def create_query_engine_over_doc(docs, nodes_save_path=None):\n",
" \"\"\"Big function to go from document path -> recursive retriever.\"\"\"\n",
" if nodes_save_path is not None and os.path.exists(nodes_save_path):\n",
@@ -166,13 +161,16 @@
" if nodes_save_path is not None:\n",
" pickle.dump(raw_nodes, open(nodes_save_path, \"wb\"))\n",
"\n",
" base_nodes, objects = node_parser.get_nodes_and_objects(raw_nodes)\n",
" base_nodes, objects = node_parser.get_nodes_and_objects(\n",
" raw_nodes\n",
" )\n",
"\n",
" ### Construct Retrievers\n",
" # construct top-level vector index + query engine\n",
" vector_index = VectorStoreIndex(nodes=base_nodes + objects)\n",
" vector_index = VectorStoreIndex(nodes=base_nodes+objects)\n",
" query_engine = vector_index.as_query_engine(\n",
" similarity_top_k=15, node_postprocessors=[reranker]\n",
" similarity_top_k=15,\n",
" node_postprocessors=[reranker]\n",
" )\n",
" return query_engine, base_nodes"
]
@@ -180,7 +178,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"query_engine_2021, nodes_2021 = create_query_engine_over_doc(\n",
@@ -193,8 +193,10 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"execution_count": 38,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from llama_index.core.tools import QueryEngineTool, ToolMetadata\n",
@@ -207,14 +209,18 @@
" query_engine=query_engine_2021,\n",
" metadata=ToolMetadata(\n",
" name=\"apple_2021_10k\",\n",
" description=(\"Provides information about Apple financials for year 2021\"),\n",
" description=(\n",
" \"Provides information about Apple financials for year 2021\"\n",
" ),\n",
" ),\n",
" ),\n",
" QueryEngineTool(\n",
" query_engine=query_engine_2020,\n",
" metadata=ToolMetadata(\n",
" name=\"apple_2020_10k\",\n",
" description=(\"Provides information about Apple financials for year 2020\"),\n",
" description=(\n",
" \"Provides information about Apple financials for year 2020\"\n",
" ),\n",
" ),\n",
" ),\n",
"]\n",
@@ -235,8 +241,10 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"execution_count": 41,
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
@@ -263,8 +271,10 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"execution_count": 40,
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
@@ -280,8 +290,10 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"execution_count": 42,
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
@@ -304,8 +316,10 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"execution_count": 44,
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
@@ -328,8 +342,10 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"execution_count": 45,
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
@@ -342,6 +358,13 @@
"source": [
"print(str(response))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
@@ -359,7 +382,8 @@
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3"
"pygments_lexer": "ipython3",
"version": "3.10.8"
}
},
"nbformat": 4,
+35 -20
View File
@@ -18,11 +18,12 @@
"cell_type": "code",
"execution_count": null,
"id": "14cdcfaf-88b4-4489-9910-e362e0ccec53",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import nest_asyncio\n",
"\n",
"nest_asyncio.apply()\n",
"\n",
"from llama_parse import LlamaParse"
@@ -30,13 +31,12 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"id": "6f5b5841-dd3e-4169-9bd4-6a672b5b34ee",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"\n",
"os.environ[\"LLAMA_CLOUD_API_KEY\"] = \"llx-\""
]
},
@@ -68,7 +68,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"id": "0dd0f860-8e92-43a7-9443-ad1a4fb9365c",
"metadata": {},
"outputs": [],
@@ -78,7 +78,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"id": "fd932bef-ba82-4449-b7a0-5c2a9b55089f",
"metadata": {},
"outputs": [
@@ -104,7 +104,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"id": "2a73e553-2194-4ac9-9764-0edab0d6fdce",
"metadata": {},
"outputs": [
@@ -308,9 +308,11 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"id": "c779547f-e4f7-4c84-9786-2b6b749827ab",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from llama_index.core import VectorStoreIndex"
@@ -318,7 +320,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"id": "68b3a95e-ce19-4df1-9fdd-e6efb2fc423a",
"metadata": {},
"outputs": [],
@@ -328,9 +330,11 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 10,
"id": "a2ae28f6-4b3a-4130-8e65-0921b7678739",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"query_engine = index.as_query_engine()"
@@ -338,21 +342,23 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 11,
"id": "232091ee-aa22-4f51-838c-410024acc344",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"response = query_engine.query(\n",
" \"What are some response quality challenges with naive RAG?\"\n",
")"
"response = query_engine.query(\"What are some response quality challenges with naive RAG?\") "
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 12,
"id": "75f32aa7-c308-4221-af60-779822cfdba1",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
@@ -365,6 +371,14 @@
"source": [
"print(str(response))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d309d8fb-750a-4393-a1b2-67b14b7c121f",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
@@ -382,7 +396,8 @@
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3"
"pygments_lexer": "ipython3",
"version": "3.10.8"
}
},
"nbformat": 4,
+135 -48
View File
@@ -2,7 +2,9 @@
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"metadata": {
"id": "eld1dKaN7P8B"
},
"source": [
"# LlamaParse - Parsing Financial Powerpoints 📊\n",
"\n",
@@ -11,7 +13,9 @@
},
{
"cell_type": "markdown",
"metadata": {},
"metadata": {
"id": "goB1sV8zu_Xl"
},
"source": [
"## Installation\n",
"\n",
@@ -23,17 +27,25 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "7Y3_BwQLu-qK",
"outputId": "b1129c52-7a70-44cc-ad03-1f8d3a8c794a"
},
"outputs": [],
"source": [
"%pip install llama-index\n",
"%pip install llama-parse\n",
"%pip install torch transformers python-pptx Pillow"
"!pip install llama-index\n",
"!pip install llama-parse\n",
"!pip install torch transformers python-pptx Pillow"
]
},
{
"cell_type": "markdown",
"metadata": {},
"metadata": {
"id": "i-Rg2D_Rvf2i"
},
"source": [
"## API Key\n",
"\n",
@@ -43,26 +55,32 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"id": "af6i2P1vuU-U"
},
"outputs": [],
"source": [
"import os\n",
"\n",
"os.environ[\"LLAMA_CLOUD_API_KEY\"] = \"llx-...\"\n",
"os.environ[\"OPENAI_API_KEY\"] = \"sk-...\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"metadata": {
"id": "p8Eq-aX-wAEo"
},
"source": [
"**NOTE**: Since LlamaParse is natively async, running the sync code in a notebook requires the use of nest_asyncio.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"execution_count": 1,
"metadata": {
"id": "4OB0BkTqv_0l",
"tags": []
},
"outputs": [],
"source": [
"import nest_asyncio\n",
@@ -72,7 +90,9 @@
},
{
"cell_type": "markdown",
"metadata": {},
"metadata": {
"id": "dz927ecMyYo_"
},
"source": [
"## Importing the package\n",
"\n",
@@ -82,7 +102,10 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"id": "nSW-6sEwyXwx",
"tags": []
},
"outputs": [],
"source": [
"from llama_parse import LlamaParse"
@@ -90,7 +113,9 @@
},
{
"cell_type": "markdown",
"metadata": {},
"metadata": {
"id": "l_D4YsAHwUSk"
},
"source": [
"## Using LlamaParse to Parse Presentations\n",
"\n",
@@ -102,7 +127,14 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "d3qeuiyawT0U",
"outputId": "cec0ea0a-be8b-49b6-9376-797c91f63be7",
"tags": []
},
"outputs": [],
"source": [
"! mkdir data; wget \"https://meetings.wmo.int/Cg-19/PublishingImages/SitePages/FINAC-43/7%20-%20EC-77-Doc%205%20Financial%20Statements%20for%202022%20(FINAC).pptx\" -O data/presentation.pptx"
@@ -110,7 +142,9 @@
},
{
"cell_type": "markdown",
"metadata": {},
"metadata": {
"id": "Gbr8RiHEyF3-"
},
"source": [
"### Parsing the presentation\n",
"\n",
@@ -121,15 +155,24 @@
},
{
"cell_type": "markdown",
"metadata": {},
"metadata": {
"id": "osocsofoJ42S"
},
"source": [
"#### Llama Index default"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"execution_count": 5,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "PTVy5XCNJwW-",
"outputId": "d0e2cc4b-1407-45a9-b5e6-d06f91a533b4",
"tags": []
},
"outputs": [],
"source": [
"from llama_index.core import SimpleDirectoryReader\n",
@@ -139,15 +182,24 @@
},
{
"cell_type": "markdown",
"metadata": {},
"metadata": {
"id": "oucbsciZJwxt"
},
"source": [
"#### Llama Parse"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"execution_count": 6,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "3jKnXCuAyQ9_",
"outputId": "1f668f17-1e20-46e5-fbab-9a55e4b28891",
"tags": []
},
"outputs": [
{
"name": "stdout",
@@ -158,9 +210,7 @@
}
],
"source": [
"llama_parse_documents = LlamaParse(result_type=\"markdown\").load_data(\n",
" \"./data/presentation.pptx\"\n",
")"
"llama_parse_documents = LlamaParse(result_type=\"markdown\").load_data(\"./data/presentation.pptx\")"
]
},
{
@@ -174,8 +224,10 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"execution_count": 27,
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
@@ -207,7 +259,9 @@
},
{
"cell_type": "markdown",
"metadata": {},
"metadata": {
"tags": []
},
"source": [
"Compared against the original slide image.\n",
"![Demo](demo_ppt_financial_1.png)"
@@ -215,7 +269,9 @@
},
{
"cell_type": "markdown",
"metadata": {},
"metadata": {
"id": "p4GVOdWzzvYg"
},
"source": [
"## Comparing the two for RAG\n",
"\n",
@@ -224,15 +280,20 @@
},
{
"cell_type": "markdown",
"metadata": {},
"metadata": {
"id": "oVcdGus5NDxi"
},
"source": [
"### Query Engine on SimpleDirectoryReader results"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"execution_count": 19,
"metadata": {
"id": "DqXYsLCWNg9_",
"tags": []
},
"outputs": [],
"source": [
"from llama_index.core import VectorStoreIndex, SimpleDirectoryReader\n",
@@ -243,15 +304,20 @@
},
{
"cell_type": "markdown",
"metadata": {},
"metadata": {
"id": "ZLkHt9l2Nbxx"
},
"source": [
"### Query Engine on LlamaParse Results\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"execution_count": 20,
"metadata": {
"id": "ZllaDcfRNLv3",
"tags": []
},
"outputs": [],
"source": [
"llama_parse_index = VectorStoreIndex.from_documents(llama_parse_documents)\n",
@@ -260,7 +326,10 @@
},
{
"cell_type": "markdown",
"metadata": {},
"metadata": {
"id": "0dY_0_1bNg0X",
"tags": []
},
"source": [
"### Liability provision\n",
"What was the liability provision as of Dec 31 2021?\n",
@@ -270,8 +339,15 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"execution_count": 21,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Tmn-qNTEN-cb",
"outputId": "a9bffc00-9cfc-43d8-b159-596a6c1aca64",
"tags": []
},
"outputs": [
{
"name": "stdout",
@@ -282,16 +358,21 @@
}
],
"source": [
"vanilla_response = vanilla_query_engine.query(\n",
" \"What was the liability provision as of Dec 31 2021?\"\n",
")\n",
"vanilla_response = vanilla_query_engine.query(\"What was the liability provision as of Dec 31 2021?\")\n",
"print(vanilla_response)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"execution_count": 22,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "4EZ_uqlROP7R",
"outputId": "0645a159-06c6-411e-d1f6-79ea95d32b42",
"tags": []
},
"outputs": [
{
"name": "stdout",
@@ -302,11 +383,16 @@
}
],
"source": [
"llama_parse_response = llama_parse_query_engine.query(\n",
" \"What was the liability provision as of Dec 31 2021?\"\n",
")\n",
"llama_parse_response = llama_parse_query_engine.query(\"What was the liability provision as of Dec 31 2021?\")\n",
"print(llama_parse_response)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
@@ -327,7 +413,8 @@
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3"
"pygments_lexer": "ipython3",
"version": "3.10.8"
}
},
"nbformat": 4,
@@ -1,327 +0,0 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "f20600ce-d57a-446e-b033-3aadeec39c1b",
"metadata": {},
"source": [
"# LlamaParse with GPT-4o\n",
"\n",
"\n",
"<a href=\"https://colab.research.google.com/github/run-llama/llama_parse/blob/main/examples/demo_gpt4o/gpt4o_tesla_impact_report.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>\n",
"\n",
"GPT-4o is a [fully multimodal model by OpenAI](https://openai.com/index/hello-gpt-4o/) released in May 2024. It matches GPT-4 Turbo performance in text and code, and has significantly improved vision and audio capabilities.\n",
"\n",
"The expanded vision/audio capabilities mean that it can be used for document parsing, by treating each page as an image and performing document extraction. We support using GPT-4o natively in LlamaParse for document parsing. The notebook below walks you through an example of using GPT-4o over the Tesla impact report.\n",
"\n",
"**NOTE**: The pricing for LlamaParse + gpt4o is an order more expensive than using LlamaParse by default. Currently, every page parsed with gpt4o counts for 200 pages in the LlamaParse usage tracker.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "86b173ac-9fce-4813-bdf1-6dd7d93a491d",
"metadata": {},
"outputs": [],
"source": [
"import nest_asyncio\n",
"\n",
"nest_asyncio.apply()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ecc5eba5-96ce-4db7-bba1-f9ece33e681c",
"metadata": {},
"outputs": [],
"source": [
"import os"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b805592b-d1a5-4cd2-b916-348f66ca7941",
"metadata": {},
"outputs": [],
"source": [
"os.environ[\"LLAMA_CLOUD_API_KEY\"] = \"<LLAMA_CLOUD_API_KEY>"
]
},
{
"cell_type": "markdown",
"id": "6e73e3c4-9e09-4cba-805f-326c82be812d",
"metadata": {},
"source": [
"### Use LlamaParse with `gpt4o_mode=True`\n",
"\n",
"By turning on gpt4o, we use GPT-4o multimodal capabilities to do document parsing per page instead of the LlamaParse default pipeline.\n",
"\n",
"We load a snippet of the [2019 Tesla impact report](https://www.tesla.com/ns_videos/2019-tesla-impact-report.pdf). To help you save tokens, we only load 4 pages of this report (which will count as 800 pages of LlamaParse pages). \n",
"\n",
"You can optionally choose to provide a `gpt4o_api_key`. If you do this, then we will use your API key to make GPT-4o calls, and your LlamaParse usage will be counted as if `gpt4o_mode` was not turned on (each page will be counted as a page instead of 200 pages). "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f46991c1-031b-461f-b9a6-9237a821f4c8",
"metadata": {},
"outputs": [],
"source": [
"from llama_parse import LlamaParse\n",
"\n",
"parser_gpt4o = LlamaParse(\n",
" result_type=\"markdown\",\n",
" # base_url=\"https://api.staging.llamaindex.ai\",\n",
" api_key=api_key,\n",
" gpt4o_mode=True,\n",
" # gpt4o_api_key=\"<gpt4o_api_key>\"\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1136ba82-074b-489d-9b0a-d609ccbf02b6",
"metadata": {},
"outputs": [],
"source": [
"documents_gpt4o = parser_gpt4o.load_data(\"./2019-tesla-impact-report-short.pdf\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9e65c54f-3e4c-4c78-b1e8-a55ebeba1f24",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"# Impact Report\n",
"## 2019\n",
"\n",
"![Tesla Logo](https://www.tesla.com/sites/default/files/blog_images/tesla_announcement_social.jpg)\n",
"\n",
"![Earth and Car in Space](https://www.tesla.com/sites/default/files/blog_images/tesla_announcement_social.jpg)\n",
"---\n",
"# Introduction 03\n",
"\n",
"# Mission and Tesla Ecosystem 04\n",
"\n",
"# Environmental Impact 06\n",
"- Lifecycle Analysis of Tesla Vehicles versus Average ICE\n",
"- Battery Recycling\n",
"- NOx, Particulates and Other Pollutants\n",
"- Water Used per Vehicle Manufactured\n",
"- Emissions Credits\n",
"- Net Energy Impact of Our Products\n",
"\n",
"# Product Impact 20\n",
"- Price Equivalency\n",
"- Primary Driver\n",
"- Long Distance Travel\n",
"- Active Safety\n",
"- Passive Safety\n",
"- Tesla Safety Awards\n",
"- Fire Safety\n",
"- Cyber Security\n",
"- Disaster Relief\n",
"- Resilience of the Grid\n",
"- Megapack\n",
"- Solar Roof\n",
"\n",
"# Supply Chain 33\n",
"- Responsible Material Sourcing\n",
"- Cobalt Sourcing\n",
"\n",
"# People and Culture 37\n",
"- Our Environmental, Health, and Safety Strategy\n",
"- Safety Improvements\n",
"- Case Study: Ergonomics and Model Y Design\n",
"- Rewarding the Individual\n",
"- Culture of Diversity and Inclusion\n",
"- Workforce Development\n",
"- Community Engagement\n",
"- Employee Mobility and Transportation Programs\n",
"- Corporate Governance\n",
"\n",
"# Appendix 52\n",
"---\n",
"# Introduction\n",
"\n",
"The very purpose of Teslas existence is to accelerate the worlds transition to sustainable energy. In furtherance of this mission, we are excited to publish our second annual Impact Report. Transparency and disclosure are important for our customers, employees, and shareholders, which is why we have expanded the Impact Reports content this year.\n",
"\n",
"While many environmental reports focus on emissions generated by the manufacturing phase of products and future goals for energy consumption, we highlight the totality of the environmental impact of our products today. After all, the vast majority of emissions generated by vehicles today occur in the product-use phase—that is, when consumers are driving their vehicles. We believe that providing information on both sides of the manufacturing and consumer-use equation provides a clearer picture of the environmental impact of Tesla products, and we have done so this year largely through a lifecycle analysis detailed in this report.\n",
"\n",
"Tesla aims to continue to increase the proportion of renewable energy usage at our factories in an effort to minimize the carbon footprint for every mile traveled by our products and their components in our supply chain. All of the factories that we built from the ground-up, such as Gigafactory Nevada and Gigafactory Shanghai, and our forthcoming Gigafactories in Berlin and North America, are designed from the beginning to use energy from renewable sources.\n",
"\n",
"Making a significant and lasting impact on environmental sustainability is difficult to achieve without securing financial sustainability for the long term. We generated positive Free Cash Flow (operating cash flow less capex) of more than $1 billion for the first time in 2019. We believe the notion that a sustainable future is not economically feasible is no longer valid.\n",
"---\n",
"# Mission & Tesla Ecosystem\n",
"\n",
"Climate change is reaching alarming levels in large part due to emissions from burning fossil fuels for transportation and electricity generation. In 2016, carbon dioxide (CO2) concentration levels in the atmosphere exceeded the 400 parts per million threshold on a sustained basis - a level that climate scientists believe will have a catastrophic impact on the environment. Worse, annual global CO2 emissions continue to increase and have approximately doubled over the past 50 years to over 43 gigatons in 2019. The worlds current path is unwise and unsustainable.\n",
"\n",
"The world cannot reduce CO2 emissions without addressing both energy generation and consumption. And the world cannot address its energy habits without first directly reducing emissions in the transportation and energy sectors. We are focused on creating a complete energy and transportation ecosystem from solar generation and energy storage to all-electric vehicles that produce zero tailpipe emissions.\n",
"\n",
"Since the onset of shelter-in-place orders and travel restrictions due to COVID-19, we have seen dramatic increases in air quality across the planet, as well as projections for CO2 emissions to drop in excess of 4% in 2020 compared to pre-COVID-19 levels, according to researchers. Because these improvements in air quality and reductions in CO2 are a result of a global economic disruption and not due to systemic changes in how we produce and consume energy, they are not expected to be sustained absent intervention. However, these changes have shown us the positive impacts of reduced pollution in a very short period of time. At Tesla, we believe that we all have an unprecedented opportunity to learn from this disruption and accelerate the deployment of clean energy solutions as part of a recovery for all economies throughout the world, and we will actively continue to advocate for the realization of these long-term changes.\n",
"\n",
"## Global Greenhouse Gas (GHG) Emissions by Economic Sector\n",
"\n",
"| Sector | Percentage |\n",
"|---------------------------------------------|------------|\n",
"| Electricity & Heat Production* | 31% |\n",
"| Agriculture, Forestry & Other Land Use | 20% |\n",
"| Industry | 18% |\n",
"| Transportation* | 16% |\n",
"| Other Energy | 9% |\n",
"| Buildings | 6% |\n",
"\n",
"*Tesla-related sectors. Source: World Resources Institute\n",
"\n",
"According to the Global Carbon project, when fully tallied, total carbon emissions from 2019 are expected to hit another record high of over 43 gigatons for the year. Energy use through electricity and heat production (31%) and transportation (16%) are significant drivers of these GHG emissions.\n"
]
}
],
"source": [
"print(documents_gpt4o[0].get_content())"
]
},
{
"cell_type": "markdown",
"id": "d62cbb62-37ea-4370-9411-d979aa3a627e",
"metadata": {},
"source": [
"## Build RAG pipeline over the Parsed Report\n",
"\n",
"We now try building a RAG pipeline over this parsed report. It's not a lot of text, but we split it into chunks and load it into a simple in-memory vector store.\n",
"\n",
"We ask a question over the parsed markdown table and get back the right answer! We also ask a question over the text."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f624e243-1878-4d87-841d-69e57360a7d9",
"metadata": {},
"outputs": [],
"source": [
"from llama_index.core import VectorStoreIndex\n",
"from llama_index.core.node_parser import SentenceSplitter\n",
"\n",
"\n",
"splitter = SentenceSplitter()\n",
"nodes = splitter.get_nodes_from_documents(documents_gpt4o)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d8b7c3ad-2147-448c-bcbe-3e6fcd8d5361",
"metadata": {},
"outputs": [],
"source": [
"vector_index = VectorStoreIndex(nodes)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8013351a-180d-4947-9f81-513042175c19",
"metadata": {},
"outputs": [],
"source": [
"query_engine = vector_index.as_query_engine()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "795dc5c4-e122-4ff3-94d2-747fa51d5add",
"metadata": {},
"outputs": [],
"source": [
"response = query_engine.query(\n",
" \"What are the greenhouse emissions for agriculture and transportation?\"\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "39d2e6bd-3316-49b5-9a5d-5b4b95343e5a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Agriculture accounts for 20% of global greenhouse gas emissions, while transportation contributes 16% to these emissions.\n"
]
}
],
"source": [
"print(str(response))"
]
},
{
"cell_type": "markdown",
"id": "9beb5cd4-4041-48c7-b22b-de5540f92a6d",
"metadata": {},
"source": [
"Let's also try asking a question over another piece of the text."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "543c8b63-5cd1-47a1-a8a1-81abbfd3e52b",
"metadata": {},
"outputs": [],
"source": [
"response = query_engine.query(\"What does Tesla aim to do?\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e739eabf-732b-4f59-9628-972c4bf6c857",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Tesla aims to accelerate the world's transition to sustainable energy by creating a complete energy and transportation ecosystem that includes solar generation, energy storage, and all-electric vehicles with zero tailpipe emissions.\n"
]
}
],
"source": [
"print(str(response))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "llama_parse",
"language": "python",
"name": "llama_parse"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
+1 -1
View File
@@ -1,3 +1,3 @@
from llama_parse.base import LlamaParse, ResultType
__all__ = ["LlamaParse", "ResultType"]
__all__ = ["LlamaParse", "ResultType"]
+205 -167
View File
@@ -3,6 +3,7 @@ import asyncio
import httpx
import mimetypes
import time
from enum import Enum
from pathlib import Path
from typing import List, Optional, Union
@@ -11,15 +12,151 @@ from llama_index.core.bridge.pydantic import Field, validator
from llama_index.core.constants import DEFAULT_BASE_URL
from llama_index.core.readers.base import BasePydanticReader
from llama_index.core.schema import Document
from llama_parse.utils import (
nest_asyncio_err,
nest_asyncio_msg,
ResultType,
Language,
SUPPORTED_FILE_TYPES,
)
nest_asyncio_err = "cannot be called from a running event loop"
nest_asyncio_msg = "The event loop is already running. Add `import nest_asyncio; nest_asyncio.apply()` to your code to fix this issue."
class ResultType(str, Enum):
"""The result type for the parser."""
TXT = "text"
MD = "markdown"
class Language(str, Enum):
BAZA = "abq"
ADYGHE = "ady"
AFRIKAANS = "af"
ANGIKA = "ang"
ARABIC = "ar"
ASSAMESE = "as"
AVAR = "ava"
AZERBAIJANI = "az"
BELARUSIAN = "be"
BULGARIAN = "bg"
BIHARI = "bh"
BHOJPURI = "bho"
BENGALI = "bn"
BOSNIAN = "bs"
SIMPLIFIED_CHINESE = "ch_sim"
TRADITIONAL_CHINESE = "ch_tra"
CHECHEN = "che"
CZECH = "cs"
WELSH = "cy"
DANISH = "da"
DARGWA = "dar"
GERMAN = "de"
ENGLISH = "en"
SPANISH = "es"
ESTONIAN = "et"
PERSIAN_FARSI = "fa"
FRENCH = "fr"
IRISH = "ga"
GOAN_KONKANI = "gom"
HINDI = "hi"
CROATIAN = "hr"
HUNGARIAN = "hu"
INDONESIAN = "id"
INGUSH = "inh"
ICELANDIC = "is"
ITALIAN = "it"
JAPANESE = "ja"
KABARDIAN = "kbd"
KANNADA = "kn"
KOREAN = "ko"
KURDISH = "ku"
LATIN = "la"
LAK = "lbe"
LEZGHIAN = "lez"
LITHUANIAN = "lt"
LATVIAN = "lv"
MAGAHI = "mah"
MAITHILI = "mai"
MAORI = "mi"
MONGOLIAN = "mn"
MARATHI = "mr"
MALAY = "ms"
MALTESE = "mt"
NEPALI = "ne"
NEWARI = "new"
DUTCH = "nl"
NORWEGIAN = "no"
OCCITAN = "oc"
PALI = "pi"
POLISH = "pl"
PORTUGUESE = "pt"
ROMANIAN = "ro"
RUSSIAN = "ru"
SERBIAN_CYRILLIC = "rs_cyrillic"
SERBIAN_LATIN = "rs_latin"
NAGPURI = "sck"
SLOVAK = "sk"
SLOVENIAN = "sl"
ALBANIAN = "sq"
SWEDISH = "sv"
SWAHILI = "sw"
TAMIL = "ta"
TABASSARAN = "tab"
TELUGU = "te"
THAI = "th"
TAJIK = "tjk"
TAGALOG = "tl"
TURKISH = "tr"
UYGHUR = "ug"
UKRANIAN = "uk"
URDU = "ur"
UZBEK = "uz"
VIETNAMESE = "vi"
SUPPORTED_FILE_TYPES = [
".pdf",
".602",
".abw",
".cgm",
".cwk",
".doc",
".docx",
".docm",
".dot",
".dotm",
".hwp",
".key",
".lwp",
".mw",
".mcw",
".pages",
".pbd",
".ppt",
".pptm",
".pptx",
".pot",
".potm",
".potx",
".rtf",
".sda",
".sdd",
".sdp",
".sdw",
".sgl",
".sti",
".sxi",
".sxw",
".stw",
".sxg",
".txt",
".uof",
".uop",
".uot",
".vor",
".wpd",
".wps",
".xml",
".zabw",
".epub",
".htm",
".html"
]
class LlamaParse(BasePydanticReader):
"""A smart-parser for files."""
@@ -34,8 +171,8 @@ class LlamaParse(BasePydanticReader):
num_workers: int = Field(
default=4,
gt=0,
lt=10,
description="The number of workers to use sending API requests for parsing.",
lt=10,
description="The number of workers to use sending API requests for parsing."
)
check_interval: int = Field(
default=1,
@@ -48,26 +185,12 @@ class LlamaParse(BasePydanticReader):
verbose: bool = Field(
default=True, description="Whether to print the progress of the parsing."
)
show_progress: bool = Field(
default=True, description="Show progress when parsing multiple files."
)
language: Language = Field(
default=Language.ENGLISH, description="The language of the text to parse."
language: Language = Field(
default=Language.ENGLISH, description="The language of the text to parse."
)
parsing_instruction: Optional[str] = Field(
default="", description="The parsing instruction for the parser."
)
gpt4o_mode: bool = Field(
default=False,
description="Whether to use gpt-4o extract text from documents.",
)
gpt4o_api_key: Optional[str] = Field(
default=None,
description="The API key for the GPT-4o API. Lowers the cost of parsing.",
)
ignore_errors: bool = Field(
default=True,
description="Whether or not to ignore and skip errors raised during parsing.",
default="",
description="The parsing instruction for the parser."
)
@validator("api_key", pre=True, always=True)
@@ -75,14 +198,13 @@ class LlamaParse(BasePydanticReader):
"""Validate the API key."""
if not v:
import os
api_key = os.getenv("LLAMA_CLOUD_API_KEY", None)
if api_key is None:
raise ValueError("The API key is required.")
return api_key
return v
@validator("base_url", pre=True, always=True)
def validate_base_url(cls, v: str) -> str:
"""Validate the base URL."""
@@ -90,9 +212,7 @@ class LlamaParse(BasePydanticReader):
return url or v or DEFAULT_BASE_URL
# upload a document and get back a job_id
async def _create_job(
self, file_path: str, extra_info: Optional[dict] = None
) -> str:
async def _create_job(self, file_path: str, extra_info: Optional[dict] = None) -> str:
file_path = str(file_path)
file_ext = os.path.splitext(file_path)[1]
if file_ext not in SUPPORTED_FILE_TYPES:
@@ -114,17 +234,7 @@ class LlamaParse(BasePydanticReader):
# send the request, start job
url = f"{self.base_url}/api/parsing/upload"
async with httpx.AsyncClient(timeout=self.max_timeout) as client:
response = await client.post(
url,
files=files,
headers=headers,
data={
"language": self.language.value,
"parsing_instruction": self.parsing_instruction,
"gpt4o_mode": self.gpt4o_mode,
"gpt4o_api_key": self.gpt4o_api_key,
},
)
response = await client.post(url, files=files, headers=headers, data={"language": self.language.value, "parsing_instruction": self.parsing_instruction})
if not response.is_success:
raise Exception(f"Failed to parse the file: {response.text}")
@@ -132,65 +242,43 @@ class LlamaParse(BasePydanticReader):
job_id = response.json()["id"]
return job_id
async def _get_job_result(
self, job_id: str, result_type: str, verbose: bool = False
) -> dict:
async def _get_job_result(self, job_id: str, result_type: str) -> dict:
result_url = f"{self.base_url}/api/parsing/job/{job_id}/result/{result_type}"
status_url = f"{self.base_url}/api/parsing/job/{job_id}"
headers = {"Authorization": f"Bearer {self.api_key}"}
start = time.time()
tries = 0
while True:
await asyncio.sleep(self.check_interval)
async with httpx.AsyncClient(timeout=self.max_timeout) as client:
tries += 1
async with httpx.AsyncClient(timeout=self.max_timeout) as client:
tries += 1
result = await client.get(result_url, headers=headers)
result = await client.get(status_url, headers=headers)
if result.status_code != 200:
if result.status_code == 404:
end = time.time()
if end - start > self.max_timeout:
raise Exception(f"Timeout while parsing the file: {job_id}")
if verbose and tries % 10 == 0:
raise Exception(
f"Timeout while parsing the file: {job_id}"
)
if self.verbose and tries % 10 == 0:
print(".", end="", flush=True)
await asyncio.sleep(self.check_interval)
continue
# Allowed values "PENDING", "SUCCESS", "ERROR", "CANCELED"
status = result.json()["status"]
if status == "SUCCESS":
parsed_result = await client.get(result_url, headers=headers)
return parsed_result.json()
elif status == "PENDING":
end = time.time()
if end - start > self.max_timeout:
raise Exception(f"Timeout while parsing the file: {job_id}")
if verbose and tries % 10 == 0:
print(".", end="", flush=True)
if result.status_code == 400:
detail = result.json().get("detail", "Unknown error")
raise Exception(f"Failed to parse the file: {detail}")
await asyncio.sleep(self.check_interval)
return result.json()
continue
else:
raise Exception(
f"Failed to parse the file: {job_id}, status: {status}"
)
async def _aload_data(
self, file_path: str, extra_info: Optional[dict] = None, verbose: bool = False
) -> List[Document]:
async def _aload_data(self, file_path: str, extra_info: Optional[dict] = None) -> List[Document]:
"""Load data from the input path."""
try:
job_id = await self._create_job(file_path, extra_info=extra_info)
if verbose:
if self.verbose:
print("Started parsing the file under job_id %s" % job_id)
result = await self._get_job_result(
job_id, self.result_type.value, verbose=verbose
)
result = await self._get_job_result(job_id, self.result_type.value)
return [
Document(
@@ -198,39 +286,22 @@ class LlamaParse(BasePydanticReader):
metadata=extra_info or {},
)
]
except Exception as e:
print(f"Error while parsing the file '{file_path}':", e)
if self.ignore_errors:
return []
else:
raise e
raise e
return []
async def aload_data(
self, file_path: Union[List[str], str], extra_info: Optional[dict] = None
) -> List[Document]:
async def aload_data(self, file_path: Union[List[str], str], extra_info: Optional[dict] = None) -> List[Document]:
"""Load data from the input path."""
if isinstance(file_path, (str, Path)):
return await self._aload_data(
file_path, extra_info=extra_info, verbose=self.verbose
)
return await self._aload_data(file_path, extra_info=extra_info)
elif isinstance(file_path, list):
jobs = [
self._aload_data(
f,
extra_info=extra_info,
verbose=self.verbose and not self.show_progress,
)
for f in file_path
]
jobs = [self._aload_data(f, extra_info=extra_info) for f in file_path]
try:
results = await run_jobs(
jobs,
workers=self.num_workers,
desc="Parsing files",
show_progress=self.show_progress,
)
results = await run_jobs(jobs, workers=self.num_workers)
# return flattened results
return [item for sublist in results for item in sublist]
except RuntimeError as e:
@@ -239,13 +310,9 @@ class LlamaParse(BasePydanticReader):
else:
raise e
else:
raise ValueError(
"The input file_path must be a string or a list of strings."
)
raise ValueError("The input file_path must be a string or a list of strings.")
def load_data(
self, file_path: Union[List[str], str], extra_info: Optional[dict] = None
) -> List[Document]:
def load_data(self, file_path: Union[List[str], str], extra_info: Optional[dict] = None) -> List[Document]:
"""Load data from the input path."""
try:
return asyncio.run(self.aload_data(file_path, extra_info))
@@ -254,44 +321,35 @@ class LlamaParse(BasePydanticReader):
raise RuntimeError(nest_asyncio_msg)
else:
raise e
async def _aget_json(
self, file_path: str, extra_info: Optional[dict] = None
) -> List[dict]:
async def _aget_json(self, file_path: str, extra_info: Optional[dict] = None) -> List[dict]:
"""Load data from the input path."""
try:
job_id = await self._create_job(file_path, extra_info=extra_info)
if self.verbose:
print("Started parsing the file under job_id %s" % job_id)
result = await self._get_job_result(job_id, "json")
result["job_id"] = job_id
result["file_path"] = file_path
return [result]
except Exception as e:
print(f"Error while parsing the file '{file_path}':", e)
if self.ignore_errors:
return []
else:
raise e
raise e
async def aget_json(
self, file_path: Union[List[str], str], extra_info: Optional[dict] = None
) -> List[dict]:
async def aget_json(self, file_path: Union[List[str], str], extra_info: Optional[dict] = None) -> List[dict]:
"""Load data from the input path."""
if isinstance(file_path, (str, Path)):
return await self._aget_json(file_path, extra_info=extra_info)
elif isinstance(file_path, list):
jobs = [self._aget_json(f, extra_info=extra_info) for f in file_path]
try:
results = await run_jobs(
jobs,
workers=self.num_workers,
desc="Parsing files",
show_progress=self.show_progress,
)
results = await run_jobs(jobs, workers=self.num_workers)
# return flattened results
return [item for sublist in results for item in sublist]
except RuntimeError as e:
@@ -300,13 +358,10 @@ class LlamaParse(BasePydanticReader):
else:
raise e
else:
raise ValueError(
"The input file_path must be a string or a list of strings."
)
raise ValueError("The input file_path must be a string or a list of strings.")
def get_json_result(
self, file_path: Union[List[str], str], extra_info: Optional[dict] = None
) -> List[dict]:
def get_json_result(self, file_path: Union[List[str], str], extra_info: Optional[dict] = None) -> List[dict]:
"""Parse the input path."""
try:
return asyncio.run(self.aget_json(file_path, extra_info))
@@ -315,15 +370,10 @@ class LlamaParse(BasePydanticReader):
raise RuntimeError(nest_asyncio_msg)
else:
raise e
def get_images(self, json_result: List[dict], download_path: str) -> List[dict]:
def get_images(self, json_result: list[dict], download_path: str) -> List[dict]:
"""Download images from the parsed result."""
headers = {"Authorization": f"Bearer {self.api_key}"}
# make the download path
if not os.path.exists(download_path):
os.makedirs(download_path)
try:
images = []
for result in json_result:
@@ -333,28 +383,16 @@ class LlamaParse(BasePydanticReader):
print(f"> Image for page {page['page']}: {page['images']}")
for image in page["images"]:
image_name = image["name"]
# get the full path
image_path = os.path.join(
download_path, f"{job_id}-{image_name}"
)
# get a valid image path
if not image_path.endswith(".png"):
image_path += ".png"
image["path"] = image_path
image["job_id"] = job_id
image["original_pdf_path"] = result["file_path"]
image["page_number"] = page["page"]
image_path = os.path.join(download_path, f"{job_id}-{image_name}")
image["path"]=image_path
image["job_id"]=job_id
image["original_pdf_path"]=result["file_path"]
image["page_number"]=page["page"]
with open(image_path, "wb") as f:
image_url = f"{self.base_url}/api/parsing/job/{job_id}/result/image/{image_name}"
f.write(httpx.get(image_url, headers=headers).content)
images.append(image)
return images
except Exception as e:
print("Error while downloading images from the parsed result:", e)
if self.ignore_errors:
return []
else:
raise e
print(f"Error while downloading images from the parsed result:", e)
return []
-152
View File
@@ -1,152 +0,0 @@
from enum import Enum
# Asyncio error messages
nest_asyncio_err = "cannot be called from a running event loop"
nest_asyncio_msg = "The event loop is already running. Add `import nest_asyncio; nest_asyncio.apply()` to your code to fix this issue."
class ResultType(str, Enum):
"""The result type for the parser."""
TXT = "text"
MD = "markdown"
JSON = "json"
class Language(str, Enum):
BAZA = "abq"
ADYGHE = "ady"
AFRIKAANS = "af"
ANGIKA = "ang"
ARABIC = "ar"
ASSAMESE = "as"
AVAR = "ava"
AZERBAIJANI = "az"
BELARUSIAN = "be"
BULGARIAN = "bg"
BIHARI = "bh"
BHOJPURI = "bho"
BENGALI = "bn"
BOSNIAN = "bs"
SIMPLIFIED_CHINESE = "ch_sim"
TRADITIONAL_CHINESE = "ch_tra"
CHECHEN = "che"
CZECH = "cs"
WELSH = "cy"
DANISH = "da"
DARGWA = "dar"
GERMAN = "de"
ENGLISH = "en"
SPANISH = "es"
ESTONIAN = "et"
PERSIAN_FARSI = "fa"
FRENCH = "fr"
IRISH = "ga"
GOAN_KONKANI = "gom"
HINDI = "hi"
CROATIAN = "hr"
HUNGARIAN = "hu"
INDONESIAN = "id"
INGUSH = "inh"
ICELANDIC = "is"
ITALIAN = "it"
JAPANESE = "ja"
KABARDIAN = "kbd"
KANNADA = "kn"
KOREAN = "ko"
KURDISH = "ku"
LATIN = "la"
LAK = "lbe"
LEZGHIAN = "lez"
LITHUANIAN = "lt"
LATVIAN = "lv"
MAGAHI = "mah"
MAITHILI = "mai"
MAORI = "mi"
MONGOLIAN = "mn"
MARATHI = "mr"
MALAY = "ms"
MALTESE = "mt"
NEPALI = "ne"
NEWARI = "new"
DUTCH = "nl"
NORWEGIAN = "no"
OCCITAN = "oc"
PALI = "pi"
POLISH = "pl"
PORTUGUESE = "pt"
ROMANIAN = "ro"
RUSSIAN = "ru"
SERBIAN_CYRILLIC = "rs_cyrillic"
SERBIAN_LATIN = "rs_latin"
NAGPURI = "sck"
SLOVAK = "sk"
SLOVENIAN = "sl"
ALBANIAN = "sq"
SWEDISH = "sv"
SWAHILI = "sw"
TAMIL = "ta"
TABASSARAN = "tab"
TELUGU = "te"
THAI = "th"
TAJIK = "tjk"
TAGALOG = "tl"
TURKISH = "tr"
UYGHUR = "ug"
UKRAINIAN = "uk"
URDU = "ur"
UZBEK = "uz"
VIETNAMESE = "vi"
SUPPORTED_FILE_TYPES = [
".pdf",
# Microsoft word - all versions
".doc",
".docx",
".docm",
".dot",
".dotx",
".dotm",
# Rich text format
".rtf",
# Microsoft Works
".wps",
# Word Perfect
".wpd",
# Open Office
".sxw",
".stw",
".sxg",
# Apple
".pages",
# Mac Write
".mw",
".mcw",
# Unified Office Format text
".uot",
".uof",
".uos",
".uop",
# Microsoft powerpoints
".ppt",
".pptx",
".pot",
".pptm",
".potx",
".potm",
# Apple keynote
".key",
# Open Office Presentations
".odp",
".odg",
".otp",
".fopd",
".sxi",
".sti",
# ebook
".epub",
# html
".html",
".htm",
]
Generated
+363 -375
View File
File diff suppressed because it is too large Load Diff
+6 -6
View File
@@ -1,10 +1,6 @@
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
[tool.poetry]
name = "llama-parse"
version = "0.4.3"
version = "0.3.9"
description = "Parse files into RAG-Optimized formats."
authors = ["Logan Markewich <logan@llamaindex.ai>"]
license = "MIT"
@@ -13,8 +9,12 @@ packages = [{include = "llama_parse"}]
[tool.poetry.dependencies]
python = ">=3.8.1,<4.0"
llama-index-core = ">=0.10.29"
llama-index-core = ">=0.10.7"
[tool.poetry.group.dev.dependencies]
pytest = "^8.0.0"
ipykernel = "^6.29.0"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
+4 -45
View File
@@ -1,59 +1,18 @@
import os
import pytest
from llama_parse import LlamaParse
@pytest.mark.skipif(
os.environ.get("LLAMA_CLOUD_API_KEY", "") == "",
reason="LLAMA_CLOUD_API_KEY not set",
)
def test_simple_page_text() -> None:
def test_simple_page_text():
parser = LlamaParse(result_type="text")
filepath = os.path.join(
os.path.dirname(__file__), "test_files/attention_is_all_you_need.pdf"
)
filepath = os.path.join(os.path.dirname(__file__), "test_files/attention_is_all_you_need.pdf")
result = parser.load_data(filepath)
assert len(result) == 1
assert len(result[0].text) > 0
@pytest.mark.skipif(
os.environ.get("LLAMA_CLOUD_API_KEY", "") == "",
reason="LLAMA_CLOUD_API_KEY not set",
)
def test_simple_page_markdown() -> None:
def test_simple_page_markdown():
parser = LlamaParse(result_type="markdown")
filepath = os.path.join(
os.path.dirname(__file__), "test_files/attention_is_all_you_need.pdf"
)
filepath = os.path.join(os.path.dirname(__file__), "test_files/attention_is_all_you_need.pdf")
result = parser.load_data(filepath)
assert len(result) == 1
assert len(result[0].text) > 0
@pytest.mark.skipif(
os.environ.get("LLAMA_CLOUD_API_KEY", "") == "",
reason="LLAMA_CLOUD_API_KEY not set",
)
def test_simple_page_progress_workers() -> None:
parser = LlamaParse(result_type="markdown", show_progress=True, verbose=True)
filepath = os.path.join(
os.path.dirname(__file__), "test_files/attention_is_all_you_need.pdf"
)
result = parser.load_data([filepath, filepath])
assert len(result) == 2
assert len(result[0].text) > 0
parser = LlamaParse(
result_type="markdown", show_progress=True, num_workers=2, verbose=True
)
filepath = os.path.join(
os.path.dirname(__file__), "test_files/attention_is_all_you_need.pdf"
)
result = parser.load_data([filepath, filepath])
assert len(result) == 2
assert len(result[0].text) > 0