12
12
13
13
from langchain_core .documents .base import Document
14
14
from langchain_core .messages import SystemMessage , ToolMessage
15
- from langchain_core .output_parsers import StrOutputParser
15
+ from langchain_core .output_parsers import StrOutputParser , PydanticOutputParser
16
16
from langchain_core .prompts import PromptTemplate
17
17
from langchain_core .runnables import RunnableConfig
18
18
from langchain_community .vectorstores .oraclevs import OracleVS
@@ -233,7 +233,12 @@ class Grade(BaseModel):
233
233
if config ["metadata" ]["rag_settings" ].grading :
234
234
# LLM (Bound to Tool)
235
235
model = config ["configurable" ].get ("ll_client" , None )
236
- llm_with_grader = model .with_structured_output (Grade )
236
+ try :
237
+ llm_with_grader = model .with_structured_output (Grade )
238
+ except NotImplementedError :
239
+ logger .error ("Model does not support structured output" )
240
+ parser = PydanticOutputParser (pydantic_object = Grade )
241
+ llm_with_grader = model | parser
237
242
238
243
# Prompt
239
244
grade_template = """
@@ -257,13 +262,13 @@ class Grade(BaseModel):
257
262
question = state ["context_input" ]
258
263
logger .debug ("Grading %s against Documents: %s" , question , documents )
259
264
chain = grader | llm_with_grader
260
- scored_result = chain .invoke ({"question" : question , "context" : documents })
261
265
try :
266
+ scored_result = chain .invoke ({"question" : question , "context" : documents })
262
267
logger .info ("Grading completed." )
263
268
score = scored_result .binary_score
264
269
except Exception :
265
- logger .error ("LLM is not returning binary score in grader! " )
266
- score = "no "
270
+ logger .error ("LLM is not returning binary score in grader; marking all results relevant. " )
271
+ score = "yes "
267
272
else :
268
273
logger .info ("RAG Grading disabled; marking all results relevant." )
269
274
score = "yes"
0 commit comments