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
86 changes: 86 additions & 0 deletions tests/local_agent_runner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import os
import json
import re
from dataclasses import dataclass
from typing import List, Optional

# 1. DYNAMIC PATH LOADING (Finds json in the same folder)
def load_local_data():
"""
Loads local_knowledge.json from the SAME directory as this script.
"""
# Get the directory where THIS script is located
current_dir = os.path.dirname(os.path.abspath(__file__))

# Build the full path to the json file
json_path = os.path.join(current_dir, "local_knowledge.json")

print(f" Loading local data from: {json_path}")

if not os.path.exists(json_path):
print(" Error: File not found.")
return []

try:
with open(json_path, "r", encoding="utf-8") as f:
return json.load(f)
except Exception as e:
print(f" Error reading JSON: {e}")
return []

# 2. OFFLINE SEARCH LOGIC (BM25)
@dataclass
class RetrievedItem:
id: str
title_guess: str
content: str
similarity: float

def local_offline_search(query: str):
data = load_local_data()
if not data:
return []

print(f" Searching for: '{query}'...")

query_words = set(re.findall(r'\w+', query.lower()))
results = []

for item in data:
score = 0.0
title_text = item.get("title", "")
content_text = item.get("content", "")

# Simple scoring logic
for word in query_words:
if word in title_text.lower():
score += 1.5
if word in content_text.lower():
score += 1.0

if score > 0:
results.append(RetrievedItem(
id=str(item.get("id")),
title_guess=title_text,
content=content_text,
similarity=float(score)
))

# Sort results
results.sort(key=lambda x: x.similarity, reverse=True)
return results[:5]

# 3.TEST RUNNER
if __name__ == "__main__":
print(" STARTING OFFLINE AGENT TEST ")

# Test Query
test_query = "Alzheimer"
hits = local_offline_search(test_query)

if not hits:
print(" No results found. Check your json file content.")

for hit in hits:
print(f"\n Found: {hit.title_guess} (Score: {hit.similarity})")
print(f" Context: {hit.content[:100]}...")
44 changes: 44 additions & 0 deletions tests/local_knowledge.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
[
{
"id": "1",
"title": "fMRI Study of Alzheimer's Disease",
"content": "This dataset contains functional MRI scans of 50 patients with early-stage Alzheimer's. The study focuses on hippocampal atrophy and memory loss.",
"metadata": {"year": 2023, "modality": "fMRI"}
},
{
"id": "2",
"title": "Mouse Brain Atlas - Allen Institute",
"content": "A high-resolution map of the mouse brain. Includes gene expression data and cortical mapping layers for spatial transcriptomics.",
"metadata": {"year": 2022, "species": "Mouse"}
},
{
"id": "3",
"title": "EEG Recordings during Sleep",
"content": "Raw EEG data collected during REM sleep cycles. Useful for studying sleep disorders and neural oscillations in humans.",
"metadata": {"year": 2024, "modality": "EEG"}
},
{
"id": "4",
"title": "Visual Cortex Neuron Spiking",
"content": "Single-unit recordings from the visual cortex of macaque monkeys. Data shows spike trains in response to visual stimuli.",
"metadata": {"year": 2021, "species": "Macaque"}
},
{
"id": "5",
"title": "Drosophila Melanogaster Genetics",
"content": "Genetic sequencing data of the fruit fly (Drosophila). Focuses on hereditary traits and mutation patterns.",
"metadata": {"year": 2020, "species": "Fruit Fly"}
},
{
"id": "6",
"title": "Genetic Risk Factors for Alzheimer's",
"content": "A study on the APOE4 gene variant and its correlation with late-onset Alzheimer's disease. Includes blood sample analysis.",
"metadata": {"year": 2019, "modality": "Genetics"}
},
{
"id": "7",
"title": "Mouse Visual Cortex Calcium Imaging",
"content": "Two-photon calcium imaging of neuronal activity in the mouse visual cortex during visual stimulation.",
"metadata": {"year": 2023, "species": "Mouse"}
}
]