generated from langchain-ai/integration-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
136 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
name: integration-test | ||
|
||
on: | ||
workflow_call: | ||
inputs: | ||
working-directory: | ||
required: true | ||
type: string | ||
description: "From which folder this pipeline executes" | ||
|
||
env: | ||
POETRY_VERSION: "1.7.1" | ||
DOCKER_COMPOSE_YAML: "libs/elasticsearch/integration_tests/docker-compose.yml" | ||
|
||
jobs: | ||
build: | ||
defaults: | ||
run: | ||
working-directory: ${{ inputs.working-directory }} | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
python-version: | ||
- "3.8" | ||
- "3.9" | ||
- "3.10" | ||
- "3.11" | ||
name: "Integration tests" | ||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Set up Python ${{ matrix.python-version }} + Poetry ${{ env.POETRY_VERSION }} | ||
uses: "./.github/actions/poetry_setup" | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
poetry-version: ${{ env.POETRY_VERSION }} | ||
working-directory: ${{ inputs.working-directory }} | ||
cache-key: integration-test | ||
|
||
- name: Install dependencies | ||
shell: bash | ||
run: poetry install --with=test_integration,test | ||
|
||
- name: Start containers | ||
shell: bash | ||
run: docker-compose -f "$DOCKER_COMPOSE_YAML" up elasticsearch -d --build | ||
|
||
- name: Run integration tests | ||
shell: bash | ||
env: | ||
OPENAI_API_KEY: ${{ secrets.SuperSecret }} | ||
run: make integration_test | ||
|
||
- name: Stop containers | ||
if: always() | ||
shell: bash | ||
run: docker-compose -f "$DOCKER_COMPOSE_YAML" down elasticsearch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 33 additions & 43 deletions
76
libs/elasticsearch/tests/integration_tests/test_embeddings.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,38 @@ | ||
"""Test elasticsearch_embeddings embeddings.""" | ||
|
||
import pytest | ||
from langchain_core.utils import get_from_env | ||
import os | ||
|
||
from elasticsearch import Elasticsearch | ||
|
||
from langchain_elasticsearch.embeddings import ElasticsearchEmbeddings | ||
|
||
# deployed with | ||
# https://www.elastic.co/guide/en/machine-learning/current/ml-nlp-text-emb-vector-search-example.html | ||
DEFAULT_MODEL = "sentence-transformers__msmarco-minilm-l-12-v3" | ||
DEFAULT_NUM_DIMENSIONS = "384" | ||
|
||
|
||
@pytest.fixture | ||
def model_id() -> str: | ||
return get_from_env("model_id", "MODEL_ID", DEFAULT_MODEL) | ||
|
||
|
||
@pytest.fixture | ||
def expected_num_dimensions() -> int: | ||
return int( | ||
get_from_env( | ||
"expected_num_dimensions", "EXPECTED_NUM_DIMENSIONS", DEFAULT_NUM_DIMENSIONS | ||
) | ||
) | ||
|
||
|
||
def test_elasticsearch_embedding_documents( | ||
model_id: str, expected_num_dimensions: int | ||
) -> None: | ||
"""Test Elasticsearch embedding documents.""" | ||
documents = ["foo bar", "bar foo", "foo"] | ||
embedding = ElasticsearchEmbeddings.from_credentials(model_id) | ||
output = embedding.embed_documents(documents) | ||
assert len(output) == 3 | ||
assert len(output[0]) == expected_num_dimensions | ||
assert len(output[1]) == expected_num_dimensions | ||
assert len(output[2]) == expected_num_dimensions | ||
|
||
|
||
def test_elasticsearch_embedding_query( | ||
model_id: str, expected_num_dimensions: int | ||
) -> None: | ||
"""Test Elasticsearch embedding query.""" | ||
document = "foo bar" | ||
embedding = ElasticsearchEmbeddings.from_credentials(model_id) | ||
output = embedding.embed_query(document) | ||
assert len(output) == expected_num_dimensions | ||
from ._test_utilities import deploy_model | ||
|
||
ES_CLIENT = Elasticsearch(hosts=[os.environ.get("ES_URL", "http://localhost:9200")]) | ||
MODEL_ID = ".elser_model_2" | ||
|
||
|
||
class TestEmbeddings: | ||
@classmethod | ||
def setup_class(cls) -> None: | ||
deploy_model(ES_CLIENT, MODEL_ID) | ||
|
||
def test_elasticsearch_embedding_documents(self) -> None: | ||
"""Test Elasticsearch embedding documents.""" | ||
documents = ["foo bar", "bar foo", "foo"] | ||
embedding = ElasticsearchEmbeddings(ES_CLIENT.ml, MODEL_ID) | ||
output = embedding.embed_documents(documents) | ||
assert len(output) == 3 | ||
assert "foo" in output[0] | ||
assert "##bar" in output[0] | ||
assert "bar" in output[1] | ||
assert "foo" in output[1] | ||
assert "foo" in output[2] | ||
|
||
def test_elasticsearch_embedding_query(self) -> None: | ||
"""Test Elasticsearch embedding query.""" | ||
document = "foo bar" | ||
embedding = ElasticsearchEmbeddings(ES_CLIENT.ml, MODEL_ID) | ||
output = embedding.embed_query(document) | ||
assert "foo" in output | ||
assert "##bar" in output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters