# cpkee/techjam-conversational-search architecture

> Architecture diagram of the GitHub repository cpkee/techjam-conversational-search, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/cpkee/techjam-conversational-search
- Repository: https://github.com/cpkee/techjam-conversational-search

## Overview

#### Purpose and execution model

This repository implements and evaluates a turn-based conversational e-commerce search agent. The agent receives an anonymized profile plus customer messages, may ask one structured clarification per turn, and returns up to 10 catalog `parent_asin` recommendations. It has at most 10 turns to surface the hidden target.

Primary entry points are `starter/agent.py` (agent implementation) and `evaluator/local_evaluator.py` (local session simulator and scorer). The submission package mirrors the runtime implementation under `submission/agent.py` and `submission/starter/`.

#### Agent orchestration and session state

`Agent.reset()` initializes per-session state from the preference profile; `Agent.respond()` drives each turn, combines incoming language with accumulated state, decides whether/how to clarify, and emits the API-contract response.

Core orchestration and state boundaries:
- `starter/agent.py`
- `starter/slot_state.py`
- `starter/profile_store.py`

State is session-scoped: profile-derived preferences, extracted conversational slots, prior questions, and retrieval context must not leak between session IDs.

#### Intent extraction, slot schema, and clarification policy

The agent converts user utterances into normalized product constraints such as category, material, color, size, style, brand, budget, feature, and use case. A fixed schema constrains both extracted fields and the evaluator-facing `ask_attribute` value.

Clarification logic selects an unresolved, high-value field and produces a natural-language question. Policy must balance information gathering against early recommendation because successful Top-10 results end sessions.

Relevant components:
- `starter/clarify.py`
- `starter/slot_schema.py`
- `starter/policy.py`

#### Retrieval and catalog access

Candidate generation operates over the local 50,000-product catalog, keyed by `parent_asin`. The catalog index supports lexical/catalog-field retrieval, while dense retrieval embeds user intent and catalog text for semantic browsing-style requests. Candidate IDs are subsequently available for ranking and response serialization.

Key paths:
- `starter/catalog_index.py`
- `starter/embeddings.py`
- `data/catalog.bge_embeddings_ids.json`

The full catalog (`data/catalog.jsonl`) is an externally downloaded runtime asset, not present in this tree. Embedding vectors are cached locally; first use may generate `data/catalog.bge_embeddings.npy`.

#### Fusion and ranking stages

The system combines retrieval signals and applies a ranking layer to produce the final bounded recommendation list. Fusion is the boundary between candidate generators and rank ordering; rank output must preserve exact catalog `parent_asin` IDs.

Core paths:
- `starter/fusion.py`
- `starter/rankllm.py`
- `starter/stage2.py`

This multi-stage design permits cheap broad retrieval before more selective model-assisted ranking.

#### Model and service dependencies

The runtime uses Python 3.10+, NumPy, PyTorch, and Transformers. Dense embeddings default to Hugging Face model `BAAI/bge-small-en-v1.5`, using Apple MPS when available or CPU otherwise.

LLM-backed extraction/ranking/response behavior calls a locally served Ollama API at `http://localhost:11434/api/chat`, with `qwen3:8b` configured by the agent. Service failures are handled as no-op/fallback behavior rather than evaluator crashes.

Integration boundary:
- `starter/ollama_client.py`
- `starter/embeddings.py`
- `starter/agent.py`

#### Evaluation and delivery boundary

The deterministic evaluator reads released sessions, invokes the Agent API turn-by-turn, validates/scorers recommendations, and writes aggregate/per-session results. It measures Hit Rate@10, MRR, and mean time to conversion; private sessions are held externally.

Key paths:
- `evaluator/local_evaluator.py`
- `data/public_set.jsonl`
- `docs/agent_api_contract.json`

For delivery, `submission/agent.py` and its copied `submission/starter/` package form the portable submission artifact; changes intended for grading must be reflected there if submission packaging is used.

