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

core[patch], langchain[patch], experimental[patch]: import CI #14414

Merged
merged 19 commits into from
Dec 8, 2023
4 changes: 2 additions & 2 deletions .github/workflows/_all_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ jobs:
working-directory: ${{ inputs.working-directory }}
secrets: inherit

pydantic-compatibility:
uses: ./.github/workflows/_pydantic_compatibility.yml
dependencies:
uses: ./.github/workflows/_dependencies.yml
with:
working-directory: ${{ inputs.working-directory }}
secrets: inherit
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: pydantic v1/v2 compatibility
name: dependencies

on:
workflow_call:
Expand Down Expand Up @@ -28,7 +28,7 @@ jobs:
- "3.9"
- "3.10"
- "3.11"
name: Pydantic v1/v2 compatibility - Python ${{ matrix.python-version }}
name: dependencies - Python ${{ matrix.python-version }}
steps:
- uses: actions/checkout@v4

Expand All @@ -42,7 +42,15 @@ jobs:

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

- name: Check imports with base dependencies
shell: bash
run: poetry run make check_imports

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

- name: Install langchain editable
working-directory: ${{ inputs.working-directory }}
Expand Down
6 changes: 5 additions & 1 deletion libs/core/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ tests:
test_watch:
poetry run ptw --snapshot-update --now . -- -vv -x tests/unit_tests

check_imports: langchain_core/**/*.py
for f in $^ ; do \
python -c "from importlib.machinery import SourceFileLoader; SourceFileLoader('x', '$$f').load_module()" || exit 1; \
done
extended_tests:
poetry run pytest --only-extended $(TEST_FILE)

Expand All @@ -32,7 +36,7 @@ lint_tests: PYTHON_FILES=tests

lint lint_diff lint_package lint_tests:
./scripts/check_pydantic.sh .
./scripts/check_imports.sh
./scripts/lint_imports.sh
Copy link
Member Author

Choose a reason for hiding this comment

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

renamed this to not confuse with make check_imports

poetry run ruff .
[ "$(PYTHON_FILES)" = "" ] || poetry run ruff format $(PYTHON_FILES) --diff
[ "$(PYTHON_FILES)" = "" ] || poetry run ruff --select I $(PYTHON_FILES)
Expand Down
File renamed without changes.
5 changes: 5 additions & 0 deletions libs/experimental/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ extended_tests:
integration_tests:
poetry run pytest tests/integration_tests

check_imports: langchain_experimental/**/*.py
for f in $^ ; do \
python -c "from importlib.machinery import SourceFileLoader; SourceFileLoader('x', '$$f').load_module()" || exit 1; \
done


######################
# LINTING AND FORMATTING
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import re
from collections import defaultdict
from dataclasses import dataclass, field
from typing import Dict, List
from typing import TYPE_CHECKING, Dict, List

from presidio_analyzer import RecognizerResult
from presidio_anonymizer.entities import EngineResult
if TYPE_CHECKING:
from presidio_analyzer import RecognizerResult
from presidio_anonymizer.entities import EngineResult

MappingDataType = Dict[str, Dict[str, str]]

Expand Down Expand Up @@ -62,8 +63,8 @@ def update(self, new_mapping: MappingDataType) -> None:

