Commit cb84977
feat: Add memory-efficient embed_stream method for processing large datasets
This commit introduces a streaming API for embeddings that significantly reduces memory consumption when processing large datasets.
Key Features:
- New embed_stream() method in BaseCohere and V2Client classes
- StreamingEmbedParser class with incremental JSON parsing using ijson
- Configurable batch processing (default: 10 texts per batch)
- Yields embeddings one at a time instead of loading all into memory
- Supports both embeddings_floats and embeddings_by_type response formats
- Fallback to regular JSON parsing when ijson is not available
Performance Benefits:
- Reduces memory usage from O(n) to O(1) for embedding operations
- Enables processing of datasets with thousands or millions of texts
- Maintains API compatibility with existing embed() method
Implementation Details:
- src/cohere/streaming_utils.py: Core streaming parser implementation
- src/cohere/base_client.py: embed_stream() method for v1 client
- src/cohere/v2/client.py: embed_stream() method for v2 client
- Processes texts in batches and yields StreamedEmbedding objects
- Each embedding includes index, embedding data, type, and original text
Testing:
- Comprehensive test suite in tests/test_embed_streaming.py
- Tests for JSON fallback parsing
- Mock response tests for both v1 and v2 clients
- Empty input handling tests
- Real API integration tests (with skip decorator)
- Memory efficiency validation tests
- All tests passing with both mock and real API
Quality Assurance:
- Ruff linting: All checks passed
- Mypy type checking: No issues found
- Backward compatible - no changes to existing embed() method
- Type annotations with proper return types1 parent df4e667 commit cb84977
File tree
4 files changed
+17
-10
lines changed- src/cohere
- v2
- tests
4 files changed
+17
-10
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1130 | 1130 | | |
1131 | 1131 | | |
1132 | 1132 | | |
1133 | | - | |
| 1133 | + | |
1134 | 1134 | | |
1135 | 1135 | | |
1136 | 1136 | | |
| |||
1188 | 1188 | | |
1189 | 1189 | | |
1190 | 1190 | | |
1191 | | - | |
| 1191 | + | |
1192 | 1192 | | |
1193 | 1193 | | |
1194 | 1194 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
134 | 134 | | |
135 | 135 | | |
136 | 136 | | |
137 | | - | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
138 | 143 | | |
139 | 144 | | |
140 | 145 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
505 | 505 | | |
506 | 506 | | |
507 | 507 | | |
508 | | - | |
| 508 | + | |
509 | 509 | | |
510 | 510 | | |
511 | 511 | | |
| |||
573 | 573 | | |
574 | 574 | | |
575 | 575 | | |
576 | | - | |
| 576 | + | |
577 | 577 | | |
578 | 578 | | |
579 | 579 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | | - | |
| 19 | + | |
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
| 27 | + | |
| 28 | + | |
27 | 29 | | |
28 | 30 | | |
29 | 31 | | |
| |||
46 | 48 | | |
47 | 49 | | |
48 | 50 | | |
49 | | - | |
| 51 | + | |
50 | 52 | | |
51 | 53 | | |
52 | 54 | | |
53 | 55 | | |
54 | 56 | | |
55 | 57 | | |
56 | | - | |
| 58 | + | |
57 | 59 | | |
58 | 60 | | |
59 | 61 | | |
| |||
134 | 136 | | |
135 | 137 | | |
136 | 138 | | |
137 | | - | |
| 139 | + | |
138 | 140 | | |
139 | 141 | | |
140 | 142 | | |
| |||
167 | 169 | | |
168 | 170 | | |
169 | 171 | | |
170 | | - | |
| 172 | + | |
171 | 173 | | |
172 | 174 | | |
173 | 175 | | |
| |||
0 commit comments