Main entry points: [`starter/agent.py`](https://github.com/cpkee/techjam-conversational-search/blob/main/starter/agent.py) (Agent API), [`starter/embeddings.py`](https://github.com/cpkee/techjam-conversational-search/blob/main/starter/embeddings.py) (Dense retrieval), [`starter/rankllm.py`](https://github.com/cpkee/techjam-conversational-search/blob/main/starter/rankllm.py) (LLM ranker), [`starter/fusion.py`](https://github.com/cpkee/techjam-conversational-search/blob/main/starter/fusion.py) (Signal fusion)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_agent["Agent runtime"]
  node_agent["Agent API<br/>orchestrator<br/>[agent.py]"]
  node_profile_store[("Profile store<br/>session state<br/>[profile_store.py]")]
  node_slot_state[("Slot state<br/>session state<br/>[slot_state.py]")]
  node_slot_schema["Slot schema<br/>constraint schema<br/>[slot_schema.py]"]
  node_clarify["Clarification builder<br/>dialog component<br/>[clarify.py]"]
  node_policy["Turn policy<br/>decision policy<br/>[policy.py]"]
end

subgraph group_retrieval["Retrieval and ranking"]
  node_catalog_index[("Catalog index<br/>lexical retrieval<br/>[catalog_index.py]")]
  node_embedding_store[("Embedding ID map<br/>embedding cache")]
  node_embeddings["Dense retrieval<br/>semantic retrieval<br/>[embeddings.py]"]
  node_fusion["Signal fusion<br/>candidate fusion<br/>[fusion.py]"]
  node_rankllm["LLM ranker<br/>model-assisted ranking<br/>[rankllm.py]"]
  node_stage2["Final ranking<br/>ranking stage<br/>[stage2.py]"]
end

subgraph group_models["Local model dependencies"]
  node_ollama{{"Ollama chat API<br/>local service<br/>[ollama_client.py]"}}
  node_bge_model{{"BGE embedding model<br/>Hugging Face model<br/>[embeddings.py]"}}
end

subgraph group_evaluation["Evaluation and delivery"]
  node_evaluator["Local evaluator<br/>session simulator<br/>[local_evaluator.py]"]
  node_public_sessions["Public sessions<br/>evaluation data<br/>[public_set.jsonl]"]
  node_api_contract["Agent API contract<br/>interface contract"]
  node_submission["Submission artifact<br/>portable package<br/>[agent.py]"]
end

node_evaluator -->|"invokes turns"| node_agent
node_public_sessions -->|"released sessions"| node_evaluator
node_api_contract -->|"validates"| node_evaluator
node_api_contract -->|"response contract"| node_agent
node_agent -->|"initializes"| node_profile_store
node_agent -->|"updates per turn"| node_slot_state
node_slot_schema -->|"constrains slots"| node_slot_state
node_agent -->|"chooses action"| node_policy
node_policy -->|"requests question"| node_clarify
node_clarify -->|"uses ask field"| node_slot_schema
node_agent -->|"retrieves candidates"| node_catalog_index
node_agent -->|"semantic retrieval"| node_embeddings
node_embeddings -->|"loads ID mapping"| node_embedding_store
node_embeddings -->|"embeds intent"| node_bge_model
node_catalog_index -->|"lexical candidates"| node_fusion
node_embeddings -->|"dense candidates"| node_fusion
node_fusion -->|"fused candidates"| node_rankllm
node_rankllm -->|"rerank signal"| node_stage2
node_stage2 -->|"Top-10 parent_asins"| node_agent
node_agent -.->|"extraction and response"| node_ollama
node_rankllm -.->|"LLM ranking"| node_ollama
node_submission -.->|"mirrors runtime"| node_agent

click node_agent "https://github.com/cpkee/techjam-conversational-search/blob/main/starter/agent.py"
click node_profile_store "https://github.com/cpkee/techjam-conversational-search/blob/main/starter/profile_store.py"
click node_slot_state "https://github.com/cpkee/techjam-conversational-search/blob/main/starter/slot_state.py"
click node_slot_schema "https://github.com/cpkee/techjam-conversational-search/blob/main/starter/slot_schema.py"
click node_clarify "https://github.com/cpkee/techjam-conversational-search/blob/main/starter/clarify.py"
click node_policy "https://github.com/cpkee/techjam-conversational-search/blob/main/starter/policy.py"
click node_catalog_index "https://github.com/cpkee/techjam-conversational-search/blob/main/starter/catalog_index.py"
click node_embedding_store "https://github.com/cpkee/techjam-conversational-search/blob/main/data/catalog.bge_embeddings_ids.json"
click node_embeddings "https://github.com/cpkee/techjam-conversational-search/blob/main/starter/embeddings.py"
click node_fusion "https://github.com/cpkee/techjam-conversational-search/blob/main/starter/fusion.py"
click node_rankllm "https://github.com/cpkee/techjam-conversational-search/blob/main/starter/rankllm.py"
click node_stage2 "https://github.com/cpkee/techjam-conversational-search/blob/main/starter/stage2.py"
click node_ollama "https://github.com/cpkee/techjam-conversational-search/blob/main/starter/ollama_client.py"
click node_bge_model "https://github.com/cpkee/techjam-conversational-search/blob/main/starter/embeddings.py"
click node_evaluator "https://github.com/cpkee/techjam-conversational-search/blob/main/evaluator/local_evaluator.py"
click node_public_sessions "https://github.com/cpkee/techjam-conversational-search/blob/main/data/public_set.jsonl"
click node_api_contract "https://github.com/cpkee/techjam-conversational-search/blob/main/docs/agent_api_contract.json"
click node_submission "https://github.com/cpkee/techjam-conversational-search/blob/main/submission/agent.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_agent,node_profile_store,node_slot_state,node_slot_schema,node_clarify,node_policy toneBlue
class node_catalog_index,node_embedding_store,node_embeddings,node_fusion,node_rankllm,node_stage2 toneAmber
class node_ollama,node_bge_model toneMint
class node_evaluator,node_public_sessions,node_api_contract,node_submission toneRose
```

## Components

### Agent runtime

Turn-based session orchestration and decision policy.

- [Agent API](https://github.com/cpkee/techjam-conversational-search/blob/main/starter/agent.py): orchestrator · Resets sessions and produces one contract response per turn. · `starter/agent.py`
- [Profile store](https://github.com/cpkee/techjam-conversational-search/blob/main/starter/profile_store.py): session state · Holds profile-derived preferences by session. · `starter/profile_store.py`
- [Slot state](https://github.com/cpkee/techjam-conversational-search/blob/main/starter/slot_state.py): session state · Tracks extracted constraints, prior questions, and retrieval context. · `starter/slot_state.py`
- [Slot schema](https://github.com/cpkee/techjam-conversational-search/blob/main/starter/slot_schema.py): constraint schema · Constrains normalized slots and ask_attribute values. · `starter/slot_schema.py`
- [Clarification builder](https://github.com/cpkee/techjam-conversational-search/blob/main/starter/clarify.py): dialog component · Builds a natural-language question for an unresolved field. · `starter/clarify.py`
- [Turn policy](https://github.com/cpkee/techjam-conversational-search/blob/main/starter/policy.py): decision policy · Balances clarification against early recommendation. · `starter/policy.py`

### Retrieval and ranking

Catalog candidate generation through final Top-10 ordering.

- [Catalog index](https://github.com/cpkee/techjam-conversational-search/blob/main/starter/catalog_index.py): lexical retrieval · Searches catalog fields and parent_asin candidates. · `starter/catalog_index.py`
- [Embedding ID map](https://github.com/cpkee/techjam-conversational-search/blob/main/data/catalog.bge_embeddings_ids.json): embedding cache · Maps cached catalog vectors to catalog IDs. · `data/catalog.bge_embeddings_ids.json`
- [Dense retrieval](https://github.com/cpkee/techjam-conversational-search/blob/main/starter/embeddings.py): semantic retrieval · Embeds intent and catalog text for semantic requests. · `starter/embeddings.py`
- [Signal fusion](https://github.com/cpkee/techjam-conversational-search/blob/main/starter/fusion.py): candidate fusion · Combines lexical and dense candidate signals. · `starter/fusion.py`
- [LLM ranker](https://github.com/cpkee/techjam-conversational-search/blob/main/starter/rankllm.py): model-assisted ranking · Selectively reranks fused candidates. · `starter/rankllm.py`
- [Final ranking](https://github.com/cpkee/techjam-conversational-search/blob/main/starter/stage2.py): ranking stage · Produces bounded recommendations with exact parent_asin IDs. · `starter/stage2.py`

### Local model dependencies

Optional semantic and LLM capabilities with fallbacks.

- [Ollama chat API](https://github.com/cpkee/techjam-conversational-search/blob/main/starter/ollama_client.py): local service · qwen3:8b extraction, ranking, and response support; failures fall back. · `starter/ollama_client.py`
- [BGE embedding model](https://github.com/cpkee/techjam-conversational-search/blob/main/starter/embeddings.py): Hugging Face model · BAAI/bge-small-en-v1.5 runs on MPS or CPU. · `starter/embeddings.py`

### Evaluation and delivery

Local scoring and portable grading artifact.

- [Local evaluator](https://github.com/cpkee/techjam-conversational-search/blob/main/evaluator/local_evaluator.py): session simulator · Runs released sessions, validates outputs, and scores results. · `evaluator/local_evaluator.py`
- [Public sessions](https://github.com/cpkee/techjam-conversational-search/blob/main/data/public_set.jsonl): evaluation data · Released turn-by-turn evaluation set. · `data/public_set.jsonl`
- [Agent API contract](https://github.com/cpkee/techjam-conversational-search/blob/main/docs/agent_api_contract.json): interface contract · Defines evaluator-facing response requirements. · `docs/agent_api_contract.json`
- [Submission artifact](https://github.com/cpkee/techjam-conversational-search/blob/main/submission/agent.py): portable package · Mirrors the agent runtime for grading delivery. · `submission/agent.py`

## Connections

- Local evaluator → Agent API: invokes turns
- Public sessions → Local evaluator: released sessions
- Agent API contract → Local evaluator: validates
- Agent API contract → Agent API: response contract
- Agent API → Profile store: initializes
- Agent API → Slot state: updates per turn
- Slot schema → Slot state: constrains slots
- Agent API → Turn policy: chooses action
- Turn policy → Clarification builder: requests question
- Clarification builder → Slot schema: uses ask field
- Agent API → Catalog index: retrieves candidates
- Agent API → Dense retrieval: semantic retrieval
- Dense retrieval → Embedding ID map: loads ID mapping
- Dense retrieval → BGE embedding model: embeds intent
- Catalog index → Signal fusion: lexical candidates
- Dense retrieval → Signal fusion: dense candidates
- Signal fusion → LLM ranker: fused candidates
- LLM ranker → Final ranking: rerank signal
- Final ranking → Agent API: Top-10 parent_asins
- Agent API → Ollama chat API: extraction and response
- LLM ranker → Ollama chat API: LLM ranking
- Submission artifact → Agent API: mirrors runtime
