Skip to content

Commit 8a19bb9

Browse files
Add Python ML examples to the API docs (#2489)
Co-authored-by: Quentin Pradet <[email protected]>
1 parent 8a97cc1 commit 8a19bb9

21 files changed

+256
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// ml/trained-models/apis/infer-trained-model.asciidoc:1043
2+
3+
[source, python]
4+
----
5+
resp = client.ml.infer_trained_model(
6+
model_id="cross-encoder__ms-marco-tinybert-l-2-v2",
7+
body={
8+
"docs": [
9+
{
10+
"text_field": "Berlin has a population of 3,520,031 registered inhabitants in an area of 891.82 square kilometers."
11+
},
12+
{
13+
"text_field": "New York City is famous for the Metropolitan Museum of Art."
14+
},
15+
],
16+
"inference_config": {
17+
"text_similarity": {"text": "How many people live in Berlin?"}
18+
},
19+
},
20+
)
21+
print(resp)
22+
----
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// ml/trained-models/apis/start-trained-model-deployment.asciidoc:173
2+
3+
[source, python]
4+
----
5+
resp = client.ml.start_trained_model_deployment(
6+
model_id="my_model",
7+
deployment_id="my_model_for_ingest",
8+
wait_for="started",
9+
timeout="1m",
10+
)
11+
print(resp)
12+
----
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// ml/trained-models/apis/put-trained-models-aliases.asciidoc:82
2+
3+
[source, python]
4+
----
5+
resp = client.ml.put_trained_model_alias(
6+
model_id="flight-delay-prediction-1574775339910",
7+
model_alias="flight_delay_model",
8+
)
9+
print(resp)
10+
----
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// ml/trained-models/apis/put-trained-model-vocabulary.asciidoc:63
2+
3+
[source, python]
4+
----
5+
resp = client.ml.put_trained_model_vocabulary(
6+
model_id="elastic__distilbert-base-uncased-finetuned-conll03-english",
7+
body={"vocabulary": ["[PAD]", "[unused0]", ...]},
8+
)
9+
print(resp)
10+
----
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// ml/trained-models/apis/infer-trained-model.asciidoc:849
2+
3+
[source, python]
4+
----
5+
resp = client.ml.infer_trained_model(
6+
model_id="lang_ident_model_1",
7+
body={
8+
"docs": [
9+
{
10+
"text": "The fool doth think he is wise, but the wise man knows himself to be a fool."
11+
}
12+
]
13+
},
14+
)
15+
print(resp)
16+
----
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// ml/trained-models/apis/infer-trained-model.asciidoc:1008
2+
3+
[source, python]
4+
----
5+
resp = client.ml.infer_trained_model(
6+
model_id="model2",
7+
body={
8+
"docs": [{"text_field": "<long text to extract answer>"}],
9+
"inference_config": {
10+
"question_answering": {"question": "<question to be answered>"}
11+
},
12+
},
13+
)
14+
print(resp)
15+
----
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// ml/trained-models/apis/get-trained-models.asciidoc:1460
2+
3+
[source, python]
4+
----
5+
resp = client.ml.get_trained_models()
6+
print(resp)
7+
----
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// ml/trained-models/apis/start-trained-model-deployment.asciidoc:181
2+
3+
[source, python]
4+
----
5+
resp = client.ml.start_trained_model_deployment(
6+
model_id="my_model",
7+
deployment_id="my_model_for_search",
8+
)
9+
print(resp)
10+
----
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// ml/trained-models/apis/infer-trained-model.asciidoc:880
2+
3+
[source, python]
4+
----
5+
resp = client.ml.infer_trained_model(
6+
model_id="model2",
7+
body={"docs": [{"text_field": "The movie was awesome!!"}]},
8+
)
9+
print(resp)
10+
----
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// ml/trained-models/apis/clear-trained-model-deployment-cache.asciidoc:43
2+
3+
[source, python]
4+
----
5+
resp = client.ml.clear_trained_model_deployment_cache(
6+
model_id="elastic__distilbert-base-uncased-finetuned-conll03-english",
7+
)
8+
print(resp)
9+
----
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// ml/trained-models/apis/delete-trained-models-aliases.asciidoc:51
2+
3+
[source, python]
4+
----
5+
resp = client.ml.delete_trained_model_alias(
6+
model_id="flight-delay-prediction-1574775339910",
7+
model_alias="flight_delay_model",
8+
)
9+
print(resp)
10+
----
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// ml/trained-models/apis/get-trained-models-stats.asciidoc:405
2+
3+
[source, python]
4+
----
5+
resp = client.ml.get_trained_models_stats()
6+
print(resp)
7+
----
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// ml/trained-models/apis/infer-trained-model.asciidoc:945
2+
3+
[source, python]
4+
----
5+
resp = client.ml.infer_trained_model(
6+
model_id="model2",
7+
body={
8+
"docs": [{"text_field": "This is a very happy person"}],
9+
"inference_config": {
10+
"zero_shot_classification": {
11+
"labels": ["glad", "sad", "bad", "rad"],
12+
"multi_label": False,
13+
}
14+
},
15+
},
16+
)
17+
print(resp)
18+
----
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// ml/trained-models/apis/start-trained-model-deployment.asciidoc:133
2+
3+
[source, python]
4+
----
5+
resp = client.ml.start_trained_model_deployment(
6+
model_id="elastic__distilbert-base-uncased-finetuned-conll03-english",
7+
wait_for="started",
8+
timeout="1m",
9+
)
10+
print(resp)
11+
----
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// ml/trained-models/apis/put-trained-model-definition-part.asciidoc:64
2+
3+
[source, python]
4+
----
5+
resp = client.ml.put_trained_model_definition_part(
6+
model_id="elastic__distilbert-base-uncased-finetuned-conll03-english",
7+
part="0",
8+
body={
9+
"definition": "...",
10+
"total_definition_length": 265632637,
11+
"total_parts": 64,
12+
},
13+
)
14+
print(resp)
15+
----
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// ml/trained-models/apis/delete-trained-models.asciidoc:54
2+
3+
[source, python]
4+
----
5+
resp = client.ml.delete_trained_model(
6+
model_id="regression-job-one-1574775307356",
7+
)
8+
print(resp)
9+
----
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// ml/trained-models/apis/infer-trained-model.asciidoc:1078
2+
3+
[source, python]
4+
----
5+
resp = client.ml.infer_trained_model(
6+
model_id="model2",
7+
body={
8+
"docs": [
9+
{
10+
"text_field": "The Amazon rainforest covers most of the Amazon basin in South America"
11+
}
12+
],
13+
"inference_config": {
14+
"ner": {"tokenization": {"bert": {"truncate": "first"}}}
15+
},
16+
},
17+
)
18+
print(resp)
19+
----
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// ml/trained-models/apis/put-trained-models-aliases.asciidoc:94
2+
3+
[source, python]
4+
----
5+
resp = client.ml.put_trained_model_alias(
6+
model_id="flight-delay-prediction-1580004349800",
7+
model_alias="flight_delay_model",
8+
reassign="true",
9+
)
10+
print(resp)
11+
----
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// ml/trained-models/apis/stop-trained-model-deployment.asciidoc:67
2+
3+
[source, python]
4+
----
5+
resp = client.ml.stop_trained_model_deployment(
6+
model_id="my_model_for_search",
7+
)
8+
print(resp)
9+
----
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// ml/trained-models/apis/infer-trained-model.asciidoc:905
2+
3+
[source, python]
4+
----
5+
resp = client.ml.infer_trained_model(
6+
model_id="model2",
7+
body={
8+
"docs": [{"text_field": "Hi my name is Josh and I live in Berlin"}]
9+
},
10+
)
11+
print(resp)
12+
----

utils/generate-examples.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,20 @@
110110
"inference/delete-inference.asciidoc",
111111
"inference/post-inference.asciidoc",
112112
"inference/put-inference.asciidoc",
113+
"ml/trained-models/apis/clear-trained-model-deployment-cache.asciidoc",
114+
"ml/trained-models/apis/delete-trained-models-aliases.asciidoc",
115+
"ml/trained-models/apis/delete-trained-models.asciidoc",
116+
"ml/trained-models/apis/get-trained-models-stats.asciidoc",
117+
"ml/trained-models/apis/get-trained-models.asciidoc",
118+
"ml/trained-models/apis/infer-trained-model-deployment.asciidoc",
119+
"ml/trained-models/apis/infer-trained-model.asciidoc",
120+
"ml/trained-models/apis/put-trained-model-definition-part.asciidoc",
121+
"ml/trained-models/apis/put-trained-model-vocabulary.asciidoc",
122+
"ml/trained-models/apis/put-trained-models-aliases.asciidoc",
123+
"ml/trained-models/apis/put-trained-models.asciidoc",
124+
"ml/trained-models/apis/start-trained-model-deployment.asciidoc",
125+
"ml/trained-models/apis/stop-trained-model-deployment.asciidoc",
126+
"ml/trained-models/apis/update-trained-model-deployment.asciidoc",
113127
]
114128

115129

0 commit comments

Comments
 (0)