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

chore: use ruff in the project #87

Merged
merged 33 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
f9b562e
Applying ruff automatic changes to files.
PatrykWyzgowski Oct 8, 2024
4209360
Adding config options to support Ruff. Disabling no new line at the b…
PatrykWyzgowski Oct 8, 2024
5bedf86
One line docs are still pretty.
PatrykWyzgowski Oct 8, 2024
5fcf55b
Applying ruff with isort (with isort).
PatrykWyzgowski Oct 10, 2024
de78074
Switching to just ruff and mypy.
PatrykWyzgowski Oct 11, 2024
c833d28
Merge branch 'pw/alternate-unilint' into 68-feat-use-ruff-in-the-project
PatrykWyzgowski Oct 11, 2024
a76ab51
Switching off automatic fixes. Ruff ignores D200.
PatrykWyzgowski Oct 11, 2024
9359caa
Merge branch 'main' into 68-feat-use-ruff-in-the-project
PatrykWyzgowski Oct 11, 2024
71029fe
Manually fixed one-line docstrings to comply with our standards.
PatrykWyzgowski Oct 11, 2024
a962e5c
And one more file.
PatrykWyzgowski Oct 11, 2024
17b6b09
Fixing fixable ruff lint errors.
PatrykWyzgowski Oct 11, 2024
802188f
Adding reasonable fixes from --unsafe-fixes.
PatrykWyzgowski Oct 11, 2024
dda1287
Mypy issues resolved.
PatrykWyzgowski Oct 11, 2024
4d2d9a0
Fixed vast majority of ruff's alerts.
PatrykWyzgowski Oct 11, 2024
c98bda1
Fixed the rest of ruff's claims.
PatrykWyzgowski Oct 11, 2024
57ef338
Aligning to micpst's comments.
PatrykWyzgowski Oct 14, 2024
1246038
Unifying default dir argument assignment.
PatrykWyzgowski Oct 14, 2024
3d0e547
Unnecessary modification of first entries of prompt chat.
PatrykWyzgowski Oct 15, 2024
72e6989
Missing corrections.
PatrykWyzgowski Oct 15, 2024
26b8f0b
Back to ABC for LLMOptions.
PatrykWyzgowski Oct 15, 2024
7f40eb4
Excluding tests from formatting.
PatrykWyzgowski Oct 16, 2024
c4140f5
Merge branch 'refs/heads/main' into 68-feat-use-ruff-in-the-project
PatrykWyzgowski Oct 16, 2024
e5fc913
After-update pre-commit run.
PatrykWyzgowski Oct 16, 2024
38dd2c5
Last error deruffed.
PatrykWyzgowski Oct 16, 2024
a903dd6
This works as intended. Let's have tests that are formatted by `ruff-…
PatrykWyzgowski Oct 18, 2024
d5eca9a
Merge branch 'main' into 68-feat-use-ruff-in-the-project
PatrykWyzgowski Oct 21, 2024
f5e081f
Merge branch 'main' into 68-feat-use-ruff-in-the-project
akonarski-ds Oct 24, 2024
6a43980
Fix mypy and ruff issues
akonarski-ds Oct 24, 2024
5fe6eb6
Update contributing guide
akonarski-ds Oct 24, 2024
7d23532
Fix ci
akonarski-ds Oct 24, 2024
f809b4d
Fix ci
akonarski-ds Oct 24, 2024
8cf70aa
Add more repositories for trivy
akonarski-ds Oct 24, 2024
f29285d
Remove unnecessary empty line
akonarski-ds Oct 24, 2024
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
80 changes: 14 additions & 66 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
default_language_version:
python: python3.10
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
Expand All @@ -12,30 +14,20 @@ repos:
- id: check-json
- id: check-yaml

# PEP 8 compliant opinionated formatter.
- repo: https://github.com/psf/black
rev: 23.10.1
hooks:
- id: black
exclude: (docs/|notebooks/)
args: [--config, pyproject.toml]
- id: black-jupyter
files: \.ipynb$

# Cleaning unused imports.
- repo: https://github.com/hadialqattan/pycln
rev: v2.3.0
hooks:
- id: pycln
args: ["-a"]
exclude: (docs/|notebooks/)

