Skip to content

Commit 336a0f3

Browse files
ENG-1393: Index improvements (#360)
* Index improvements * Remove add/update methods
1 parent cb8c65a commit 336a0f3

File tree

4 files changed

+13
-25
lines changed

4 files changed

+13
-25
lines changed

aixplain/modules/model/index_model.py

+3-15
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from aixplain.utils import config
44
from aixplain.modules.model.response import ModelResponse
55
from typing import Text, Optional, Union, Dict
6-
from aixplain.modules.model.document_index import DocumentIndex
6+
from aixplain.modules.model.record import Record
77
from typing import List
88

99

@@ -55,26 +55,14 @@ def search(self, query: str, top_k: int = 10) -> ModelResponse:
5555
data = {"action": "search", "data": query, "payload": {"filters": {}, "top_k": top_k}}
5656
return self.run(data=data)
5757

58-
def add(self, documents: List[DocumentIndex]) -> ModelResponse:
58+
def upsert(self, documents: List[Record]) -> ModelResponse:
5959
payloads = [doc.to_dict() for doc in documents]
6060
data = {"action": "ingest", "data": "", "payload": {"payloads": payloads}}
6161
response = self.run(data=data)
6262
if response.status == ResponseStatus.SUCCESS:
6363
response.data = payloads
6464
return response
65-
raise Exception(f"Failed to add documents: {response.error_message}")
66-
67-
def update(self, documents: List[DocumentIndex]) -> ModelResponse:
68-
payloads = [
69-
{"value": doc.value, "value_type": doc.value_type, "id": str(doc.id), "uri": doc.uri, "attributes": doc.attributes}
70-
for doc in documents
71-
]
72-
data = {"action": "update", "data": "", "payload": {"payloads": payloads}}
73-
response = self.run(data=data)
74-
if response.status == ResponseStatus.SUCCESS:
75-
response.data = payloads
76-
return response
77-
raise Exception(f"Failed to update documents: {response.error_message}")
65+
raise Exception(f"Failed to upsert documents: {response.error_message}")
7866

7967
def count(self) -> float:
8068
data = {"action": "count", "data": ""}

aixplain/modules/model/document_index.py aixplain/modules/model/record.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from uuid import uuid4
33

44

5-
class DocumentIndex:
5+
class Record:
66
def __init__(self, value: str, value_type: str = "text", id: Optional[str] = None, uri: str = "", attributes: dict = {}):
77
self.value = value
88
self.value_type = value_type

tests/functional/model/run_model_test.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ def test_run_async():
5555

5656

5757
def test_index_model():
58-
from aixplain.modules.model.document_index import DocumentIndex
58+
from aixplain.modules.model.record import Record
5959
from aixplain.factories import IndexFactory
6060

6161
index_model = IndexFactory.create("test", "test")
62-
index_model.add([DocumentIndex(value="Hello, world!", value_type="text", uri="", attributes={})])
62+
index_model.upsert([Record(value="Hello, world!", value_type="text", uri="", attributes={})])
6363
response = index_model.search("Hello")
6464
assert str(response.status) == "SUCCESS"
6565
assert index_model.count() == 1

tests/unit/index_model_test.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import requests_mock
22
from aixplain.enums import Function, ResponseStatus
3-
from aixplain.modules.model.document_index import DocumentIndex
3+
from aixplain.modules.model.record import Record
44
from aixplain.modules.model.response import ModelResponse
55
from aixplain.modules.model.index_model import IndexModel
66
from aixplain.utils import config
@@ -28,16 +28,16 @@ def test_add_success():
2828
mock_response = {"status": "SUCCESS"}
2929

3030
mock_documents = [
31-
DocumentIndex(value="Sample document content 1", value_type="text", id=0, uri="", attributes={}),
32-
DocumentIndex(value="Sample document content 2", value_type="text", id=1, uri="", attributes={}),
31+
Record(value="Sample document content 1", value_type="text", id=0, uri="", attributes={}),
32+
Record(value="Sample document content 2", value_type="text", id=1, uri="", attributes={}),
3333
]
3434

3535
with requests_mock.Mocker() as mock:
3636
mock.post(execute_url, json=mock_response, status_code=200)
3737

3838
index_model = IndexModel(id=index_id, data=data, name="name", function=Function.SEARCH)
3939

40-
response = index_model.add(mock_documents)
40+
response = index_model.upsert(mock_documents)
4141

4242
assert isinstance(response, ModelResponse)
4343
assert response.status == ResponseStatus.SUCCESS
@@ -47,8 +47,8 @@ def test_update_success():
4747
mock_response = {"status": "SUCCESS"}
4848

4949
mock_documents = [
50-
DocumentIndex(value="Updated document content 1", value_type="text", id=0, uri="", attributes={}),
51-
DocumentIndex(value="Updated document content 2", value_type="text", id=1, uri="", attributes={}),
50+
Record(value="Updated document content 1", value_type="text", id=0, uri="", attributes={}),
51+
Record(value="Updated document content 2", value_type="text", id=1, uri="", attributes={}),
5252
]
5353

5454
with requests_mock.Mocker() as mock:
@@ -57,7 +57,7 @@ def test_update_success():
5757

5858
index_model = IndexModel(id=index_id, data=data, name="name", function=Function.SEARCH)
5959

60-
response = index_model.update(mock_documents)
60+
response = index_model.upsert(mock_documents)
6161

6262
assert isinstance(response, ModelResponse)
6363
assert response.status == ResponseStatus.SUCCESS

0 commit comments

Comments
 (0)