diff options
| author | Johannes Medagbe <johanmedagbe@gmail.com> | 2026-09-21 10:03:20 +0000 |
|---|---|---|
| committer | Johannes Medagbe <johanmedagbe@gmail.com> | 2026-09-22 12:18:32 +0000 |
| commit | dce53892085c02c8571d8a6495e39c449294aa5f (patch) | |
| tree | ae64d32cc845d30ec2a4cc3514a23dc5683a09a1 /aisearch/src | |
| download | genenetwork_ai_search-master.tar.gz | |
New: commit aisearch repo master
Diffstat (limited to 'aisearch/src')
21 files changed, 3951 insertions, 0 deletions
diff --git a/aisearch/src/gnais/__init__.py b/aisearch/src/gnais/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/aisearch/src/gnais/__init__.py diff --git a/aisearch/src/gnais/config.py b/aisearch/src/gnais/config.py new file mode 100644 index 0000000..6c1573c --- /dev/null +++ b/aisearch/src/gnais/config.py @@ -0,0 +1,120 @@ +__all__ = ("Config",) +import os + +import dspy +from dotenv import load_dotenv + +load_dotenv() + + +class Config: + CORPUS_PATH = os.environ.get("CORPUS_PATH") + if CORPUS_PATH is None: + raise RuntimeError("CORPUS_PATH is not set") + + DB_PATH = os.environ.get("DB_PATH") + if DB_PATH is None: + raise RuntimeError("DB_PATH is not set") + + MEM0_PATH = os.path.join(DB_PATH, "mem0_chroma") + + SEED = int(os.environ.get("SEED")) + if SEED is None: + raise RuntimeError("SEED is not set") + + DEFAULT_MODEL = os.environ.get("DEFAULT_MODEL") + if DEFAULT_MODEL is None: + raise RuntimeError("DEFAULT_MODEL is not set") + + ALTERNATIVE_MODEL = os.environ.get("ALTERNATIVE_MODEL") + if ALTERNATIVE_MODEL is None: + raise RuntimeError("ALTERNATIVE_MODEL is not set") + + MEMORY_MODEL = os.environ.get("MEMORY_MODEL") + if MEMORY_MODEL is None: + raise RuntimeError("MEMORY_MODEL is not set") + + MODEL_TYPE = int(os.environ.get("MODEL_TYPE")) + if MODEL_TYPE is None: + raise RuntimeError("MODEL_TYPE is not set") + if MODEL_TYPE not in ( + 1, + 2, + 3, + ): + raise ValueError("MODEL_TYPE must be 1, 2 or 3") + + API_KEY = os.environ.get("API_KEY") + if MODEL_TYPE and API_KEY is None: + raise RuntimeError("API_KEY is not set") + + SPARQL_ENDPOINT = os.environ.get("SPARQL_ENDPOINT") + if SPARQL_ENDPOINT is None: + raise RuntimeError("SPARQL_ENDPOINT is not set") + + AUTH_SERVER_URL = os.environ.get("AUTH_SERVER_URL") + if AUTH_SERVER_URL is None: + raise RuntimeError("AUTH_SERVER_URL is not set") + + SECRET_KEY = os.environ.get("SECRET_KEY") + if SECRET_KEY is None: + raise RuntimeError("SECRET_KEY is not set") + + PORT = os.environ.get("PORT") + if PORT is None: + raise RuntimeError("PORT for local model is not set") + + if MODEL_TYPE == 1: # only frontier models + DEFAULT_LLM = dspy.LM( + model=DEFAULT_MODEL, + api_key=API_KEY, + max_tokens=50_000, + temperature=1, + cache=False, # essential for evaluation; switch on for local deploy + verbose=False, + ) + ALTERNATIVE_LLM = dspy.LM( + model=ALTERNATIVE_MODEL, + api_key=API_KEY, + max_tokens=50_000, + temperature=1, + cache=False, # essential for evaluation; switch on for local deploy + verbose=False, + ) + elif MODEL_TYPE == 2: # only local models + DEFAULT_LLM = dspy.LM( + model=f"ollama_chat/{DEFAULT_MODEL}", + api_key="local", + api_base=f"http://localhost:{PORT}", + max_tokens=50_000, + temperature=1, + cache=False, # essential for evaluation; switch on for local deploy + verbose=False, + ) + ALTERNATIVE_LLM = dspy.LM( + model=f"ollama_chat/{ALTERNATIVE_MODEL}", + api_key="local", + api_base=f"http://localhost:{PORT}", + max_tokens=50_000, + temperature=1, + cache=False, # essential for evaluation; switch on for local deploy + verbose=False, + ) + elif MODEL_TYPE == 3: # smart combination of frontier and local models + DEFAULT_LLM = dspy.LM( + model=DEFAULT_MODEL, + api_key=API_KEY, + max_tokens=50_000, + temperature=1, + cache=False, # essential for evaluation; switch on for local deploy + verbose=False, + ) + ALTERNATIVE_LLM = dspy.LM( + model=f"ollama_chat/{ALTERNATIVE_MODEL}", + api_key="local", + api_base=f"http://localhost:{PORT}", + max_tokens=50_000, + temperature=1, + cache=False, # essential for evaluation; switch on for local deploy + verbose=False, + ) diff --git a/aisearch/src/gnais/evaluation/__init__.py b/aisearch/src/gnais/evaluation/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/aisearch/src/gnais/evaluation/__init__.py diff --git a/aisearch/src/gnais/evaluation/utils.py b/aisearch/src/gnais/evaluation/utils.py new file mode 100644 index 0000000..8e9e335 --- /dev/null +++ b/aisearch/src/gnais/evaluation/utils.py @@ -0,0 +1,159 @@ +"""Common utilities for evaluation""" + +import asyncio +import uuid +from concurrent.futures import ThreadPoolExecutor +from typing import Any + +import dspy +import pandas as pd + +from gnais.config import Config +from gnais.search.agent import agent_search +from gnais.search.classification import classify_search, extract_keywords +from gnais.search.corpus import create_ensemble_retriever, get_chroma_db, get_docs +from gnais.search.grag import graph_rag_search +from gnais.search.rag import rag_search +from gnais.search.ragent import hybrid_search + + +def get_dataset( + dataset_path: str, + column_names: list[str] = ["query", "answer"], +) -> list[dspy.Example]: + data = pd.read_csv( + dataset_path, + usecols=column_names, + ) + data_dicts = data[column_names].to_dict(orient="records") + evaluation_set = [ + dspy.Example({name: x[name] for name in column_names}).with_inputs( + column_names[0] + ) + for x in data_dicts + ] + return evaluation_set + + +def mark( + query: str, + generated_response: str, + true_response: str, +) -> tuple[float]: + + assess = dspy.ChainOfThought("question -> answer: int") + context = f"""Your task is to compare elements of response in the answer generated by AI to the ones in the true answer. + You must compare only informations that are strictly relevant to the query. + User query: {query}\nAI answer: {generated_response}\nTrue answer: {true_response}""" + + tp_question = f"{context}\nThe objective is to compute the number of true positives by comparing the generated answer and true answer. True positives are elements of response to the query that are mentioned in the generated answer and somewhat highlighted in the true answer as well. How many true positives do you count?" + tp_score = assess(question=tp_question).get("answer") + + fp_question = f"{context}\nThe objective is to compute the number of false positives by comparing the generated answer and true answer. False positives are explicit and highly relevant elements of response to the query that are present in the generated answer and are not supported in any ways by the true answer. They should not be covered by a reformulation of the true answer. How many false positives do you count?" + fp_score = assess(question=fp_question).get("answer") + + fn_question = f"{context}\nThe objective is to compute the number of false negatives by comparing the generated answer and true answer. False negatives are elements of response to the query that feature in the true answer and are not supported by the generated answer. How many false negatives do you count?" + fn_score = assess(question=fn_question).get("answer") + + print( + f"Question: {query}\nTrue answer: {true_response}\nAI answer: {generated_response}" + ) + print(f"TP: {tp_score}\nFP: {fp_score}\nFN: {fn_score}") + + tiny_term = 1e-10 + precision = tp_score / (tp_score + fp_score + tiny_term) + recall = tp_score / (tp_score + fn_score + tiny_term) + f1 = (2 * tp_score) / ((2 * tp_score) + fp_score + fn_score + tiny_term) + + return precision, recall, f1 + + +_TOOL_EXECUTOR = ThreadPoolExecutor(max_workers=1, thread_name_prefix="search") + + +def _run_async(async_fn, *args, **kwargs): + """Run async function in a separate thread with a fresh event loop""" + + def _worker(): + return asyncio.run(async_fn(*args, **kwargs)) + + return _TOOL_EXECUTOR.submit(_worker).result() + + +def rag_digest(query: str, memory: Any = None) -> str: + async def _run() -> str: + docs = get_docs(Config.CORPUS_PATH) + chroma_db = get_chroma_db(embed_model="Qwen/Qwen3-Embedding-0.6B") + decision = classify_search(query).get("decision") + retriever = create_ensemble_retriever( + chroma_db=chroma_db, + docs=docs, + keyword_weight=0.7 if decision == "keyword" else 0.5, + ) + + parts = [] + async for chunk in rag_search( + query=( + extract_keywords(query).get("keywords") + if decision == "keyword" + else query + ), + retriever=retriever, + memory=memory, + user_id=str(uuid.uuid4()), + ): + parts.append(str(chunk)) + return "".join(parts) + + return _run_async(_run) + + +def sparql_digest( + query: str, handler: Any, memory: Any = None, user_id: str = "default_user" +) -> str: + async def _run(): + output = "" + async for chunk in handler( + query=query, + sparql_url=Config.SPARQL_ENDPOINT, + memory=memory, + user_id=user_id, + ): + if isinstance(chunk, dict) and "final" in chunk: + final = chunk["final"] + output = final + print(final, end="", flush=True) + else: + output += str(chunk) + print(chunk, end="", flush=True) + print() + return output + + return _run_async(_run) + + +def graph_rag_digest(query: str, memory: Any = None) -> str: + return sparql_digest( + query, graph_rag_search, memory=memory, user_id=str(uuid.uuid4()) + ) + + +def agent_digest(query: str, memory: Any = None) -> str: + return sparql_digest(query, agent_search, memory=memory, user_id=str(uuid.uuid4())) + + +def hybrid_digest(query: str, memory: Any = None) -> str: + async def _run(): + final_html = None + async for event in hybrid_search( + query, memory=memory, user_id=str(uuid.uuid4()) + ): + source = event["source"] + kind = event["kind"] + content = event["content"] + if source == "hybrid" and kind == "final": + final_html = content + break + return final_html + + return _run_async(_run) diff --git a/aisearch/src/gnais/search/__init__.py b/aisearch/src/gnais/search/__init__.py new file mode 100644 index 0000000..5d2dccd --- /dev/null +++ b/aisearch/src/gnais/search/__init__.py @@ -0,0 +1,5 @@ +from gnais.search.agent import agent_search +from gnais.search.classification import classify_search, extract_keywords +from gnais.search.grag import graph_rag_search +from gnais.search.rag import rag_search +from gnais.search.ragent import hybrid_search diff --git a/aisearch/src/gnais/search/agent.py b/aisearch/src/gnais/search/agent.py new file mode 100644 index 0000000..03b6dc5 --- /dev/null +++ b/aisearch/src/gnais/search/agent.py @@ -0,0 +1,88 @@ +"""Agent with SPARQL tool calling for AI search in GeneNetwork""" + +from typing import Any + +import dspy +from gnais.search.prompts import GENERAL_SYSTEM_PROMPT +from gnais.search.tools import ( + MemoryTools, + check_link, + make_sparql_fetch_tool, + route_model, + with_memory, +) + + +class AgentSig(dspy.Signature): + query: str = dspy.InputField() + chat_history: list = dspy.InputField(desc="Previous conversation context") + solution: str = dspy.OutputField( + desc="""Return the answer as valid HTML only. Use the chat +history as context. Use only safe body tags such as <p>, <ul>, <ol>, +<li>, <a>, <strong>, <em>, <code>, <pre>, and <br>. Do not use +Markdown. Do not wrap the answer in ```html fences. + +Link correctness is mandatory. Every <a href="..."> URL MUST come directly +from a SPARQL tool result returned by the configured Virtuoso endpoint. +Never invent, infer, concatenate, normalize, rewrite, shorten, or guess URLs. +Never create links from literals, labels, names, descriptions, IDs, QNames, +CURIEs, blank nodes, or user text. Only bind and use URI/IRI values returned +by SPARQL variables. + +Before emitting any hyperlink, verify that the href value is exactly one of +the URI/IRI strings present in the latest relevant SPARQL result set. If no +verified URI/IRI is available, omit the hyperlink and render plain text instead. +Broken links are worse than no links. + +The final answer must include detailed reasoning where useful, followed by a +clear final answer, but all content must remain valid HTML.""" + ) + + +def _make_agent_stream(sparql_url: str): + routed = route_model()(dspy.Predict(AgentSig)) + chosen_model = routed.choose_model() + chosen_lm = routed.options[chosen_model] + print(f"Choice made: {chosen_model} for Agent") + tools = [ + make_sparql_fetch_tool(sparql_url, chosen_lm), + check_link, + ] + react = dspy.ReAct( + signature=AgentSig, + tools=tools, + max_iters=7, + ) + react.set_lm(chosen_lm) + return dspy.streamify( + react, + stream_listeners=[ + dspy.streaming.StreamListener( + signature_field_name="next_thought", + allow_reuse=True, + ), + dspy.streaming.StreamListener(signature_field_name="solution"), + ], + include_final_prediction_in_output_stream=True, + ) + + +@with_memory(memory_type="agent") +async def agent_search( + query: str, + sparql_url: str, + system_prompt: str = GENERAL_SYSTEM_PROMPT, + user_id: str = "default_user", + memory=None, + chat_history: list = [], +): + yield {"status": "Planning search strategy…"} + yield {"status": "Streaming response…"} + async for value in _make_agent_stream(sparql_url)( + query=f"{system_prompt}\nQuery: {query}", + chat_history=chat_history, + ): + if isinstance(value, dspy.Prediction): + yield {"final": value.solution} + else: + yield getattr(value, "chunk", str(value)) diff --git a/aisearch/src/gnais/search/classification.py b/aisearch/src/gnais/search/classification.py new file mode 100644 index 0000000..833a16e --- /dev/null +++ b/aisearch/src/gnais/search/classification.py @@ -0,0 +1,56 @@ +from functools import lru_cache + +import dspy +from gnais.search.tools import route_model + + +class Classification(dspy.Signature): + input_text: str = dspy.InputField() + decision: str = dspy.OutputField(desc='"keyword" or "semantic"') + + +class Extraction(dspy.Signature): + input_text: str = dspy.InputField() + keywords: str = dspy.OutputField() + + +@lru_cache(maxsize=2048) +def extract_keywords(query: str) -> str: + """Extract list of keywords from query + + Args: + query: user query + + Returns: + list of keywords + """ + return route_model()(dspy.Predict(Extraction))( + input_text=f""" +You are extremely good at extracting keywords from a search query related to specific entities (traits, markers, etc) in GeneNetwork. +Produce a list of space separated keywords featured in the query below. Only return that list. + +{query}""" + ) + + +@lru_cache(maxsize=2048) +def classify_search(query: str) -> str: + """Classify user query as keyword search or semantic search + + Args: + query: user query + + Returns: + type of search for query processing + """ + return route_model()(dspy.Predict(Classification))( + input_text=f""" +You are an experienced search classifier. +You can accurately tell from a query if a keyword search or semantic search is more appropriate to provide satisfactory answers to the user. +A keyword search is appropriate when specific entities feature in the query (i.e trait id, marker code, etc.). +A semantic search is better when the system needs to understand the meaning of the query and make implicit connections. +Infer the type of search that should be performed given the query below: + +{query} +""" + ) diff --git a/aisearch/src/gnais/search/corpus.py b/aisearch/src/gnais/search/corpus.py new file mode 100644 index 0000000..420a7a1 --- /dev/null +++ b/aisearch/src/gnais/search/corpus.py @@ -0,0 +1,119 @@ +import json +import warnings +from functools import lru_cache +from pathlib import Path +from typing import Any + +import chromadb +import torch +from langchain_classic.retrievers import EnsembleRetriever +from langchain_community.retrievers import BM25Retriever +from langchain_community.vectorstores import Chroma +from langchain_huggingface import HuggingFaceEmbeddings +from tqdm import tqdm + +warnings.filterwarnings("ignore") + + +def get_docs(corpus_dir: str) -> dict: + corpus_dir = Path(corpus_dir) + metadata = [] + for corpus in corpus_dir.iterdir(): + with open(corpus, "r") as data: + metadata.extend(json.load(data)) + return metadata + + +def _docs_to_tuple(docs: list) -> tuple: + """Convert docs list to a hashable tuple for caching.""" + if not docs: + return tuple() + if isinstance(docs[0], str): + return tuple(docs) + return tuple(json.dumps(d, sort_keys=True) for d in docs) + + +@lru_cache(maxsize=4) +def get_embed_model(model_name: str): + """Load (and cache) the embedding model so it is only instantiated once. + + Automatically uses CUDA when a GPU is available, otherwise falls back to + CPU. (sentence-transformers does not accept torch_dtype.)""" + device = "cuda" if torch.cuda.is_available() else "cpu" + return HuggingFaceEmbeddings( + model_name=model_name, + model_kwargs={"trust_remote_code": True, "device": device}, + encode_kwargs={"batch_size": 1024}, + ) + + +@lru_cache(maxsize=2048) +def _cached_bm25_retriever(docs_tuple: tuple, k: int): + """Build (and cache) the BM25 retriever. Tokenizing the corpus is + expensive, so we only do it once per unique (docs, k) pair.""" + return BM25Retriever.from_texts( + texts=list(docs_tuple), + metadatas=[{"source": f"Document {ind + 1}"} for ind in range(len(docs_tuple))], + k=k, + ) + + +def init_chroma_db( + docs: list, + embed_model: Any, + chroma_host: str = "localhost", + chroma_port: int = 8000, + chunk_size: int = 1024, +): + client = chromadb.HttpClient(host=chroma_host, port=chroma_port) + db = Chroma( + client=client, + embedding_function=get_embed_model(embed_model), + ) + for i in tqdm(range(0, len(docs), chunk_size)): + chunk = docs[i : i + chunk_size] + metadatas = [ + {"source": f"Document {ind + 1}"} for ind in range(i, i + len(chunk)) + ] + db.add_texts( + texts=chunk, + metadatas=metadatas, + ) + # No db.persist() needed — the server handles persistence + return db + + +def get_chroma_db( + chroma_host: str = "localhost", + chroma_port: int = 8000, + embed_model: Any = None, +): + client = chromadb.HttpClient(host=chroma_host, port=chroma_port) + return Chroma( + client=client, + embedding_function=get_embed_model(embed_model), + ) + + +def create_ensemble_retriever( + chroma_db: Any, docs: list, keyword_weight: float = 0.5, **kwargs +): + k = kwargs.get("k") if kwargs.get("k") else 3 + c = kwargs.get("c") if kwargs.get("c") else 60 + weights = ( + kwargs.get("weights") + if kwargs.get("weights") + else [1 - keyword_weight, keyword_weight] + ) + + bm25_retriever = _cached_bm25_retriever(_docs_to_tuple(docs), k) + + return EnsembleRetriever( + retrievers=[ + # might need fine-tuning + chroma_db.as_retriever(search_kwargs={"k": k}), + bm25_retriever, + ], + weights=weights, + c=c, + ) diff --git a/aisearch/src/gnais/search/grag.py b/aisearch/src/gnais/search/grag.py new file mode 100644 index 0000000..cfb6979 --- /dev/null +++ b/aisearch/src/gnais/search/grag.py @@ -0,0 +1,160 @@ +"""Module with GraphRAG system for AI search in GeneNetwork""" + +import asyncio +import functools + +import dspy +from gnais.search import tools +from gnais.search.prompts import GENERAL_SYSTEM_PROMPT, SPARQL_SYSTEM_PROMPT +from gnais.search.tools import build_schema_hint, route_model, sparql_fetch, with_memory + + +class KeywordSPARQLGenerator(dspy.Signature): + """ + Extract the essential keywords from the user's query, then generate fast, + efficient SPARQL SELECT queries that avoid Virtuoso timeouts (504 errors). + Use the schema hint to match keywords to exact properties and classes. + + CRITICAL PERFORMANCE RULES (to prevent 504s): + 1. Always add `LIMIT` - start with `LIMIT 50`, increase only if needed. Never omit `LIMIT`. + 2. Never use `SELECT *` - list only the variables you actually need. + 3. Avoid expensive operations: no Cartesian products, no cross joins, no full graph scans. + 4. Use specific FILTER patterns that leverage indexes: + - Prefer `STRSTARTS(?label, "prefix")` over `CONTAINS` or regex. + - Avoid `FILTER regex(...)` - it disables indexes. + - Use `FILTER(?value = "exact")` or `IN` with small lists. + 5. Prefer property paths over multiple joins when traversing a chain. + 6. Use VALUES blocks for small sets of constants instead of UNION or OPTIONAL. + 7. Avoid ORDER BY on large result sets - if needed, combine with `LIMIT` and a narrow `WHERE` clause. + 8. Never use nested subqueries unless absolutely necessary; flatten them. + 9. Use `OPTIONAL` only for truly optional patterns – otherwise, use a simple triple pattern. + 10. Limit the number of generated queries - output at most 10 (not 20) per request. + + CRITICAL SCHEMA RULES (remember): + - Literal properties (e.g., gnt:gene_symbol, dct:title) hold strings/numbers. Use `FILTER(?literal = "value")`, not `?o a <Class>`. + - ALWAYS include `FROM <http://rdf.genenetwork.org/v1>` between SELECT and WHERE. + - Strain URIs: use gn:set_BXD, gn:set_B6D2F1, gn:set_HMDP — NEVER gn:BXD, gn:B6D2F1, gn:HMDP. + - gnc:phenotype is metadata only (abbreviation, description, lab_code, submitter, contributor). Phenotype TRAITS (with mean, locus, lod_score, additive, sequence, has_trait_page) are gnc:phenotype_trait. + - Probesets and DNA markers use gnt:chr for chromosome. Genes use gnt:chromosome. + - gnt:has_uniprot_id, gnt:has_homologene_id, gnt:has_kegg_id, gnt:has_omim_id, gnt:has_chebi_id, gnt:has_pub_chem_id exist on gnc:probeset, NOT on gnc:gene. + - gnt:has_align_id, gnt:has_protein_id, gnt:has_rgd_id, gnc:has_kg_id, gnc:has_unigen_id exist on gnc:gene, NOT on gnc:dna_marker. Markers use skos:prefLabel or skos:altLabel for names. + - gnt:locus on phenotype_trait contains chromosome positions (e.g. "1-59904011"), NOT phenotype names. Never FILTER gnt:locus for trait names like "liver_weight". Instead link trait -> has_phenotype -> phenotype and filter gnt:abbreviation. + - gnt:symbol is for probesets. gnt:gene_symbol is for genes and strains. + - Datasets are dcat:Dataset. They have dct:title, gnt:has_strain, gnt:has_tissue_info, gnt:has_samples, gnt:has_summary, gnt:has_citation, gnt:has_contributors, gnt:has_case_info, gnt:has_platform_info, gnt:has_specifics, gnt:has_data_processing_info, gnt:has_experiment_design, gnt:has_experiment_type, gnt:has_genotype_files. + - Do NOT use taxon: for species. Use gn:Mus_musculus, gn:Rattus_norvegicus, gn:Homo_sapiens, etc. + - Gene chips / platforms are skos:Concept with skos:inScheme gnc:gene_chip. They have gnt:has_go_tree_value, gnt:has_geo_series_id. + - Mapping methods and averaging methods are skos:Concept in gnc:mapping_method and gnc:avg_method schemes. + - gnt:has_trait_page gives the URL directly. Never build trait URLs manually. + - Object properties (e.g., gnt:has_phenotype_trait) link to resources. Chain with `?s gnt:has_phenotype_trait ?o . ?o rdf:type <Class>`. + - Only use properties listed in the provided schema. Do NOT invent new ones. + - EVERY query MUST start with the PREFIX declarations from the schema. + - To extract information about a specific trait, find the closest matches in SNAPSHOT_OBJECTS and use them to infer the proper RDF subject for the trait. + - To get trait page or URL, use the predicate `gnt:has_trait_page` with the trait as subject. Its object correspond to the URL. Never build trait URLs manually. + - To get publication for a trait, search for object having the predicate `dcterms:references` and the trait as subject. + - To get highest LOD score and corresponding marker and additive effect, use the predicates `gnt:lod_score`, `gnt:locus`, and `gnt:additive`, respectively. + - Mean trait measurements can be accessed by looking up object with predicate gnt:mean and the trait as subject. + - Trait descriptions can be accessed via the predicate `dcterms:description` + - When you have the description of a trait, you should use regex pattern in the object term with the predicate `dcterms:description` to find the corresponding trait (subject) + Example of an efficient query: + + ``` + PREFIX gnt: <http://rdf.genenetwork.org/v1/term/> + PREFIX gnc: <http://rdf.genenetwork.org/v1/category/> + PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> + + SELECT ?gene ?symbol FROM <http://rdf.genenetwork.org/v1> WHERE { + ?gene rdf:type gnc:gene . + ?gene gnt:gene_symbol ?symbol . + FILTER(STRSTARTS(?symbol, "Shh")) + } LIMIT 50 + ``` + """ + + original_query: str = dspy.InputField(desc="User query") + schema_hint: str = dspy.InputField(desc="GeneNetwork schema from Virtuoso") + keywords: str = dspy.OutputField( + desc="Comma-separated essential keywords from the query" + ) + sparql_queries: list[str] = dspy.OutputField( + desc="Top 15 valid SPARQL SELECT queries with PREFIX declarations." + ) + + +class GraphRAG(dspy.Signature): + original_query: str = dspy.InputField(desc="Query provided") + sparql_results: str = dspy.InputField(desc="JSON results from the SPARQL query") + chat_history: list = dspy.InputField(desc="History of conversation") + feedback: str = dspy.OutputField( + desc="System response to the query with detailed answers and the final answer, formatted as valid HTML using tags such as <p>, <ul>, <li>, <a>, <strong>, <em>, and <br>. Links from sparql results can only be formed by valid IRIs and NOT literals. Use the orinial query, sparql results and chat history when answering." + ) + + +def _make_grag_stream(): + grag = dspy.Predict(GraphRAG) + routed_grag = route_model()(grag) + chosen_model = routed_grag.choose_model() + print(f"Choice made: {chosen_model} for Graph RAG") + grag.set_lm(routed_grag.options[chosen_model]) + + return dspy.streamify( + grag, + stream_listeners=[ + dspy.streaming.StreamListener( + signature_field_name="feedback", + allow_reuse=True, + ) + ], + include_final_prediction_in_output_stream=True, + ) + + +@with_memory(memory_type="grag") +async def graph_rag_search( + query: str, + sparql_url: str, + system_prompt: str = GENERAL_SYSTEM_PROMPT, + memory=None, + user_id: str = "default_user", + chat_history: list = [], +): + grag_prompt = f"{system_prompt}\nQuery: {query}" + sparql_prompt = f"{SPARQL_SYSTEM_PROMPT}\nQuery: {query}" + schema_hint = build_schema_hint(sparql_url) + yield {"status": "Generating keywords and SPARQL queries…"} + + kw_sparql = dspy.Predict(KeywordSPARQLGenerator) + routed_kw = route_model()(kw_sparql) + chosen_model = routed_kw.choose_model() + print(f"Choice made: {chosen_model} for KeywordSPARQL") + kw_sparql.set_lm(routed_kw.options[chosen_model]) + + loop = asyncio.get_running_loop() + combined = await loop.run_in_executor( + tools.LLM_EXECUTOR, + functools.partial( + kw_sparql, + original_query=sparql_prompt, + schema_hint=schema_hint, + ), + ) + keywords = getattr(combined, "keywords", "") + sparql_queries = getattr(combined, "sparql_queries", []) + yield {"status": f"Extracted keywords: {keywords}"} + if sparql_queries is None: + sparql_queries = [] + for i, query in enumerate(sparql_queries): + yield {"status": f"({i+1}). sparql query: {query}"} + + yield {"status": "Querying knowledge graph…"} + sparql_results = await sparql_fetch(sparql_queries, sparql_url) + + yield {"status": "Streaming response…"} + async for value in _make_grag_stream()( + original_query=grag_prompt, + sparql_results=sparql_results, + chat_history=chat_history, + ): + if isinstance(value, dspy.Prediction): + yield {"final": value.feedback} + else: + yield value.chunk diff --git a/aisearch/src/gnais/search/prompts.py b/aisearch/src/gnais/search/prompts.py new file mode 100644 index 0000000..cee16ce --- /dev/null +++ b/aisearch/src/gnais/search/prompts.py @@ -0,0 +1,159 @@ +GENERAL_SYSTEM_PROMPT = """You are a scientific literature/document/ontology/knowledge analyst and a world class semantic data engineer. Answer the question/query below using information at your disposal only. + +RULES: +1. Work even with partial data +2. Do not repeat information. +3. Do not apologize for query errors. +4. Ensure response is in line with the query and do not deviate. +5. Do not include irrelevant information. +Example: Do not refer to publications when the query is about phenotypes in GeneNetwork. + +**Prefix expansion** – When you see any of these prefixes in the results or query, expand them to their full IRI before using in <a href>: + +dcat: → http://www.w3.org/ns/dcat# +dct: → http://purl.org/dc/terms/ +gn: → http://rdf.genenetwork.org/v1/id/ +owl: → http://www.w3.org/2002/07/owl# +gnc: → http://rdf.genenetwork.org/v1/category/ +gnt: → http://rdf.genenetwork.org/v1/term/has_trait_page +obo: → http://purl.obolibrary.org/obo/ +bfo: → http://purl.obolibrary.org/obo/BFO_ +sdmx-measure: → http://purl.org/linked-data/sdmx/2009/measure# +skos: → http://www.w3.org/2004/02/skos/core# +rdf: → http://www.w3.org/1999/02/22-rdf-syntax-ns# +rdfs: → http://www.w3.org/2000/01/rdf-schema# +xsd: → http://www.w3.org/2001/XMLSchema# +qb: → http://purl.org/linked-data/cube# +xkos: → http://rdf-vocabulary.ddialliance.org/xkos# +pubmed: → http://rdf.ncbi.nlm.nih.gov/pubmed/ +schema: → https://schema.org/ + +**Linking rule**: Only create `<a href>` for IRIs or URLs that are returned from the SPARQL results (after expansion if needed). Do not invent or construct links. If no IRI exists, use plain text. + +**Output**: Clean HTML.""" + +SPARQL_SYSTEM_PROMPT = """You are a world class knowledge engineer and SPARQL optimization specialist operating a SPARQL query system. Your task is to construct SPARQL queries that return results efficiently. + +**Guidelines**: +- Use only schema terms and predicates that exist in the data. Never invent properties or IRIs. +- Bind real IRIs from the dataset (use full IRIs or known prefixes). +- Force result‑returning patterns with FILTER, VALUES, and/or LIMIT. +- Avoid `SELECT *` – list only needed variables. +- Avoid broad scans, unnecessary OPTIONAL blocks, and expensive regex. +- Ensure every query returns at least one row (test mentally). + +**Prefixes you can use** (expand as needed): + +dcat: → http://www.w3.org/ns/dcat# +dct: → http://purl.org/dc/terms/ +gn: → http://rdf.genenetwork.org/v1/id/ +owl: → http://www.w3.org/2002/07/owl# +gnc: → http://rdf.genenetwork.org/v1/category/ +gnt: → http://rdf.genenetwork.org/v1/term/has_trait_page +obo: → http://purl.obolibrary.org/obo/ +bfo: → http://purl.obolibrary.org/obo/BFO_ +sdmx-measure: → http://purl.org/linked-data/sdmx/2009/measure# +skos: → http://www.w3.org/2004/02/skos/core# +rdf: → http://www.w3.org/1999/02/22-rdf-syntax-ns# +rdfs: → http://www.w3.org/2000/01/rdf-schema# +xsd: → http://www.w3.org/2001/XMLSchema# +qb: → http://purl.org/linked-data/cube# +xkos: → http://rdf-vocabulary.ddialliance.org/xkos# +pubmed: → http://rdf.ncbi.nlm.nih.gov/pubmed/ +schema: → https://schema.org/ + +**Output**: +- Valid SPARQL queries only. +- No explanation, no markdown – just the query text.""" + + +GN_FACT_EXTRACTION_PROMPT = """You are a memory extraction system for a genomics and bioinformatics research assistant (GeneNetwork). Your job is to extract ALL meaningful facts, entities, and contextual information from the conversation that would help answer future scientific queries. + +Extract facts related to: +1. **Genes and gene symbols** (e.g., "Gnai3", "Apoe", "Brca1") +2. **Phenotypes and traits** (e.g., "body weight", "glucose level", "anxiety behavior", trait IDs like "10001") +3. **Datasets and data resources** (e.g., "BXD", "HMDP", "HCPublish", dataset accession IDs) +4. **Species and strains** (e.g., "Mus musculus", "Rattus norvegicus", "C57BL/6J", "BXD") +5. **Genetic markers and genomic locations** (e.g., "rs13478303", "Chr1:45.2 Mb", "D1Mit10") +6. **QTL / mapping results** (e.g., "LOD score of 5.2", "significant peak on Chr 4", "additive effect of 2.3") +7. **Publications and PubMed IDs** (e.g., "PMID:12345678") +8. **Experimental design and methods** (e.g., "GEMMA mapping", "GWAS", "eQTL analysis") +9. **External databases and tools mentioned** (e.g., "GTEx", "BioGPS", "GeneMANIA", "STRING") +10. **User preferences or constraints** (e.g., "only mouse data", "exclude BXD", "interested in liver tissue") +11. **Any scientific conclusions, relationships, or insights** drawn from the data + +RULES: +- Extract facts even if they seem like "general knowledge" — in a genomics context, knowing that "Trees have branches" is irrelevant, but knowing that "Apoe is associated with lipid metabolism" or "BXD is a mouse recombinant inbred panel" is highly valuable. +- Be specific. Prefer "User asked about QTL mapping for body weight in BXD mice" over "User asked about genetics". +- Organize information chronologically using timestamps to facilitate retrieval based on time. The last interaction should the last in your collection. +- Preserve identifiers (gene symbols, trait IDs, marker names, PubMed IDs) exactly as they appear. +- If the input is a simple greeting or purely conversational filler with no scientific content, return an empty array. +- If the input contains actionable scientific information, extract it. + +Return facts in JSON format: {"facts": ["fact1", "fact2", ...]} + +EXAMPLES: + +Input: Hi there. +Output: {"facts": []} + +Input: What QTLs are associated with body weight in BXD mice? +Output: {"facts": ["User is interested in QTL mapping for body weight", "User is querying the BXD mouse panel", "Trait of interest: body weight"]} + +Input: The gene Apoe on Chr 7 at 45.2 Mb has a LOD score of 6.3 for cholesterol levels in the HMDP dataset. +Output: {"facts": ["Gene Apoe located on Chr 7 at 45.2 Mb", "LOD score of 6.3 for cholesterol levels linked to Apoe", "Dataset: HMDP", "Trait: cholesterol levels"]} + +Input: Can you find probesets for Gnai3 in the Hippocampus dataset? +Output: {"facts": ["User is searching for probesets targeting gene Gnai3", "Tissue/dataset context: Hippocampus", "Gene of interest: Gnai3"]} + +Input: I only care about rat data. Show me phenotype traits for Rattus norvegicus. +Output: {"facts": ["User prefers rat data only", "Species constraint: Rattus norvegicus", "User is interested in phenotype traits"]} + +Input: There are branches in trees. +Output: {"facts": []} + +Input: The publication PMID:15234567 mentions a significant QTL on Chr 1 for anxiety behavior in the BXD panel. +Output: {"facts": ["Publication PMID:15234567 mentions significant QTL on Chr 1 for anxiety behavior", "Trait: anxiety behavior", "Panel: BXD", "Chromosomal location: Chr 1"]}""" + +GN_UPDATE_MEMORY_PROMPT = """You are a memory manager for a genomics research assistant (GeneNetwork). Compare the new facts below with the existing memories and determine the appropriate action for EACH new fact. + +ACTIONS: +- ADD: The new fact contains information not present in existing memories. This includes: + - Different gene symbols, trait names, dataset names, or marker IDs + - Different chromosomal locations or genomic coordinates + - Different species or strains + - Different publications or PubMed IDs + - Different QTL results, LOD scores, p-values, or statistical findings + - Different experimental methods or databases + - Expanded or additional context about a related topic +- UPDATE: The new fact provides additional detail, a correction, or a more complete version of an existing memory (e.g., adding a LOD score to a gene-trait association already in memory). +- NONE: The new fact is truly identical to an existing memory. Only use this if the fact conveys exactly the same information with no new details. Note: in genomics, related but different entities (e.g., "gene Apoe" vs "gene Apoa1", "Chr 1" vs "Chr 2", "BXD" vs "HMDP") are DIFFERENT and should be ADDED, not treated as identical. +- DELETE: The new fact directly contradicts an existing memory (e.g., a corrected gene location, a retracted finding). + +CRITICAL RULES FOR GENOMICS DATA: +1. **Gene symbols are case-sensitive and distinct.** "Apoe" and "APOE" may refer to the same gene (mouse vs human ortholog), but "Apoe" and "Apoa1" are completely different. When in doubt, ADD. +2. **Trait names are distinct.** "body weight" and "liver weight" are different traits. ADD both. +3. **Dataset/panel names are distinct.** "BXD", "HMDP", "Collaborative Cross" are different. ADD each. +4. **Chromosomal locations are distinct.** "Chr 1:45.2 Mb" and "Chr 2:45.2 Mb" are different. ADD both. +5. **QTL/statistical results are additive.** If memory says "Apoe linked to cholesterol" and new fact says "Apoe has LOD 6.3 for cholesterol", this is an UPDATE (adds quantitative detail). If new fact says "Apoe has LOD 6.3 for glucose", this is an ADD (different trait). +6. **Publications are distinct by PMID.** Different PMIDs always ADD. +7. **User preferences evolve.** If user previously said "only mouse data" and now says "include rat data too", UPDATE the preference to "mouse and rat data". + +Existing memories: +{existing_memories} + +New facts: +{new_memories} + +Return a JSON object in the following format: +{{ + "tool_calls": [ + {{ + "tool_name": "add_memory" | "update_memory" | "delete_memory" | "none", + "data": "new or updated memory text", + "memory_id": "id of the memory to update or delete (if applicable)" + }} + ] +}} + +For each new fact, decide independently. Be more permissive with ADD than the default behavior — missing a useful genomics fact is worse than a minor duplication.""" diff --git a/aisearch/src/gnais/search/rag.py b/aisearch/src/gnais/search/rag.py new file mode 100644 index 0000000..7366e26 --- /dev/null +++ b/aisearch/src/gnais/search/rag.py @@ -0,0 +1,93 @@ +"""Module with RAG system for AI search in GeneNetwork""" + +import asyncio +from typing import Any + +import dspy +from gnais.search.prompts import GENERAL_SYSTEM_PROMPT +from gnais.search.tools import route_model, with_memory + + +class RAG(dspy.Signature): + input_text: str = dspy.InputField(desc="Query and instructions") + chat_history: list = dspy.InputField(desc="History of conversation") + context: list = dspy.InputField(desc="Background information") + feedback: str = dspy.OutputField( + desc="""System response to the query — answer ONLY from the context and chat history provided. +HTML answer. Link rules: +- ONLY use <a href> for full web URLs that literally appear in the context. +- NEVER invent RDF/IRI links. +- For entities (datasets, genes, traits, strains) use PLAIN TEXT or <strong> tags. +- If a URL is not in the context, do NOT create a link for it. +- Always fully expand prefixes (.e.g. gn:87e8288b-697c-5b77-a944-bc27d89b19c3 -> https://rdf.genenetwork.org/v1/id/87e8288b-697c-5b77-a944-bc27d89b19c3) + +Fully expand prefixes according to the following map: + +PREFIX dcat: <http://www.w3.org/ns/dcat#> +PREFIX dct: <http://purl.org/dc/terms/> +PREFIX ex: <http://example.org/stuff/1.0/> +PREFIX fabio: <http://purl.org/spar/fabio/> +PREFIX foaf: <http://xmlns.com/foaf/0.1/> +PREFIX generif: <http://www.ncbi.nlm.nih.gov/gene?cmd=Retrieve&dopt=Graphics&list_uids=> +PREFIX genotype: <http://rdf.genenetwork.org/v1/genotype/> +PREFIX gn: <http://rdf.genenetwork.org/v1/id/> +PREFIX gnc: <http://rdf.genenetwork.org/v1/category/> +PREFIX gnt: <http://rdf.genenetwork.org/v1/term/> +PREFIX owl: <http://www.w3.org/2002/07/owl#> +PREFIX phenotype: <http://rdf.genenetwork.org/v1/phenotype/> +PREFIX prism: <http://prismstandard.org/namespaces/basic/2.0/> +PREFIX publication: <http://rdf.genenetwork.org/v1/publication/> +PREFIX pubmed: <http://rdf.ncbi.nlm.nih.gov/pubmed/> +PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> +PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> +PREFIX skos: <http://www.w3.org/2004/02/skos/core#> +PREFIX taxon: <https://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi?mode=Info&id=> +PREFIX up: <http://purl.uniprot.org/core/> +PREFIX xkos: <http://rdf-vocabulary.ddialliance.org/xkos#> +PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> +""" + ) + + +def _make_rag_stream(): + routed = route_model()(dspy.Predict(RAG)) + chosen_model = routed.choose_model() + print(f"Choice made: {chosen_model} for RAG") + options = routed.options + rag = routed.module + rag.set_lm(options[chosen_model]) + + return dspy.streamify( + rag, + stream_listeners=[ + dspy.streaming.StreamListener( + signature_field_name="feedback", + allow_reuse=True, + ) + ], + include_final_prediction_in_output_stream=True, + ) + + +@with_memory(memory_type="rag") +async def rag_search( + query: str, + retriever: Any, + system_prompt: str = GENERAL_SYSTEM_PROMPT, + user_id: str = "default_user", + memory=None, + chat_history: list = [], +): + prompt = f"{system_prompt}\nQuery: {query}" + yield {"status": "Fetching context…"} + context = await asyncio.to_thread(retriever.invoke, query) + yield {"status": "Streaming response…"} + async for value in _make_rag_stream()( + input_text=prompt, + chat_history=chat_history, + context=context, + ): + if isinstance(value, dspy.Prediction): + yield {"final": value.feedback} + else: + yield getattr(value, "chunk", str(value)) diff --git a/aisearch/src/gnais/search/ragent.py b/aisearch/src/gnais/search/ragent.py new file mode 100644 index 0000000..249c305 --- /dev/null +++ b/aisearch/src/gnais/search/ragent.py @@ -0,0 +1,216 @@ +"""Hybrid system of RAG and Agent for GeneNetwork's AI Search""" + +__all__ = ( + "hybrid_search", + "Synthesis", + "StreamEvent", +) + +import asyncio +import time +from functools import lru_cache, partial +from typing import Any + +import dspy +from gnais.config import Config +from gnais.search.agent import agent_search +from gnais.search.classification import classify_search +from gnais.search.corpus import create_ensemble_retriever, get_chroma_db, get_docs +from gnais.search.grag import graph_rag_search +from gnais.search.rag import rag_search +from gnais.search.tools import LLM_EXECUTOR, route_model +from typing_extensions import TypedDict + + +class StreamEvent(TypedDict): + """SSE-ready event emitted by the hybrid search stream.""" + + source: str + kind: str + content: str + + +class Synthesis(dspy.Signature): + """Synthesize the final response from all search components. + Make sure the final synthesis is in line with the query. + + Format your entire response as valid HTML. Use tags such as + <p>, <ul>, <li>, <a>, <strong>, <em>, <h3>, and <br>. + Do not wrap the response in markdown code blocks. + + CRITICAL – URL SAFETY RULES (prevents hallucination): + 1. NEVER invent, guess, or construct any URL. + 2. Only use URLs that appear verbatim in the `all_generation` input. + 3. Before outputting, verify: every `href` attribute value must be an exact substring + of the `all_generation` string. If you cannot verify, omit the link. + 4. Do not append, prepend, or modify URL fragments (e.g., no `#`, `?`, or path changes). + + Structure the HTML as follows: + - A short status banner (<div class="status-success"> or "status-partial">). + - A summary paragraph (<p class="summary">). + - For each group of results, an <h3> heading with the type + (e.g. "Genes", "Traits", "Datasets") followed by a <ul class="card-list">. + Each item should be a <li class="card-item"> containing: + - <div class="card-title"><a href="URL">Name</a></div> + - <div class="card-description">Description</div> (optional) + - <div class="card-meta">Extra info</div> (optional) + - If there's need for a table, add a table with a <table class="data"> + - If no results were found, a <div class="note-box"> explaining why. + - Any additional notes in a <div class="note-box"> at the end. + + """ + + original_query: str = dspy.InputField() + all_generation: list[str] = dspy.InputField() + feedback: str = dspy.OutputField( + desc="Final synthesized response formatted as valid HTML" + ) + + +_synthesize = dspy.streamify( + route_model()(dspy.Predict(Synthesis)), + stream_listeners=[ + dspy.streaming.StreamListener(signature_field_name="feedback", allow_reuse=True) + ], + include_final_prediction_in_output_stream=True, +) + + +# KLUDGE: Creating the retrievers takes the longest time. Here, we +# create them at module time once. XXXX: Find a better way to remove +# these globals! +_CHROMA_DB = get_chroma_db( + chroma_host="localhost", + chroma_port=8000, + embed_model="Qwen/Qwen3-Embedding-0.6B", +) +_DOCS = get_docs(Config.CORPUS_PATH) +_KW_RETRIEVER = create_ensemble_retriever( + chroma_db=_CHROMA_DB, docs=_DOCS, keyword_weight=0.7 +) +_SEM_RETRIEVER = create_ensemble_retriever( + chroma_db=_CHROMA_DB, docs=_DOCS, keyword_weight=0.5 +) + + +async def _rag_search(query: str, user_id: str = "default_user", memory=None): + yield {"status": "Classifying search type…"} + loop = asyncio.get_running_loop() + classification = await loop.run_in_executor( + LLM_EXECUTOR, classify_search, query + ) + yield {"status": f"Search type is: '{classification.get('decision')}'"} + retriever = ( + _KW_RETRIEVER if classification.get("decision") == "keyword" else _SEM_RETRIEVER + ) + yield {"status": "Retrieving documents…"} + async for item in rag_search( + query=query, retriever=retriever, user_id=user_id, memory=memory + ): + yield item + + +async def _grag_search(query: str, user_id: str = "default_user", memory=None): + async for chunk in graph_rag_search( + query=query, sparql_url=Config.SPARQL_ENDPOINT, memory=memory, user_id=user_id + ): + yield chunk + + +_agent_search = partial(agent_search, sparql_url=Config.SPARQL_ENDPOINT) + + +async def _stream_component( + source: str, search_func: Any, queue: asyncio.Queue, **kwargs +) -> None: + start = time.monotonic() + try: + async for chunk in search_func(**kwargs): + if isinstance(chunk, dict) and "final" in chunk: + await queue.put( + StreamEvent(source=source, kind="final", content=chunk["final"]) + ) + elif isinstance(chunk, dict) and "status" in chunk: + await queue.put( + StreamEvent(source=source, kind="status", content=chunk["status"]) + ) + else: + await queue.put( + StreamEvent(source=source, kind="chunk", content=str(chunk)) + ) + except Exception as exc: + await queue.put(StreamEvent(source=source, kind="error", content=str(exc))) + finally: + elapsed = time.monotonic() - start + await queue.put( + StreamEvent(source=source, kind="timing", content=f"{elapsed:.2f}s") + ) + await queue.put(StreamEvent(source=source, kind="done", content="")) + + +async def hybrid_search(query: str, user_id: str = "default_user", memory=None): + """Run hybrid search with concurrent RAG, GraphRAG, and Agent. + + Yields :class:`StreamEvent` dicts for progress from each component, + followed by a final synthesis event with ``source="hybrid"``. + """ + total_start = time.monotonic() + queue: asyncio.Queue = asyncio.Queue() + combined_outputs = {"rag": "", "grag": "", "agent": ""} + + async with asyncio.TaskGroup() as tg: + tg.create_task( + _stream_component( + "rag", _rag_search, queue, query=query, user_id=user_id, memory=memory + ) + ) + tg.create_task( + _stream_component( + "grag", _grag_search, queue, query=query, user_id=user_id, memory=memory + ) + ) + tg.create_task( + _stream_component( + "agent", + _agent_search, + queue, + query=query, + user_id=user_id, + memory=memory, + ) + ) + + remaining = 3 + while remaining: + event = await queue.get() + yield event + + if event["kind"] == "final": + combined_outputs[event["source"]] = event["content"] + elif event["kind"] == "done": + remaining -= 1 + + messages = [ + query, + combined_outputs["rag"], + combined_outputs["grag"], + combined_outputs["agent"], + ] + + synthesis_text = "" + has_chunks = False + async for value in _synthesize(original_query=query, all_generation=messages): + if isinstance(value, dspy.Prediction): + synthesis_text = value.feedback + else: + chunk = getattr(value, "chunk", str(value)) + synthesis_text += chunk + has_chunks = True + yield StreamEvent(source="synthesis", kind="chunk", content=chunk) + + if not has_chunks and synthesis_text: + yield StreamEvent(source="synthesis", kind="chunk", content=synthesis_text) + + total_elapsed = time.monotonic() - total_start + yield StreamEvent(source="hybrid", kind="timing", content=f"{total_elapsed:.2f}s") + yield StreamEvent(source="hybrid", kind="final", content=synthesis_text) diff --git a/aisearch/src/gnais/search/tools.py b/aisearch/src/gnais/search/tools.py new file mode 100644 index 0000000..5ac7a0a --- /dev/null +++ b/aisearch/src/gnais/search/tools.py @@ -0,0 +1,717 @@ +import asyncio +import concurrent.futures +import functools +import logging +import os +import random +from typing import Any + +import dspy +import httpx +import redis +from gnais.config import Config + +_redis = redis.Redis(host="localhost", port=6379, decode_responses=True) + +# Dedicated thread pool for LLM inference so heavy model calls don't +# saturate the default asyncio executor used for lighter I/O work. +LLM_EXECUTOR = concurrent.futures.ThreadPoolExecutor( + max_workers=os.cpu_count() // 2, thread_name_prefix="llm-worker" +) + +# mem0's internal history store can spew sqlite transaction warnings; +# suppress them so they don't clutter CLI output. +for _mem0_logger in ("mem0", "mem0.memory", "mem0.memory.main"): + logging.getLogger(_mem0_logger).addFilter( + lambda record: "Failed to add history" not in record.getMessage() + ) + + +async def _exec_sparql( + sparql_uri: str, + query: str, + max_retries: int = 3, + base_delay: float = 1, +) -> dict: + """Execute a single SPARQL query with retry + exponential jitter via httpx.""" + client = httpx.AsyncClient( + timeout=httpx.Timeout(180.0, connect=5.0), + limits=httpx.Limits(max_connections=20, max_keepalive_connections=10), + ) + for attempt in range(max_retries): + try: + resp = await client.post( + sparql_uri, + data={"query": query}, + headers={"Accept": "application/sparql-results+json"}, + ) + resp.raise_for_status() + return resp.json() + except httpx.HTTPStatusError as e: + if e.response.status_code in (504, 503, 502) and attempt < max_retries - 1: + await asyncio.sleep(base_delay * (2**attempt) + random.uniform(0, 1)) + continue + raise + return {} + + +def build_schema_hint(sparql_uri: str) -> str: + """Build a compact schema hint from the live Virtuoso endpoint. + + Cached in Redis for 1 week so multiple workers / restarts share it. + """ + cache_key = f"gn:schema_hint:{sparql_uri}" + cached = _redis.get(cache_key) + if cached: + return cached + + hint = """ +=== HIGH LEVEL STRUCTURE OF THE GENENETWORK RDF GRAPH === +```mermaid +graph TD + A[gnc:resource_classification_scheme<br/>a skos:ConceptScheme] -->|xkos:levels| B[gnc:taxonomic_family<br/>a xkos:ClassificationLevel<br/>xkos:depth 1] + A -->|xkos:levels| C[gnc:species<br/>a xkos:ClassificationLevel<br/>xkos:depth 2] + A -->|xkos:levels| D[gnc:population_category<br/>a xkos:ClassificationLevel<br/>xkos:depth 3] + A -->|xkos:levels| E[gnc:set<br/>a xkos:ClassificationLevel<br/>xkos:depth 4] + + B -->|xkos:nextLevel| C + C -->|xkos:nextLevel| D + D -->|xkos:nextLevel| E + + B -->|gnt:has_taxonomic_family| F[gni:family_*<br/>no explicit rdf:type] + C -->|skos:member| G[gni:species_*<br/>a gnc:species] + D -->|gnt:has_reference_population| H[gni:population_*<br/>a gnc:reference_population] + E -->|skos:member| I[gni:set_*<br/>a gnc:set] + + F -->|gnt:has_species| G + G -->|gnt:has_strain| I + H -->|gnt:has_strain| I + + I -->|gnt:has_species| G + I -->|gnt:has_reference_population| H + + G -->|gnt:has_uniprot_taxon_id| J[uniprot taxon ID] + + I -->|gnt:has_genotype_data| K[gni:dataset_*] + I -->|gnt:has_phenotype_data| K + I -->|gnt:has_probeset_data| K + I -->|gnt:uses_mapping_method| L[gni:mapping_method_*] + + K -->|gnt:has_molecular_trait| M[gni:trait_*] + K -->|gnt:has_phenotype_trait| M + K -->|gnt:uses_genechip| N[gni:platform_*] + K -->|gnt:uses_normalization_method| O[gni:avg_method_*] + K -->|gnt:has_strain| I +``` + +=== DETAILED SCHEMA OF GENENETWORK RDF DATABASE === +```turtle +@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . +@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . +@prefix skos: <http://www.w3.org/2004/02/skos/core#> . +@prefix xkos: <http://rdf-vocabulary.ddialliance.org/xkos#> . +@prefix dcterms: <http://purl.org/dc/terms/> . +@prefix dcat: <http://www.w3.org/ns/dcat#> . +@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . +@prefix owl: <http://www.w3.org/2002/07/owl#> . +@prefix gnc: <http://rdf.genenetwork.org/v1/category/> . +@prefix gni: <http://rdf.genenetwork.org/v1/id/> . +@prefix gnt: <http://rdf.genenetwork.org/v1/term/> . +@prefix uniprot: <http://purl.uniprot.org/taxonomy/> . + +# ------------------------------------------------- +# Schema and its four levels +# ------------------------------------------------- + +gnc:resource_classification_scheme a skos:ConceptScheme ; + rdfs:label "GeneNetwork Resource Classification Scheme" ; + skos:prefLabel "GeneNetwork Resource Classification Scheme" ; + skos:definition "A hierarchical classification scheme for organizing GeneNetwork resources by dataset type, resource set (inbredset group), or species." ; + xkos:numberOfLevels 4 ; + xkos:levels gnc:taxonomic_family , gnc:species , gnc:population_category , gnc:set . + +# Level containers. Each is an xkos:ClassificationLevel and also a class for its members. +gnc:taxonomic_family a xkos:ClassificationLevel , rdfs:Class ; + rdfs:label "Family" ; + skos:prefLabel "Family" ; + skos:definition "An organizational classification level used in GeneNetwork to group resources into families." ; + xkos:depth 1 ; + xkos:nextLevel gnc:species ; + skos:inScheme gnc:resource_classification_scheme . + +gnc:species a xkos:ClassificationLevel , rdfs:Class ; + rdfs:label "Species" ; + skos:prefLabel "Species" ; + skos:definition "A classification level that associates a given resource to a species in GeneNetwork." ; + xkos:depth 2 ; + xkos:previousLevel gnc:taxonomic_family ; + xkos:nextLevel gnc:population_category ; + skos:inScheme gnc:resource_classification_scheme . + +gnc:population_category a xkos:ClassificationLevel , rdfs:Class ; + rdfs:label "Population Category" ; + skos:prefLabel "Population Category" ; + xkos:depth 3 ; + xkos:previousLevel gnc:species ; + xkos:nextLevel gnc:set ; + skos:inScheme gnc:resource_classification_scheme . + +gnc:set a xkos:ClassificationLevel , rdfs:Class ; + rdfs:label "Set" ; + skos:prefLabel "Set" ; + skos:definition "A category representing groups of genetically related strains or individuals (inbred sets, recombinant inbred lines, etc.)." ; + xkos:depth 4 ; + xkos:previousLevel gnc:population_category ; + skos:inScheme gnc:resource_classification_scheme . + +# ------------------------------------------------- +# Classes for non-level member instances +# ------------------------------------------------- + +gnc:reference_population a rdfs:Class ; + rdfs:label "Reference Population" ; + rdfs:comment "Class of the entities linked from gnc:population_category via gnt:has_reference_population." . + +# Note: gni:family_* entities are linked from gnc:taxonomic_family but do not carry an explicit rdf:type in the source data. + +# ------------------------------------------------- +# Properties +# ------------------------------------------------- + +# XKOS / SKOS structural properties +xkos:levels a rdf:Property ; + rdfs:label "levels" ; + rdfs:domain skos:ConceptScheme ; + rdfs:range xkos:ClassificationLevel . + +xkos:numberOfLevels a rdf:Property ; + rdfs:label "number of levels" ; + rdfs:domain skos:ConceptScheme ; + rdfs:range xsd:integer . + +xkos:nextLevel a rdf:Property ; + rdfs:label "next level" ; + rdfs:domain xkos:ClassificationLevel ; + rdfs:range xkos:ClassificationLevel . + +xkos:previousLevel a rdf:Property ; + rdfs:label "previous level" ; + rdfs:domain xkos:ClassificationLevel ; + rdfs:range xkos:ClassificationLevel . + +xkos:depth a rdf:Property ; + rdfs:label "depth" ; + rdfs:domain xkos:ClassificationLevel ; + rdfs:range xsd:integer . + +skos:member a rdf:Property ; + rdfs:label "member" ; + rdfs:domain skos:Collection ; + rdfs:range skos:Concept ; + rdfs:comment "Used here by gnc:species and gnc:set to link to their member instances." . + +# GeneNetwork-specific membership properties + +gnt:has_taxonomic_family a rdf:Property ; + rdfs:label "has taxonomic family" ; + rdfs:domain gnc:taxonomic_family ; + rdfs:range rdfs:Resource ; + rdfs:comment "Links the taxonomic_family level to gni:family_* entities." . + +gnt:has_reference_population a rdf:Property ; + rdfs:label "has reference population" ; + rdfs:domain [ a rdfs:Class ; owl:unionOf ( gnc:population_category gnc:set ) ] ; + rdfs:range gnc:reference_population ; + rdfs:comment "Links the population_category level to gni:population_* entities; also used from gni:set_* back to populations." . + +gnt:has_species a rdf:Property ; + rdfs:label "has species" ; + rdfs:domain [ a rdfs:Class ; owl:unionOf ( gnc:taxonomic_family gnc:set ) ] ; + rdfs:range gnc:species ; + rdfs:comment "Links family entities to species, and set entities back to species." . + +gnt:has_strain a rdf:Property ; + rdfs:label "has strain" ; + rdfs:domain [ a rdfs:Class ; owl:unionOf ( gnc:species gnc:reference_population ) ] ; + rdfs:range gnc:set ; + rdfs:comment "Links species or reference populations to gni:set_* entities." . + +# GeneNetwork-specific descriptive properties on instances + +gnt:has_uniprot_taxon_id a rdf:Property ; + rdfs:label "has UniProt taxon ID" ; + rdfs:domain gnc:species ; + rdfs:range uniprot:Taxon . + +gnt:short_name a rdf:Property ; + rdfs:label "short name" ; + rdfs:domain gnc:species ; + rdfs:range xsd:string . + +gnt:has_family_order_id a rdf:Property ; + rdfs:label "has family order ID" ; + rdfs:domain rdfs:Resource ; + rdfs:range xsd:integer ; + rdfs:comment "Used on gni:family_* entities for ordering." . + +gnt:has_population_order_id a rdf:Property ; + rdfs:label "has population order ID" ; + rdfs:domain gnc:reference_population ; + rdfs:range xsd:integer . + +gnt:has_set_code a rdf:Property ; + rdfs:label "has set code" ; + rdfs:domain gnc:set ; + rdfs:range xsd:string . + +# Dataset linkage properties from gni:set_* entities + +gnt:has_genotype_data a rdf:Property ; + rdfs:label "has genotype data" ; + rdfs:domain gnc:set ; + rdfs:range dcat:Dataset . + +gnt:has_phenotype_data a rdf:Property ; + rdfs:label "has phenotype data" ; + rdfs:domain gnc:set ; + rdfs:range dcat:Dataset . + +gnt:has_probeset_data a rdf:Property ; + rdfs:label "has probeset data" ; + rdfs:domain gnc:set ; + rdfs:range dcat:Dataset . + +gnt:uses_mapping_method a rdf:Property ; + rdfs:label "uses mapping method" ; + rdfs:domain gnc:set ; + rdfs:range skos:Concept ; + rdfs:comment "Refers to concepts in the gnc:mapping_method SKOS scheme." . + +# Dataset-level properties + +gnt:has_molecular_trait a rdf:Property ; + rdfs:label "has molecular trait" ; + rdfs:domain dcat:Dataset ; + rdfs:range skos:Concept ; + rdfs:comment "Refers to concepts in the gnc:molecular_trait collection." . + +gnt:has_phenotype_trait a rdf:Property ; + rdfs:label "has phenotype trait" ; + rdfs:domain dcat:Dataset ; + rdfs:range skos:Concept ; + rdfs:comment "Used by phenotype datasets." . + +gnt:uses_genechip a rdf:Property ; + rdfs:label "uses genechip" ; + rdfs:domain dcat:Dataset ; + rdfs:range rdfs:Resource ; + rdfs:comment "Refers to gni:platform_* entities." . + +gnt:uses_normalization_method a rdf:Property ; + rdfs:label "uses normalization method" ; + rdfs:domain dcat:Dataset ; + rdfs:range skos:Concept ; + rdfs:comment "Refers to concepts in the gnc:avg_method SKOS scheme." . + +# Common annotation properties + +rdfs:label a rdf:Property ; + rdfs:label "label" ; + rdfs:domain rdfs:Resource ; + rdfs:range xsd:string . + +skos:prefLabel a rdf:Property ; + rdfs:label "preferred label" ; + rdfs:domain rdfs:Resource ; + rdfs:range xsd:string . + +skos:altLabel a rdf:Property ; + rdfs:label "alternative label" ; + rdfs:domain rdfs:Resource ; + rdfs:range xsd:string . + +skos:definition a rdf:Property ; + rdfs:label "definition" ; + rdfs:domain rdfs:Resource ; + rdfs:range xsd:string . + +dcterms:description a rdf:Property ; + rdfs:label "description" ; + rdfs:domain rdfs:Resource ; + rdfs:range rdf:HTML ; + rdfs:comment "On gni:set_* entities this is often an rdf:HTML literal." . + +dcterms:created a rdf:Property ; + rdfs:label "created date" ; + rdfs:domain dcat:Dataset ; + rdfs:range xsd:datetime . + +dcterms:identifier a rdf:Property ; + rdfs:label "identifier" ; + rdfs:domain dcat:Dataset ; + rdfs:range xsd:string . + +dcterms:title a rdf:Property ; + rdfs:label "title" ; + rdfs:domain dcat:Dataset ; + rdfs:range xsd:string . +``` + +=== EXAMPLES OF SUCCESSFUL QUERIES WITH KEY QUERY PATTERNS === + +Question: List all classification levels in order +SPARQL Query: +```sparql +SELECT ?level ?depth ?label +WHERE { + gnc:resource_classification_scheme xkos:levels ?level . + ?level xkos:depth ?depth ; + skos:prefLabel ?label . +} +ORDER BY ?depth +``` + +Question: Find all sets belonging to a species +SPARQL Query: +```sparql +SELECT ?species ?set +WHERE { + ?species a gnc:species ; + gnt:has_strain ?set . +} +``` + +Question: Find all datasets reachable from a species (through sets) +SPARQL Query: +```sparql +SELECT ?species ?set ?dataset +WHERE { + ?species a gnc:species ; + gnt:has_strain ?set . + ?set gnt:has_genotype_data|gnt:has_phenotype_data|gnt:has_probeset_data ?dataset . +} +``` + +Question: Find all sets for a given reference population +SPARQL Query: +```sparql +SELECT ?population ?set +WHERE { + ?population a gnc:reference_population ; + gnt:has_strain ?set . +} +``` + +Question: Traverse family → species → set → dataset +SPARQL Query: +```sparql +SELECT ?family ?species ?set ?dataset +WHERE { + gnc:taxonomic_family gnt:has_taxonomic_family ?family . + ?family gnt:has_species ?species . + ?species gnt:has_strain ?set . + ?set gnt:has_genotype_data|gnt:has_phenotype_data|gnt:has_probeset_data ?dataset . +} +``` + """ + _redis.setex(cache_key, 604800, hint) # 1 week + return hint + + +class QueryTranslation(dspy.Signature): + """Use schema hints to generate valid SPARQL SELECT queries that can retrieve relevant information for the query. + + CRITICAL SCHEMA RULES (derived from GeneNetwork RDF schema): + 1. Every query MUST start with the PREFIX declarations. Only use declared prefixes. + 2. ALWAYS include `FROM <http://rdf.genenetwork.org/v1>` between SELECT and WHERE. + 3. Strain URIs: use gn:set_BXD, gn:set_B6D2F1, gn:set_HMDP — NEVER gn:BXD, gn:B6D2F1, gn:HMDP. + 4. gnc:phenotype is metadata only (abbreviation, description, lab_code, submitter, contributor). Phenotype TRAITS (with mean, locus, lod_score, additive, sequence, has_trait_page) are gnc:phenotype_trait. + 5. Probesets and DNA markers use gnt:chr for chromosome. Genes use gnt:chromosome. + 6. gnt:has_uniprot_id, gnt:has_homologene_id, gnt:has_kegg_id, gnt:has_omim_id, gnt:has_chebi_id, gnt:has_pub_chem_id exist on gnc:probeset, NOT on gnc:gene. + 7. gnt:has_align_id, gnt:has_protein_id, gnt:has_rgd_id, gnc:has_kg_id, gnc:has_unigen_id exist on gnc:gene, NOT on gnc:dna_marker. Markers use skos:prefLabel or skos:altLabel for names. + 8. gnt:locus on phenotype_trait contains chromosome positions (e.g. "1-59904011"), NOT phenotype names. Never FILTER gnt:locus for trait names like "liver_weight". Instead link trait -> has_phenotype -> phenotype and filter gnt:abbreviation. + 9. gnt:symbol is for probesets. gnt:gene_symbol is for genes and strains. + 10. Datasets are dcat:Dataset. They have dct:title, gnt:has_strain, gnt:has_tissue_info, gnt:has_samples, gnt:has_summary, gnt:has_citation, gnt:has_contributors, gnt:has_case_info, gnt:has_platform_info, gnt:has_specifics, gnt:has_data_processing_info, gnt:has_experiment_design, gnt:has_experiment_type, gnt:has_genotype_files. + 11. Do NOT use taxon: for species. Use gn:Mus_musculus, gn:Rattus_norvegicus, gn:Homo_sapiens, etc. + 12. Gene chips / platforms are skos:Concept with skos:inScheme gnc:gene_chip. They have gnt:has_go_tree_value, gnt:has_geo_series_id. + 13. Mapping methods and averaging methods are skos:Concept in gnc:mapping_method and gnc:avg_method schemes. + 14. gnt:has_trait_page gives the URL directly. Never build trait URLs manually. + 15. Leverage as many schema hints as possible. + 16. To extract information about a specific trait, find the closest matches in SNAPSHOT_OBJECTS and use them to infer the proper RDF subject for the trait. + 17. To get trait page or URL, use the predicate `gnt:has_trait_page` with the trait as subject. Its object correspond to the URL. Never build trait URLs manually. + 18. To get publication for a trait, search for object having the predicate `dcterms:references` and the trait as subject. + 19. To get highest LOD score and corresponding marker and additive effect, use the predicates `gnt:lod_score`, `gnt:locus`, and `gnt:additive`, respectively. + 20. Mean trait measurements can be accessed by looking up object with predicate gnt:mean and the trait as subject. + 21. Trait descriptions can be accessed via the predicate `dcterms:description` + 22. When you have the description of a trait, you should use regex pattern in the object term with the predicate `dcterms:description` to find the corresponding trait (subject). + + When querying SPARQL, prefer fast, efficient SPARQL SELECT queries + that avoid Virtuoso timeouts (504 errors). + + CRITICAL PERFORMANCE RULES (to prevent 504s): + 1. Always add `LIMIT` - start with `LIMIT 50`, increase only if needed. Never omit `LIMIT`. + 2. Never use `SELECT *` - list only the variables you actually need. + 3. Avoid expensive operations: no Cartesian products, no cross joins, no full graph scans. + 4. Use specific FILTER patterns that leverage indexes: + - Prefer `STRSTARTS(?label, "prefix")` over `CONTAINS` or regex. + - Avoid `FILTER regex(...)` - it disables indexes. + - Use `FILTER(?value = "exact")` or `IN` with small lists. + 5. Prefer property paths over multiple joins when traversing a chain. + 6. Use VALUES blocks for small sets of constants instead of UNION or OPTIONAL. + 7. Avoid ORDER BY on large result sets - if needed, combine with `LIMIT` and a narrow `WHERE` clause. + 8. Never use nested subqueries unless absolutely necessary; flatten them. + 9. Use `OPTIONAL` only for truly optional patterns – otherwise, use a simple triple pattern. + """ + + original_query: str = dspy.InputField(desc="User query") + schema_hint: str = dspy.InputField(desc="GeneNetwork schema from Virtuoso") + translated_queries: list[str] = dspy.OutputField( + desc="Top 7 valid most relevant SPARQL queries with the highest likelihood of getting non-empty results." + ) + + +async def sparql_fetch( + sparql_queries: list[str], + sparql_uri: str, + max_retries: int = 3, + base_delay: float = 0.5, +) -> str: + """Execute *sparql_queries* concurrently against *sparql_uri*.""" + if not sparql_queries: + return "No SPARQL queries to run." + + async def _fetch_one(query: str, idx: int) -> str: + try: + result = await _exec_sparql(sparql_uri, query, max_retries, base_delay) + bindings = result.get("results", {}).get("bindings", []) + return f"Query {idx} succeeded ({len(bindings)} rows): {bindings}" + except Exception as e: + return f"Query {idx} failed: {e}\nQuery was:\n{query}" + + tasks = [_fetch_one(q, i) for i, q in enumerate(sparql_queries)] + results = await asyncio.gather(*tasks) + return "\n\n".join(results) + + +@functools.lru_cache(maxsize=64) +def make_sparql_fetch_tool( + sparql_uri: str, lm: dspy.LM = Config.DEFAULT_LLM +) -> dspy.Tool: + def _fetch(query: str) -> Any: + schema_hint = build_schema_hint(sparql_uri) + pred = dspy.Predict(QueryTranslation) + pred.set_lm(lm) + pred = pred( + original_query=query, + schema_hint=schema_hint, + ) + sparql_queries = pred.get("translated_queries") if pred else [] + if not sparql_queries: + return "No SPARQL queries generated." + future = LLM_EXECUTOR.submit( + asyncio.run, sparql_fetch(sparql_queries, sparql_uri) + ) + return future.result() + + return dspy.Tool( + name="fetch_data", + desc="Fetch RDF data around GeneNetwork data through SPARQL", + args={ + "query": { + "type": "string", + "desc": "SPARQL query to run to fetch relevant data", + }, + }, + func=_fetch, + ) + + +def _check_link(url: str) -> str: + """Check whether a URL is reachable. + + Returns a short status string suitable for feeding back to the LLM. + """ + try: + response = httpx.Client(follow_redirects=True, timeout=10).head(url) + ok = response.is_success + except Exception: + ok = False + + return f"{'Valid URL' if ok else 'Invalid URL'} URL: {url}" + + +check_link = dspy.Tool( + name="check_link", + desc="Check whether a URL is valid and reachable. Call this BEFORE putting any URL in an <a href>. Returns 'Valid URL: ...' or 'Invalid URL: ...'.", + args={ + "url": { + "type": "string", + "desc": "URL or link to check", + }, + }, + func=_check_link, +) + + +# KLUDGE: For now this is lifted from: +# <https://dspy.ai/tutorials/mem0_react_agent/> +class MemoryTools: + """Tools for interacting with the Mem0 memory system.""" + + def __init__(self, memory): + self.memory = memory + + def store_memory( + self, content: str, user_id: str, run_id: str, metadata: dict = {} + ) -> str: + """Store information in memory.""" + try: + self.memory.add( + content, user_id=user_id, run_id=run_id, metadata=metadata, infer=True + ) + return f"Stored memory: {content}" + except Exception as e: + return f"Error storing memory: {str(e)}" + + def search_memories( + self, query: str, user_id: str, run_id: str, limit: int = 10 + ) -> str: + """Search for relevant memories.""" + results = self.memory.search( + query, filters={"user_id": user_id, "run_id": run_id}, top_k=limit + ) + chat_history = "" + if results and (memories := results.get("results")): + for memory in memories: + if query := memory.get("metadata", {}).get("query"): + chat_history += f"Query: {query}\n" + chat_history += ( + f"\nMemory({memory['updated_at']}): {memory['memory']}\n" + ) + return chat_history.strip() + + def get_all_memories(self, user_id: str, run_id: str, filters: dict = {}) -> str: + """Get all memories for a user.""" + try: + results = self.memory.get_all( + filters={"user_id": user_id, "run_id": run_id} + ) + if not results or not results.get("results"): + return "No memories found for this user." + + memory_text = "All memories for user:\n" + for i, result in enumerate(results["results"]): + memory_text += f"{i}. {result['memory']}\n" + return memory_text + except Exception as e: + return f"Error retrieving memories: {str(e)}" + + def update_memory(self, memory_id: str, new_content: str) -> str: + """Update an existing memory.""" + try: + self.memory.update(memory_id, new_content) + return f"Updated memory with new content: {new_content}" + except Exception as e: + return f"Error updating memory: {str(e)}" + + def delete_memory(self, memory_id: str) -> str: + """Delete a specific memory.""" + try: + self.memory.delete(memory_id) + return "Memory deleted successfully." + except Exception as e: + return f"Error deleting memory: {str(e)}" + + +def with_memory(memory_type: str = "interaction"): + """Decorator factory that injects chat_history from mem0 and persists the interaction after streaming.""" + + def decorator(func): + @functools.wraps(func) + async def wrapper(*args, **kwargs): + query = kwargs.get("query") + memory = kwargs.get("memory") + user_id = kwargs.get("user_id") + + # Pre: search memories and build chat_history + chat_history = [] + memory_tools = None + if memory is not None: + memory_tools = MemoryTools(memory) + memories = memory_tools.search_memories( + query, user_id=user_id, run_id=memory_type + ) + if memories: + chat_history = [memories] + + kwargs["chat_history"] = chat_history + async for value in func(*args, **kwargs): + if isinstance(value, dict): + feedback = str(value.get("final")) + if memory_tools and feedback: + # Fire-and-forget: don't block the stream on SQLite writes + asyncio.create_task( + asyncio.to_thread( + memory_tools.store_memory, + feedback, + user_id=user_id, + run_id=memory_type, + metadata={"query": query}, + ) + ) + yield value + + return wrapper + + return decorator + + +class Route(dspy.Signature): + """ + Choose the most efficient model to handle the task assigned to the program between the LLMs available. + The most efficient model is the LLM that can follow the instructions defined in the program with good fidelity while saving on unneccessary cost. + The length and complexity of instructions in the task description should orient choice. + Tasks with very complex instructions should be handled by the bigger model while those relatively simple can be offloaded to the smaller model. + """ + + task: str = dspy.InputField(desc="The task of the DSPy program") + models: list[str] = dspy.InputField(desc="The 2 models or LLMs available") + best_model: str = dspy.OutputField(desc="The most efficient model for the task") + + +class RoutedModule(dspy.Module): + def __init__(self, module: dspy.Module, options: dict[str, dspy.LM]): + super().__init__() + self.module = module + self.options = options + self.router = dspy.Predict(Route) + + def choose_model(self) -> str: + task = str(self.module.signature) + models = list(self.options.keys()) + return self.router(task=task, models=models).get("best_model") + + def forward(self, **kwargs): + chosen_model = self.choose_model() + print( + f"Choice made: {chosen_model} for {self.module.__dict__['signature'].__name__}" + ) + self.module.set_lm(self.options[chosen_model]) + return self.module(**kwargs) + + def get(self, field_name, default=None): + return getattr(self.module, field_name, default) + + +def route_model( + options: dict[str, dspy.LM] = { + Config.DEFAULT_MODEL: Config.DEFAULT_LLM, + Config.ALTERNATIVE_MODEL: Config.ALTERNATIVE_LLM, + } +): + """Decorator that returns a dspy.Module wrapping the routed module.""" + + def decorator(module: dspy.Module): + return RoutedModule(module, options) + + return decorator diff --git a/aisearch/src/web/app.py b/aisearch/src/web/app.py new file mode 100644 index 0000000..68d4a3d --- /dev/null +++ b/aisearch/src/web/app.py @@ -0,0 +1,348 @@ +import asyncio +import concurrent.futures +import os +import uuid + +import dspy +import quart +import quart_flask_patch # noqa: F401 +import torch +from flask_caching import Cache +from flask_limiter import Limiter +from flask_limiter.util import get_remote_address +from gnais.config import Config +from gnais.search.prompts import GN_FACT_EXTRACTION_PROMPT, GN_UPDATE_MEMORY_PROMPT +from gnais.search.ragent import hybrid_search +from markupsafe import escape +from mem0 import Memory +from mem0.configs.base import MemoryConfig +from quart import ( + Quart, + Response, + flash, + jsonify, + redirect, + render_template, + request, + session, + url_for, +) +from quart.typing import ResponseReturnValue +from quart_auth import ( + AuthUser, + QuartAuth, + Unauthorized, + current_user, + login_required, + login_user, + logout_user, +) + +app = Quart(__name__) +app.config.from_object(Config) + +# Basic Login +app.secret_key = Config.SECRET_KEY +QuartAuth(app) + +# Set up template and static directories +template_dir = os.path.join(os.path.dirname(__file__), "templates") +app.template_folder = template_dir +static_dir = os.path.join(os.path.dirname(__file__), "static") +app.static_folder = static_dir + + +limiter = Limiter( + get_remote_address, + app=app, + default_limits=["100 per minute"], + storage_uri="redis://localhost:6379", + strategy="fixed-window", +) + +cache = Cache(config={"CACHE_TYPE": "RedisCache"}) +cache.init_app(app) + +# Bootstrapping our model +# NOTE: The backend serving localhost:7501 should also run on the GPU +# (e.g. vLLM with --device cuda) for maximum inference throughput. +torch.manual_seed(app.config["SEED"]) +if torch.cuda.is_available(): + torch.cuda.manual_seed_all(app.config["SEED"]) + + +dspy.configure(lm=app.config["DEFAULT_LLM"]) + + +@app.before_serving +async def _set_default_executor(): + loop = asyncio.get_running_loop() + loop.set_default_executor(concurrent.futures.ThreadPoolExecutor(max_workers=64)) + + +# Shared mem0 memory instance (loaded once at server startup) +_MEMORY = Memory( + config=MemoryConfig( + custom_fact_extraction_prompt=GN_FACT_EXTRACTION_PROMPT, + custom_update_extraction_prompt=GN_UPDATE_MEMORY_PROMPT, + llm={ + "provider": "litellm", + "config": { + "model": Config.MEMORY_MODEL, + "temperature": 0.5, + "max_tokens": 100_000, + "api_key": Config.API_KEY, + }, + }, + embedder={ + "provider": "huggingface", + "config": { + "model": "Qwen/Qwen3-Embedding-0.6B", + "embedding_dims": 1024, + "model_kwargs": { + "trust_remote_code": True, + "device": "cuda" if torch.cuda.is_available() else "cpu", + }, + }, + }, + vector_store={ + "provider": "chroma", + "config": { + "collection_name": "mem0", + "host": "localhost", + "port": 8001, + }, + }, + ) +) + + +def _format_sse(event: str, data: str) -> str: + lines = data.splitlines() or [""] + payload = [f"event: {event}"] + payload.extend(f"data: {line}" for line in lines) + payload.append("") + return "\n".join(payload) + "\n" + + +def _stream_chunk_markup(content: str, error: bool = False) -> str: + css_class = "stream-fragment stream-fragment-error" if error else "stream-fragment" + return f"<span class='{css_class}'>{escape(content)}</span>" + + +def _stream_status_markup(label: str, tone: str = "working") -> str: + return f"<span class='stream-stage-badge is-{tone}'>{escape(label)}</span>" + + +def _stream_final_status_markup(message: str, tone: str = "waiting") -> str: + icon = "🔍" if tone == "waiting" else "✨" + return ( + f"<div class='stream-final-status is-{tone}'>" + f"<div class='stream-final-status-icon'>{icon}</div>" + f"<div class='stream-final-status-text'>{escape(message)}</div>" + "</div>" + ) + + +@app.route("/login", methods=["GET", "POST"]) +async def login(): + # KLUDGE: Set a static password for now to prevent token abuse + # from non-GN testers. + users = {"test@balg-qa.genenetwork.org": os.environ.get("USER_PASS")} + if quart.request.method == "GET": + return await render_template("login.html") + form = await request.form + email, password = form.get("email"), form.get("password") + + if email in users and users[email] == password: + login_user(AuthUser(email)) + return redirect(url_for("index")) + + flash("Set the correct user/password") + return redirect(url_for("login")) + + +@app.route("/logout") +async def logout(): + logout_user() + return "Logged out" + + +@app.errorhandler(Unauthorized) +async def redirect_to_login(*_: Exception) -> ResponseReturnValue: + return redirect(url_for("login")) + + +@app.route("/") +@login_required +async def index(): + """Serve the AI search web interface.""" + return await render_template("index.html") + + +@app.route("/search/stream-shell", methods=["GET"]) +@limiter.limit("300 per day") +@login_required +async def search_stream_shell(): + """Shell endpoint that returns the streaming message container.""" + query = request.args.get("q", "").strip() + if not query: + return "<div class='error-message'>Missing query parameter 'q'</div>", 400 + if len(query) > 1000: + return "<div class='error-message'>Query too long</div>", 400 + if "user_id" not in session: + session["user_id"] = str(uuid.uuid4()) + return await render_template("partials/stream_shell.html", query=query) + + +@app.route("/search/stream", methods=["GET"]) +@limiter.limit("300 per day") +@login_required +async def search_stream(): + """SSE endpoint for live hybrid search updates.""" + query = request.args.get("q", "").strip() + if not query: + return Response( + _format_sse( + "final_html", + "<div class='error-message'>Missing query parameter 'q'</div>", + ), + mimetype="text/event-stream", + ) + if len(query) > 1000: + return Response( + _format_sse( + "final_html", "<div class='error-message'>Query too long</div>" + ), + mimetype="text/event-stream", + ) + + user_id = session.get("user_id") + if not user_id: + session["user_id"] = str(uuid.uuid4()) + user_id = session["user_id"] + + async def event_stream(): + completed = set() + final_sent = False + + yield _format_sse("search_state", _stream_status_markup("Streaming", "working")) + yield _format_sse( + "final_html", + _stream_final_status_markup("Waiting for searches to complete…", "waiting"), + ) + try: + async for event in hybrid_search(query, user_id=user_id, memory=_MEMORY): + if final_sent: + break + + source = event["source"] + kind = event["kind"] + content = event["content"] + + if source in {"rag", "grag", "agent"} and kind == "status": + yield _format_sse( + f"{source}_chunk", + f"<div class='stream-status-msg'>{escape(content)}</div>", + ) + continue + + if source in {"rag", "grag", "agent"} and kind == "chunk": + yield _format_sse( + f"{source}_chunk", + _stream_chunk_markup(content), + ) + continue + + if source in {"rag", "grag", "agent"} and kind == "final": + yield _format_sse( + f"{source}_final_html", + str(content), + ) + continue + + if source in {"rag", "grag", "agent"} and kind == "timing": + yield _format_sse( + f"{source}_timing", + escape(content), + ) + continue + + if source in {"rag", "grag", "agent"} and kind == "error": + yield _format_sse( + f"{source}_chunk", + _stream_chunk_markup(content, error=True), + ) + yield _format_sse( + f"{source}_done", + _stream_status_markup("Error", "error"), + ) + completed.add(source) + if len(completed) == 3 and not final_sent: + yield _format_sse( + "search_state", + _stream_status_markup("Synthesizing", "working"), + ) + yield _format_sse( + "final_html", + "<div class='synthesis-stream' sse-swap='synthesis_chunk' hx-swap='beforeend'></div>", + ) + continue + + if source in {"rag", "grag", "agent"} and kind == "done": + yield _format_sse( + f"{source}_done", + _stream_status_markup("Complete", "complete"), + ) + completed.add(source) + if len(completed) == 3 and not final_sent: + yield _format_sse( + "search_state", + _stream_status_markup("Synthesizing", "working"), + ) + yield _format_sse( + "final_html", + "<div class='synthesis-stream' sse-swap='synthesis_chunk' hx-swap='beforeend'></div>", + ) + continue + + if source == "synthesis" and kind == "chunk": + yield _format_sse("synthesis_chunk", content) + continue + + if source == "hybrid" and kind == "timing": + yield _format_sse( + "total_timing", + f"Total: {escape(content)}", + ) + continue + + if source == "hybrid" and kind == "final": + final_sent = True + # await asyncio.to_thread(cache.set, cache_key, content, timeout=3600) + yield _format_sse("final_html", content) + yield _format_sse( + "search_state", _stream_status_markup("Complete", "complete") + ) + yield _format_sse("stream_end", "<div></div>") + return + except Exception as exc: + error_html = f"<div class='error-display'>{escape(str(exc))}</div>" + yield _format_sse("final_html", error_html) + yield _format_sse("stream_end", "<div></div>") + + response = Response( + event_stream(), + mimetype="text/event-stream", + headers={ + "Cache-Control": "no-cache", + "Connection": "keep-alive", + "X-Accel-Buffering": "no", + }, + ) + response.timeout = None + return response + + +if __name__ == "__main__": + app.run(host="0.0.0.0", port=5001, debug=True) diff --git a/aisearch/src/web/auth.py b/aisearch/src/web/auth.py new file mode 100644 index 0000000..af15505 --- /dev/null +++ b/aisearch/src/web/auth.py @@ -0,0 +1,167 @@ +"""Authentication and authorization utilities for ai-search. + +This module provides JWT token validation using JWKS from the auth server. +""" + +import logging +from datetime import datetime +from functools import wraps +from typing import Optional +from urllib.parse import urljoin + +import requests +from authlib.jose import JsonWebKey, JsonWebToken, KeySet +from authlib.jose.errors import BadSignatureError +from quart import request, jsonify, current_app + +logger = logging.getLogger(__name__) + + +class TokenValidationError(Exception): + """Raised when token validation fails.""" + + pass + + +def fetch_jwks(auth_server_url: str, timeout: int = 30) -> KeySet: + """Fetch JSON Web Keys from the auth server.""" + jwks_uri = urljoin(auth_server_url, "auth/public-jwks") + response = requests.get(jwks_uri, timeout=timeout) + response.raise_for_status() + jwks_data = response.json() + keys = [ + JsonWebKey.import_key(key) for key in jwks_data.get("jwks", {}).get("keys", []) + ] + if not keys: + raise TokenValidationError("No keys found in JWKS response") + return KeySet(keys) + + +def get_cached_jwks(auth_server_url: str) -> KeySet: + """Get cached JWKS from Redis, fetching if necessary.""" + cache = current_app.extensions["cache"] + cached = cache.get("auth_server_jwks") + + if cached is not None: + return KeySet([JsonWebKey.import_key(key) for key in cached]) + + # Fetch and cache for 2 hours + jwks = fetch_jwks(auth_server_url) + key_data = [key.export() for key in jwks.keys] + cache.set("auth_server_jwks", key_data, timeout=7200) + return jwks + + +def validate_token(token: str, keys: KeySet) -> dict: + """Validate a JWT token against the provided keys. + + Args: + token: The JWT token string + keys: KeySet containing valid public keys + + Returns: + Decoded JWT payload + + Raises: + TokenValidationError: If token is invalid or cannot be verified + """ + for key in keys.keys: + try: + jwt = JsonWebToken(["RS256"]) + payload = jwt.decode(token, key) + + # Check expiration + if payload.get("exp") and datetime.now().timestamp() > payload["exp"]: + raise TokenValidationError("Token has expired") + + logger.debug("Token validated successfully") + return payload + except BadSignatureError: + continue + + raise TokenValidationError("Token signature could not be verified") + + +def require_token(func): + """Decorator that requires a valid bearer token for the endpoint. + + Expects the Authorization header with a Bearer token. + Adds `auth_token` kwarg to the decorated function with the decoded JWT payload. + + Example: + @app.route("/api/v1/search") + @require_token + async def search(auth_token=None): + # auth_token contains the decoded JWT payload + user_id = auth_token.get("sub") + ... + """ + + @wraps(func) + async def wrapper(*args, **kwargs): + auth_server_url = current_app.config.get("AUTH_SERVER_URL") + + if not auth_server_url: + logger.error("AUTH_SERVER_URL not configured") + return jsonify( + { + "error": "ConfigurationError", + "description": "Authentication not properly configured", + } + ), 500 + + # Extract bearer token from Authorization header + bearer = request.headers.get("Authorization", "") + + if not bearer.startswith("Bearer "): + logger.debug("No bearer token provided") + return jsonify( + { + "error": "AuthenticationError", + "description": "Expected 'Authorization: Bearer <token>' header", + } + ), 401 + + try: + _, token = bearer.split(" ", 1) + token = token.strip() + + # Get cached JWKS and validate token + jwks = get_cached_jwks(auth_server_url) + jwt_payload = validate_token(token, jwks) + + # Add auth token info to kwargs + kwargs["auth_token"] = {"access_token": token, "jwt": jwt_payload} + + return await func(*args, **kwargs) + + except TokenValidationError as exc: + logger.debug("Token validation failed: %s", exc) + return jsonify( + {"error": "TokenValidationError", "description": str(exc)} + ), 401 + except Exception: + logger.error("Unexpected error during token validation", exc_info=True) + return jsonify( + { + "error": "InternalError", + "description": "Failed to validate authentication token", + } + ), 500 + + return wrapper + + +def get_user_id(auth_token: Optional[dict] = None) -> Optional[str]: + """Extract user ID from auth token. + + Args: + auth_token: The auth token dict with 'jwt' key containing the payload + + Returns: + User ID (subject claim) or None if not available + """ + if auth_token is None: + return None + jwt_payload = auth_token.get("jwt", {}) + return jwt_payload.get("sub") diff --git a/aisearch/src/web/static/css/style.css b/aisearch/src/web/static/css/style.css new file mode 100644 index 0000000..38fc5a5 --- /dev/null +++ b/aisearch/src/web/static/css/style.css @@ -0,0 +1,962 @@ +:root { + --bg: #fff; + --bg-accent: #f5f5f5; + --surface: #fff; + --surface-strong: #fff; + --surface-muted: #f9f9f9; + --line: #ddd; + --line-strong: #ccc; + --text: #000; + --muted: #777; + --accent: #3071a9; + --accent-strong: #245269; + --blue: #3071a9; + --success: #3c763d; + --warning: #8a6d3b; + --error: #a94442; + --shadow: none; + --shadow-soft: none; + --radius-xl: 24px; + --radius-lg: 18px; + --radius-md: 14px; +} + +* { + margin: 0; + padding: 0; + box-sizing: border-box; +} + +html, +body { + min-height: 100%; +} + +body { + font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; + background: #fff; + color: var(--text); + height: 100vh; + display: flex; + flex-direction: column; +} + +.header { + padding: 1rem 1.4rem; + display: flex; + align-items: center; + gap: 1rem; + background: #222; + border-bottom: 1px solid #080808; + position: sticky; + top: 0; + z-index: 20; +} + +.header-logo { + width: 38px; + height: 38px; + border-radius: 14px; + background: #3071a9; + display: flex; + align-items: center; + justify-content: center; + color: white; + font-weight: 700; + letter-spacing: 0.04em; +} + +.header-title { + font-size: 1.05rem; + font-weight: 700; + letter-spacing: 0.02em; + color: #fff; +} + +.chat-container { + flex: 1; + min-height: 0; + overflow-y: auto; + padding: 1.5rem 1rem 2rem; + max-width: 1100px; + margin: 0 auto; + width: 100%; + scrollbar-width: none; +} + +.chat-container::-webkit-scrollbar { + display: none; +} + +#messages { + display: flex; + flex-direction: column; + gap: 1.4rem; +} + +.empty-state { + text-align: center; + padding: 4rem 1rem 2rem; + color: var(--muted); + max-width: 820px; + margin: 0 auto; +} + +.empty-state h1 { + font-family: Georgia, "Times New Roman", serif; + font-size: clamp(2.6rem, 5vw, 4.5rem); + line-height: 0.96; + letter-spacing: -0.03em; + margin-bottom: 1rem; + color: var(--text); +} + +.empty-state p { + font-size: 1.02rem; + margin-bottom: 0.5rem; +} + +.examples { + margin-top: 2rem; + display: grid; + gap: 0.85rem; + text-align: left; +} + +.example-btn { + background: #f9f9f9; + border: 1px solid #ddd; + border-radius: 16px; + padding: 1rem 1.1rem; + cursor: pointer; + color: #333; + font-size: 0.96rem; + text-align: left; + transition: transform 0.2s ease, border-color 0.2s ease, background 0.2s ease; +} + +.example-btn:hover { + transform: translateY(-1px); + border-color: #3071a9; + background: #fff; +} + +.message { + animation: fadeIn 0.28s ease; +} + +@keyframes fadeIn { + from { + opacity: 0; + transform: translateY(8px); + } + to { + opacity: 1; + transform: translateY(0); + } +} + +.message-user { + display: flex; + justify-content: flex-end; +} + +.message-user .message-content { + max-width: min(78%, 760px); + background: #3071a9; + color: white; + border-radius: 22px 22px 8px 22px; + padding: 1rem 1.3rem; + line-height: 1.65; +} + +.message-ai { + display: flex; + gap: 1rem; + position: relative; +} + +.avatar { + width: 40px; + height: 40px; + border-radius: 14px; + background: #222; + display: flex; + align-items: center; + justify-content: center; + color: white; + font-weight: 700; + flex-shrink: 0; +} + +.message-ai .message-content { + flex: 1; + background: var(--surface); + border: 1px solid #ddd; + border-radius: var(--radius-xl); + padding: 1.25rem; + line-height: 1.75; +} + +.message-ai-stream .message-content { + padding: 1.35rem; +} + +.stream-shell { + display: grid; + gap: 1rem; +} + +.stream-shell-header { + display: flex; + justify-content: space-between; + gap: 1rem; + align-items: start; +} + +.stream-shell-kicker, +.stream-final-kicker { + text-transform: uppercase; + letter-spacing: 0.12em; + font-size: 0.72rem; + font-weight: 700; + color: var(--accent); +} + +.stream-shell-header h2 { + margin-top: 0.25rem; + font-size: 1.15rem; + line-height: 1.2; +} + +.stream-shell-intro { + color: var(--muted); + font-size: 0.96rem; +} + +.stream-stage-grid { + display: grid; + grid-template-columns: repeat(3, minmax(0, 1fr)); + gap: 0.9rem; +} + +.stream-stage-card { + background: #fafafa; + border: 1px solid var(--line); + border-radius: var(--radius-lg); + padding: 1rem; + min-height: 220px; + display: flex; + flex-direction: column; + gap: 0.8rem; +} + +.stream-stage-card.is-muted { + opacity: 0.75; +} + +.stream-stage-head, +.stream-final-head { + display: flex; + justify-content: space-between; + gap: 0.8rem; + align-items: center; +} + +.stream-stage-meta, +.stream-final-meta { + display: flex; + align-items: center; + gap: 0.6rem; +} + +.stream-stage-timing, +.stream-final-timing { + font-family: "SFMono-Regular", "Menlo", "Consolas", monospace; + font-size: 0.75rem; + color: var(--muted); + white-space: nowrap; +} + +.stream-final-timing { + font-weight: 600; + color: var(--accent); +} + +.stream-stage-name { + font-size: 0.95rem; + font-weight: 700; +} + +.stream-stage-badge { + display: inline-flex; + align-items: center; + gap: 0.35rem; + font-size: 0.74rem; + border-radius: 999px; + padding: 0.28rem 0.58rem; + border: 1px solid transparent; + white-space: nowrap; +} + +.stream-stage-badge.is-working { + color: #31708f; + background: #d9edf7; + border-color: #bce8f1; +} + +.stream-stage-badge.is-working::before { + content: ""; + display: inline-block; + width: 0.75em; + height: 0.75em; + border: 2px solid currentColor; + border-right-color: transparent; + border-radius: 50%; + animation: spin 1s linear infinite; +} + +.stream-stage-badge.is-complete { + color: var(--success); + background: #dff0d8; + border-color: #d6e9c6; +} + +.stream-stage-badge.is-complete::before { + content: "✓"; + display: inline-flex; + align-items: center; + justify-content: center; + width: 1em; + height: 1em; + font-size: 0.7em; + font-weight: 700; + background: var(--success); + color: white; + border-radius: 50%; +} + +.stream-stage-badge.is-error { + color: var(--error); + background: #f2dede; + border-color: #ebccd1; +} + +.stream-stage-body-scroll { + flex: 1; + min-height: 0; + max-height: 220px; + overflow-y: auto; + padding-right: 0.4rem; + overscroll-behavior: contain; +} + +.stream-stage-body-scroll::-webkit-scrollbar { + width: 6px; +} + +.stream-stage-body-scroll::-webkit-scrollbar-thumb { + background: #ccc; + border-radius: 999px; +} + +.stream-stage-body { + font-family: "SFMono-Regular", "Menlo", "Consolas", monospace; + font-size: 0.84rem; + line-height: 1.65; + color: #555; + white-space: pre-wrap; + word-break: break-word; +} + +.stream-init-msg { + font-family: "SFMono-Regular", "Menlo", "Consolas", monospace; + font-size: 0.84rem; + color: var(--muted); + font-style: italic; + padding: 0.25rem 0; + animation: blink 1.4s infinite ease-in-out; +} + +.stream-stage-body-scroll:has(.stream-stage-body:not(:empty)) .stream-init-msg { + display: none; +} + +@keyframes blink { + 0%, 100% { + opacity: 1; + } + 50% { + opacity: 0.35; + } +} + +.stream-fragment { + display: inline; +} + +.stream-fragment-error { + color: var(--error); +} + +.stream-status-msg { + font-family: "SFMono-Regular", "Menlo", "Consolas", monospace; + font-size: 0.84rem; + color: var(--muted); + font-style: italic; + padding: 0.15rem 0; + border-left: 2px solid var(--accent); + padding-left: 0.5rem; + margin: 0.2rem 0; + animation: fadeIn 0.3s ease; +} + +.stream-final-card { + background: #fff; + border: 1px solid var(--line); + border-radius: calc(var(--radius-xl) + 2px); + padding: 1.2rem 1.25rem; +} + +.stream-final-note { + font-size: 0.82rem; + color: var(--muted); +} + +.stream-final-body { + margin-top: 1rem; +} + +.stream-final-status { + border: 1px dashed #ccc; + border-radius: var(--radius-md); + padding: 1.25rem; + color: var(--muted); + background: #f9f9f9; + text-align: center; + display: flex; + flex-direction: column; + align-items: center; + gap: 0.5rem; +} + +.stream-final-status.is-waiting { + animation: pulse-soft 2s infinite ease-in-out; +} + +.stream-final-status.is-synthesizing { + animation: pulse-soft 1.5s infinite ease-in-out; +} + +.stream-final-status-icon { + font-size: 1.5rem; + line-height: 1; + opacity: 0.85; +} + +.stream-final-status-text { + font-size: 0.95rem; + font-weight: 500; +} + +@keyframes pulse-soft { + 0%, 100% { + opacity: 1; + } + 50% { + opacity: 0.55; + } +} + +.ai-response h3 { + color: var(--text); + margin: 1.5rem 0 0.8rem; + font-size: 1.08rem; + border-bottom: 1px solid var(--line); + padding-bottom: 0.45rem; +} + +.ai-response h3:first-child { + margin-top: 0; +} + +.summary { + background: #d9edf7; + border-left: 4px solid var(--accent); + padding: 1rem 1.1rem; + margin: 0.85rem 0 1.2rem; + border-radius: 0 12px 12px 0; + color: #245269; +} + +.status-success, +.status-partial, +.status-error { + display: inline-flex; + align-items: center; + gap: 0.45rem; + font-size: 0.88rem; + margin-bottom: 0.8rem; + font-weight: 600; +} + +.status-success { + color: var(--success); +} + +.status-partial { + color: var(--warning); +} + +.status-error { + color: var(--error); +} + +.status-success::before, +.status-partial::before, +.status-error::before { + width: 18px; + height: 18px; + border-radius: 999px; + display: flex; + align-items: center; + justify-content: center; + color: white; + font-size: 11px; +} + +.status-success::before { + content: "✓"; + background: var(--success); +} + +.status-partial::before { + content: "!"; + background: var(--warning); +} + +.status-error::before { + content: "✕"; + background: var(--error); +} + +.card-list { + list-style: none; + display: grid; + gap: 0.9rem; + margin-top: 0.9rem; +} + +.card-item { + background: #fff; + border: 1px solid var(--line); + border-radius: var(--radius-md); + padding: 1rem; + transition: transform 0.2s ease, border-color 0.2s ease; +} + +.card-item:hover { + transform: translateY(-1px); + border-color: #3071a9; +} + +.card-title { + font-weight: 700; + color: var(--blue); + font-size: 1.03rem; + margin-bottom: 0.45rem; +} + +.card-title a { + color: inherit; + text-decoration: none; +} + +.card-title a:hover { + text-decoration: underline; +} + +.card-description { + color: #333; + font-size: 0.95rem; + line-height: 1.6; + margin-bottom: 0.45rem; +} + +.card-meta { + color: var(--muted); + font-size: 0.88rem; + font-style: italic; +} + +.note-box, +.access-note, +.error-display, +.raw-response { + border-radius: var(--radius-md); + padding: 1rem; + margin-top: 1rem; +} + +.note-box { + background: #fcf8e3; + border: 1px solid #faebcc; + color: #8a6d3b; +} + +.access-note { + background: #dff0d8; + border: 1px solid #d6e9c6; + color: #3c763d; +} + +.error-display { + background: #f2dede; + border: 1px solid #ebccd1; + color: #a94442; +} + +.raw-response { + background: #f5f5f5; + border: 1px solid var(--line); + font-family: "SFMono-Regular", "Menlo", "Consolas", monospace; + font-size: 0.85rem; + white-space: pre-wrap; + word-wrap: break-word; + max-height: 200px; + overflow-y: auto; +} + +.loading-message { + display: flex; + align-items: center; + gap: 1rem; + color: var(--muted); + padding: 0.8rem 0.2rem; +} + +.loading-dots { + display: flex; + gap: 4px; +} + +.loading-dots span { + width: 8px; + height: 8px; + background: var(--accent); + border-radius: 50%; + animation: bounce 1.4s infinite ease-in-out; +} + +.loading-dots span:nth-child(2) { + animation-delay: 0.2s; +} + +.loading-dots span:nth-child(3) { + animation-delay: 0.4s; +} + +@keyframes spin { + to { + transform: rotate(360deg); + } +} + +@keyframes bounce { + 0%, 80%, 100% { + transform: scale(0.2); + opacity: 0.5; + } + 40% { + transform: scale(1); + opacity: 1; + } +} + +.htmx-indicator { + display: none !important; +} + +.htmx-request.htmx-indicator { + display: flex !important; +} + +.input-container { + padding: 1rem 1rem 1.5rem; + background: #fff; + border-top: 1px solid #f0f0f0; + display: flex; + flex-direction: column; + align-items: center; + gap: 0.25rem; +} + +.input-wrapper { + max-width: 768px; + width: 100%; + margin: 0 auto; + display: flex; + align-items: flex-end; + gap: 0.5rem; + background: #fff; + border: 1px solid #e5e5e5; + border-radius: 24px; + padding: 0.5rem 0.6rem 0.5rem 1.25rem; + box-shadow: + 0 2px 12px rgba(0, 0, 0, 0.06), + 0 0 0 1px rgba(0, 0, 0, 0.02); + transition: box-shadow 0.2s ease, border-color 0.2s ease, transform 0.2s ease; +} + +.input-wrapper:focus-within { + box-shadow: + 0 2px 16px rgba(48, 113, 169, 0.10), + 0 0 0 1px rgba(48, 113, 169, 0.15); + border-color: rgba(48, 113, 169, 0.25); +} + +.textarea-box { + flex: 1; + position: relative; + min-width: 0; + display: flex; + align-items: flex-start; +} + +.search-input { + width: 100%; + min-height: 44px; + max-height: 200px; + padding: 10px 28px 10px 0; + font-size: 16px; + line-height: 1.5; + border: none; + background: transparent; + outline: none; + color: #222; + resize: none; + overflow-y: hidden; + font-family: inherit; +} + +.search-input::placeholder { + color: #999; +} + +.search-input:disabled { + opacity: 0.5; + cursor: not-allowed; +} + +.clear-btn { + position: absolute; + right: 0; + top: 8px; + background: none; + border: none; + color: #999; + font-size: 1.2rem; + line-height: 1; + cursor: pointer; + width: 24px; + height: 24px; + display: none; + align-items: center; + justify-content: center; + border-radius: 50%; + transition: color 0.15s ease, background 0.15s ease; +} + +.clear-btn:hover { + color: #666; + background: #f0f0f0; +} + +.search-button, +.stop-button { + width: 36px; + height: 36px; + border-radius: 50%; + border: none; + color: white; + cursor: pointer; + display: flex; + align-items: center; + justify-content: center; + flex-shrink: 0; + transition: transform 0.15s ease, background 0.15s ease, opacity 0.15s ease; + margin-bottom: 4px; +} + +.search-button { + background: #3071a9; + font-size: 1.1rem; +} + +.search-button:hover { + transform: scale(1.06); + background: #245269; +} + +.search-button:disabled { + opacity: 0.4; + cursor: not-allowed; +} + +.stop-button { + background: #e53935; + display: none; +} + +.stop-button:hover { + transform: scale(1.06); + background: #c62828; +} + +.stop-button:active { + transform: scale(0.95); + background: #b71c1c; +} + +.char-counter { + max-width: 768px; + width: 100%; + margin: 0 auto; + text-align: right; + font-size: 0.75rem; + color: #999; + padding-right: 0.75rem; + min-height: 1.2em; +} + +.char-counter.caution { + color: #f5a623; +} + +.char-counter.warning { + color: #e53935; +} + +.copy-btn { + position: absolute; + top: 0.75rem; + right: 0.75rem; + background: #fff; + border: 1px solid var(--line); + border-radius: 999px; + padding: 0.36rem 0.7rem; + font-size: 0.78rem; + color: var(--muted); + cursor: pointer; + opacity: 0; + transition: opacity 0.2s ease, background 0.2s ease; +} + +.message-ai:hover .copy-btn { + opacity: 1; +} + +.copy-btn:hover { + background: white; +} + +.scroll-to-bottom-btn { + position: fixed; + bottom: 100px; + right: 24px; + z-index: 100; + background: var(--accent); + color: white; + border: none; + border-radius: 50%; + width: 44px; + height: 44px; + font-size: 1.2rem; + line-height: 1; + cursor: pointer; + opacity: 0; + pointer-events: none; + transition: opacity 0.2s ease, transform 0.2s ease, background 0.2s ease; + box-shadow: 0 2px 10px rgba(0, 0, 0, 0.18); + display: flex; + align-items: center; + justify-content: center; +} + +.scroll-to-bottom-btn.visible { + opacity: 1; + pointer-events: auto; +} + +.scroll-to-bottom-btn:hover { + transform: scale(1.06); + background: var(--accent-strong); +} + +.error-message { + background: #f2dede; + border: 1px solid #ebccd1; + border-radius: var(--radius-md); + padding: 1rem; + color: #a94442; +} + +@media (max-width: 920px) { + .stream-stage-grid { + grid-template-columns: 1fr; + } + + .stream-stage-body-scroll { + max-height: 180px; + } + + .message-user .message-content { + max-width: 92%; + } +} + +@media (max-width: 640px) { + .header { + padding: 0.85rem 1rem; + } + + .chat-container { + padding: 1rem 0.75rem 1.5rem; + } + + .message-ai { + gap: 0.75rem; + } + + .avatar { + width: 34px; + height: 34px; + border-radius: 12px; + } + + .message-ai .message-content, + .message-user .message-content { + padding: 1rem; + } + + .stream-shell-header, + .stream-final-head { + flex-direction: column; + align-items: flex-start; + } + + .copy-btn { + position: static; + opacity: 1; + margin-top: 0.75rem; + } + + .input-container { + padding: 0.75rem; + } + + .input-wrapper { + border-radius: 20px; + padding: 0.4rem 0.5rem 0.4rem 1rem; + } +} diff --git a/aisearch/src/web/templates/index.html b/aisearch/src/web/templates/index.html new file mode 100644 index 0000000..02d61f1 --- /dev/null +++ b/aisearch/src/web/templates/index.html @@ -0,0 +1,233 @@ +<!DOCTYPE html> +<html lang="en"> + <head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <title>GeneNetwork AI Search</title> + <script src="https://unpkg.com/htmx.org@2.0.8"></script> + <script src="https://unpkg.com/htmx-ext-sse@2.2.4/sse.js"></script> + <script src="https://unpkg.com/hyperscript.org@0.9.12"></script> + <link rel="stylesheet" + href="{{ url_for('static', filename='css/style.css') }}"> + </head> + <body> + <div class="header"> + <div class="header-logo">GN</div> + <div class="header-title">GeneNetwork AI Search</div> + </div> + <div class="chat-container" id="chat-container"> + <div class="empty-state" id="empty-state"> + <h1>GeneNetwork AI Search</h1> + <p>Ask natural language questions about genes, traits, and datasets</p> + <p style="font-size: 0.9em; color: #999;">Powered by Hybrid RAG + Agent</p> + <div class="examples"> + <button type="button" + class="example-btn" + _="on click set #search-input.value to 'What genes are associated with diabetes?' then call #search-input.focus()"> + What genes are associated with diabetes? + </button> + <button type="button" + class="example-btn" + _="on click set #search-input.value to 'Find datasets related to obesity' then call #search-input.focus()"> + Find datasets related to obesity + </button> + <button type="button" + class="example-btn" + _="on click set #search-input.value to 'How does the mosaic structure of BXD recombinant inbred mice genomes affect mapping resolution across generations?' then call #search-input.focus()"> + How does the mosaic structure of BXD recombinant inbred mice genomes affect mapping resolution across generations? + </button> + </div> + </div> + <!-- messages wrapper --> + <div id="messages"></div> + <!-- indicator sits after messages so it appears at the bottom --> + <div id="loading-indicator" class="message message-ai htmx-indicator"> + <div class="avatar">AI</div> + <div class="message-content"> + <div class="loading-message"> + <div class="loading-dots"> + <span></span><span></span><span></span> + </div> + <span>Searching GeneNetwork data...</span> + </div> + </div> + </div> + </div> + <div class="input-container"> + <form class="input-wrapper" + id="search-form" + hx-get="/search/stream-shell" + hx-target="#messages" + hx-swap="beforeend" + hx-indicator="#loading-indicator" + _="on htmx:beforeRequest + if no #search-input.value.trim() halt end + put 'none' into #empty-state's style.display + on htmx:afterRequest + put '' into #search-input.value + call #search-input.focus()"> + <div class="textarea-box"> + <textarea name="q" + id="search-input" + class="search-input" + placeholder="Ask a question about genes, traits, or datasets... (Shift+Enter for new line)" + rows="1" + autocomplete="off" + autofocus></textarea> + <button type="button" + class="clear-btn" + id="clear-btn" + title="Clear message" + aria-label="Clear message">×</button> + </div> + <button type="submit" + class="search-button" + id="search-btn" + aria-label="Send" + title="Send">↑</button> + <button type="button" + class="stop-button" + id="stop-btn" + aria-label="Stop generating" + title="Stop generating"> + <svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor" aria-hidden="true"> + <rect x="6" y="6" width="12" height="12" rx="2" /> + </svg> + </button> + </form> + <div class="char-counter" id="char-counter">0 / 1000</div> + </div> + <button id="scroll-to-bottom" class="scroll-to-bottom-btn" title="Scroll to bottom">↓</button> + + <script type="text/hyperscript"> + def focusInput() + call #search-input.focus() + end + </script> + <script> + (function(){ + const chat = document.getElementById('chat-container'); + const btn = document.getElementById('scroll-to-bottom'); + const messages = document.getElementById('messages'); + if (!chat || !btn || !messages) return; + const THRESHOLD = 80; + let atBottom = true; + + function check() { + atBottom = chat.scrollHeight - chat.scrollTop <= chat.clientHeight + THRESHOLD; + btn.classList.toggle('visible', !atBottom); + } + + chat.addEventListener('scroll', check); + window.addEventListener('resize', check); + + new MutationObserver(function(muts) { + var hit = muts.some(function(m) { + return m.target.closest && m.target.closest('.stream-final-body'); + }); + if (!hit) return; + if (atBottom) chat.scrollTop = chat.scrollHeight; + else btn.classList.add('visible'); + }).observe(messages, { childList: true, subtree: true }); + + btn.addEventListener('click', function(){ + chat.scrollTop = chat.scrollHeight; + atBottom = true; + btn.classList.remove('visible'); + }); + })(); + </script> + <script> + (function(){ + const input = document.getElementById('search-input'); + const sendBtn = document.getElementById('search-btn'); + const stopBtn = document.getElementById('stop-btn'); + const clearBtn = document.getElementById('clear-btn'); + const charCounter = document.getElementById('char-counter'); + const messages = document.getElementById('messages'); + const form = document.getElementById('search-form'); + if (!input || !sendBtn || !stopBtn || !messages || !form) return; + const MAX_HEIGHT = 200; + const MAX_CHARS = 1000; + + function autoGrow() { + input.style.height = 'auto'; + var h = Math.min(input.scrollHeight, MAX_HEIGHT); + input.style.height = h + 'px'; + input.style.overflowY = input.scrollHeight > MAX_HEIGHT ? 'auto' : 'hidden'; + } + + function updateUI() { + var len = input.value.length; + if (charCounter) { + charCounter.textContent = len + ' / ' + MAX_CHARS; + charCounter.classList.remove('warning', 'caution'); + if (len > 900) charCounter.classList.add('warning'); + else if (len > 700) charCounter.classList.add('caution'); + } + if (clearBtn) clearBtn.style.display = len > 0 ? 'flex' : 'none'; + } + + function setStreaming(v) { + input.disabled = v; + sendBtn.style.display = v ? 'none' : 'flex'; + stopBtn.style.display = v ? 'flex' : 'none'; + if (!v) { + input.focus(); + autoGrow(); + updateUI(); + } + } + + window.onStreamEnd = function() { setStreaming(false); }; + + form.addEventListener('htmx:afterRequest', function(evt) { + if (evt.detail.successful) setStreaming(true); + setTimeout(function() { + autoGrow(); + updateUI(); + }, 0); + }); + + stopBtn.addEventListener('click', function() { + var lastMsg = messages.lastElementChild; + if (lastMsg) lastMsg.remove(); + setStreaming(false); + }); + + if (clearBtn) { + clearBtn.addEventListener('click', function() { + input.value = ''; + input.focus(); + autoGrow(); + updateUI(); + }); + } + + input.addEventListener('input', function() { + autoGrow(); + updateUI(); + }); + + input.addEventListener('keydown', function(e) { + if (e.key !== 'Enter') return; + if (e.shiftKey) return; + e.preventDefault(); + if (!input.disabled && input.value.trim()) { + form.requestSubmit(); + } + }); + + input.addEventListener('paste', function() { + setTimeout(function() { + autoGrow(); + updateUI(); + }, 0); + }); + + autoGrow(); + updateUI(); + })(); + </script> + </body> +</html> diff --git a/aisearch/src/web/templates/login.html b/aisearch/src/web/templates/login.html new file mode 100644 index 0000000..1b87523 --- /dev/null +++ b/aisearch/src/web/templates/login.html @@ -0,0 +1,187 @@ +<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <title>GeneNetwork AI Search – Sign In</title> + <script src="https://unpkg.com/htmx.org@2.0.8"></script> + <script src="https://unpkg.com/htmx-ext-sse@2.2.4/sse.js"></script> + <script src="https://unpkg.com/hyperscript.org@0.9.12"></script> + <link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}"> + <style> + /* === Login Page Specific Styles === */ + * { + margin: 0; + padding: 0; + box-sizing: border-box; + } + + body { + font-family: system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', sans-serif; + background: linear-gradient(135deg, #0f172a 0%, #1e293b 100%); + min-height: 100vh; + display: flex; + align-items: center; + justify-content: center; + padding: 1.5rem; + } + + .login-container { + width: 100%; + max-width: 440px; + } + + .login-card { + background: rgba(255, 255, 255, 0.05); + backdrop-filter: blur(10px); + border-radius: 2rem; + padding: 2rem 1.8rem 2.2rem 1.8rem; + box-shadow: 0 25px 45px -12px rgba(0, 0, 0, 0.4), 0 0 0 1px rgba(255, 255, 255, 0.08); + transition: transform 0.2s ease; + } + + .login-card:hover { + transform: translateY(-3px); + } + + .brand { + text-align: center; + margin-bottom: 2rem; + } + + .brand h1 { + font-size: 1.9rem; + font-weight: 600; + background: linear-gradient(120deg, #c084fc, #60a5fa); + background-clip: text; + -webkit-background-clip: text; + color: transparent; + letter-spacing: -0.3px; + } + + .brand p { + color: #94a3b8; + font-size: 0.9rem; + margin-top: 0.5rem; + } + + .input-group { + margin-bottom: 1.5rem; + } + + .input-group label { + display: block; + color: #e2e8f0; + font-size: 0.85rem; + font-weight: 500; + margin-bottom: 0.4rem; + letter-spacing: 0.3px; + } + + .input-group input { + width: 100%; + padding: 0.85rem 1rem; + background: rgba(15, 23, 42, 0.6); + border: 1px solid #334155; + border-radius: 1rem; + font-size: 1rem; + color: #f1f5f9; + transition: all 0.2s ease; + outline: none; + } + + .input-group input:focus { + border-color: #818cf8; + box-shadow: 0 0 0 3px rgba(129, 140, 248, 0.3); + background: rgba(30, 41, 59, 0.8); + } + + .input-group input::placeholder { + color: #64748b; + font-size: 0.9rem; + } + + button { + width: 100%; + background: linear-gradient(95deg, #4f46e5, #7c3aed); + border: none; + padding: 0.85rem; + border-radius: 1.5rem; + font-weight: 600; + font-size: 1rem; + color: white; + cursor: pointer; + transition: all 0.2s ease; + margin-top: 0.5rem; + box-shadow: 0 4px 12px rgba(79, 70, 229, 0.3); + } + + button:hover { + background: linear-gradient(95deg, #6366f1, #8b5cf6); + transform: scale(0.98); + box-shadow: 0 6px 16px rgba(79, 70, 229, 0.4); + } + + .footer-note { + text-align: center; + margin-top: 1.8rem; + font-size: 0.75rem; + color: #64748b; + } + + .footer-note a { + color: #a78bfa; + text-decoration: none; + } + + .footer-note a:hover { + text-decoration: underline; + } + + /* Simple error styling (if any flash messages appear) */ + .error-message { + background: rgba(239, 68, 68, 0.15); + border-left: 4px solid #ef4444; + padding: 0.75rem; + border-radius: 0.75rem; + margin-bottom: 1.5rem; + color: #fecaca; + font-size: 0.85rem; + } + </style> +</head> +<body> + <div class="login-container"> + <div class="login-card"> + <div class="brand"> + <h1> GeneNetwork</h1> + <p>AI-powered genomics search</p> + </div> + + <!-- Show error if provided via query param or flash (optional) --> + {% with messages = get_flashed_messages(with_categories=true) %} + {% if messages %} + {% for category, message in messages %} + <div class="error-message">{{ message }}</div> + {% endfor %} + {% endif %} + {% endwith %} + + <form method="post" action="/login"> + <div class="input-group"> + <label>📧 Email address</label> + <input type="email" name="email" placeholder="you@example.org" required autofocus> + </div> + <div class="input-group"> + <label>🔒 Password</label> + <input type="password" name="password" placeholder="••••••••" required> + </div> + <button type="submit">Sign in →</button> + </form> + <div class="footer-note"> + Secure access · <a href="#">Request access</a> + </div> + </div> + </div> +</body> +</html> diff --git a/aisearch/src/web/templates/partials/response.html b/aisearch/src/web/templates/partials/response.html new file mode 100644 index 0000000..cc8a0fd --- /dev/null +++ b/aisearch/src/web/templates/partials/response.html @@ -0,0 +1,12 @@ +<div class="message message-ai"> + <div class="avatar">AI</div> + <div class="message-content"> + {% include "partials/response_body.html" %} + </div> + <button type="button" class="copy-btn" + _="on click + writeText(my previousElementSibling's innerText) on navigator.clipboard + put 'Copied!' into me + wait 1.5s + put 'Copy' into me">Copy</button> +</div> diff --git a/aisearch/src/web/templates/partials/response_body.html b/aisearch/src/web/templates/partials/response_body.html new file mode 100644 index 0000000..4b305b5 --- /dev/null +++ b/aisearch/src/web/templates/partials/response_body.html @@ -0,0 +1,55 @@ +<div class="ai-response"> + {% if data.error or data.status == 'error' %} + <div class="status-error">Search encountered an error</div> + <div class="error-display">{{ data.error or data.message or 'Unknown error' }}</div> + {% if data.raw_response %} + <div class="raw-response"> + <strong>Raw Response:</strong><br> + {{ data.raw_response }} + </div> + {% endif %} + {% else %} + {% if data.status == 'success' %} + <div class="status-success">Search completed successfully</div> + {% elif data.status == 'partial_success' %} + <div class="status-partial">Partial success - some data sources unavailable</div> + {% endif %} + + {% if data.summary %} + <div class="summary">{{ data.summary }}</div> + {% endif %} + + {% if grouped and grouped|length > 0 %} + {% for type, items in grouped.items() %} + {% set display_type = type[0]|upper + type[1:] + 's' %} + <h3>{{ display_type }} ({{ items|length }})</h3> + <ul class="card-list"> + {% for item in items %} + {% set url = item.url or item.link or '#' %} + {% set name = item.name or item.symbol or item.trait_name or 'Unnamed' %} + <li class="card-item"> + <div class="card-title"> + <a href="{{ url }}" target="_blank">{{ name }}</a> + </div> + {% if item.description %} + <div class="card-description">{{ item.description }}</div> + {% endif %} + {% if item.extra %} + <div class="card-meta">{{ item.extra }}</div> + {% endif %} + </li> + {% endfor %} + </ul> + {% endfor %} + {% else %} + <div class="note-box">No specific results were found for this query.</div> + {% endif %} + + {% if data.note %} + <div class="note-box">{{ data.note }}</div> + {% endif %} + {% if data.access_note %} + <div class="access-note">{{ data.access_note }}</div> + {% endif %} + {% endif %} +</div> diff --git a/aisearch/src/web/templates/partials/stream_shell.html b/aisearch/src/web/templates/partials/stream_shell.html new file mode 100644 index 0000000..24d4ffe --- /dev/null +++ b/aisearch/src/web/templates/partials/stream_shell.html @@ -0,0 +1,95 @@ +<div class="message message-user"> + <div class="message-content"> + {{ query }} + </div> +</div> + +<div class="message message-ai message-ai-stream"> + <div class="avatar">AI</div> + <div class="message-content stream-shell" + hx-ext="sse" + sse-connect="{{ url_for('search_stream') }}?q={{ query|urlencode }}" + sse-close="stream_end"> + <div class="stream-shell-header"> + <div> + <div class="stream-shell-kicker">Live Research Trace</div> + <h2>Cross-checking RAG, GraphRAG, and AgentSearch</h2> + </div> + <div class="stream-shell-state" sse-swap="search_state" hx-swap="innerHTML"> + <span class="stream-stage-badge is-working">Starting</span> + </div> + </div> + + <div class="stream-shell-intro"> + Intermediate system output stays muted while the final answer is assembled. + </div> + + <div class="stream-stage-grid"> + <section class="stream-stage-card is-muted"> + <div class="stream-stage-head"> + <span class="stream-stage-name">RAGSearch</span> + <div class="stream-stage-meta"> + <span class="stream-stage-timing" sse-swap="rag_timing" hx-swap="innerHTML"></span> + <span class="stream-stage-state" sse-swap="rag_done" hx-swap="innerHTML" hx-on:htmx:after-swap="this.closest('.stream-stage-card').querySelector('.stream-init-msg').style.display='none'"> + <span class="stream-stage-badge is-working">Running</span> + </span> + </div> + </div> + <div class="stream-stage-body-scroll" sse-swap="rag_final_html" hx-target="find .stream-stage-body" hx-swap="innerHTML" hx-on:htmx:after-swap="this.scrollTop = this.scrollHeight"> + <div class="stream-init-msg">Initializing search…</div> + <div class="stream-stage-body" sse-swap="rag_chunk" hx-target="this" hx-swap="beforeend" hx-on:htmx:after-swap="this.parentElement.scrollTop = this.parentElement.scrollHeight"></div> + </div> + </section> + + <section class="stream-stage-card is-muted"> + <div class="stream-stage-head"> + <span class="stream-stage-name">GraphRAGSearch</span> + <div class="stream-stage-meta"> + <span class="stream-stage-timing" sse-swap="grag_timing" hx-swap="innerHTML"></span> + <span class="stream-stage-state" sse-swap="grag_done" hx-swap="innerHTML" hx-on:htmx:after-swap="this.closest('.stream-stage-card').querySelector('.stream-init-msg').style.display='none'"> + <span class="stream-stage-badge is-working">Running</span> + </span> + </div> + </div> + <div class="stream-stage-body-scroll" sse-swap="grag_final_html" hx-target="find .stream-stage-body" hx-swap="innerHTML" hx-on:htmx:after-swap="this.scrollTop = this.scrollHeight"> + <div class="stream-init-msg">Initializing search…</div> + <div class="stream-stage-body" sse-swap="grag_chunk" hx-target="this" hx-swap="beforeend" hx-on:htmx:after-swap="this.parentElement.scrollTop = this.parentElement.scrollHeight"></div> + </div> + </section> + + <section class="stream-stage-card is-muted"> + <div class="stream-stage-head"> + <span class="stream-stage-name">AgentSearch</span> + <div class="stream-stage-meta"> + <span class="stream-stage-timing" sse-swap="agent_timing" hx-swap="innerHTML"></span> + <span class="stream-stage-state" sse-swap="agent_done" hx-swap="innerHTML" hx-on:htmx:after-swap="this.closest('.stream-stage-card').querySelector('.stream-init-msg').style.display='none'"> + <span class="stream-stage-badge is-working">Running</span> + </span> + </div> + </div> + <div class="stream-stage-body-scroll" sse-swap="agent_final_html" hx-target="find .stream-stage-body" hx-swap="innerHTML" hx-on:htmx:after-swap="this.scrollTop = this.scrollHeight"> + <div class="stream-init-msg">Initializing search…</div> + <div class="stream-stage-body" sse-swap="agent_chunk" hx-target="this" hx-swap="beforeend" hx-on:htmx:after-swap="this.parentElement.scrollTop = this.parentElement.scrollHeight"></div> + </div> + </section> + </div> + + <section class="stream-final-card"> + <div class="stream-final-head"> + <span class="stream-final-kicker">Final Answer</span> + <div class="stream-final-meta"> + <span class="stream-final-timing" sse-swap="total_timing" hx-swap="innerHTML"></span> + <span class="stream-final-note">Rendered after the three systems complete</span> + </div> + </div> + <div class="stream-final-body" sse-swap="final_html" hx-swap="innerHTML"> + <div class="stream-final-status is-waiting"> + <div class="stream-final-status-icon">🔍</div> + <div class="stream-final-status-text">Waiting for searches to complete…</div> + </div> + </div> + </section> + + <div hidden sse-swap="stream_end" hx-on:htmx:after-swap="window.onStreamEnd()"></div> + </div> +</div> |
