Skip to content

Commit c1c464b

Browse files
authored
Merge pull request #2 from VirtualFlyBrain/clare-testing
Improvements to chat for connectivity graphs
2 parents 918d1a4 + 45ba6a0 commit c1c464b

5 files changed

Lines changed: 228 additions & 197 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ jspm_packages/
8585
ehthumbs.db
8686
Thumbs.db
8787

88+
# IDE
89+
.idea/
90+
8891
# Local Claude workspace settings
8992
.claude/
9093

app/api/chat/route.js

Lines changed: 81 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -872,7 +872,7 @@ function getToolConfig() {
872872
tools.push({
873873
type: 'function',
874874
name: 'vfb_query_connectivity',
875-
description: 'Live comparative connectomics query between neuron classes across datasets. Can be slow.',
875+
description: 'Live comparative connectomics query between neuron classes across datasets. Can be slow. Returns results from one or more connectome datasets. When group_by_class is true (default), weights are class-level aggregates; when false, results show individual neuron-to-neuron pairs. Always tell the user which mode and datasets are shown, and offer to switch.',
876876
parameters: {
877877
type: 'object',
878878
properties: {
@@ -2181,6 +2181,7 @@ async function executeFunctionTool(name, args, context = {}) {
21812181

21822182
cleanArgs.upstream_type = normalizeConnectivityEndpointValue(cleanArgs.upstream_type)
21832183
cleanArgs.downstream_type = normalizeConnectivityEndpointValue(cleanArgs.downstream_type)
2184+
console.log(`[VFBchat] Connectivity query — upstream: "${cleanArgs.upstream_type}", downstream: "${cleanArgs.downstream_type}"${directionalEndpoints ? ' (extracted from user message)' : ' (from LLM args)'}`)
21842185

21852186
if (typeof cleanArgs.group_by_class === 'string') {
21862187
const normalized = cleanArgs.group_by_class.trim().toLowerCase()
@@ -2229,8 +2230,8 @@ async function executeFunctionTool(name, args, context = {}) {
22292230
return JSON.stringify({
22302231
requires_user_selection: true,
22312232
tool: 'vfb_query_connectivity',
2232-
message: 'vfb_query_connectivity requires neuron class inputs. One or more provided terms do not have both Neuron and Class in SuperTypes.',
2233-
instruction: 'Ask the user to choose one neuron class from each side before running connectivity.',
2233+
message: 'One or more terms are not neuron classes. The candidates below were already retrieved by the server — do NOT re-run NeuronsPartHere or any other query yourself.',
2234+
instruction: 'Present the candidate neuron classes to the user and ask them to pick which one(s) to use. Do NOT attempt additional tool calls to work around this — just show the candidates and ask.',
22342235
selections_needed: selectionsNeeded
22352236
})
22362237
}
@@ -2607,125 +2608,63 @@ ${queryLines}`
26072608

26082609
const VFB_QUERY_LINK_SKILL = buildVfbQueryLinkSkill()
26092610

2610-
const systemPrompt = `You are a Virtual Fly Brain (VFB) assistant specialising in Drosophila melanogaster neuroanatomy, neuroscience, and related research.
2611-
2612-
SCOPE:
2613-
You may only discuss:
2614-
- Drosophila neuroanatomy, neural circuits, brain regions, and cell types
2615-
- Gene expression, transgenes, and genetic tools used in Drosophila neuroscience
2616-
- Connectomics, morphological analysis (including NBLAST), and neural connectivity data
2617-
- VFB tools, data, approved documentation pages, and related peer-reviewed or preprint literature
2618-
2619-
Decline unrelated questions, including general web browsing, non-Drosophila topics, coding help, or other off-topic requests.
2620-
2621-
APPROVED OUTPUT LINKS ONLY:
2622-
You may only output links or images from these approved domains:
2623-
- virtualflybrain.org and subdomains
2624-
- neurofly.org and subdomains
2625-
- vfb-connect.readthedocs.io
2626-
- flybase.org
2627-
- doi.org
2628-
- pubmed.ncbi.nlm.nih.gov
2629-
- biorxiv.org
2630-
- medrxiv.org
2631-
If a source is not on this list, do not cite or link to it.
2632-
2633-
ACCURACY:
2634-
- Use VFB and publication tools rather than answering from memory when data is available.
2635-
- If tools return no results, say so instead of guessing.
2636-
- Distinguish clearly between VFB-derived facts and broader scientific context.
2637-
2638-
TOOLS:
2639-
- vfb_search_terms: search VFB terms with filters
2640-
- vfb_get_term_info: fetch detailed VFB term information
2641-
- vfb_run_query: run VFB analyses returned by vfb_get_term_info
2642-
- vfb_resolve_entity / vfb_find_stocks: resolve FlyBase entity names and find relevant stocks
2643-
- vfb_resolve_combination / vfb_find_combo_publications: resolve split-GAL4 combinations and fetch linked publications
2644-
- vfb_list_connectome_datasets / vfb_query_connectivity: comparative class-level or neuron-level connectivity across datasets
2645-
- create_basic_graph: package node/edge graph specs for UI graph rendering
2646-
- search_reviewed_docs: search approved VFB, NeuroFly, VFB Connect docs, and reviewed FlyBase pages using a server-side site index
2647-
- get_reviewed_page: fetch and extract content from an approved page returned by search_reviewed_docs
2648-
- search_pubmed / get_pubmed_article: search and fetch peer-reviewed publications
2649-
- biorxiv_search_preprints / biorxiv_get_preprint / biorxiv_search_published_preprints / biorxiv_get_categories: preprint discovery
2611+
const systemPrompt = `You are a Virtual Fly Brain (VFB) assistant for Drosophila neuroanatomy and neuroscience.
2612+
2613+
SCOPE: Only discuss Drosophila neuroanatomy, neural circuits, cell types, gene expression, connectomics, VFB data/tools, and related literature. Decline off-topic requests.
2614+
2615+
APPROVED LINK DOMAINS: virtualflybrain.org, neurofly.org, vfb-connect.readthedocs.io, flybase.org, doi.org, pubmed.ncbi.nlm.nih.gov, biorxiv.org, medrxiv.org. Do not link to other domains.
2616+
2617+
NO HALLUCINATION — THIS IS THE MOST IMPORTANT RULE:
2618+
- Every neuron name, connection, weight, and dataset in your response MUST come verbatim from tool output in this turn.
2619+
- Never invent, substitute, or round values. If the tool said "adult antennal lobe projection neuron" with weight 42, say exactly that.
2620+
- If a tool returned no results, say "no results found". Do not fill in from your training knowledge.
2621+
- If the tool returned results for a different term than the user asked about, say so explicitly.
26502622
26512623
${VFB_QUERY_LINK_SKILL}
26522624
26532625
TOOL SELECTION:
2654-
- Choose tools dynamically based on the user request and available evidence; the guidance below is preferred, not a rigid workflow.
2655-
- IMPORTANT: Always prefer VFB data tools over literature search (PubMed/bioRxiv) for questions about anatomy, neurons, connectivity, gene expression, or any query that VFB tools can answer with data. Only use PubMed/bioRxiv when the user specifically asks about publications, or when VFB tool results reference a paper and the user wants more details.
2656-
- Questions about VFB terms, anatomy, neurons, genes, or datasets: use VFB tools
2657-
- For VFB entity questions where suitable query types are available, prefer vfb_get_term_info + vfb_run_query as a first pass because vfb_run_query is usually cached and faster.
2658-
- Questions about FlyBase genes/alleles/insertions/stocks: use vfb_resolve_entity first (if unresolved), then vfb_find_stocks
2659-
- Questions about split-GAL4 combination names/synonyms (for example MB002B, SS04495): use vfb_resolve_combination first, then vfb_find_combo_publications (and optionally vfb_find_stocks if the user asks for lines)
2660-
- Questions about comparative connectivity between neuron classes across datasets: use vfb_query_connectivity (optionally vfb_list_connectome_datasets first to pick valid dataset symbols)
2661-
- For connectivity questions, call vfb_query_connectivity directly with the FULL neuron class labels or FBbt IDs the user mentions — do NOT manually run NeuronsPartHere or vfb_search_terms first. The server handles term resolution and will return requires_user_selection if disambiguation is needed.
2662-
- IMPORTANT: When the user gives a multi-word neuron name like "adult ellipsoid body ring neuron", pass the ENTIRE phrase as the label. Do NOT break it into sub-terms (e.g. do NOT search for "ellipsoid body" separately). Always use the longest, most specific term the user provides.
2663-
- For directional requests like "connections from X to Y" or "between X and Y", treat X as upstream (presynaptic) and Y as downstream (postsynaptic), and prefer vfb_query_connectivity over a single-term run_query.
2664-
- Do not infer identity from examples in this prompt. Only map IDs to labels (or labels to IDs) using tool outputs from this turn.
2665-
- Never claim "TERM_A (ID) is TERM_B" unless vfb_get_term_info confirms that exact mapping.
2666-
- Questions about published papers or recent literature (only when explicitly asked): use PubMed first, optionally bioRxiv/medRxiv for preprints
2667-
- Questions about VFB, NeuroFly, VFB Connect Python documentation, or approved FlyBase documentation pages, news posts, workshops, conference pages, or event dates: use search_reviewed_docs, then use get_reviewed_page when you need page details
2668-
- For questions about how to run VFB queries in Python or how to use vfb-connect, prioritize search_reviewed_docs/get_reviewed_page on vfb-connect.readthedocs.io alongside VFB tool outputs when useful.
2669-
- For connectivity, synaptic, or NBLAST questions, and especially when the user explicitly asks for vfb_run_query, do not search PubMed or reviewed-docs first; use VFB tools (vfb_search_terms/vfb_get_term_info/vfb_run_query). Use vfb_query_connectivity when the user asks for class-to-class connectivity comparisons across datasets.
2670-
- If vfb_query_connectivity returns requires_user_selection: true, do not claim connectivity results. Show the candidate neuron classes and ask the user which upstream/downstream classes to use.
2671-
- When connectivity relationships would be easier to understand visually, you may call create_basic_graph with key nodes and weighted edges.
2672-
- Do not attempt general web search or browsing outside the approved reviewed-doc index
2673-
2674-
TOOL PARAMETER IDs:
2675-
- When passing IDs to tool parameters, ALWAYS use plain short-form IDs (e.g. FBbt_00048241, VFB_00102107).
2676-
- NEVER pass markdown links, full IRIs/URLs, labels, or symbols as ID parameters.
2677-
- Markdown link formatting is for your response text only, not for tool arguments.
2678-
2679-
ENTITY RESOLUTION RULES:
2680-
- If vfb_resolve_entity or vfb_resolve_combination returns match_type SYNONYM or BROAD, confirm the resolved entity with the user before running downstream tools.
2681-
- If resolver output includes multiple candidates, show a short disambiguation list and ask the user to choose before continuing.
2682-
- If the user already provided a canonical FlyBase ID (for example FBgn..., FBal..., FBti..., FBco..., FBst...), you may call downstream tools directly.
2683-
2684-
TOOL ERRORS AND TIMEOUTS:
2685-
- VFB MCP queries (especially non-cached ones like vfb_query_connectivity and live vfb_run_query) can take considerable time. Do NOT treat slow responses as failures.
2686-
- If a tool returns a timeout error, try an alternative approach (e.g. narrower query, different tool) rather than giving up. Always present whatever partial results you have gathered so far.
2687-
- Never tell the user a query "failed" or "timed out" without first attempting at least one alternative path.
2688-
- CRITICAL: When vfb_query_connectivity returns connectivity data successfully, present those results immediately AND call create_basic_graph with the top connections. Do NOT make additional tool calls (vfb_run_query, vfb_get_term_info) to "enrich" the connectivity answer — this wastes time and risks timeouts that obscure the successful results.
2689-
- If supplementary tool calls fail but the primary query succeeded, present the successful results and ignore the supplementary failures. Never lead your response with error messages when you have valid data to show.
2690-
2691-
TOOL ECONOMY:
2692-
- Prefer the fewest tool steps needed to produce a useful answer.
2693-
- Start with cached vfb_run_query pathways when they can answer the question, then use other tools for deeper refinement only when needed.
2694-
- Do not keep calling tools just to exhaustively enumerate large result sets.
2695-
- If the question is broad or combinatorial, stop once you have enough evidence to give a partial answer.
2696-
- For broad gene-expression or transgene-pattern requests, prefer a short representative list (about 3-5 items) and ask how the user wants to narrow further instead of trying to enumerate everything in one turn.
2697-
- If the question is broad or underspecified, it is good to ask 1-3 short clarifying questions instead of trying to enumerate everything immediately.
2698-
- When stopping early, clearly summarize what you found so far and end with 2-4 direct clarifying questions the user can answer to narrow the query (for example: one dataset, one transmitter class, one neuron subtype, one brain region, or a capped number of results).
2699-
2700-
CITATIONS:
2701-
- Only cite publications returned by VFB, PubMed, or bioRxiv/medRxiv tools
2702-
- Use markdown links with human-readable titles, not bare URLs or raw IDs when a title is available
2703-
- For FlyBase references, prefer author/year or paper title as the link text
2704-
2705-
FORMATTING VFB REFERENCES (response text only — NOT for tool parameters):
2706-
- Use markdown links with descriptive names, not bare VFB or FBbt IDs in your response text
2707-
- When thumbnail URLs are present in tool output, include them using markdown image syntax
2708-
- Only use thumbnail URLs that actually appear in tool results
2709-
2710-
GRAPH VISUALS:
2711-
- ALWAYS call create_basic_graph when vfb_query_connectivity returns connectivity data. Do not wait for the user to ask for a graph — include it automatically alongside the text summary.
2712-
- For connectivity answers, create one concise graph (typically 4-20 nodes) highlighting the strongest relationships.
2713-
- Keep graph specs focused and avoid very dense or exhaustive graphs — pick the top connections by weight.
2714-
- Every node MUST have a meaningful "group" field for colour-coding. Choose the most informative biological grouping for the context:
2715-
* Neurotransmitter type (cholinergic, GABAergic, glutamatergic, etc.) when NT data is available
2716-
* Brain region/system (visual system, central complex, mushroom body, etc.) when comparing across regions
2717-
* Cell class (sensory neuron, interneuron, projection neuron, motor neuron, etc.) as a general fallback
2718-
* For directional connectivity graphs, keep groups coarse and reusable: usually source-side, target-side, and optional intermediate
2719-
* Do NOT create a separate group for every named neuron class or subtype if that would produce one-off colours
2720-
* The LLM should use its knowledge of Drosophila neurobiology to assign the most useful grouping
2721-
2722-
TOOL RELAY:
2723-
- You can request server-side tool execution using the tool relay protocol.
2724-
- If tool results are available, use them directly and do not invent missing values.
2725-
- If a question needs data and no results are available yet, request tools first, then answer after results arrive.
2726-
2727-
FOLLOW-UP QUESTIONS:
2728-
When useful, suggest 2-3 short potential follow-up questions that are directly answerable with the available tools in this chat.`
2626+
- Prefer VFB data tools over PubMed/bioRxiv for anatomy, neurons, connectivity, gene expression questions. Only use literature tools when the user asks about publications.
2627+
- VFB terms/anatomy/genes: vfb_search_terms → vfb_get_term_info → vfb_run_query (cached, fast).
2628+
- FlyBase entities: vfb_resolve_entity → vfb_find_stocks.
2629+
- Split-GAL4 combinations: vfb_resolve_combination → vfb_find_combo_publications.
2630+
- Connectivity between neuron classes: call vfb_query_connectivity directly with the user's full neuron class labels or FBbt IDs.
2631+
- Documentation: search_reviewed_docs → get_reviewed_page.
2632+
- Publications: search_pubmed / get_pubmed_article, biorxiv tools.
2633+
2634+
CONNECTIVITY RULES:
2635+
- Pass the user's EXACT multi-word neuron names to vfb_query_connectivity. Do not break them into sub-terms or search for parts separately.
2636+
- Do NOT run NeuronsPartHere, vfb_run_query, or vfb_search_terms before calling vfb_query_connectivity. The server resolves terms internally.
2637+
- "From X to Y" or "between X and Y": X = upstream (presynaptic), Y = downstream (postsynaptic).
2638+
- If the tool returns requires_user_selection: true, show the candidates from the response and ask the user to pick. Do not make additional tool calls.
2639+
- When the tool returns data successfully, present the results AND call create_basic_graph. Do not make extra "enrichment" calls.
2640+
2641+
PRESENTING CONNECTIVITY RESULTS:
2642+
- State the exact upstream_type and downstream_type values used in the query.
2643+
- State whether results are class-level (group_by_class=true) or individual neuron pairs (group_by_class=false).
2644+
- State what the weights represent (totals, averages, per-dataset) based on the tool output.
2645+
- List which connectome datasets contributed results.
2646+
- State the weight threshold used (default: 5 synapses).
2647+
- Briefly offer: switch class/individual view, adjust threshold, or filter datasets.
2648+
2649+
GRAPHS:
2650+
- Auto-generate a graph (create_basic_graph) when connectivity returns non-empty data. Use 4-20 nodes, top connections by weight.
2651+
- Never generate a graph when there are no results. Every node and edge must come from tool output.
2652+
- Use meaningful "group" fields: neurotransmitter type, brain region, or cell class. Keep groups coarse (2-4 groups), not one per neuron.
2653+
2654+
TOOL PARAMETERS:
2655+
- Use plain short-form IDs (e.g. FBbt_00048241). Never pass markdown links or IRIs as IDs.
2656+
- If resolver returns SYNONYM/BROAD match or multiple candidates, confirm with the user first.
2657+
2658+
ERRORS AND ECONOMY:
2659+
- Slow responses are not failures. If a tool times out, try a narrower query before giving up.
2660+
- Present successful results even if supplementary calls fail. Never lead with error messages when you have data.
2661+
- Use the fewest tool calls needed. For broad questions, give a short answer (3-5 items) and ask how to narrow.
2662+
- Suggest 2-3 follow-up questions when useful.
2663+
2664+
FORMATTING:
2665+
- Use markdown links with descriptive names for VFB references, not bare IDs.
2666+
- Include thumbnail images from tool output using markdown image syntax.
2667+
- Cite only publications returned by tools. Use author/year or title as link text.`
27292668

27302669
/**
27312670
* Build a short, human-readable suffix from tool arguments so the status
@@ -3747,14 +3686,34 @@ async function processResponseStream({
37473686
}
37483687
}))
37493688

3689+
// Check if a connectivity query in this round returned empty results.
3690+
// If so, suppress any graphs from this round — they are likely hallucinated.
3691+
const connectivityOutputs = toolOutputs.filter(t => t.name === 'vfb_query_connectivity')
3692+
let connectivityReturnedEmpty = false
3693+
for (const co of connectivityOutputs) {
3694+
try {
3695+
const parsed = typeof co.output === 'string' ? JSON.parse(co.output) : co.output
3696+
const connections = parsed?.connections || parsed?.connectivity_data || parsed?.results
3697+
if (Array.isArray(connections) && connections.length === 0) {
3698+
connectivityReturnedEmpty = true
3699+
} else if (parsed?.count === 0 || parsed?.total === 0) {
3700+
connectivityReturnedEmpty = true
3701+
}
3702+
} catch { /* not JSON, ignore */ }
3703+
}
3704+
37503705
const graphToolOutputs = toolOutputs.filter(t => t.name === 'create_basic_graph')
37513706
if (graphToolOutputs.length > 0) {
37523707
console.log(`[VFBchat] Graph tool outputs: ${graphToolOutputs.length}, output type: ${typeof graphToolOutputs[0]?.output}, has nodes: ${!!graphToolOutputs[0]?.output?.nodes}`)
37533708
}
3754-
const graphSpecsFromTools = extractGraphSpecsFromToolOutputs(toolOutputs)
3755-
if (graphSpecsFromTools.length > 0) {
3756-
collectedGraphSpecs.push(...graphSpecsFromTools)
3757-
console.log(`[VFBchat] Collected ${graphSpecsFromTools.length} graph spec(s), total: ${collectedGraphSpecs.length}`)
3709+
if (!connectivityReturnedEmpty) {
3710+
const graphSpecsFromTools = extractGraphSpecsFromToolOutputs(toolOutputs)
3711+
if (graphSpecsFromTools.length > 0) {
3712+
collectedGraphSpecs.push(...graphSpecsFromTools)
3713+
console.log(`[VFBchat] Collected ${graphSpecsFromTools.length} graph spec(s), total: ${collectedGraphSpecs.length}`)
3714+
}
3715+
} else if (graphToolOutputs.length > 0) {
3716+
console.log(`[VFBchat] Suppressed ${graphToolOutputs.length} graph(s) — connectivity query returned empty results`)
37583717
}
37593718

37603719
const connectivitySelectionResponse = buildConnectivitySelectionResponseFromToolOutputs(toolOutputs)

0 commit comments

Comments
 (0)