From a863e9e33431cdbe0ed8e8f7330fd294753dcb9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kaan=20=C3=87ayl=C4=B1?= <38523756+kaancayli@users.noreply.github.com> Date: Mon, 11 Nov 2024 23:50:47 +0100 Subject: [PATCH] fix: Import errors an syntax --- app/common/__init__.py | 4 -- app/common/message_converters.py | 10 ++--- app/common/pyris_message.py | 4 +- app/domain/event/pyris_event_dto.py | 6 +-- app/llm/external/openai_chat.py | 42 ++++++------------- .../langchain/iris_langchain_chat_model.py | 6 +-- app/pipeline/chat/course_chat_pipeline.py | 2 +- .../chat/exercise_chat_agent_pipeline.py | 4 +- .../chat/interaction_suggestion_pipeline.py | 2 +- app/pipeline/chat/lecture_chat_pipeline.py | 2 +- app/retrieval/lecture_retrieval.py | 2 +- requirements.txt | 4 +- 12 files changed, 35 insertions(+), 53 deletions(-) diff --git a/app/common/__init__.py b/app/common/__init__.py index b2186c1d..7492ca1d 100644 --- a/app/common/__init__.py +++ b/app/common/__init__.py @@ -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, -) diff --git a/app/common/message_converters.py b/app/common/message_converters.py index 6347b872..28b27840 100644 --- a/app/common/message_converters.py +++ b/app/common/message_converters.py @@ -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( diff --git a/app/common/pyris_message.py b/app/common/pyris_message.py index 630e71a2..fe5906d7 100644 --- a/app/common/pyris_message.py +++ b/app/common/pyris_message.py @@ -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 @@ -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}" diff --git a/app/domain/event/pyris_event_dto.py b/app/domain/event/pyris_event_dto.py index 4e9855eb..0a2f39fc 100644 --- a/app/domain/event/pyris_event_dto.py +++ b/app/domain/event/pyris_event_dto.py @@ -1,4 +1,4 @@ -from typing import TypeVar, Generic +from typing import TypeVar, Generic, Optional from pydantic import Field, BaseModel @@ -6,5 +6,5 @@ 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") diff --git a/app/llm/external/openai_chat.py b/app/llm/external/openai_chat.py index 1cf54401..8f1d49ae 100644 --- a/app/llm/external/openai_chat.py +++ b/app/llm/external/openai_chat.py @@ -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 @@ -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 @@ -172,25 +168,29 @@ 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, @@ -198,7 +198,7 @@ def chat( tools=self.tools, ) else: - response = self._client.chat.completions.create( + response = client.chat.completions.create( model=self.model, messages=messages, temperature=arguments.temperature, @@ -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): @@ -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"] @@ -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}')" diff --git a/app/llm/langchain/iris_langchain_chat_model.py b/app/llm/langchain/iris_langchain_chat_model.py index c02dce9d..54970ae1 100644 --- a/app/llm/langchain/iris_langchain_chat_model.py +++ b/app/llm/langchain/iris_langchain_chat_model.py @@ -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 diff --git a/app/pipeline/chat/course_chat_pipeline.py b/app/pipeline/chat/course_chat_pipeline.py index 290793f0..b5b976ca 100644 --- a/app/pipeline/chat/course_chat_pipeline.py +++ b/app/pipeline/chat/course_chat_pipeline.py @@ -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 diff --git a/app/pipeline/chat/exercise_chat_agent_pipeline.py b/app/pipeline/chat/exercise_chat_agent_pipeline.py index c47c3865..ea346b9f 100644 --- a/app/pipeline/chat/exercise_chat_agent_pipeline.py +++ b/app/pipeline/chat/exercise_chat_agent_pipeline.py @@ -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, ) diff --git a/app/pipeline/chat/interaction_suggestion_pipeline.py b/app/pipeline/chat/interaction_suggestion_pipeline.py index e463bad5..d792e81a 100644 --- a/app/pipeline/chat/interaction_suggestion_pipeline.py +++ b/app/pipeline/chat/interaction_suggestion_pipeline.py @@ -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 ( diff --git a/app/pipeline/chat/lecture_chat_pipeline.py b/app/pipeline/chat/lecture_chat_pipeline.py index 22eb8c7a..cd17c9cb 100644 --- a/app/pipeline/chat/lecture_chat_pipeline.py +++ b/app/pipeline/chat/lecture_chat_pipeline.py @@ -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, diff --git a/app/retrieval/lecture_retrieval.py b/app/retrieval/lecture_retrieval.py index 7bcd8ce0..b681b589 100644 --- a/app/retrieval/lecture_retrieval.py +++ b/app/retrieval/lecture_retrieval.py @@ -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 diff --git a/requirements.txt b/requirements.txt index 1ac07b29..2fe18cef 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 \ No newline at end of file +langgraph~=0.1.17 +langchain-core~=0.2.41 +langchain-text-splitters~=0.2.1 \ No newline at end of file