-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from OpenGenenerativeAI/some-code-improvements
Some Fix & code quality improvements
- Loading branch information
Showing
12 changed files
with
97 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,52 @@ | ||
import time | ||
import uuid | ||
from typing import Any | ||
from typing import Self | ||
|
||
from pydantic import BaseModel, Field | ||
|
||
from genoss.entities.chat.message import Message | ||
|
||
|
||
# TODO: why is this nested classes ? | ||
# TODO: why don't we use a pydantic ? | ||
class ChatCompletion: | ||
class Choice: | ||
def __init__( | ||
self, message: Message, finish_reason: str = "stop", index: int = 0 | ||
): | ||
self.message = message | ||
self.finish_reason = finish_reason | ||
self.index = index | ||
|
||
def to_dict(self) -> dict[str, Any]: | ||
return { | ||
"message": self.message.to_dict(), | ||
"finish_reason": self.finish_reason, | ||
"index": self.index, | ||
} | ||
|
||
class Usage: | ||
def __init__( | ||
self, prompt_tokens: int, completion_tokens: int, total_tokens: int | ||
): | ||
self.prompt_tokens = prompt_tokens | ||
self.completion_tokens = completion_tokens | ||
self.total_tokens = total_tokens | ||
|
||
def to_dict(self) -> dict[str, Any]: | ||
return { | ||
"prompt_tokens": self.prompt_tokens, | ||
"completion_tokens": self.completion_tokens, | ||
"total_tokens": self.total_tokens, | ||
} | ||
|
||
def __init__(self, model: str, question: str, answer: str): | ||
self.id = str(uuid.uuid4()) | ||
self.object = "chat.completion" | ||
self.created = int(time.time()) | ||
self.model = model | ||
self.usage = self.Usage(len(question), len(answer), len(question) + len(answer)) | ||
self.choices = [ | ||
self.Choice(Message(role="assistant", content=answer), "stop", 0) | ||
] | ||
|
||
def to_dict(self) -> dict[str, Any]: | ||
return { | ||
"id": self.id, | ||
"object": self.object, | ||
"created": self.created, | ||
"model": self.model, | ||
"usage": self.usage.to_dict(), | ||
"choices": [choice.to_dict() for choice in self.choices], | ||
} | ||
class Choice(BaseModel): | ||
message: Message | ||
finish_reason: str = "stop" | ||
index: int = 0 | ||
|
||
@classmethod | ||
def from_model_answer(cls, answer: str) -> Self: | ||
return cls( | ||
message=Message(role="assistant", content=answer), | ||
finish_reason="stop", | ||
index=0, | ||
) | ||
|
||
|
||
class Usage(BaseModel): | ||
prompt_tokens: int | ||
completion_tokens: int | ||
total_tokens: int | ||
|
||
@classmethod | ||
def from_question_and_answer(cls, question: str, answer: str) -> Self: | ||
return cls( | ||
prompt_tokens=len(question), | ||
completion_tokens=len(answer), | ||
total_tokens=len(question) + len(answer), | ||
) | ||
|
||
|
||
class ChatCompletion(BaseModel): | ||
id: uuid.UUID = Field(default_factory=uuid.uuid4) | ||
object: str = "chat.completion" | ||
created: int = Field(default_factory=lambda: int(time.time())) | ||
model: str | ||
usage: Usage | ||
choices: list[Choice] | ||
|
||
@classmethod | ||
def from_model_question_answer(cls, model: str, question: str, answer: str) -> Self: | ||
return cls( | ||
model=model, | ||
usage=Usage.from_question_and_answer(question, answer), | ||
choices=[Choice.from_model_answer(answer)], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,16 @@ | ||
from typing import Any | ||
from typing import Literal | ||
|
||
from pydantic import BaseModel, Field | ||
|
||
MessageRole = Literal["system", "user", "assistant", "function"] | ||
|
||
|
||
class Message(BaseModel): | ||
role: str = Field( | ||
role: MessageRole = Field( | ||
..., | ||
description="The role of the messages author. One of system, user, assistant, or function.", | ||
) | ||
content: str = Field( | ||
..., | ||
description="The contents of the message. content is required for all messages, and may be null for assistant messages with function calls.", | ||
) | ||
|
||
def to_dict(self) -> dict[str, Any]: | ||
return {"role": self.role, "content": self.content} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,35 @@ | ||
from abc import abstractmethod | ||
from typing import Any | ||
|
||
from langchain import LLMChain | ||
from pydantic import BaseModel | ||
|
||
from genoss.entities.chat.chat_completion import ChatCompletion | ||
from genoss.entities.chat.message import Message | ||
from genoss.prompts.prompt_template import prompt_template | ||
|
||
|
||
class BaseGenossLLM(BaseModel): | ||
name: str | ||
description: str | ||
|
||
@abstractmethod | ||
def generate_answer(self, messages: list[Message]) -> dict[str, Any]: | ||
def generate_answer(self, messages: list[Message]) -> ChatCompletion: | ||
pass | ||
|
||
def _chat_completion_from_langchain_llm( | ||
self, llm: BaseModel, messages: list[Message] | ||
) -> ChatCompletion: | ||
llm_chain = LLMChain(prompt=prompt_template, llm=llm) | ||
|
||
question = messages[-1].content | ||
response_text = llm_chain(question) | ||
|
||
answer = response_text["text"] | ||
|
||
return ChatCompletion.from_model_question_answer( | ||
model=self.name, answer=answer, question=question | ||
) | ||
|
||
@abstractmethod | ||
def generate_embedding(self, text: str) -> list[float]: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.