Skip to content

feat: update generate answer #778

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions scrapegraphai/nodes/generate_answer_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
GenerateAnswerNode Module
"""
from typing import List, Optional
from json.decoder import JSONDecodeError
from langchain.prompts import PromptTemplate
from langchain_core.output_parsers import JsonOutputParser
from langchain_core.runnables import RunnableParallel
Expand Down Expand Up @@ -121,9 +122,21 @@ def execute(self, state: dict) -> dict:
partial_variables={"context": doc, "format_instructions": format_instructions}
)
chain = prompt | self.llm_model
raw_response = str((prompt | self.llm_model).invoke({"question": user_prompt}))

if output_parser:
chain = chain | output_parser
answer = chain.invoke({"question": user_prompt})
try:
answer = output_parser.parse(raw_response)
except JSONDecodeError:
lines = raw_response.split('\n')
if lines[0].strip().startswith('```'):
lines = lines[1:]
if lines[-1].strip().endswith('```'):
lines = lines[:-1]
cleaned_response = '\n'.join(lines)
answer = output_parser.parse(cleaned_response)
else:
answer = raw_response

state.update({self.output[0]: answer})
return state
Expand Down
Loading