Skip to content
Merged
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
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
from datasets import load_dataset
from datetime import date

# ── config ────────────────────────────────────────────────────────────────────
REPO_ID = "pavanmantha/doctor_patient_conversation"
COLLECTION = "doctor_patient_conversation"
OUTPUT_FILE = "data_sets/source_data.qql"
TODAY = date.today().isoformat() # e.g. 2026-05-16
BATCH_SIZE = 200 # rows per INSERT block (stays under 33MB)
# ─────────────────────────────────────────────────────────────────────────────


def escape(text: str) -> str:
"""Escape single-quotes inside field values."""
return text.replace("\\", "\\\\").replace("'", "\\'")


def build_record(row: dict) -> str:
description = escape((row.get("description") or "").strip())
text = escape((row.get("conversation") or "").strip())
status = escape((row.get("status") or "").strip())

return (
" {\n"
f" 'description': '{description}',\n"
f" 'text': '{text}',\n"
f" 'status': '{status}'\n"
" }"
)


def write_batch(f, batch: list, batch_num: int, collection: str):
f.write(f"\n-- Batch {batch_num} ({len(batch)} records)\n")
f.write(f"INSERT BULK INTO COLLECTION {collection} VALUES [\n")
for i, record in enumerate(batch):
is_last = (i == len(batch) - 1)
f.write(record)
f.write("\n" if is_last else ",\n")
f.write("]\n")


def main():
print(f"Loading dataset from '{REPO_ID}' ...")
ds = load_dataset(REPO_ID, split="train")
total = len(ds)
num_batches = (total + BATCH_SIZE - 1) // BATCH_SIZE
print(f" -> {total} rows loaded")
print(f" -> {BATCH_SIZE} rows per INSERT block")
print(f" -> {num_batches} total batches\n")

header = f"""\
-- Qdrant Query Language
-- BULK INSERT -- DOCTOR PATIENT CONVERSATION (BATCHED)

-- ============================================================
-- QQL -- Doctor Patient Conversation
-- Collection : {COLLECTION}
-- Source : {REPO_ID}
-- Total rows : {total}
-- Batch size : {BATCH_SIZE}
-- Generated : {TODAY}
-- ============================================================

-- Step 0: Show Collections
SHOW COLLECTIONS

-- Step 1: Create the collection
CREATE COLLECTION {COLLECTION}

-- ============================================================
-- BULK INSERT -- BATCHED (each block <= {BATCH_SIZE} records)
-- ============================================================
"""

print(f"Writing {OUTPUT_FILE} ...")
with open(OUTPUT_FILE, "w", encoding="utf-8") as f:
f.write(header)

batch = []
batch_num = 1

for i, row in enumerate(ds):
batch.append(build_record(row))

if len(batch) == BATCH_SIZE:
write_batch(f, batch, batch_num, COLLECTION)
print(f" batch {batch_num} written ({i + 1}/{total} rows)")
batch = []
batch_num += 1

# flush remaining rows
if batch:
write_batch(f, batch, batch_num, COLLECTION)
print(f" batch {batch_num} written ({total}/{total} rows)")

print(f"\nDone. '{OUTPUT_FILE}' written -- {total} records across {batch_num} INSERT blocks.")


if __name__ == "__main__":
main()

Large diffs are not rendered by default.

20,039 changes: 20,039 additions & 0 deletions examples_and_applications/healthcare_conversation_rag/data_sets/source_data.qql

Large diffs are not rendered by default.

46 changes: 46 additions & 0 deletions examples_and_applications/healthcare_conversation_rag/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from agno.agent import Agent
from agno.models.ollama import Ollama
from qql import Connection

def search_medical_records(question: str) -> str:
"""
Use this function when you need to retrieve relevant medical context
for a single natural language question from the 'medical_records'
collection stored in Qdrant via QQL.

This function performs a semantic similarity search against the
collection and aggregates the top matching document texts into a
single context string, which can then be passed to an LLM or used
for further processing (e.g. RAG pipelines, answer generation).

Args:
question (str): A single medical question in natural language.

Returns:
str: Aggregated context text from the top matching records.
Returns an empty string if no results are found or an
error occurs.
"""
LIMIT = 5
CONTEXT = ""

try:
with Connection(url="http://localhost:6333", secret="th3s3cr3tk3y") as conn:
query = f"SEARCH medical_records SIMILAR TO '{question}' LIMIT {LIMIT} WITH {{ hnsw_ef: 128, mmr_diversity: 0.5, mmr_candidates: 50}}"
result = conn.run_query(query=query)
for hit in result.data:
CONTEXT += hit["payload"]["text"]
except Exception as e:
print(f"Search failed for question: '{question}' | Error: {e}")

return CONTEXT

agent = Agent(
tools=[search_medical_records],
model=Ollama(id="qwen3.5:latest", host="http://localhost:11434", timeout=300),
markdown=True,
debug_mode=True,
reasoning=True,
enable_agentic_memory=True
)
agent.print_response("hi doctor I am just wondering what is abutting and abutment of the nerve root means in a back issue please explain what treatment is required for annular bulging and tear", stream=True)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[project]
name = "qql-application"
version = "0.1.0"
description = "QQL and its application"
requires-python = ">=3.13"
dependencies = [
"qql-cli>=2.4.1",
"datasets>=4.8.5",
"agno>=2.6.7",
"ollama>=0.6.2",
"openai>=2.37.0"
]
Loading