|
| 1 | +## `COMPLETION` [esql-completion] |
| 2 | + |
| 3 | +```yaml {applies_to} |
| 4 | +serverless: preview |
| 5 | +stack: preview 9.1.0 |
| 6 | +``` |
| 7 | +
|
| 8 | +The `COMPLETION` command allows you to send prompts and context to a Large Language Model (LLM) directly within your ES|QL queries, to perform text generation tasks. |
| 9 | + |
| 10 | +**Syntax** |
| 11 | + |
| 12 | +```esql |
| 13 | +COMPLETION [column =] prompt WITH inference_id |
| 14 | +``` |
| 15 | + |
| 16 | +**Parameters** |
| 17 | + |
| 18 | +`column` |
| 19 | +: (Optional) The name of the output column containing the LLM's response. |
| 20 | + If not specified, the results will be stored in a column named `completion`. |
| 21 | + If the specified column already exists, it will be overwritten with the new results. |
| 22 | + |
| 23 | +`prompt` |
| 24 | +: The input text or expression used to prompt the LLM. |
| 25 | + This can be a string literal or a reference to a column containing text. |
| 26 | + |
| 27 | +`inference_id` |
| 28 | +: The ID of the [inference endpoint](docs-content://explore-analyze/elastic-inference/inference-api.md) to use for the task. |
| 29 | + The inference endpoint must be configured with the `completion` task type. |
| 30 | + |
| 31 | +**Description** |
| 32 | + |
| 33 | +The `COMPLETION` command provides a general-purpose interface for |
| 34 | +text generation tasks using a Large Language Model (LLM) in ES|QL. |
| 35 | + |
| 36 | +`COMPLETION` supports a wide range of text generation tasks. Depending on your |
| 37 | +prompt and the model you use, you can perform arbitrary text generation tasks |
| 38 | +including: |
| 39 | + |
| 40 | +- Question answering |
| 41 | +- Summarization |
| 42 | +- Translation |
| 43 | +- Content rewriting |
| 44 | +- Creative generation |
| 45 | + |
| 46 | +**Requirements** |
| 47 | + |
| 48 | +To use this command, you must deploy your LLM model in Elasticsearch as |
| 49 | +an [≈inference endpoint](https://www.elastic.co/docs/api/doc/elasticsearch/operation/operation-inference-put) with the |
| 50 | +task type `completion`. |
| 51 | + |
| 52 | +**Examples** |
| 53 | + |
| 54 | +Use the default column name (results stored in `completion` column): |
| 55 | + |
| 56 | +```esql |
| 57 | +ROW question = "What is Elasticsearch?" |
| 58 | +| COMPLETION question WITH test_completion_model |
| 59 | +| KEEP question, completion |
| 60 | +``` |
| 61 | + |
| 62 | +| question:keyword | completion:keyword | |
| 63 | +|------------------------|-------------------------------------------| |
| 64 | +| What is Elasticsearch? | A distributed search and analytics engine | |
| 65 | + |
| 66 | +Specify the output column (results stored in `answer` column): |
| 67 | + |
| 68 | +```esql |
| 69 | +ROW question = "What is Elasticsearch?" |
| 70 | +| COMPLETION answer = question WITH test_completion_model |
| 71 | +| KEEP question, answer |
| 72 | +``` |
| 73 | + |
| 74 | +| question:keyword | answer:keyword | |
| 75 | +| --- | --- | |
| 76 | +| What is Elasticsearch? | A distributed search and analytics engine | |
| 77 | + |
| 78 | +Summarize the top 10 highest-rated movies using a prompt: |
| 79 | + |
| 80 | +```esql |
| 81 | +FROM movies |
| 82 | +| SORT rating DESC |
| 83 | +| LIMIT 10 |
| 84 | +| EVAL prompt = CONCAT( |
| 85 | + "Summarize this movie using the following information: \n", |
| 86 | + "Title: ", title, "\n", |
| 87 | + "Synopsis: ", synopsis, "\n", |
| 88 | + "Actors: ", MV_CONCAT(actors, ", "), "\n", |
| 89 | + ) |
| 90 | +| COMPLETION summary = prompt WITH test_completion_model |
| 91 | +| KEEP title, summary, rating |
| 92 | +``` |
| 93 | + |
| 94 | + |
| 95 | +| title:keyword | summary:keyword | rating:double | |
| 96 | +| --- | --- | --- | |
| 97 | +| The Shawshank Redemption | A tale of hope and redemption in prison. | 9.3 | |
| 98 | +| The Godfather | A mafia family's rise and fall. | 9.2 | |
| 99 | +| The Dark Knight | Batman battles the Joker in Gotham. | 9.0 | |
| 100 | +| Pulp Fiction | Interconnected crime stories with dark humor. | 8.9 | |
| 101 | +| Fight Club | A man starts an underground fight club. | 8.8 | |
| 102 | +| Inception | A thief steals secrets through dreams. | 8.8 | |
| 103 | +| The Matrix | A hacker discovers reality is a simulation. | 8.7 | |
| 104 | +| Parasite | Class conflict between two families. | 8.6 | |
| 105 | +| Interstellar | A team explores space to save humanity. | 8.6 | |
| 106 | +| The Prestige | Rival magicians engage in dangerous competition. | 8.5 | |
0 commit comments