Skip to content

Commit e4ab6cb

Browse files
committed
test: add prints
1 parent a5887eb commit e4ab6cb

2 files changed

Lines changed: 12 additions & 8 deletions

File tree

compose.text2sparql.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@ services:
1616
env_file:
1717
- .env
1818
environment:
19-
- BENCH_MODEL=openrouter/openai/gpt-oss-120b
19+
# - BENCH_MODEL=openrouter/openai/gpt-oss-120b
20+
- BENCH_MODEL=openrouter/openai/gpt-5.2
2021
- PYTHONUNBUFFERED=1
2122
- VECTORDB_URL=http://vectordb:6334/
2223
# - DBPEDIA_URL=http://141.57.8.18:9081/sparql
2324
# - CORPORATE_URL=http://141.57.8.18:9080/sparql
2425
- DBPEDIA_URL=http://virtuoso-dbpedia:8890/sparql
2526
- CORPORATE_URL=http://virtuoso-corporate:8890/sparql
2627
entrypoint: uv run
27-
command: uvicorn tests.text2sparql.api:app --host 0.0.0.0 --port 8765 --workers 4
28+
command: uvicorn tests.text2sparql.api:app --host 0.0.0.0 --port 8765 --workers 2
2829

2930
virtuoso-dbpedia:
3031
image: openlink/virtuoso-opensource-7:latest

tests/text2sparql/api.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,12 @@ async def get_answer(question: str, dataset: str):
172172
except Exception:
173173
resp_msg += "## No SPARQL query could be extracted from the model response. Please provide a valid SPARQL query based on the provided information and try again.\n"
174174
if generated_sparql != "":
175+
print(f"[Try {num_of_tries}] Generated SPARQL:\n{generated_sparql}\n")
175176
try:
176177
res = query_sparql(generated_sparql, endpoint_url)
177-
if res.get("results", {}).get("bindings"):
178-
# break
178+
bindings = res.get("results", {}).get("bindings", [])
179+
print(f"[Try {num_of_tries}] Query returned {len(bindings)} result(s)")
180+
if bindings:
179181
# Ask the LLM if the results actually answer the original question
180182
results_preview = str(res)
181183
with contextlib.suppress(Exception):
@@ -188,7 +190,7 @@ async def get_answer(question: str, dataset: str):
188190
f'The following SPARQL query was executed to answer this question: "{question}"\n\n'
189191
f"```sparql\n{generated_sparql}\n```\n\n"
190192
f"It returned these results:\n```json\n{results_preview}\n```\n\n"
191-
"Do these results satisfactorily answer the original question? Regarding the endpoint content"
193+
"Do these results satisfactorily answer the original question?\n"
192194
'Reply with only "YES" or "NO" on the first line, optionally followed by a brief explanation of what is wrong.'
193195
)
194196
)
@@ -201,12 +203,10 @@ async def get_answer(question: str, dataset: str):
201203
total_output_tokens += satisfaction_response.model_dump()["response_metadata"]["token_usage"][
202204
"completion_tokens"
203205
]
206+
print(f"[Try {num_of_tries}] Satisfaction check: {satisfaction_content[:300]}")
204207

205208
if satisfaction_content.upper().startswith("YES"):
206209
# Successfully generated a query with satisfactory results
207-
# if num_of_tries > 0:
208-
# for msg in messages:
209-
# print(f"{msg.type}: {msg.content}\n")
210210
break
211211
else:
212212
resp_msg += (
@@ -219,18 +219,21 @@ async def get_answer(question: str, dataset: str):
219219
raise Exception("No results")
220220

221221
except Exception as e:
222+
print(f"[Try {num_of_tries}] Query failed: {e}")
222223
validation_output = validate_sparql(
223224
query=generated_sparql, endpoint_url=endpoint_url, endpoints_void_dict=SCHEMAS.get(dataset, {})
224225
)
225226
if validation_output["errors"]:
226227
error_str = "- " + "\n- ".join(validation_output["errors"])
228+
print(f"[Try {num_of_tries}] Validation errors: {error_str[:300]}")
227229
resp_msg += f"## SPARQL query not valid. Please fix the query based on the provided information and try again.\n### Erroneous SPARQL query\n```sparql\n{validation_output['original_query']}\n```\n### Validation Errors:\n{error_str}\n"
228230
else:
229231
resp_msg += f"## SPARQL query returned error: {e}. Please provide an alternative query based on the provided information and try again.\n### Erroneous SPARQL query\n```sparql\n{generated_sparql}\n```\n"
230232

231233
num_of_tries += 1
232234
if num_of_tries == settings.default_max_try_fix_sparql:
233235
print(f"❌ Could not fix generate SPARQL query for question: {question} \n")
236+
break
234237

235238
# If no valid SPARQL query was generated, ask the model to fix it
236239
messages.append(HumanMessage(content=question + resp_msg))

0 commit comments

Comments
 (0)