Skip to content

Commit

Permalink
core[patch], langchain[patch]: fix required deps (#14373)
Browse files Browse the repository at this point in the history
  • Loading branch information
baskaryan authored Dec 7, 2023
1 parent 7186fae commit b2280fd
Show file tree
Hide file tree
Showing 34 changed files with 1,528 additions and 109 deletions.
9 changes: 1 addition & 8 deletions .github/workflows/_all_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ jobs:

compile-integration-tests:
uses: ./.github/workflows/_compile_integration_test.yml
if: ${{ inputs.working-directory != 'libs/core' }}
with:
working-directory: ${{ inputs.working-directory }}
secrets: inherit
Expand All @@ -68,7 +67,6 @@ jobs:
- "3.9"
- "3.10"
- "3.11"
if: ${{ inputs.working-directory == 'libs/langchain' }}
name: Python ${{ matrix.python-version }} extended tests
defaults:
run:
Expand All @@ -88,12 +86,7 @@ jobs:
shell: bash
run: |
echo "Running extended tests, installing dependencies with poetry..."
poetry install -E extended_testing
- name: Install langchain core editable
shell: bash
run: |
poetry run pip install -e ../core
poetry install -E extended_testing --with test
- name: Run extended tests
run: make extended_tests
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/_compile_integration_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:

- name: Install integration dependencies
shell: bash
run: poetry install --with=test_integration
run: poetry install --with=test_integration,test

- name: Check integration tests compile
shell: bash
Expand Down
29 changes: 28 additions & 1 deletion .github/workflows/_lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,31 @@ jobs:
- name: Analysing the code with our lint
working-directory: ${{ inputs.working-directory }}
run: |
make lint
make lint_package
- name: Install test dependencies
# Also installs dev/lint/test/typing dependencies, to ensure we have
# type hints for as many of our libraries as possible.
# This helps catch errors that require dependencies to be spotted, for example:
# https://github.com/langchain-ai/langchain/pull/10249/files#diff-935185cd488d015f026dcd9e19616ff62863e8cde8c0bee70318d3ccbca98341
#
# If you change this configuration, make sure to change the `cache-key`
# in the `poetry_setup` action above to stop using the old cache.
# It doesn't matter how you change it, any change will cause a cache-bust.
working-directory: ${{ inputs.working-directory }}
run: |
poetry install --with test
- name: Get .mypy_cache to speed up mypy
uses: actions/cache@v3
env:
SEGMENT_DOWNLOAD_TIMEOUT_MIN: "2"
with:
path: |
${{ env.WORKDIR }}/.mypy_cache
key: mypy-test-${{ runner.os }}-${{ runner.arch }}-py${{ matrix.python-version }}-${{ inputs.working-directory }}-${{ hashFiles(format('{0}/poetry.lock', env.WORKDIR)) }}

- name: Analysing the code with our lint
working-directory: ${{ inputs.working-directory }}
run: |
make lint_tests
2 changes: 1 addition & 1 deletion .github/workflows/_pydantic_compatibility.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:

- name: Install dependencies
shell: bash
run: poetry install
run: poetry install --with test

- name: Install langchain editable
working-directory: ${{ inputs.working-directory }}
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/templates_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,4 @@ jobs:
./.github/workflows/_lint.yml
with:
working-directory: templates
langchain-location: ../libs/langchain
secrets: inherit
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ spell_fix:
# LINTING AND FORMATTING
######################

lint:
lint lint_package lint_tests:
poetry run ruff docs templates cookbook
poetry run ruff format docs templates cookbook --diff
poetry run ruff --select I docs templates cookbook
Expand Down
7 changes: 6 additions & 1 deletion libs/core/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ tests:
test_watch:
poetry run ptw --snapshot-update --now . -- -vv -x tests/unit_tests

extended_tests:
poetry run pytest --only-extended $(TEST_FILE)


######################
# LINTING AND FORMATTING
Expand All @@ -24,8 +27,10 @@ test_watch:
PYTHON_FILES=.
lint format: PYTHON_FILES=.
lint_diff format_diff: PYTHON_FILES=$(shell git diff --relative=libs/experimental --name-only --diff-filter=d master | grep -E '\.py$$|\.ipynb$$')
lint_package: PYTHON_FILES=langchain_core
lint_tests: PYTHON_FILES=tests

lint lint_diff:
lint lint_diff lint_package lint_tests:
./scripts/check_pydantic.sh .
./scripts/check_imports.sh
poetry run ruff .
Expand Down
2 changes: 1 addition & 1 deletion libs/core/langchain_core/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def messages(self) -> Sequence[BaseMessage]:


def _convert_agent_action_to_messages(
agent_action: AgentAction
agent_action: AgentAction,
) -> Sequence[BaseMessage]:
"""Convert an agent action to a message.
Expand Down
7 changes: 6 additions & 1 deletion libs/core/langchain_core/tracers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@
"Run",
"RunLog",
"RunLogPatch",
"LogStreamCallbackHandler",
]

from langchain_core.tracers.base import BaseTracer
from langchain_core.tracers.evaluation import EvaluatorCallbackHandler
from langchain_core.tracers.langchain import LangChainTracer
from langchain_core.tracers.log_stream import RunLog, RunLogPatch
from langchain_core.tracers.log_stream import (
LogStreamCallbackHandler,
RunLog,
RunLogPatch,
)
from langchain_core.tracers.schemas import Run
from langchain_core.tracers.stdout import ConsoleCallbackHandler
30 changes: 29 additions & 1 deletion libs/core/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions libs/core/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,37 @@ pydantic = ">=1,<3"
langsmith = "~0.0.63"
tenacity = "^8.1.0"
jsonpatch = "^1.33"
anyio = ">=3,<5"
PyYAML = ">=5.3"
requests = "^2"
packaging = "^23.2"
jinja2 = {version = "^3", optional = true}

[tool.poetry.group.lint]
optional = true

[tool.poetry.group.lint.dependencies]
ruff = "^0.1.5"

[tool.poetry.group.typing]
optional = true

[tool.poetry.group.typing.dependencies]
mypy = "^0.991"
types-pyyaml = "^6.0.12.2"
types-requests = "^2.28.11.5"
types-jinja2 = "^2.11.9"

[tool.poetry.group.dev]
optional = true

[tool.poetry.group.dev.dependencies]
jupyter = "^1.0.0"
setuptools = "^67.6.1"

[tool.poetry.group.test]
optional = true

[tool.poetry.group.test.dependencies]
# The only dependencies that should be added are
# dependencies used for running tests (e.g., pytest, freezegun, response).
Expand All @@ -43,6 +61,9 @@ pytest-asyncio = "^0.21.1"
optional = true
dependencies = {}

[tool.poetry.extras]
extended_testing = ["jinja2"]

[tool.ruff]
select = [
"E", # pycodestyle
Expand Down
Empty file.
7 changes: 7 additions & 0 deletions libs/core/tests/integration_tests/test_compile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import pytest


@pytest.mark.compile
def test_placeholder() -> None:
"""Used for compiling integration tests without running any real tests."""
pass
87 changes: 87 additions & 0 deletions libs/core/tests/unit_tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
"""Configuration for unit tests."""
from importlib import util
from typing import Dict, Sequence

import pytest
from pytest import Config, Function, Parser


def pytest_addoption(parser: Parser) -> None:
"""Add custom command line options to pytest."""
parser.addoption(
"--only-extended",
action="store_true",
help="Only run extended tests. Does not allow skipping any extended tests.",
)
parser.addoption(
"--only-core",
action="store_true",
help="Only run core tests. Never runs any extended tests.",
)


def pytest_collection_modifyitems(config: Config, items: Sequence[Function]) -> None:
"""Add implementations for handling custom markers.
At the moment, this adds support for a custom `requires` marker.
The `requires` marker is used to denote tests that require one or more packages
to be installed to run. If the package is not installed, the test is skipped.
The `requires` marker syntax is:
.. code-block:: python
@pytest.mark.requires("package1", "package2")
def test_something():
...
"""
# Mapping from the name of a package to whether it is installed or not.
# Used to avoid repeated calls to `util.find_spec`
required_pkgs_info: Dict[str, bool] = {}

only_extended = config.getoption("--only-extended") or False
only_core = config.getoption("--only-core") or False

if only_extended and only_core:
raise ValueError("Cannot specify both `--only-extended` and `--only-core`.")

for item in items:
requires_marker = item.get_closest_marker("requires")
if requires_marker is not None:
if only_core:
item.add_marker(pytest.mark.skip(reason="Skipping not a core test."))
continue

# Iterate through the list of required packages
required_pkgs = requires_marker.args
for pkg in required_pkgs:
# If we haven't yet checked whether the pkg is installed
# let's check it and store the result.
if pkg not in required_pkgs_info:
try:
installed = util.find_spec(pkg) is not None
except Exception:
installed = False
required_pkgs_info[pkg] = installed

if not required_pkgs_info[pkg]:
if only_extended:
pytest.fail(
f"Package `{pkg}` is not installed but is required for "
f"extended tests. Please install the given package and "
f"try again.",
)

else:
# If the package is not installed, we immediately break
# and mark the test as skipped.
item.add_marker(
pytest.mark.skip(reason=f"Requires pkg: `{pkg}`")
)
break
else:
if only_extended:
item.add_marker(
pytest.mark.skip(reason="Skipping not an extended test.")
)
6 changes: 3 additions & 3 deletions libs/core/tests/unit_tests/prompts/test_few_shot.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def test_partial() -> None:

@pytest.mark.requires("jinja2")
def test_prompt_jinja2_functionality(
example_jinja2_prompt: Tuple[PromptTemplate, List[Dict[str, str]]]
example_jinja2_prompt: Tuple[PromptTemplate, List[Dict[str, str]]],
) -> None:
prefix = "Starting with {{ foo }}"
suffix = "Ending with {{ bar }}"
Expand All @@ -256,7 +256,7 @@ def test_prompt_jinja2_functionality(

@pytest.mark.requires("jinja2")
def test_prompt_jinja2_missing_input_variables(
example_jinja2_prompt: Tuple[PromptTemplate, List[Dict[str, str]]]
example_jinja2_prompt: Tuple[PromptTemplate, List[Dict[str, str]]],
) -> None:
"""Test error is raised when input variables are not provided."""
prefix = "Starting with {{ foo }}"
Expand Down Expand Up @@ -303,7 +303,7 @@ def test_prompt_jinja2_missing_input_variables(

@pytest.mark.requires("jinja2")
def test_prompt_jinja2_extra_input_variables(
example_jinja2_prompt: Tuple[PromptTemplate, List[Dict[str, str]]]
example_jinja2_prompt: Tuple[PromptTemplate, List[Dict[str, str]]],
) -> None:
"""Test error is raised when there are too many input variables."""
prefix = "Starting with {{ foo }}"
Expand Down
Empty file.
1 change: 1 addition & 0 deletions libs/core/tests/unit_tests/tracers/test_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"Run",
"RunLog",
"RunLogPatch",
"LogStreamCallbackHandler",
]


Expand Down
4 changes: 3 additions & 1 deletion libs/experimental/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ integration_tests:
PYTHON_FILES=.
lint format: PYTHON_FILES=.
lint_diff format_diff: PYTHON_FILES=$(shell git diff --relative=libs/experimental --name-only --diff-filter=d master | grep -E '\.py$$|\.ipynb$$')
lint_package: PYTHON_FILES=langchain_experimental
lint_tests: PYTHON_FILES=tests

lint lint_diff:
lint lint_diff lint_package lint_tests:
poetry run ruff .
poetry run ruff format $(PYTHON_FILES) --diff
poetry run ruff --select I $(PYTHON_FILES)
Expand Down
Loading

0 comments on commit b2280fd

Please sign in to comment.