Skip to content

Commit

Permalink
Apply autoformatter
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelOwenDyer committed Feb 14, 2024
1 parent 7c53184 commit 63aaf03
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 28 deletions.
4 changes: 3 additions & 1 deletion app/llm/basic_request_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ def complete(self, prompt: str, arguments: CompletionArguments) -> str:
llm = self.llm_manager.get_by_id(self.model_id)
return llm.complete(prompt, arguments)

def chat(self, messages: list[IrisMessage], arguments: CompletionArguments) -> IrisMessage:
def chat(
self, messages: list[IrisMessage], arguments: CompletionArguments
) -> IrisMessage:
llm = self.llm_manager.get_by_id(self.model_id)
return llm.chat(messages, arguments)

Expand Down
4 changes: 1 addition & 3 deletions app/llm/langchain/iris_langchain_completion_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ def _generate(
generations = []
args = CompletionArguments(stop=stop)
for prompt in prompts:
completion = self.request_handler.complete(
prompt=prompt, arguments=args
)
completion = self.request_handler.complete(prompt=prompt, arguments=args)
generations.append([Generation(text=completion)])
return LLMResult(generations=generations)

Expand Down
4 changes: 1 addition & 3 deletions app/llm/llm_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ def load_llms() -> dict[str, LanguageModel]:
yaml_dict = yaml.safe_load(file)

llms = LLMList.model_validate({"llms": yaml_dict}).llms
return {
llm.id: llm for llm in llms
}
return {llm.id: llm for llm in llms}


class LlmManager(metaclass=Singleton):
Expand Down
9 changes: 6 additions & 3 deletions app/llm/request_handler_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ class RequestHandler(metaclass=ABCMeta):
@classmethod
def __subclasshook__(cls, subclass) -> bool:
return (
hasattr(subclass, "complete") and callable(subclass.complete)
and hasattr(subclass, "chat") and callable(subclass.chat)
and hasattr(subclass, "embed") and callable(subclass.embed)
hasattr(subclass, "complete")
and callable(subclass.complete)
and hasattr(subclass, "chat")
and callable(subclass.chat)
and hasattr(subclass, "embed")
and callable(subclass.embed)
)

@abstractmethod
Expand Down
24 changes: 15 additions & 9 deletions app/llm/wrapper/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
from llm.wrapper.model import LanguageModel
from llm.wrapper.openai_completion import NativeOpenAICompletionModel, AzureOpenAICompletionModel
from llm.wrapper.openai_completion import (
NativeOpenAICompletionModel,
AzureOpenAICompletionModel,
)
from llm.wrapper.openai_chat import NativeOpenAIChatModel, AzureOpenAIChatModel
from llm.wrapper.openai_embeddings import NativeOpenAIEmbeddingModel, AzureOpenAIEmbeddingModel
from llm.wrapper.openai_embeddings import (
NativeOpenAIEmbeddingModel,
AzureOpenAIEmbeddingModel,
)
from llm.wrapper.ollama import OllamaModel

type AnyLLM = (
NativeOpenAICompletionModel
| AzureOpenAICompletionModel
| NativeOpenAIChatModel
| AzureOpenAIChatModel
| NativeOpenAIEmbeddingModel
| AzureOpenAIEmbeddingModel
| OllamaModel
NativeOpenAICompletionModel
| AzureOpenAICompletionModel
| NativeOpenAIChatModel
| AzureOpenAIChatModel
| NativeOpenAIEmbeddingModel
| AzureOpenAIEmbeddingModel
| OllamaModel
)
16 changes: 12 additions & 4 deletions app/llm/wrapper/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ def __subclasshook__(cls, subclass) -> bool:
@abstractmethod
def complete(self, prompt: str, arguments: CompletionArguments) -> str:
"""Create a completion from the prompt"""
raise NotImplementedError(f"The LLM {self.__str__()} does not support completion")
raise NotImplementedError(
f"The LLM {self.__str__()} does not support completion"
)


class ChatModel(LanguageModel, metaclass=ABCMeta):
Expand All @@ -34,9 +36,13 @@ def __subclasshook__(cls, subclass) -> bool:
return hasattr(subclass, "chat") and callable(subclass.chat)

@abstractmethod
def chat(self, messages: list[IrisMessage], arguments: CompletionArguments) -> IrisMessage:
def chat(
self, messages: list[IrisMessage], arguments: CompletionArguments
) -> IrisMessage:
"""Create a completion from the chat messages"""
raise NotImplementedError(f"The LLM {self.__str__()} does not support chat completion")
raise NotImplementedError(
f"The LLM {self.__str__()} does not support chat completion"
)


class EmbeddingModel(LanguageModel, metaclass=ABCMeta):
Expand All @@ -49,4 +55,6 @@ def __subclasshook__(cls, subclass) -> bool:
@abstractmethod
def embed(self, text: str) -> list[float]:
"""Create an embedding from the text"""
raise NotImplementedError(f"The LLM {self.__str__()} does not support embeddings")
raise NotImplementedError(
f"The LLM {self.__str__()} does not support embeddings"
)
8 changes: 6 additions & 2 deletions app/llm/wrapper/ollama.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@ def complete(self, prompt: str, arguments: CompletionArguments) -> str:
response = self._client.generate(model=self.model, prompt=prompt)
return response["response"]

def chat(self, messages: list[IrisMessage], arguments: CompletionArguments) -> IrisMessage:
response = self._client.chat(model=self.model, messages=convert_to_ollama_messages(messages))
def chat(
self, messages: list[IrisMessage], arguments: CompletionArguments
) -> IrisMessage:
response = self._client.chat(
model=self.model, messages=convert_to_ollama_messages(messages)
)
return convert_to_iris_message(response["message"])

def embed(self, text: str) -> list[float]:
Expand Down
12 changes: 9 additions & 3 deletions app/llm/wrapper/openai_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
from llm.wrapper.model import ChatModel


def convert_to_open_ai_messages(messages: list[IrisMessage]) -> list[ChatCompletionMessageParam]:
return [{"role": message.role.value, "content": message.text} for message in messages]
def convert_to_open_ai_messages(
messages: list[IrisMessage],
) -> list[ChatCompletionMessageParam]:
return [
{"role": message.role.value, "content": message.text} for message in messages
]


def convert_to_iris_message(message: ChatCompletionMessage) -> IrisMessage:
Expand All @@ -24,7 +28,9 @@ class OpenAIChatModel(ChatModel):
api_key: str
_client: OpenAI

def chat(self, messages: list[IrisMessage], arguments: CompletionArguments) -> IrisMessage:
def chat(
self, messages: list[IrisMessage], arguments: CompletionArguments
) -> IrisMessage:
response = self._client.chat.completions.create(
model=self.model,
messages=convert_to_open_ai_messages(messages),
Expand Down

0 comments on commit 63aaf03

Please sign in to comment.