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 agent framework/throttling/hidden model/OS assistant and update conversational search documentation #6354

Merged
merged 41 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
2102d08
Add agent framework documentation
kolchfa-aws Feb 5, 2024
b50da8c
Add hidden model and API updates
kolchfa-aws Feb 5, 2024
45963b0
Merge branch 'main' into agent-framework
kolchfa-aws Feb 6, 2024
0382604
Vale error
kolchfa-aws Feb 6, 2024
7f4499b
Updated field names
kolchfa-aws Feb 6, 2024
56c148c
Add updating credentials
kolchfa-aws Feb 6, 2024
00d53a9
Added tools table
kolchfa-aws Feb 6, 2024
afc9ac6
Add OpenSearch forum thread for OS Assistant
kolchfa-aws Feb 7, 2024
36bf022
Merge branch 'main' into agent-framework
kolchfa-aws Feb 8, 2024
7583223
Add tech review for conv search
kolchfa-aws Feb 8, 2024
92e2af7
Fix links
kolchfa-aws Feb 8, 2024
16943ed
Add tools
kolchfa-aws Feb 10, 2024
6185c1d
Add links to tools
kolchfa-aws Feb 10, 2024
3f61951
More info about tools
kolchfa-aws Feb 12, 2024
ba3983f
Tool parameters
kolchfa-aws Feb 12, 2024
70eab78
Update cat-index-tool.md
kolchfa-aws Feb 12, 2024
e7671b8
Parameter clarification
kolchfa-aws Feb 13, 2024
eaf1f46
Tech review feedback
kolchfa-aws Feb 13, 2024
c8d7c3c
Typo fix
kolchfa-aws Feb 13, 2024
76833f7
More tech review feedback: RAG tool
kolchfa-aws Feb 13, 2024
9044eaf
Tech review feedback: memory APis
kolchfa-aws Feb 13, 2024
1a0e818
Update _ml-commons-plugin/agents-tools/index.md
kolchfa-aws Feb 14, 2024
8369db2
Update _ml-commons-plugin/agents-tools/tools/neural-sparse-tool.md
kolchfa-aws Feb 14, 2024
31d7690
Update _ml-commons-plugin/agents-tools/tools/neural-sparse-tool.md
kolchfa-aws Feb 14, 2024
efeaca4
Update _ml-commons-plugin/agents-tools/tools/neural-sparse-tool.md
kolchfa-aws Feb 14, 2024
3881fb0
Update _ml-commons-plugin/opensearch-assistant.md
kolchfa-aws Feb 14, 2024
8d9b2fe
Update _ml-commons-plugin/agents-tools/tools/ppl-tool.md
kolchfa-aws Feb 14, 2024
544456d
Apply suggestions from code review
kolchfa-aws Feb 14, 2024
bf49d69
Separated search and get APIs and add conversational flow agent
kolchfa-aws Feb 14, 2024
96343b2
More parameters for PPL tool
kolchfa-aws Feb 14, 2024
33b3d2b
Added more parameters
kolchfa-aws Feb 15, 2024
c3ecb48
Tech review feedback: PPL tool
kolchfa-aws Feb 15, 2024
9cced21
Apply suggestions from code review
kolchfa-aws Feb 15, 2024
aaba606
Resolve merge conflicts
kolchfa-aws Feb 15, 2024
de47bf9
Rename to automating configurations
kolchfa-aws Feb 15, 2024
01ab264
Editorial comments on the new text
kolchfa-aws Feb 16, 2024
bb7e0a3
Merge conflicts
kolchfa-aws Feb 16, 2024
e78a542
Add parameter to PPl tool
kolchfa-aws Feb 16, 2024
a28e5d8
Changed link to configurations
kolchfa-aws Feb 16, 2024
31beeb9
Resolve merge conflicts and add links to UI assistant
kolchfa-aws Feb 19, 2024
0ccdc7f
Rate limiter feedback and added warning
kolchfa-aws Feb 20, 2024
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
1 change: 1 addition & 0 deletions .github/vale/styles/Vocab/OpenSearch/Products/accept.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ Open Distro
OpenAI
OpenID Connect
OpenSearch
OpenSearch Assistant
OpenSearch Benchmark
OpenSearch Dashboards
OpenSearch Playground
Expand Down
353 changes: 353 additions & 0 deletions _ml-commons-plugin/agents-tools/agents-tools-tutorial.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,353 @@
---
layout: default
title: Agents and tools tutorial
parent: Agents and tools
grand_parent: ML Commons APIs
nav_order: 10
---

