Skip to content
Open
Show file tree
Hide file tree
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
5 changes: 3 additions & 2 deletions scrapegraphai/nodes/generate_answer_node_k_level.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,18 @@ def execute(self, state: dict) -> dict:
):
prompt = PromptTemplate(
template=template_chunks_prompt,
input_variables=["format_instructions"],
input_variables=["question"],
partial_variables={
"content": chunk.get("document"),
"chunk_id": i + 1,
"format_instructions": format_instructions,
},
)
chain_name = f"chunk{i + 1}"
chains_dict[chain_name] = prompt | self.llm_model

async_runner = RunnableParallel(**chains_dict)
batch_results = async_runner.invoke({"format_instructions": user_prompt})
batch_results = async_runner.invoke({"question": user_prompt})

merge_prompt = PromptTemplate(
template=template_merge_prompt,
Expand Down
2 changes: 2 additions & 0 deletions scrapegraphai/prompts/generate_answer_node_prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
and things that will invalidate the dictionary. \n
Do not start the response with ```json because it will invalidate the postprocessing. \n
OUTPUT INSTRUCTIONS: {format_instructions}\n
USER QUESTION: {question}
Content of {chunk_id}: {content}. \n
"""

Expand Down Expand Up @@ -56,6 +57,7 @@
and things that will invalidate the dictionary. \n
Do not start the response with ```json because it will invalidate the postprocessing. \n
OUTPUT INSTRUCTIONS: {format_instructions}\n
USER QUESTION: {question}
Content of {chunk_id}: {content}. \n
"""

Expand Down
67 changes: 67 additions & 0 deletions tests/test_chunk_prompt_question.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import pytest
from langchain_core.prompts import PromptTemplate
from scrapegraphai import prompts


@pytest.mark.parametrize(
"template_name",
["TEMPLATE_CHUNKS_MD", "TEMPLATE_CHUNKS"],
)
def test_chunk_prompts_include_user_question(template_name):
"""Ensure each chunk receives the user's extraction question."""
from langchain_core.prompts import PromptTemplate
from scrapegraphai import prompts

template = getattr(prompts, template_name)

prompt = PromptTemplate(
template=template,
input_variables=["question"],
partial_variables={
"content": "Django and Flask are Python web frameworks.",
"chunk_id": 1,
"format_instructions": "Return valid JSON.",
},
)

rendered = prompt.format(question="Which Python frameworks are mentioned?")

assert "USER QUESTION: Which Python frameworks are mentioned?" in rendered
assert "Django and Flask" in rendered


def test_k_level_chunk_receives_question(monkeypatch):
"""Verify K-level chunk processing includes the question and JSON instructions."""
from langchain_core.runnables import RunnableLambda
from scrapegraphai.nodes.generate_answer_node_k_level import GenerateAnswerNodeKLevel

captured_prompts = []

def fake_llm(prompt):
captured_prompts.append(prompt.to_string())
return '{"content": "Django"}'

class FakeResult:
id = 1
score = 0.9

class FakeDB:
def query(self, collection_name, query_text):
return [FakeResult()]

node = GenerateAnswerNodeKLevel(
"user_prompt & docs & vectorial_db",
["answer"],
node_config={"llm_model": RunnableLambda(fake_llm), "verbose": False},
)

node.execute({
"user_prompt": "Which Python frameworks are mentioned?",
"docs": [{"document": "Django is a Python framework."}],
"vectorial_db": FakeDB(),
})

assert captured_prompts
assert "USER QUESTION: Which Python frameworks are mentioned?" in captured_prompts[0]
assert "OUTPUT INSTRUCTIONS:" in captured_prompts[0]
assert "OUTPUT INSTRUCTIONS: Return a JSON object." in captured_prompts[0]