def create_anonymizer_mapping(
original_text: str,
analyzer_results: List[RecognizerResult],
anonymizer_results: EngineResult,
analyzer_results: List["RecognizerResult"],
anonymizer_results: "EngineResult",
is_reversed: bool = False,
) -> MappingDataType:
"""Creates or updates the mapping used to anonymize and/or deanonymize text.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,62 @@
get_pseudoanonymizer_mapping,
)

try:
from presidio_analyzer import AnalyzerEngine
if TYPE_CHECKING:
from presidio_analyzer import AnalyzerEngine, EntityRecognizer
from presidio_analyzer.nlp_engine import NlpEngineProvider

except ImportError as e:
raise ImportError(
"Could not import presidio_analyzer, please install with "
"`pip install presidio-analyzer`. You will also need to download a "
"spaCy model to use the analyzer, e.g. "
"`python -m spacy download en_core_web_lg`."
) from e
try:
from presidio_anonymizer import AnonymizerEngine
from presidio_anonymizer.entities import OperatorConfig
except ImportError as e:
raise ImportError(
"Could not import presidio_anonymizer, please install with "
"`pip install presidio-anonymizer`."
) from e

if TYPE_CHECKING:
from presidio_analyzer import EntityRecognizer

def _import_analyzer_engine() -> "AnalyzerEngine":
try:
from presidio_analyzer import AnalyzerEngine

except ImportError as e:
raise ImportError(
"Could not import presidio_analyzer, please install with "
"`pip install presidio-analyzer`. You will also need to download a "
"spaCy model to use the analyzer, e.g. "
"`python -m spacy download en_core_web_lg`."
) from e
return AnalyzerEngine


def _import_nlp_engine_provider() -> "NlpEngineProvider":
try:
from presidio_analyzer.nlp_engine import NlpEngineProvider

except ImportError as e:
raise ImportError(
"Could not import presidio_analyzer, please install with "
"`pip install presidio-analyzer`. You will also need to download a "
"spaCy model to use the analyzer, e.g. "
"`python -m spacy download en_core_web_lg`."
) from e
return NlpEngineProvider


def _import_anonymizer_engine() -> "AnonymizerEngine":
try:
from presidio_anonymizer import AnonymizerEngine
except ImportError as e:
raise ImportError(
"Could not import presidio_anonymizer, please install with "
"`pip install presidio-anonymizer`."
) from e
return AnonymizerEngine


def _import_operator_config() -> "OperatorConfig":
try:
from presidio_anonymizer.entities import OperatorConfig
except ImportError as e:
raise ImportError(
"Could not import presidio_anonymizer, please install with "
"`pip install presidio-anonymizer`."
) from e
return OperatorConfig


# Configuring Anonymizer for multiple languages
# Detailed description and examples can be found here:
Expand Down Expand Up @@ -89,6 +123,11 @@ def __init__(
Defaults to None, in which case faker will be seeded randomly
and provide random values.
"""
OperatorConfig = _import_operator_config()
AnalyzerEngine = _import_analyzer_engine()
NlpEngineProvider = _import_nlp_engine_provider()
AnonymizerEngine = _import_anonymizer_engine()

self.analyzed_fields = (
analyzed_fields
if analyzed_fields is not None
Expand Down
7 changes: 6 additions & 1 deletion libs/langchain/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ docker_tests:
docker build -t my-langchain-image:test .
docker run --rm my-langchain-image:test

check_imports: langchain/**/*.py
for f in $^ ; do \
python -c "from importlib.machinery import SourceFileLoader; SourceFileLoader('x', '$$f').load_module()" || exit 1; \
done

######################
# LINTING AND FORMATTING
######################
Expand All @@ -53,7 +58,7 @@ lint_tests: PYTHON_FILES=tests

lint lint_diff lint_package lint_tests:
./scripts/check_pydantic.sh .
./scripts/check_imports.sh
./scripts/lint_imports.sh
poetry run ruff .
[ "$(PYTHON_FILES)" = "" ] || poetry run ruff format $(PYTHON_FILES) --diff
[ "$(PYTHON_FILES)" = "" ] || poetry run ruff --select I $(PYTHON_FILES)
Expand Down
15 changes: 14 additions & 1 deletion libs/langchain/langchain/utilities/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from typing import TYPE_CHECKING, Any, Dict, List, Optional

import requests
import tiktoken
from langchain_core.pydantic_v1 import BaseModel, Extra, root_validator

from langchain.utils import get_from_dict_or_env
Expand All @@ -15,6 +14,18 @@
from github.PullRequest import PullRequest


def _import_tiktoken() -> Any:
"""Import tiktoken."""
try:
import tiktoken
except ImportError:
raise ImportError(
"tiktoken is not installed. "
"Please install it with `pip install tiktoken`"
)
return tiktoken


class GitHubAPIWrapper(BaseModel):
"""Wrapper for GitHub API."""

Expand Down Expand Up @@ -385,6 +396,7 @@ def list_pull_request_files(self, pr_number: int) -> List[Dict[str, Any]]:
dict: A dictionary containing the issue's title,
body, and comments as a string
"""
tiktoken = _import_tiktoken()
MAX_TOKENS_FOR_FILES = 3_000
pr_files = []
pr = self.github_repo_instance.get_pull(number=int(pr_number))
Expand Down Expand Up @@ -453,6 +465,7 @@ def get_pull_request(self, pr_number: int) -> Dict[str, Any]:
total_tokens = 0

def get_tokens(text: str) -> int:
tiktoken = _import_tiktoken()
return len(tiktoken.get_encoding("cl100k_base").encode(text))

def add_to_dict(data_dict: Dict[str, Any], key: str, value: str) -> None:
Expand Down
71 changes: 0 additions & 71 deletions libs/langchain/langchain/vectorstores/_pgvector_data_models.py

This file was deleted.

Loading
Loading