# Agents and tools tutorial
**Introduced 2.12**
{: .label .label-purple }

This is an experimental feature and is not recommended for use in a production environment. For updates on the progress of the feature or if you want to leave feedback, see the associated [GitHub issue](https://github.com/opensearch-project/ml-commons/issues/1161).
{: .warning}

The following tutorial illustrates creating a flow agent for retrieval-augmented generation (RAG). A flow agent runs its configured tools sequentially, in the order specified. In this example, you'll create an agent with two tools:

1. `VectorDBTool`: The agent will use this tool to retrieve OpenSearch documents relevant to the user question. You'll ingest supplementary information to an OpenSearch index. To facilitate vector search, you'll deploy a text embedding model that translates text into vector embeddings. OpenSearch will translate the ingested documents into embeddings and store them in the index. When you provide a user question to the agent, the agent will construct a query from the question, run vector search on the OpenSearch index, and pass the retrieved relevant documents to the `MLModelTool`.
kolchfa-aws marked this conversation as resolved.
Show resolved Hide resolved
1. `MLModelTool`: The agent will run this tool to connect to a large language model (LLM) and send the user query augmented with OpenSearch documents to the model. In this example, you'll use the Anthropic Claude model hosted on Amazon Bedrock. The LLM will then answer the question based on its knowledge and the provided documents.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we link to Amazon Bedrock?

kolchfa-aws marked this conversation as resolved.
Show resolved Hide resolved

## Prerequisites

To use the memory feature, fist configure the following cluster settings:
kolchfa-aws marked this conversation as resolved.
Show resolved Hide resolved

```json
PUT _cluster/settings
{
"persistent": {
"plugins.ml_commons.only_run_on_ml_node": "false",
"plugins.ml_commons.memory_feature_enabled": "true"
}
}
```
{% include copy-curl.html %}

## Step 1: Register and deploy a text embedding model

You need a text embedding model to facilitate vector search. For this tutorial, you'll use one of the OpenSearch-provided pretrained models. To register and deploy the model, send the following request:
kolchfa-aws marked this conversation as resolved.
Show resolved Hide resolved

```json
POST /_plugins/_ml/models/_register?deploy=true
{
"name": "huggingface/sentence-transformers/all-MiniLM-L12-v2",
"version": "1.0.1",
"model_format": "TORCH_SCRIPT"
}
```
{% include copy-curl.html %}

Registering a model is an asynchronous task. OpenSearch sends back a task ID for this task:
Copy link
Collaborator

Choose a reason for hiding this comment

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

"returns" instead of "sends back"?

kolchfa-aws marked this conversation as resolved.
Show resolved Hide resolved

```json
{
"task_id": "aFeif4oB5Vm0Tdw8yoN7",
"status": "CREATED"
}
```

You can check the status of the task by calling the Tasks API:

```json
GET /_plugins/_ml/tasks/aFeif4oB5Vm0Tdw8yoN7
```
{% include copy-curl.html %}

Once the task is complete, the task state changes to `COMPLETED` and the Tasks API response contains a model ID for the deployed model:
kolchfa-aws marked this conversation as resolved.
Show resolved Hide resolved

```json
{
"model_id": "aVeif4oB5Vm0Tdw8zYO2",
"task_type": "REGISTER_MODEL",
"function_name": "TEXT_EMBEDDING",
"state": "COMPLETED",
"worker_node": [
"4p6FVOmJRtu3wehDD74hzQ"
],
"create_time": 1694358489722,
"last_update_time": 1694358499139,
"is_async": true
}
```

## Step 2: Create an ingest pipeline

To translate text into vector embeddings, you'll set up an ingest pipeline. The pipeline translates the `text` field and writes the resulting vector embeddings into the `embedding` field. Create the pipeline by specifying the `model_id` from the previous step in the following request:

```json
PUT /_ingest/pipeline/test-pipeline-local-model
{
"description": "text embedding pipeline",
"processors": [
{
"text_embedding": {
"model_id": "aVeif4oB5Vm0Tdw8zYO2",
"field_map": {
"text": "embedding"
}
}
}
]
}
```

## Step 3: Create a k-NN index and ingest data

Now you'll ingest supplementary data into an OpenSearch index. In OpenSearch, vectors are stored in a k-NN index. You can create a k-NN index by sending the following request:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we define k-NN, or are we treating it as commonly understood?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I can link to it but I think it's commonly understood.

kolchfa-aws marked this conversation as resolved.
Show resolved Hide resolved

```json
PUT my_test_data
{
"mappings": {
"properties": {
"text": {
"type": "text"
},
"embedding": {
"type": "knn_vector",
"dimension": 384
}
}
},
"settings": {
"index": {
"knn.space_type": "cosinesimil",
"default_pipeline": "test-pipeline-local-model",
"knn": "true"
}
}
}
```
{% include copy-curl.html %}

Then, ingest data into the index using a bulk request:
kolchfa-aws marked this conversation as resolved.
Show resolved Hide resolved

```json
POST _bulk
{"index": {"_index": "my_test_data", "_id": "1"}}
{"text": "Chart and table of population level and growth rate for the Ogden-Layton metro area from 1950 to 2023. United Nations population projections are also included through the year 2035.\nThe current metro area population of Ogden-Layton in 2023 is 750,000, a 1.63% increase from 2022.\nThe metro area population of Ogden-Layton in 2022 was 738,000, a 1.79% increase from 2021.\nThe metro area population of Ogden-Layton in 2021 was 725,000, a 1.97% increase from 2020.\nThe metro area population of Ogden-Layton in 2020 was 711,000, a 2.16% increase from 2019."}
{"index": {"_index": "my_test_data", "_id": "2"}}
{"text": "Chart and table of population level and growth rate for the New York City metro area from 1950 to 2023. United Nations population projections are also included through the year 2035.\\nThe current metro area population of New York City in 2023 is 18,937,000, a 0.37% increase from 2022.\\nThe metro area population of New York City in 2022 was 18,867,000, a 0.23% increase from 2021.\\nThe metro area population of New York City in 2021 was 18,823,000, a 0.1% increase from 2020.\\nThe metro area population of New York City in 2020 was 18,804,000, a 0.01% decline from 2019."}
{"index": {"_index": "my_test_data", "_id": "3"}}
{"text": "Chart and table of population level and growth rate for the Chicago metro area from 1950 to 2023. United Nations population projections are also included through the year 2035.\\nThe current metro area population of Chicago in 2023 is 8,937,000, a 0.4% increase from 2022.\\nThe metro area population of Chicago in 2022 was 8,901,000, a 0.27% increase from 2021.\\nThe metro area population of Chicago in 2021 was 8,877,000, a 0.14% increase from 2020.\\nThe metro area population of Chicago in 2020 was 8,865,000, a 0.03% increase from 2019."}
{"index": {"_index": "my_test_data", "_id": "4"}}
{"text": "Chart and table of population level and growth rate for the Miami metro area from 1950 to 2023. United Nations population projections are also included through the year 2035.\\nThe current metro area population of Miami in 2023 is 6,265,000, a 0.8% increase from 2022.\\nThe metro area population of Miami in 2022 was 6,215,000, a 0.78% increase from 2021.\\nThe metro area population of Miami in 2021 was 6,167,000, a 0.74% increase from 2020.\\nThe metro area population of Miami in 2020 was 6,122,000, a 0.71% increase from 2019."}
{"index": {"_index": "my_test_data", "_id": "5"}}
{"text": "Chart and table of population level and growth rate for the Austin metro area from 1950 to 2023. United Nations population projections are also included through the year 2035.\\nThe current metro area population of Austin in 2023 is 2,228,000, a 2.39% increase from 2022.\\nThe metro area population of Austin in 2022 was 2,176,000, a 2.79% increase from 2021.\\nThe metro area population of Austin in 2021 was 2,117,000, a 3.12% increase from 2020.\\nThe metro area population of Austin in 2020 was 2,053,000, a 3.43% increase from 2019."}
{"index": {"_index": "my_test_data", "_id": "6"}}
{"text": "Chart and table of population level and growth rate for the Seattle metro area from 1950 to 2023. United Nations population projections are also included through the year 2035.\\nThe current metro area population of Seattle in 2023 is 3,519,000, a 0.86% increase from 2022.\\nThe metro area population of Seattle in 2022 was 3,489,000, a 0.81% increase from 2021.\\nThe metro area population of Seattle in 2021 was 3,461,000, a 0.82% increase from 2020.\\nThe metro area population of Seattle in 2020 was 3,433,000, a 0.79% increase from 2019."}
```
{% include copy-curl.html %}

## Step 4: Create a connector to an externally hosted model

You'll need an LLM to generate responses to user questions. The LLM is too large for the OpenSearch cluster, so you'll create a connector to connect to an externally hosted LLM. For this example, you'll create a connector to the Anthropic Claude model hosted on Amazon Bedrock:
kolchfa-aws marked this conversation as resolved.
Show resolved Hide resolved

```json
POST /_plugins/_ml/connectors/_create
{
"name": "BedRock test claude Connector",
"description": "The connector to BedRock service for claude model",
"version": 1,
"protocol": "aws_sigv4",
"parameters": {
"region": "us-east-1",
"service_name": "bedrock",
"anthropic_version": "bedrock-2023-05-31",
"endpoint": "bedrock.us-east-1.amazonaws.com",
"auth": "Sig_V4",
"content_type": "application/json",
"max_tokens_to_sample": 8000,
"temperature": 0.0001,
"response_filter": "$.completion"
},
"credential": {
"access_key": "<bedrock_access_key>",
"secret_key": "<bedrock_secret_key>",
"session_token": "<bedrock_session_token>"
},
"actions": [
{
"action_type": "predict",
"method": "POST",
"url": "https://bedrock-runtime.us-east-1.amazonaws.com/model/anthropic.claude-v2/invoke",
"headers": {
"content-type": "application/json",
"x-amz-content-sha256": "required"
},
"request_body": "{\"prompt\":\"${parameters.prompt}\", \"max_tokens_to_sample\":${parameters.max_tokens_to_sample}, \"temperature\":${parameters.temperature}, \"anthropic_version\":\"${parameters.anthropic_version}\" }"
}
]
}
```
{% include copy-curl.html %}

The response contains the connector ID for the newly created connector:

```json
{
"connector_id": "a1eMb4kBJ1eYAeTMAljY"
}
```

## Step 5: Register and deploy the externally hosted model

Just like the text embedding model, an LLM needs to be registered and deployed to OpenSearch. To set up the externally hosted model, first create a model group for this model:

Check warning on line 205 in _ml-commons-plugin/agents-tools/agents-tools-tutorial.md

View workflow job for this annotation

GitHub Actions / vale

[vale] _ml-commons-plugin/agents-tools/agents-tools-tutorial.md#L205

[OpenSearch.Simple] Don't use 'Just' because it's not neutral in tone. If you mean 'only', use 'only' instead.
Raw output
{"message": "[OpenSearch.Simple] Don't use 'Just' because it's not neutral in tone. If you mean 'only', use 'only' instead.", "location": {"path": "_ml-commons-plugin/agents-tools/agents-tools-tutorial.md", "range": {"start": {"line": 205, "column": 1}}}, "severity": "WARNING"}

```json
POST /_plugins/_ml/model_groups/_register
{
"name": "test_model_group_bedrock",
"description": "This is a public model group"
}
```
{% include copy-curl.html %}

The response contains the model group ID that you’ll use to register a model to this model group:

```json
{
"model_group_id": "wlcnb4kBJ1eYAeTMHlV6",
"status": "CREATED"
}

```

Next, register and deploy the externally hosted Claude model:

```json
POST /_plugins/_ml/models/_register?deploy=true
{
"name": "Bedrock Claude V2 model",
"function_name": "remote",
"model_group_id": "wlcnb4kBJ1eYAeTMHlV6",
"description": "test model",
"connector_id": "a1eMb4kBJ1eYAeTMAljY"
}
```
{% include copy-curl.html %}

Similarly to [Step 1](#step-1-register-and-deploy-a-text-embedding-model), the response contains a task ID that you can use to check the status of the deployment. Once the model is deployed, the status changes to `COMPLETED` and the response includes the model ID for the Claude model:

```json
{
"model_id": "NWR9YIsBUysqmzBdifVJ",
"task_type": "REGISTER_MODEL",
"function_name": "remote",
"state": "COMPLETED",
"worker_node": [
"4p6FVOmJRtu3wehDD74hzQ"
],
"create_time": 1694358489722,
"last_update_time": 1694358499139,
"is_async": true
}
```

To test the LLM, send the following predict request:

```json
POST /_plugins/_ml/models/NWR9YIsBUysqmzBdifVJ/_predict
{
"parameters": {
"prompt": "\n\nHuman:hello\n\nnAssistant:"
}
}
```
{% include copy-curl.html %}

## Step 6: Register and execute an agent

Finally, you'll use the text embedding model created in Step 1 and the Claude model created in Step 5 to create a flow agent. This flow agent will run a `VectorDBTool`, and then an `MLModelTool`. The `VectorDBTool` is configured with the model ID for the text embedding model created in Step 1 for vector search, and the `MLModelTool` is configured with the Claude model created in step 5:
kolchfa-aws marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Collaborator

Choose a reason for hiding this comment

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

In the last sentence, could "for vector search" be removed? That sentence is a bit lengthy.


```json
POST /_plugins/_ml/agents/_register
{
"name": "Test_Agent_For_RAG",
"type": "flow",
"description": "this is a test agent",
"tools": [
{
"type": "VectorDBTool",
"parameters": {
"model_id": "aVeif4oB5Vm0Tdw8zYO2",
"index": "my_test_data",
"embedding_field": "embedding",
"source_field": ["text"],
"input": "${parameters.question}"
}
},
{
"type": "MLModelTool",
"description": "A general tool to answer any question",
"parameters": {
"model_id": "NWR9YIsBUysqmzBdifVJ",
"prompt": "\n\nHuman:You are a professional data analyst. You will always answer a question based on the given context first. If the answer is not directly shown in the context, you will analyze the data and find the answer. If you don't know the answer, just say you don't know. \n\n Context:\n${parameters.VectorDBTool.output}\n\nHuman:${parameters.question}\n\nAssistant:"
}
}
]
}
```
{% include copy-curl.html %}

OpenSearch returns an agent ID for the newly created agent:

```json
{
"agent_id": "879v9YwBjWKCe6Kg12Tx"
}
```

You can inspect the agent by sending a request to the `agents` endpoint and providing the agent ID:

```json
GET /_plugins/_ml/agents/879v9YwBjWKCe6Kg12Tx
```
{% include copy-curl.html %}

To execute the agent, send the following request. When registering the agent, you configured it to take in `parameters.question`, so you need to provide this parameter in this request. This parameter represents a human-generated user question:
kolchfa-aws marked this conversation as resolved.
Show resolved Hide resolved

```json
POST /_plugins/_ml/agents/879v9YwBjWKCe6Kg12Tx/_execute
{
"parameters": {
"question": "what's the population increase of Seattle from 2021 to 2023"
}
}
```
{% include copy-curl.html %}

The LLM does not have the recent information in its knowledge base so it infers the response to the question based on the ingested data, demonstrating RAG:
kolchfa-aws marked this conversation as resolved.
Show resolved Hide resolved

```json
{
"inference_results": [
{
"output": [
{
"result": """ Based on the given context, the key information is:

The metro area population of Seattle in 2021 was 3,461,000.
The metro area population of Seattle in 2023 is 3,519,000.

To calculate the population increase from 2021 to 2023:

Population in 2023 (3,519,000) - Population in 2021 (3,461,000) = 58,000

Therefore, the population increase of Seattle from 2021 to 2023 is 58,000."""
}
]
}
]
}
```
Loading
Loading