Skip to content

Commit

Permalink
Refactor types to datatypes to not overshadow built-in package
Browse files Browse the repository at this point in the history
  • Loading branch information
nachollorca committed Jan 21, 2025
1 parent 083b569 commit 09c6c56
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 37 deletions.
4 changes: 2 additions & 2 deletions HOW-TO.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
],
"source": [
"from lamine.core import get_answer\n",
"from lamine.types import Message, Model\n",
"from lamine.datatypes import Message, Model\n",
"\n",
"model = Model(\"anthropic\", \"claude-3-5-haiku-latest\")\n",
"conversation = [Message(\"user\", \"Gimme a brief pasta recipe\")]\n",
Expand Down Expand Up @@ -203,7 +203,7 @@
],
"source": [
"from lamine.core import get_answers\n",
"from lamine.types import Message, Model\n",
"from lamine.datatypes import Message, Model\n",
"\n",
"model = Model(\"together\", \"meta-llama/Llama-3-8b-chat-hf\")\n",
"conversations = [\n",
Expand Down
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@ dev = [
[tool.ruff]
fix = true
line-length = 120

[tool.mypy]
ignore_missing_imports = true
disable_error_code = ["override", "misc"]
2 changes: 1 addition & 1 deletion src/lamine/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from importlib import import_module
from time import time

from .types import Answer, Message, Model
from .datatypes import Answer, Message, Model
from .utils import run_in_parallel


Expand Down
59 changes: 30 additions & 29 deletions src/lamine/types.py → src/lamine/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
from abc import ABC, abstractmethod
from dataclasses import asdict, dataclass
from importlib import import_module
from typing import Optional, Any
from typing import Any, Optional

from dotenv import load_dotenv

from .providers import PROVIDER_IDS
Expand Down Expand Up @@ -72,34 +73,6 @@ def message(self) -> Message:
return Message("assistant", self.content)


@dataclass
class Action(_Base):
"""
Represents an tool request by a Language Model.
Attributes:
name (str): The name of the tool.
params (dict): A dictionary with input parameters.
"""

name: str
params: dict


@dataclass
class Observation(_Base):
"""
Represents the result of an Action.
Attributes:
status (str): The status of the observation (e.g., "success", "failure").
content (str): The content of the observation.
"""

status: str
content: str


@dataclass
class Model(_Base):
"""
Expand Down Expand Up @@ -183,3 +156,31 @@ def get_answer(self, model: Model, conversation: list[Message], **kwargs) -> Ans
Answer: The response generated by the language model.
"""
...


@dataclass
class Action(_Base):
"""
Represents an tool request by a Language Model.
Attributes:
name (str): The name of the tool.
params (dict): A dictionary with input parameters.
"""

name: str
params: dict


@dataclass
class Observation(_Base):
"""
Represents the result of an Action.
Attributes:
status (str): The status of the observation (e.g., "success", "failure").
content (str): The content of the observation.
"""

status: str
content: str
2 changes: 1 addition & 1 deletion src/lamine/providers/anthropic.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from anthropic import Anthropic as Anthropic_source

from ..types import Answer, Message, Model, Provider
from ..datatypes import Answer, Message, Model, Provider


class Anthropic(Provider):
Expand Down
2 changes: 1 addition & 1 deletion src/lamine/providers/mock.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from lamine.types import Provider
from lamine.datatypes import Provider


class Mock(Provider):
Expand Down
2 changes: 1 addition & 1 deletion src/lamine/providers/together.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from together import Together as Together_source

from ..types import Answer, Message, Model, Provider
from ..datatypes import Answer, Message, Model, Provider


class Together(Provider):
Expand Down
2 changes: 1 addition & 1 deletion src/lamine/providers/vertex.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import vertexai as vertexai_source
from vertexai.generative_models import Content, GenerativeModel, Part

from ..types import Answer, Message, Model, Provider
from ..datatypes import Answer, Message, Model, Provider


class Vertex(Provider):
Expand Down
27 changes: 27 additions & 0 deletions src/lamine/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Contains utility clases and functions for the core modules."""

import logging
import re
from concurrent.futures import ThreadPoolExecutor, as_completed
from typing import Any, Callable

Expand Down Expand Up @@ -37,3 +39,28 @@ def run_in_parallel(function: Callable, params_list: list[dict[str, Any]], max_w
index = futures_to_indices[future]
results[index] = future.result()
return results


def parse_xml(tag: str, string: str) -> list[str]:
"""
Parses and extracts content within specified XML tags from a given string.
Args:
tag (str): The name of the XML tag to match.
string (str): The input string containing the XML content.
Returns:
list[str]: A list of strings containing the matched content. If no matches are found, an empty list is returned.
"""
matches = re.findall(
pattern=f"<{tag}>(.*?)</{tag}>",
string=string,
flags=re.DOTALL,
)

if len(matches) == 0:
details = "f<{tag}> could not be matched"
logging.warning(details)
return []

return [match.strip() for match in matches]
2 changes: 1 addition & 1 deletion tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

from lamine.types import Model
from lamine.datatypes import Model


def test_unsupported_provider():
Expand Down

0 comments on commit 09c6c56

Please sign in to comment.