# Modernizes python code and upgrade syntax for newer versions of the language
- repo: https://github.com/asottile/pyupgrade
rev: v3.15.0
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.6.9
hooks:
- id: pyupgrade
args: [--py38-plus]
# E1131: unsupported operand type(s) for | (unsupported-binary-operation)
- id: ruff
types_or: [ python, pyi, jupyter ]
exclude: (/test_|tests/|docs/|notebooks/)
args: [ --fix ]
# Formats Python, Pyi, and Jupyter files, excluding specified directories
- id: ruff-format
types_or: [ python, pyi, jupyter ]
exclude: (docs/|/tests/)
PatrykWyzgowski marked this conversation as resolved.
Show resolved Hide resolved
args: [ --quiet ]

# Used to have proper type annotations for library code.
- repo: https://github.com/pre-commit/mirrors-mypy
Expand All @@ -47,47 +39,3 @@ repos:
additional_dependencies: [pydantic>=2.8.2, types-pyyaml>=6.0.12]
exclude: (/test_|setup.py|/tests/|docs/)

# Sort imports alphabetically, and automatically separated into sections and by type.
- repo: https://github.com/timothycrosley/isort
rev: 5.13.2
hooks:
- id: isort
args: ["--profile", "black"]
exclude: (docs/|notebooks/)

# Checks Python source files for errors.
- repo: https://github.com/PyCQA/flake8
rev: 7.1.1
hooks:
- id: flake8
name: flake8
entry: flake8
language: python
types: [python]
args: [--config, .flake8]
exclude: (docs/)

# Enforces a coding standard, looks for code smells, and can make suggestions about how the code could be refactored.
- repo: https://github.com/pycqa/pylint
rev: v3.2.6
hooks:
- id: pylint
exclude: (/test_|tests/|docs/)
# # You can add additional plugins for pylint here,
# here is an example for pydantic, remember to enable it in pyproject.toml
# additional_dependencies:
# - 'pylint_pydantic'
# args:
# # pylint can have issue with python libraries based on C
# # if it fails to find some objects likely you need to add them
# # here:
# ["--extension-pkg-whitelist=pydantic"]

# Finds common security issues in Python code.
- repo: https://github.com/PyCQA/bandit
rev: 1.7.5
hooks:
- id: bandit
args: [-c, pyproject.toml, --recursive, packages/]
additional_dependencies: [".[toml]"] # required for pyproject.toml support
exclude: (notebooks/)
1 change: 0 additions & 1 deletion packages/ragbits-cli/src/ragbits/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ def main() -> None:
- if found it imports the `register` function from the `cli` module and calls it with the `app` object
- register function should add the CLI commands to the `app` object
"""

cli_enabled_modules = [
module
for module in pkgutil.iter_modules(ragbits.__path__)
Expand Down
7 changes: 4 additions & 3 deletions packages/ragbits-core/examples/chromadb_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@
]


async def main():
"""Run the example."""

async def main() -> None:
"""
Run the example.
"""
chroma_client = chromadb.PersistentClient(path="chroma")
embedding_client = LiteLLMEmbeddings()

Expand Down
7 changes: 4 additions & 3 deletions packages/ragbits-core/examples/llm_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ class JokePrompt(Prompt[LoremPromptInput, LoremPromptOutput]):
"""

