Skip to content

Commit

Permalink
fix: Import errors an syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
kaancayli committed Nov 11, 2024
1 parent a1c2690 commit a863e9e
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 53 deletions.
4 changes: 0 additions & 4 deletions app/common/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
from app.common.singleton import Singleton
from app.common.message_converters import (
convert_iris_message_to_langchain_message,
convert_langchain_message_to_iris_message,
)
10 changes: 5 additions & 5 deletions app/common/message_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
ToolCall,
)

from app.domain.data.text_message_content_dto import TextMessageContentDTO
from app.domain.data.tool_call_dto import ToolCallDTO, FunctionDTO
from app.domain.data.tool_message_content_dto import ToolMessageContentDTO
from app.domain.pyris_message import (
from app.common.pyris_message import (
PyrisMessage,
IrisMessageRole,
PyrisAIMessage,
IrisMessageRole,
PyrisToolMessage,
)
from app.domain.data.text_message_content_dto import TextMessageContentDTO
from app.domain.data.tool_call_dto import ToolCallDTO, FunctionDTO
from app.domain.data.tool_message_content_dto import ToolMessageContentDTO


def convert_iris_message_to_langchain_message(
Expand Down
4 changes: 2 additions & 2 deletions app/common/pyris_message.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from datetime import datetime
from enum import Enum
from typing import List, Union, Optional
from typing import List, Optional

from pydantic import BaseModel, ConfigDict, Field

Expand All @@ -25,7 +25,7 @@ class PyrisMessage(BaseModel):
sent_at: datetime | None = Field(alias="sentAt", default=None)
sender: IrisMessageRole

contents: Union[str, List[MessageContentDTO]] = ""
contents: List[MessageContentDTO] = Field(default=[])

def __str__(self):
return f"{self.sender.lower()}: {self.contents}"
Expand Down
6 changes: 3 additions & 3 deletions app/domain/event/pyris_event_dto.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from typing import TypeVar, Generic
from typing import TypeVar, Generic, Optional

from pydantic import Field, BaseModel

T = TypeVar("T")


class PyrisEventDTO(BaseModel, Generic[T]):
event_type: str = Field(None, alias="eventType")
event: T = Field(None, alias="event")
event_type: Optional[str] = Field(default=None, alias="eventType")
event: Optional[T] = Field(default=None, alias="event")
42 changes: 13 additions & 29 deletions app/llm/external/openai_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,23 @@
APIError,
APITimeoutError,
RateLimitError,
InternalServerError,
ContentFilterFinishReasonError,
)
from openai.lib.azure import AzureOpenAI
from openai.types import CompletionUsage
from openai.types.chat import ChatCompletionMessage, ChatCompletionMessageParam
from openai.types.chat.completion_create_params import ResponseFormat
from openai.types.shared_params import ResponseFormatJSONObject
from pydantic import Field
from pydantic.v1 import BaseModel as LegacyBaseModel

from ...common.message_converters import map_str_to_role, map_role_to_str
from app.domain.data.text_message_content_dto import TextMessageContentDTO
from ...common.pyris_message import PyrisMessage
from ...common.pyris_message import PyrisMessage, PyrisAIMessage
from ...common.token_usage_dto import TokenUsageDTO
from ...domain.data.image_message_content_dto import ImageMessageContentDTO
from ...domain.data.json_message_content_dto import JsonMessageContentDTO
from ...domain.data.tool_call_dto import ToolCallDTO
from ...domain.data.tool_message_content_dto import ToolMessageContentDTO
from ...domain.pyris_message import PyrisAIMessage
from ...llm import CompletionArguments
from ...llm.external.model import ChatModel

Expand Down Expand Up @@ -147,14 +145,12 @@ def convert_to_iris_message(
)



class OpenAIChatModel(ChatModel):
model: str
api_key: str
tools: Optional[
Sequence[Union[Dict[str, Any], Type[LegacyBaseModel], Callable, BaseTool]]
] = Field(default_factory=list, alias="tools")
_client: OpenAI

def chat(
self, messages: list[PyrisMessage], arguments: CompletionArguments
Expand All @@ -172,33 +168,37 @@ def chat(
try:
if arguments.response_format == "JSON":
if self.tools:
response = self._client.chat.completions.create(
response = client.chat.completions.create(
model=self.model,
messages=messages,
temperature=arguments.temperature,
max_tokens=arguments.max_tokens,
response_format=ResponseFormatJSONObject(type="json_object"),
response_format=ResponseFormatJSONObject(
type="json_object"
),
tools=self.tools,
)
else:
response = self._client.chat.completions.create(
response = client.chat.completions.create(
model=self.model,
messages=messages,
temperature=arguments.temperature,
max_tokens=arguments.max_tokens,
response_format=ResponseFormatJSONObject(type="json_object"),
response_format=ResponseFormatJSONObject(
type="json_object"
),
)
else:
if self.tools:
response = self._client.chat.completions.create(
response = client.chat.completions.create(
model=self.model,
messages=messages,
temperature=arguments.temperature,
max_tokens=arguments.max_tokens,
tools=self.tools,
)
else:
response = self._client.chat.completions.create(
response = client.chat.completions.create(
model=self.model,
messages=messages,
temperature=arguments.temperature,
Expand Down Expand Up @@ -231,7 +231,7 @@ def bind_tools(
Union[Dict[str, Any], Type[LegacyBaseModel], Callable, BaseTool]
],
):
self.tools = tools
self.tools = [convert_to_openai_tool(tool) for tool in tools]


class DirectOpenAIChatModel(OpenAIChatModel):
Expand All @@ -243,14 +243,6 @@ def get_client(self) -> OpenAI:
def __str__(self):
return f"OpenAIChat('{self.model}')"

def bind_tools(
self,
tools: Sequence[
Union[Dict[str, Any], Type[LegacyBaseModel], Callable, BaseTool]
],
):
self.tools = [convert_to_openai_tool(tool) for tool in tools]


class AzureOpenAIChatModel(OpenAIChatModel):
type: Literal["azure_chat"]
Expand All @@ -266,13 +258,5 @@ def get_client(self) -> OpenAI:
api_key=self.api_key,
)

def bind_tools(
self,
tools: Sequence[
Union[Dict[str, Any], Type[LegacyBaseModel], Callable, BaseTool]
],
):
self.tools = [convert_to_openai_tool(tool) for tool in tools]

def __str__(self):
return f"AzureChat('{self.model}')"
6 changes: 3 additions & 3 deletions app/llm/langchain/iris_langchain_chat_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
from pydantic import BaseModel

from app.common.PipelineEnum import PipelineEnum
from ...common import (
convert_iris_message_to_langchain_message,
from app.common.token_usage_dto import TokenUsageDTO
from ...common.message_converters import (
convert_langchain_message_to_iris_message,
convert_iris_message_to_langchain_message,
)
from app.common.token_usage_dto import TokenUsageDTO
from ...llm import RequestHandler, CompletionArguments


Expand Down
2 changes: 1 addition & 1 deletion app/pipeline/chat/course_chat_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
)
from .lecture_chat_pipeline import LectureChatPipeline
from ..shared.citation_pipeline import CitationPipeline
from ...common import convert_iris_message_to_langchain_message
from ...common.message_converters import convert_iris_message_to_langchain_message
from ...common.pyris_message import PyrisMessage
from ...domain.data.metrics.competency_jol_dto import CompetencyJolDTO
from ...llm import CapabilityRequestHandler, RequirementList
Expand Down
4 changes: 2 additions & 2 deletions app/pipeline/chat/exercise_chat_agent_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
from ..shared.citation_pipeline import CitationPipeline
from ..shared.reranker_pipeline import RerankerPipeline
from ...common.message_converters import convert_iris_message_to_langchain_human_message
from ...domain import ExerciseChatPipelineExecutionDTO, IrisMessageRole
from ...domain import PyrisMessage
from ...common.pyris_message import PyrisMessage, IrisMessageRole
from ...domain import ExerciseChatPipelineExecutionDTO
from ...domain.chat.interaction_suggestion_dto import (
InteractionSuggestionPipelineExecutionDTO,
)
Expand Down
2 changes: 1 addition & 1 deletion app/pipeline/chat/interaction_suggestion_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
from langsmith import traceable
from pydantic.v1 import Field, BaseModel

from ...common import convert_iris_message_to_langchain_message
from app.domain.chat.interaction_suggestion_dto import (
InteractionSuggestionPipelineExecutionDTO,
)
from app.common.token_usage_dto import TokenUsageDTO
from ...common.message_converters import convert_iris_message_to_langchain_message
from ...common.pyris_message import PyrisMessage
from ...llm import CapabilityRequestHandler, RequirementList
from ..prompts.iris_interaction_suggestion_prompts import (
Expand Down
2 changes: 1 addition & 1 deletion app/pipeline/chat/lecture_chat_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from langsmith import traceable

from ..shared.citation_pipeline import CitationPipeline
from ...common import convert_iris_message_to_langchain_message
from ...common.message_converters import convert_iris_message_to_langchain_message
from ...common.pyris_message import PyrisMessage
from ...domain.chat.lecture_chat.lecture_chat_pipeline_execution_dto import (
LectureChatPipelineExecutionDTO,
Expand Down
2 changes: 1 addition & 1 deletion app/retrieval/lecture_retrieval.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
from weaviate import WeaviateClient
from weaviate.classes.query import Filter

from ..common import convert_iris_message_to_langchain_message
from app.common.token_usage_dto import TokenUsageDTO
from app.common.PipelineEnum import PipelineEnum
from ..common.message_converters import convert_iris_message_to_langchain_message
from ..common.pyris_message import PyrisMessage
from ..llm.langchain import IrisLangchainChatModel
from ..pipeline import Pipeline
Expand Down
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ weaviate-client==4.7.1
langchain_openai==0.1.19
starlette~=0.37.2
langsmith~=0.1.75
langgraph~=0.1.17
langgraph~=0.1.17
langchain-core~=0.2.41
langchain-text-splitters~=0.2.1

0 comments on commit a863e9e

Please sign in to comment.