File size: 8,577 Bytes
07c2476
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# Project Structure

The AI Imaging Agent is organized into modular components with clear separation of concerns.

## Directory Layout

```

ai-agent/

β”œβ”€β”€ .github/

β”‚   └── workflows/          # CI/CD workflows

β”‚       └── deploy_docs.yml # Documentation deployment

β”œβ”€β”€ artifacts/

β”‚   └── rag_index/          # FAISS index and embeddings

β”œβ”€β”€ dataset/

β”‚   └── catalog.jsonl       # Software catalog

β”œβ”€β”€ docs/                   # MkDocs documentation

β”œβ”€β”€ logs/                   # Application logs

β”œβ”€β”€ src/

β”‚   └── ai_agent/          # Main package

β”‚       β”œβ”€β”€ agent/         # PydanticAI agent

β”‚       β”œβ”€β”€ api/           # Pipeline orchestration

β”‚       β”œβ”€β”€ catalog/       # Catalog management

β”‚       β”œβ”€β”€ generator/     # VLM selection (schemas)

β”‚       β”œβ”€β”€ retriever/     # Text retrieval

β”‚       β”œβ”€β”€ ui/            # Gradio interface

β”‚       └── utils/         # Shared utilities

β”œβ”€β”€ tests/                 # Test suite

β”œβ”€β”€ config.yaml            # Model configuration

β”œβ”€β”€ mkdocs.yml            # Documentation config

β”œβ”€β”€ pyproject.toml        # Package metadata

└── README.md             # Project readme

```

## Core Modules

### src/ai_agent/



Main package containing all application code.



#### agent/



PydanticAI conversational agent implementation.



```

agent/

β”œβ”€β”€ __init__.py

β”œβ”€β”€ agent.py               # Agent definition, tool adapters

β”œβ”€β”€ models.py              # Agent output/log models

β”œβ”€β”€ utils.py               # Agent state and tool quota helpers

└── tools/                 # Tool implementations (search, repo_info, mcp)
```



**Key components**:



- `agent.py`: Agent instance, system prompt, tool definitions

- `models.py`: Agent output and tool usage schemas

- `utils.py`: `AgentState` plus call caps/prepare hooks

- `tools/`: Tool implementations (search, alternatives, repo info, mcp tools)



**Dependencies**: `api/`, `utils/`



#### api/



Pipeline orchestration and core logic.



```
api/
β”œβ”€β”€ __init__.py
└── pipeline.py            # RAGImagingPipeline main class
```



**Responsibilities**:



- File validation and metadata extraction

- Retrieval + VLM selection orchestration

- Error handling and logging

- Index management



**Dependencies**: `retriever/`, `generator/`, `utils/`



#### catalog/



Software catalog synchronization.



```
catalog/
β”œβ”€β”€ __init__.py
└── sync.py                # Catalog sync logic
```



**Functions**:



- Load catalog from JSONL

- Check for changes (SHA1)

- Trigger index rebuild



**Dependencies**: `retriever/`



#### generator/



VLM selection schemas and types.



```
generator/
β”œβ”€β”€ __init__.py
└── schema.py              # Pydantic models for responses
```



**Models**:



- `ToolRecommendation`: Individual tool recommendation

- `AgentResponse`: Complete response with status

- `ConversationStatus`: Enum for conversation states

- `ToolReason`: Enum for recommendation reasons



**Dependencies**: None (pure schemas)



#### retriever/



Text-based retrieval pipeline.



```
retriever/
β”œβ”€β”€ __init__.py
β”œβ”€β”€ text_embedder.py       # BGE-M3 embedding model

β”œβ”€β”€ vector_index.py        # FAISS index management
β”œβ”€β”€ reranker.py            # CrossEncoder reranking
└── software_doc.py        # Catalog schema and loading

```



**Pipeline flow**:



1. `text_embedder.py`: Embed query
2. `vector_index.py`: FAISS search
3. `reranker.py`: CrossEncoder reranking
4. Output: Top-K candidates

**Dependencies**: None (pure retrieval)

#### ui/

Gradio web interface.

```

ui/

β”œβ”€β”€ __init__.py

β”œβ”€β”€ app.py                 # Gradio app definition

β”œβ”€β”€ components.py          # Reusable UI components

β”œβ”€β”€ formatters.py          # Response formatting

β”œβ”€β”€ handlers.py            # Message handlers

β”œβ”€β”€ state.py               # UI state management

└── visualizations.py      # Preview and trace rendering

```

**Key files**:

