Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added an Unstructured.io test #298

Merged
merged 8 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/_run_e2e_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ jobs:
LLAMA_CLOUD_API_KEY: "${{ secrets.E2E_TESTS_LLAMA_CLOUD_API_KEY }}"
GCLOUD_ACCOUNT_KEY_JSON: "${{ secrets.E2E_TESTS_GCLOUD_ACCOUNT_KEY_JSON }}"
NVIDIA_API_KEY: "${{ secrets.E2E_TESTS_NVIDIA_API_KEY }}"
UNSTRUCTURED_API_KEY: "${{ secrets.E2E_TESTS_UNSTRUCTURED_API_KEY }}"
run: |
source scripts/ci-common-env.sh
tox -e notebooks
Expand All @@ -116,6 +117,7 @@ jobs:
NVIDIA_API_KEY: "${{ secrets.E2E_TESTS_NVIDIA_API_KEY }}"
LANGCHAIN_API_KEY: "${{ secrets.E2E_TESTS_LANGCHAIN_API_KEY }}"
LLAMA_CLOUD_API_KEY: "${{ secrets.E2E_TESTS_LLAMA_CLOUD_API_KEY }}"
UNSTRUCTURED_API_KEY: "${{ secrets.E2E_TESTS_UNSTRUCTURED_API_KEY }}"
run: |
source scripts/ci-common-env.sh
if [ "${{ inputs.suite-name == 'ragstack' }}" == "true" ]; then
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ packages = [{ include = "ragstack" }]
python = ">=3.9,<4.0"
astrapy = "~0.7.0"
cassio = "~0.1.3"
unstructured = "^0.10"
unstructured = { version = "0.12.4" }
llama-index = { version = "0.9.48", extras = ["langchain"] }
llama-parse = { version = "0.1.4" }
langchain = { version = "0.1.4" }
Expand Down
3 changes: 3 additions & 0 deletions ragstack-e2e-tests/.env.template
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@ VECTOR_DATABASE_TYPE=astradb

# LlamaIndex
# LLAMA_CLOUD_API_KEY=

# Unstructured.io
# UNSTRUCTURED_API_KEY=
79 changes: 79 additions & 0 deletions ragstack-e2e-tests/e2e_tests/langchain/test_unstructured.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import pytest

from langchain_community.document_loaders import UnstructuredAPIFileLoader
epinzur marked this conversation as resolved.
Show resolved Hide resolved
from langchain.text_splitter import TokenTextSplitter
from langchain.schema.output_parser import StrOutputParser
from langchain.prompts import PromptTemplate
from langchain_core.runnables import RunnablePassthrough
from langchain.chat_models import ChatOpenAI
from langchain.embeddings import OpenAIEmbeddings

from e2e_tests.langchain.rag_application import BASIC_QA_PROMPT
from e2e_tests.test_utils import get_local_resource_path

from e2e_tests.conftest import (
set_current_test_info,
get_required_env,
get_vector_store_handler,
)

from e2e_tests.test_utils.vector_store_handler import (
VectorStoreImplementation,
VectorStoreTestContext,
)


@pytest.fixture
def astra_db():
handler = get_vector_store_handler(VectorStoreImplementation.ASTRADB)
context = handler.before_test()
yield context
handler.after_test()


@pytest.fixture
def cassandra():
handler = get_vector_store_handler(VectorStoreImplementation.CASSANDRA)
context = handler.before_test()
yield context
handler.after_test()


@pytest.mark.parametrize("vector_store", ["cassandra", "astra_db"])
@pytest.mark.parametrize("unstructured_mode", ["single", "elements"])
def test_unstructured_api(vector_store, unstructured_mode, request):
set_current_test_info(
"langchain::unstructured-api",
f"{unstructured_mode},{vector_store}",
)

vector_store_context: VectorStoreTestContext = request.getfixturevalue(vector_store)
embedding = OpenAIEmbeddings(openai_api_key=get_required_env("OPEN_AI_KEY"))
vector_store = vector_store_context.new_langchain_vector_store(embedding=embedding)

loader = UnstructuredAPIFileLoader(
file_path=get_local_resource_path("tree.pdf"),
mode=unstructured_mode,
strategy="auto",
api_key=get_required_env("UNSTRUCTURED_API_KEY"),
)

splitter = TokenTextSplitter(chunk_size=512, chunk_overlap=0)
vector_store.add_documents(splitter.split_documents(loader.load()))

prompt = PromptTemplate.from_template(BASIC_QA_PROMPT)
llm = ChatOpenAI(
openai_api_key=get_required_env("OPEN_AI_KEY"),
model="gpt-3.5-turbo-16k",
streaming=False,
temperature=0,
)

chain = (
{"context": vector_store.as_retriever(), "question": RunnablePassthrough()}
| prompt
| llm
| StrOutputParser()
)
response = chain.invoke("What was Eldenroot?")
assert len(response) > 0
2 changes: 1 addition & 1 deletion ragstack-e2e-tests/pyproject.langchain.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ llama-parse = { version = "0.1.4" }
astrapy = "~0.7.0"
# we need this specific feature from cassio: https://github.com/CassioML/cassio/pull/128
cassio = "~0.1.4"
unstructured = "^0.10"
unstructured = { git = "https://github.com/Unstructured-IO/unstructured.git", branch = "main" }

[build-system]
requires = ["poetry-core"]
Expand Down
2 changes: 1 addition & 1 deletion ragstack-e2e-tests/pyproject.llamaindex.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ langchain-nvidia-ai-endpoints = { version = "0.0.1" }
astrapy = "~0.7.0"
# we need this specific feature from cassio: https://github.com/CassioML/cassio/pull/128
cassio = "~0.1.4"
unstructured = "^0.10"
unstructured = { version = "0.12.4" }

[build-system]
requires = ["poetry-core"]
Expand Down
1 change: 1 addition & 0 deletions ragstack-e2e-tests/tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pass_env =
LANGCHAIN_ENDPOINT
LANGCHAIN_PROJECT
LLAMA_CLOUD_API_KEY
UNSTRUCTURED_API_KEY
deps =
poetry
commands =
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pass_env =
LANGCHAIN_PROJECT
LLAMA_CLOUD_API_KEY
NVIDIA_API_KEY
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is for the notebooks, guess you can remove it

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll be adding an example notebook in my next PR. So I'll leave for now if that is okay.

UNSTRUCTURED_API_KEY
deps =
pytest
nbmake
Expand Down
Loading