Skip to content

Commit

Permalink
refactor properties to use prefix "to_" and ignore some anthropic BS
Browse files Browse the repository at this point in the history
  • Loading branch information
nachollorca committed Jan 21, 2025
1 parent 09c6c56 commit 90d31c5
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 14 deletions.
14 changes: 4 additions & 10 deletions src/lamine/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ class _Base:
"""Base class providing utility methods for other dataclasses."""

@property
def dict(self) -> dict:
def to_dict(self) -> dict:
return asdict(self)

@classmethod
def from_dict(cls, data: dict): # type: ignore
def from_dict(cls, data: dict):
return cls(**data)

@property
def str(self) -> str:
def to_str(self) -> str:
return ""


Expand Down Expand Up @@ -63,13 +63,7 @@ class Answer(_Base):
time: Optional[float] = None

@property
def message(self) -> Message:
"""
Converts the answer to a Message object with the role "assistant".
Returns:
Message: A Message object representing the answer.
"""
def to_message(self) -> Message:
return Message("assistant", self.content)


Expand Down
6 changes: 3 additions & 3 deletions src/lamine/providers/anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ class Anthropic(Provider):

def get_answer(self, model: Model, conversation: list[Message], **kwargs) -> Answer:
client = Anthropic_source()
messages = [message.dict for message in conversation]
response = client.messages.create(model=model.id, messages=messages, max_tokens=4096, **kwargs)
messages = [message.to_dict for message in conversation]
response = client.messages.create(model=model.id, messages=messages, max_tokens=4096, **kwargs) # type: ignore
return Answer(
content=response.content[0].text,
content=response.content[0].text, # type: ignore
tokens_in=response.usage.input_tokens,
tokens_out=response.usage.output_tokens,
source=response,
Expand Down
2 changes: 1 addition & 1 deletion src/lamine/providers/together.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Together(Provider):

def get_answer(self, model: Model, conversation: list[Message], **kwargs) -> Answer:
client = Together_source()
messages = [message.dict for message in conversation]
messages = [message.to_dict for message in conversation]
response = client.chat.completions.create(model=model.id, messages=messages, **kwargs)
return Answer(
content=response.choices[0].message.content,
Expand Down

0 comments on commit 90d31c5

Please sign in to comment.