Skip to content

Commit 3ff9af3

Browse files
authored
RAG workaround for models that do not support structured output (#160)
1 parent 40832be commit 3ff9af3

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/server/agents/chatbot.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
from langchain_core.documents.base import Document
1414
from langchain_core.messages import SystemMessage, ToolMessage
15-
from langchain_core.output_parsers import StrOutputParser
15+
from langchain_core.output_parsers import StrOutputParser, PydanticOutputParser
1616
from langchain_core.prompts import PromptTemplate
1717
from langchain_core.runnables import RunnableConfig
1818
from langchain_community.vectorstores.oraclevs import OracleVS
@@ -233,7 +233,12 @@ class Grade(BaseModel):
233233
if config["metadata"]["rag_settings"].grading:
234234
# LLM (Bound to Tool)
235235
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
237242

238243
# Prompt
239244
grade_template = """
@@ -257,13 +262,13 @@ class Grade(BaseModel):
257262
question = state["context_input"]
258263
logger.debug("Grading %s against Documents: %s", question, documents)
259264
chain = grader | llm_with_grader
260-
scored_result = chain.invoke({"question": question, "context": documents})
261265
try:
266+
scored_result = chain.invoke({"question": question, "context": documents})
262267
logger.info("Grading completed.")
263268
score = scored_result.binary_score
264269
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"
267272
else:
268273
logger.info("RAG Grading disabled; marking all results relevant.")
269274
score = "yes"

0 commit comments

Comments
 (0)