|
| 1 | +from utils.embedding_fields import build_knn_vector_field, get_embedding_field_name |
1 | 2 | from utils.logging_config import get_logger |
2 | 3 |
|
3 | 4 | logger = get_logger(__name__) |
4 | 5 |
|
5 | 6 |
|
6 | | -async def create_index_body() -> dict: |
| 7 | +async def create_index_body( |
| 8 | + embedding_model: str | None = None, |
| 9 | + embedding_dimensions: int | None = None, |
| 10 | +) -> dict: |
7 | 11 | """Create a static index body configuration. |
8 | 12 |
|
9 | 13 | Returns: |
10 | 14 | OpenSearch index body configuration |
11 | 15 | """ |
| 16 | + from config.embedding_constants import OPENAI_DEFAULT_EMBEDDING_MODEL |
| 17 | + from config.settings import VECTOR_DIM, get_openrag_config |
| 18 | + |
| 19 | + resolved_embedding_model = ( |
| 20 | + embedding_model |
| 21 | + or get_openrag_config().knowledge.embedding_model |
| 22 | + or OPENAI_DEFAULT_EMBEDDING_MODEL |
| 23 | + ) |
| 24 | + |
| 25 | + properties = { |
| 26 | + "document_id": {"type": "keyword"}, |
| 27 | + "filename": {"type": "keyword"}, |
| 28 | + "mimetype": {"type": "keyword"}, |
| 29 | + "page": {"type": "integer"}, |
| 30 | + "text": {"type": "text"}, |
| 31 | + # Legacy field - kept for backward compatibility and for clusters where |
| 32 | + # Langflow cannot perform mapping updates with a DLS-filtered JWT. |
| 33 | + "chunk_embedding": build_knn_vector_field(VECTOR_DIM), |
| 34 | + # Track which embedding model was used for this chunk |
| 35 | + "embedding_model": {"type": "keyword"}, |
| 36 | + "embedding_dimensions": {"type": "integer"}, |
| 37 | + "source_url": {"type": "keyword"}, |
| 38 | + "connector_type": {"type": "keyword"}, |
| 39 | + "owner": {"type": "keyword"}, |
| 40 | + "owner_email": {"type": "keyword"}, |
| 41 | + "allowed_users": {"type": "keyword"}, |
| 42 | + "allowed_groups": {"type": "keyword"}, |
| 43 | + "allowed_principals": {"type": "keyword"}, |
| 44 | + "created_time": {"type": "date"}, |
| 45 | + "modified_time": {"type": "date"}, |
| 46 | + "indexed_time": {"type": "date"}, |
| 47 | + "metadata": {"type": "object"}, |
| 48 | + } |
| 49 | + |
| 50 | + if embedding_dimensions: |
| 51 | + properties[get_embedding_field_name(resolved_embedding_model)] = build_knn_vector_field( |
| 52 | + embedding_dimensions |
| 53 | + ) |
12 | 54 |
|
13 | 55 | return { |
14 | 56 | "settings": {"index": {"knn": True}, "number_of_shards": 1, "number_of_replicas": 0}, |
15 | | - "mappings": { |
16 | | - "properties": { |
17 | | - "document_id": {"type": "keyword"}, |
18 | | - "filename": {"type": "keyword"}, |
19 | | - "mimetype": {"type": "keyword"}, |
20 | | - "page": {"type": "integer"}, |
21 | | - "text": {"type": "text"}, |
22 | | - # Track which embedding model was used for this chunk |
23 | | - "embedding_model": {"type": "keyword"}, |
24 | | - "embedding_dimensions": {"type": "integer"}, |
25 | | - "source_url": {"type": "keyword"}, |
26 | | - "connector_type": {"type": "keyword"}, |
27 | | - "owner": {"type": "keyword"}, |
28 | | - "allowed_users": {"type": "keyword"}, |
29 | | - "allowed_groups": {"type": "keyword"}, |
30 | | - "allowed_principals": {"type": "keyword"}, |
31 | | - "created_time": {"type": "date"}, |
32 | | - "modified_time": {"type": "date"}, |
33 | | - "indexed_time": {"type": "date"}, |
34 | | - "metadata": {"type": "object"}, |
35 | | - } |
36 | | - }, |
| 57 | + "mappings": {"properties": properties}, |
37 | 58 | } |
0 commit comments