system_prompt = """
You are a joke generator. The jokes you generate should be funny and not offensive. {% if not pun_allowed %}Also, make sure
that the jokes do not contain any puns.{% else %}You can use any type of joke, even if it contains puns.{% endif %}
You are a joke generator. The jokes you generate should be funny and not offensive.
{% if not pun_allowed %}Also, make sure that the jokes do not contain any puns.
{% else %}You can use any type of joke, even if it contains puns.{% endif %}

Respond as json with two fields: joke and joke_category.
"""
Expand All @@ -47,7 +48,7 @@ class JokePrompt(Prompt[LoremPromptInput, LoremPromptOutput]):
"""


async def main():
async def main() -> None:
"""
Example of using the LiteLLM client with a Prompt class. Requires the OPENAI_API_KEY environment variable to be set.
"""
Expand Down
17 changes: 9 additions & 8 deletions packages/ragbits-core/src/ragbits/core/embeddings/litellm.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Optional

try:
import litellm

Expand All @@ -8,7 +6,11 @@
HAS_LITELLM = False

from ragbits.core.embeddings import Embeddings
from ragbits.core.embeddings.exceptions import EmbeddingConnectionError, EmbeddingResponseError, EmbeddingStatusError
from ragbits.core.embeddings.exceptions import (
EmbeddingConnectionError,
EmbeddingResponseError,
EmbeddingStatusError,
)


class LiteLLMEmbeddings(Embeddings):
Expand All @@ -19,10 +21,10 @@ class LiteLLMEmbeddings(Embeddings):
def __init__(
self,
model: str = "text-embedding-3-small",
options: Optional[dict] = None,
api_base: Optional[str] = None,
api_key: Optional[str] = None,
api_version: Optional[str] = None,
options: dict | None = None,
api_base: str | None = None,
api_key: str | None = None,
api_version: str | None = None,
) -> None:
"""
Constructs the LiteLLMEmbeddingClient.
Expand Down Expand Up @@ -65,7 +67,6 @@ async def embed_text(self, data: list[str]) -> list[list[float]]:
EmbeddingStatusError: If the embedding API returns an error status code.
EmbeddingResponseError: If the embedding API response is invalid.
"""

try:
response = await litellm.aembedding(
input=data,
Expand Down
16 changes: 9 additions & 7 deletions packages/ragbits-core/src/ragbits/core/embeddings/local.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Iterator, Optional
from collections.abc import Iterator

try:
import torch
Expand All @@ -20,10 +20,9 @@ class LocalEmbeddings(Embeddings):
def __init__(
self,
model_name: str,
api_key: Optional[str] = None,
api_key: str | None = None,
) -> None:
"""
Constructs a new local LLM instance.
"""Constructs a new local LLM instance.

Args:
model_name: Name of the model to use.
Expand All @@ -45,8 +44,7 @@ def __init__(
self.tokenizer = AutoTokenizer.from_pretrained(self.model_name, token=self.hf_api_key)

async def embed_text(self, data: list[str], batch_size: int = 1) -> list[list[float]]:
"""
Calls the appropriate encoder endpoint with the given data and options.
"""Calls the appropriate encoder endpoint with the given data and options.

Args:
data: List of strings to get embeddings for.
Expand All @@ -58,7 +56,11 @@ async def embed_text(self, data: list[str], batch_size: int = 1) -> list[list[fl
embeddings = []
for batch in self._batch(data, batch_size):
batch_dict = self.tokenizer(
batch, max_length=self.tokenizer.model_max_length, padding=True, truncation=True, return_tensors="pt"
batch,
max_length=self.tokenizer.model_max_length,
padding=True,
truncation=True,
return_tensors="pt",
).to(self.device)
with torch.no_grad():
outputs = self.model(**batch_dict)
Expand Down
22 changes: 10 additions & 12 deletions packages/ragbits-core/src/ragbits/core/llms/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from abc import ABC, abstractmethod
from functools import cached_property
from typing import Generic, Optional, Type, cast, overload
from typing import Generic, cast, overload

from ragbits.core.prompt.base import BasePrompt, BasePromptWithParser, OutputT

Expand All @@ -12,9 +12,9 @@ class LLM(Generic[LLMClientOptions], ABC):
Abstract class for interaction with Large Language Model.
"""

_options_cls: Type[LLMClientOptions]
_options_cls: type[LLMClientOptions]

def __init__(self, model_name: str, default_options: Optional[LLMOptions] = None) -> None:
def __init__(self, model_name: str, default_options: LLMOptions | None = None) -> None:
"""
Constructs a new LLM instance.

Expand All @@ -39,7 +39,7 @@ def client(self) -> LLMClient:
Client for the LLM.
"""

def count_tokens(self, prompt: BasePrompt) -> int:
def count_tokens(self, prompt: BasePrompt) -> int: # noqa: PLR6301
"""
Counts tokens in the prompt.

Expand All @@ -55,7 +55,7 @@ async def generate_raw(
self,
prompt: BasePrompt,
*,
options: Optional[LLMOptions] = None,
options: LLMOptions | None = None,
) -> str:
"""
Prepares and sends a prompt to the LLM and returns the raw response (without parsing).
Expand Down Expand Up @@ -83,24 +83,22 @@ async def generate(
self,
prompt: BasePromptWithParser[OutputT],
*,
options: Optional[LLMOptions] = None,
) -> OutputT:
...
options: LLMOptions | None = None,
) -> OutputT: ...

