Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add predict functionality #318

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions opensearch_py_ml/ml_commons/ml_commons_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,34 @@ def generate_embedding(self, model_id: str, sentences: List[str]) -> object:
body=API_BODY,
)

def predict(self, model_id: str, algo_name: str, input_json):

API_URL = f"{ML_BASE_URI}/_predict/{algo_name}/{model_id}"

if isinstance(input_json, str):
try:
json_obj = json.loads(input_json)
if not isinstance(json_obj, dict):
return "Invalid JSON object passed as argument."
API_BODY = json.dumps(json_obj)
except json.JSONDecodeError:
return "Invalid JSON string passed as argument."
elif isinstance(input_json, dict):
API_BODY = json.dumps(input_json)
else:
return "Invalid JSON object passed as argument."

return self._client.transport.perform_request(
method="POST",
url=API_URL,
body=API_BODY,
)






@deprecated(
reason="Since OpenSearch 2.7.0, you can use undeploy_model instead",
version="2.7.0",
Expand Down
58 changes: 58 additions & 0 deletions tests/ml_commons/test_ml_commons_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,64 @@ def test_DEPRECATED_integration_pretrained_model_upload_unload_delete():
raised = True
assert raised == False, "Raised Exception in deleting pretrained model"

def test_predict():
input_json = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you consider taking iris_index as an argument here. Since, that is a fixture, you would have the iris data in the opensearch in an index. iris_index variable will have the actual name of the index here.. So, input_index can be iris_index

{
"input_query": {
"_source": ["petal_length_in_cm", "petal_width_in_cm"],
"size": 10000
},
"input_index": [
"iris_data"
]
}
}

raised = False
model_id = ml_client.register_pretrained_model(
model_name=PRETRAINED_MODEL_NAME,
model_version=PRETRAINED_MODEL_VERSION,
model_format=PRETRAINED_MODEL_FORMAT,
deploy_model=True,
wait_until_deployed=True,
)

try:
predict_obj = ml_client.predict(
model_id=model_id, algo_name="kmeans",input_json=input_json
)
assert predict_obj["status"] == "COMPLETED"
except: # noqa: E722
raised = True
assert raised == False, "Raised Exception in training and predicting task"

raised = False
try:
predict_obj = ml_client.predict(
model_id=model_id, algo_name="something else",input_json=input_json
)
assert predict_obj == "Invalid algorithm name passed as argument."
except: # noqa: E722
raised = True
assert raised == False, "Raised Exception in training and predicting task"

try:
predict_obj = ml_client.predict(
model_id=model_id, algo_name="something else",input_json="15"
)
assert predict_obj == "Invalid JSON object passed as argument."
except: # noqa: E722
raised = True
assert raised == False, "Raised Exception in training and predicting task"

try:
predict_obj = ml_client.predict(
model_id=model_id, algo_name="something else",input_json=15
)
assert predict_obj == "Invalid JSON object passed as argument."
except: # noqa: E722
raised = True
assert raised == False, "Raised Exception in training and predicting task"

def test_integration_pretrained_model_register_undeploy_delete():
raised = False
Expand Down
Loading