14
14
15
15
import logging
16
16
from abc import ABC , abstractmethod
17
- from typing import Generic , List , Literal , Optional , TypeVar
17
+ from typing import Any , Generic , List , Literal , Optional , TypeVar
18
18
19
19
from langchain_core .messages import BaseMessage
20
20
from langchain_core .runnables .config import DEFAULT_RECURSION_LIMIT
28
28
IMAGE_REASONING_SYSTEM_PROMPT = "You are a helpful and knowledgeable AI assistant that specializes in interpreting and analyzing visual content. Your task is to answer questions based on the images provided to you. Please response in requested structured output format."
29
29
30
30
31
- class LangchainRawOutputModel (BaseModel ):
32
- """
33
- A Pydantic model for wrapping Langchain message parsing results from a structured output agent. See documentation for more details:
34
- https://github.com/langchain-ai/langchain/blob/02001212b0a2b37d90451d8493089389ea220cab/libs/core/langchain_core/language_models/chat_models.py#L1430-L1432
35
-
36
-
37
- Attributes
38
- ----------
39
- raw : BaseMessage
40
- The original raw message object from Langchain before parsing.
41
- parsed : BaseModel
42
- The parsed and validated Pydantic model instance derived from the raw message.
43
- parsing_error : Optional[BaseException]
44
- Any exception that occurred during the parsing process, None if parsing
45
- was successful.
46
- """
47
-
48
- model_config = ConfigDict (arbitrary_types_allowed = True )
49
- raw : BaseMessage
50
- parsed : BaseModel
51
- parsing_error : Optional [BaseException ]
52
-
53
-
54
31
class TaskValidationError (Exception ):
55
32
pass
56
33
@@ -78,7 +55,30 @@ class ImageReasoningAnswer(BaseModel, Generic[AnswerT]):
78
55
justification : str = Field (..., description = "Justification for the answer." )
79
56
80
57
81
- class ImageReasoningTask (ABC , Generic [BaseModelT ]):
58
+ class LangchainRawOutputModel (BaseModel ):
59
+ """
60
+ A Pydantic model for wrapping Langchain message parsing results from a structured output agent. See documentation for more details:
61
+ https://github.com/langchain-ai/langchain/blob/02001212b0a2b37d90451d8493089389ea220cab/libs/core/langchain_core/language_models/chat_models.py#L1430-L1432
62
+
63
+
64
+ Attributes
65
+ ----------
66
+ raw : BaseMessage
67
+ The original raw message object from Langchain before parsing.
68
+ parsed : BaseModel
69
+ The parsed and validated Pydantic model instance derived from the raw message.
70
+ parsing_error : Optional[BaseException]
71
+ Any exception that occurred during the parsing process, None if parsing
72
+ was successful.
73
+ """
74
+
75
+ model_config = ConfigDict (arbitrary_types_allowed = True )
76
+ raw : BaseMessage
77
+ parsed : ImageReasoningAnswer [Any ]
78
+ parsing_error : Optional [BaseException ]
79
+
80
+
81
+ class ImageReasoningTask (ABC , Generic [AnswerT ]):
82
82
complexity : Literal ["easy" , "medium" , "hard" ]
83
83
recursion_limit : int = DEFAULT_RECURSION_LIMIT
84
84
@@ -103,13 +103,14 @@ def __init__(
103
103
self .logger = logging .getLogger (__name__ )
104
104
self .question : str
105
105
self .images_paths : List [str ]
106
+ # TODO move here task input
106
107
107
108
def set_logger (self , logger : loggers_type ):
108
109
self .logger = logger
109
110
110
111
@property
111
112
@abstractmethod
112
- def structured_output (self ) -> type [BaseModelT ]:
113
+ def structured_output (self ) -> type [ImageReasoningAnswer [ AnswerT ] ]:
113
114
"""Structured output that agent should return."""
114
115
pass
115
116
@@ -141,7 +142,7 @@ def get_prompt(self) -> str:
141
142
pass
142
143
143
144
@abstractmethod
144
- def validate (self , output : BaseModelT ) -> float :
145
+ def validate (self , output : ImageReasoningAnswer [ AnswerT ] ) -> float :
145
146
"""Validate result of the task."""
146
147
pass
147
148
@@ -158,7 +159,7 @@ def get_images(self) -> List[str]:
158
159
159
160
def get_structured_output_from_messages (
160
161
self , messages : List [BaseMessage ]
161
- ) -> BaseModelT | None :
162
+ ) -> ImageReasoningAnswer [ AnswerT ] | None :
162
163
"""Extract and validate structured output from a list of messages.
163
164
164
165
Iterates through messages in reverse order, attempting to find the message that is
0 commit comments