@overload
async def generate(
self,
prompt: BasePrompt,
*,
options: Optional[LLMOptions] = None,
) -> OutputT:
...
options: LLMOptions | None = None,
) -> OutputT: ...

async def generate(
self,
prompt: BasePrompt,
*,
options: Optional[LLMOptions] = None,
options: LLMOptions | None = None,
) -> OutputT:
"""
Prepares and sends a prompt to the LLM and returns response parsed to the
Expand Down
8 changes: 4 additions & 4 deletions packages/ragbits-core/src/ragbits/core/llms/clients/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from abc import ABC, abstractmethod
from dataclasses import asdict, dataclass
from typing import Any, ClassVar, Dict, Generic, Optional, Type, TypeVar
from typing import Any, ClassVar, Generic, TypeVar

from pydantic import BaseModel

Expand All @@ -14,7 +14,7 @@
@dataclass
class LLMOptions(ABC):
PatrykWyzgowski marked this conversation as resolved.
Show resolved Hide resolved
"""
Abstract dataclass that represents all available LLM call options.
A dataclass that represents all available LLM call options.
"""

_not_given: ClassVar[Any] = None
Expand All @@ -35,7 +35,7 @@ def __or__(self, other: "LLMOptions") -> "LLMOptions":

return self.__class__(**updated_dict)

def dict(self) -> Dict[str, Any]:
def dict(self) -> dict[str, Any]:
"""
Creates a dictionary representation of the LLMOptions instance.
If a value is None, it will be replaced with a provider-specific not-given sentinel.
Expand Down Expand Up @@ -70,7 +70,7 @@ async def call(
conversation: ChatFormat,
options: LLMClientOptions,
json_mode: bool = False,
output_schema: Optional[Type[BaseModel] | Dict] = None,
output_schema: type[BaseModel] | dict | None = None,
) -> str:
"""
Calls LLM inference API.
Expand Down
27 changes: 13 additions & 14 deletions packages/ragbits-core/src/ragbits/core/llms/clients/litellm.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from dataclasses import dataclass
from typing import Dict, List, Optional, Type, Union

from pydantic import BaseModel

Expand All @@ -25,15 +24,15 @@ class LiteLLMOptions(LLMOptions):
Each of them is described in the [LiteLLM documentation](https://docs.litellm.ai/docs/completion/input).
"""

frequency_penalty: Union[Optional[float], NotGiven] = NOT_GIVEN
max_tokens: Union[Optional[int], NotGiven] = NOT_GIVEN
n: Union[Optional[int], NotGiven] = NOT_GIVEN
presence_penalty: Union[Optional[float], NotGiven] = NOT_GIVEN
seed: Union[Optional[int], NotGiven] = NOT_GIVEN
stop: Union[Optional[Union[str, List[str]]], NotGiven] = NOT_GIVEN
temperature: Union[Optional[float], NotGiven] = NOT_GIVEN
top_p: Union[Optional[float], NotGiven] = NOT_GIVEN
mock_response: Union[Optional[str], NotGiven] = NOT_GIVEN
frequency_penalty: float | None | NotGiven = NOT_GIVEN
max_tokens: int | None | NotGiven = NOT_GIVEN
n: int | None | NotGiven = NOT_GIVEN
presence_penalty: float | None | NotGiven = NOT_GIVEN
seed: int | None | NotGiven = NOT_GIVEN
stop: str | list[str] | None | NotGiven = NOT_GIVEN
temperature: float | None | NotGiven = NOT_GIVEN
top_p: float | None | NotGiven = NOT_GIVEN
mock_response: str | None | NotGiven = NOT_GIVEN


class LiteLLMClient(LLMClient[LiteLLMOptions]):
Expand All @@ -48,9 +47,9 @@ def __init__(
self,
model_name: str,
*,
base_url: Optional[str] = None,
api_key: Optional[str] = None,
api_version: Optional[str] = None,
base_url: str | None = None,
api_key: str | None = None,
api_version: str | None = None,
use_structured_output: bool = False,
) -> None:
"""
Expand Down Expand Up @@ -80,7 +79,7 @@ async def call(
conversation: ChatFormat,
options: LiteLLMOptions,
json_mode: bool = False,
output_schema: Optional[Type[BaseModel] | Dict] = None,
output_schema: type[BaseModel] | dict | None = None,
) -> str:
"""
Calls the appropriate LLM endpoint with the given prompt and options.
Expand Down
Loading