File size: 6,210 Bytes
026ee5d
 
 
 
 
 
 
 
 
 
 
 
 
 
d45d242
 
 
026ee5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d45d242
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
026ee5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d45d242
 
 
026ee5d
 
 
 
d45d242
026ee5d
d45d242
026ee5d
 
 
 
d45d242
026ee5d
 
d45d242
 
026ee5d
 
 
 
 
 
d45d242
026ee5d
d45d242
026ee5d
 
 
 
d45d242
026ee5d
d45d242
 
026ee5d
 
 
d45d242
026ee5d
 
d45d242
 
 
 
 
 
 
 
 
 
 
 
 
026ee5d
d45d242
 
 
 
 
 
 
 
 
 
 
 
026ee5d
 
 
 
 
 
d45d242
 
 
 
 
026ee5d
 
d45d242
 
 
 
 
 
 
 
 
 
026ee5d
 
 
 
 
 
 
 
 
 
 
 
 
 
d45d242
026ee5d
d45d242
026ee5d
 
 
d45d242
026ee5d
 
d45d242
 
 
026ee5d
 
 
d45d242
 
 
 
 
 
026ee5d
 
 
 
 
 
 
 
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
# Services API Reference

This page documents the API for DeepCritical services.

## EmbeddingService

**Module**: `src.services.embeddings`

**Purpose**: Local sentence-transformers for semantic search and deduplication.

### Methods

#### `embed`

<!--codeinclude-->
[EmbeddingService.embed](../src/services/embeddings.py) start_line:55 end_line:55
<!--/codeinclude-->

Generates embedding for a text string.

**Parameters**:
- `text`: Text to embed

**Returns**: Embedding vector as list of floats.

#### `embed_batch`

```python
async def embed_batch(self, texts: list[str]) -> list[list[float]]
```

Generates embeddings for multiple texts.

**Parameters**:
- `texts`: List of texts to embed

**Returns**: List of embedding vectors.

#### `similarity`

```python
async def similarity(self, text1: str, text2: str) -> float
```

Calculates similarity between two texts.

**Parameters**:
- `text1`: First text
- `text2`: Second text

**Returns**: Similarity score (0.0-1.0).

#### `find_duplicates`

```python
async def find_duplicates(
    self,
    texts: list[str],
    threshold: float = 0.85
) -> list[tuple[int, int]]
```

Finds duplicate texts based on similarity threshold.

**Parameters**:
- `texts`: List of texts to check
- `threshold`: Similarity threshold (default: 0.85)

**Returns**: List of (index1, index2) tuples for duplicate pairs.

#### `add_evidence`

```python
async def add_evidence(
    self,
    evidence_id: str,
    content: str,
    metadata: dict[str, Any]
) -> None
```

Adds evidence to vector store for semantic search.

**Parameters**:
- `evidence_id`: Unique identifier for the evidence
- `content`: Evidence text content
- `metadata`: Additional metadata dictionary

#### `search_similar`

```python
async def search_similar(
    self,
    query: str,
    n_results: int = 5
) -> list[dict[str, Any]]
```

Finds semantically similar evidence.

**Parameters**:
- `query`: Search query string
- `n_results`: Number of results to return (default: 5)

**Returns**: List of dictionaries with `id`, `content`, `metadata`, and `distance` keys.

#### `deduplicate`

```python
async def deduplicate(
    self,
    new_evidence: list[Evidence],
    threshold: float = 0.9
) -> list[Evidence]
```

Removes semantically duplicate evidence.

**Parameters**:
- `new_evidence`: List of evidence items to deduplicate
- `threshold`: Similarity threshold (default: 0.9, where 0.9 = 90% similar is duplicate)

**Returns**: List of unique evidence items (not already in vector store).

### Factory Function

#### `get_embedding_service`

```python
@lru_cache(maxsize=1)
def get_embedding_service() -> EmbeddingService
```

Returns singleton EmbeddingService instance.

## LlamaIndexRAGService

**Module**: `src.services.rag`

**Purpose**: Retrieval-Augmented Generation using LlamaIndex.

### Methods

#### `ingest_evidence`

<!--codeinclude-->
[LlamaIndexRAGService.ingest_evidence](../src/services/llamaindex_rag.py) start_line:290 end_line:290
<!--/codeinclude-->

Ingests evidence into RAG service.

**Parameters**:
- `evidence_list`: List of Evidence objects to ingest

**Note**: Supports multiple embedding providers (OpenAI, local sentence-transformers, Hugging Face).

#### `retrieve`

```python
def retrieve(
    self,
    query: str,
    top_k: int | None = None
) -> list[dict[str, Any]]
```

Retrieves relevant documents for a query.

**Parameters**:
- `query`: Search query string
- `top_k`: Number of top results to return (defaults to `similarity_top_k` from constructor)

**Returns**: List of dictionaries with `text`, `score`, and `metadata` keys.

#### `query`

```python
def query(
    self,
    query_str: str,
    top_k: int | None = None
) -> str
```

Queries RAG service and returns synthesized response.

**Parameters**:
- `query_str`: Query string
- `top_k`: Number of results to use (defaults to `similarity_top_k` from constructor)

**Returns**: Synthesized response string.

**Raises**:
- `ConfigurationError`: If no LLM API key is available for query synthesis

#### `ingest_documents`

```python
def ingest_documents(self, documents: list[Any]) -> None
```

Ingests raw LlamaIndex Documents.

**Parameters**:
- `documents`: List of LlamaIndex Document objects

#### `clear_collection`

```python
def clear_collection(self) -> None
```

Clears all documents from the collection.

### Factory Function

#### `get_rag_service`

```python
def get_rag_service(
    collection_name: str = "deepcritical_evidence",
    oauth_token: str | None = None,
    **kwargs: Any
) -> LlamaIndexRAGService
```

Get or create a RAG service instance.

**Parameters**:
- `collection_name`: Name of the ChromaDB collection (default: "deepcritical_evidence")
- `oauth_token`: Optional OAuth token from HuggingFace login (takes priority over env vars)
- `**kwargs`: Additional arguments for LlamaIndexRAGService (e.g., `use_openai_embeddings=False`)

**Returns**: Configured LlamaIndexRAGService instance.

**Note**: By default, uses local embeddings (sentence-transformers) which require no API keys.

## StatisticalAnalyzer

**Module**: `src.services.statistical_analyzer`

**Purpose**: Secure execution of AI-generated statistical code.

### Methods

#### `analyze`

```python
async def analyze(
    self,
    query: str,
    evidence: list[Evidence],
    hypothesis: dict[str, Any] | None = None
) -> AnalysisResult
```

Analyzes a research question using statistical methods.

**Parameters**:
- `query`: The research question
- `evidence`: List of Evidence objects to analyze
- `hypothesis`: Optional hypothesis dict with `drug`, `target`, `pathway`, `effect`, `confidence` keys

**Returns**: `AnalysisResult` with:
- `verdict`: SUPPORTED, REFUTED, or INCONCLUSIVE
- `confidence`: Confidence in verdict (0.0-1.0)
- `statistical_evidence`: Summary of statistical findings
- `code_generated`: Python code that was executed
- `execution_output`: Output from code execution
- `key_takeaways`: Key takeaways from analysis
- `limitations`: List of limitations

**Note**: Requires Modal credentials for sandbox execution.

## See Also

- [Architecture - Services](../architecture/services.md) - Architecture overview
- [Configuration](../configuration/index.md) - Service configuration