- `app.py`: Main Gradio interface
- `handlers.py`: `respond()` function - core interaction logic
- `formatters.py`: Format recommendations as markdown/cards
- `components.py`: Reusable Gradio components

**Dependencies**: `agent/`, `api/`

#### utils/

Shared utilities.

```

utils/

β”œβ”€β”€ __init__.py

β”œβ”€β”€ config.py              # Configuration loading

β”œβ”€β”€ file_validator.py      # File validation

β”œβ”€β”€ image_meta.py          # Metadata extraction (DICOM, NIfTI, TIFF)

β”œβ”€β”€ previews.py            # Image preview generation

└── tags.py                # Control tag parsing

```

**Common utilities**:

- `config.py`: Load `config.yaml` with Pydantic validation
- `file_validator.py`: Size limits, format checks
- `image_meta.py`: Extract DICOM/NIfTI/TIFF metadata
- `previews.py`: Convert medical images to PNG
- `tags.py`: Parse exclusion tags and strip control tags from queries

**Dependencies**: None (pure utilities)

#### cli.py

Command-line interface entry point.

```python

def main():

    # Parse arguments

    # Route to chat or sync

```

**Commands**:

- `ai_agent chat`: Launch UI
- `ai_agent sync`: Sync catalog

### tests/

Test suite.

```

tests/

β”œβ”€β”€ data/

β”‚   └── test_data.json     # Test cases

β”œβ”€β”€ test_retrieval_pipeline.py

β”œβ”€β”€ test_deepwiki_repo_info.py

└── ...

```

**Test categories**:

- Unit tests: Individual components
- Integration tests: Full pipeline
- End-to-end tests: Real API calls (optional)

## Configuration Files

### pyproject.toml

Python package metadata and dependencies.

```toml

[project]

name = "ai_agent"

version = "1.0.0"

dependencies = [...]



[project.scripts]

ai_agent = "ai_agent.cli:main"

```

### config.yaml

Model configuration.

```yaml

agent_model:

  name: "gpt-4o-mini"

  base_url: null

  api_key_env: "OPENAI_API_KEY"



available_models:

  - display_name: "gpt-4o-mini"

    name: "gpt-4o-mini"

    ...

```

### mkdocs.yml

Documentation configuration.

```yaml

site_name: AI Imaging Agent

theme:

  name: material

nav: [...]

```

### .env

Environment variables (not committed).

```dotenv

OPENAI_API_KEY=sk-xxxx

SOFTWARE_CATALOG=dataset/catalog.jsonl

```

## Data Files

### dataset/catalog.jsonl

Software catalog in JSON Lines format.

Each line is a complete JSON object following schema.org SoftwareSourceCode.

### artifacts/rag_index/



Pre-built FAISS index and metadata.



```

artifacts/rag_index/
β”œβ”€β”€ index.faiss            # FAISS binary index
└── meta.json              # Tool IDs, config, timestamps
```



## Module Boundaries



Clear separation prevents circular dependencies:



```
ui/ β†’ agent/ β†’ api/ β†’ retriever/
                  β†’ generator/

                  β†’ utils/

```


**Rules**:

- `utils/`: No dependencies on other modules
- `retriever/`: Pure retrieval, no generation
- `generator/`: Pure schemas, no retrieval
- `api/`: Orchestrates retriever + generator
- `agent/`: Uses api for tool calls
- `ui/`: Top-level, depends on agent + api

## Import Patterns

All imports use absolute paths from `ai_agent`:

```python

from ai_agent.retriever.vector_index import VectorIndex

from ai_agent.utils.config import load_config

from ai_agent.agent.utils import AgentState

```

**Never use** relative imports like `from ..utils import ...`

## Extension Points

### Adding New Tools

Add tool adapters to `agent/agent.py` and implement logic in `agent/tools/`:

```python

@agent.tool

async def new_tool(ctx: RunContext[AgentState], param: str) -> str:

    """Tool description."""

    # Implementation

    return result

```

### Adding New Metadata Extractors

Add to `utils/image_meta.py`:

```python

def extract_custom_format(file_path: str) -> dict:

    """Extract metadata from custom format."""

    # Implementation

    return metadata

```

### Adding New Retrieval Models

Replace in `retriever/text_embedder.py`:

```python

class TextEmbedder:

    def __init__(self, model_name="new-embedding-model"):

        self.model = SentenceTransformer(model_name)

```

## Next Steps

- Learn about [Contributing](contributing.md)
- Explore [Testing](testing.md)
- Return to [Architecture Overview](../architecture/overview.md)