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

feat: search index tool #61

Merged
merged 6 commits into from
Dec 28, 2023

Conversation

yuye-aws
Copy link
Member

@yuye-aws yuye-aws commented Dec 26, 2023

Description

Implement SearchIndexTool to support agent framework. It takes in two string parameters: index and query. Here is the sample usage of this tool. Please remember to replace the model id and agent id.

POST /_plugins/_ml/agents/_register
{
  "name": "Test_Agent_For_ReAct",
  "type": "conversational",
  "description": "this is a test agent",
  "llm": {
    "model_id": "xxxxx",
    "parameters": {
      "max_iteration": 10,
      "stop_when_no_tool_found": true,
      "response_filter": "$.completion"
    }
  },
  "memory": {
    "type": "conversation_index"
  },
  "tools": [
    {
      "type": "SearchIndexTool",
      "description": "Use this tool to search index with a query. You should pass in two parameters: index and query. Index is the index name and the query is an OpenSearch DSL query."
    }
  ],
  "app_type": "my app"
}


POST /_plugins/_ml/agents/xxxxx/_execute
{
  "parameters": {
    "question": "Show me all the name field of all documents in index .plugins-ml-agent",
    "verbose": true
  }
}

The expected successful response should be

{
  "inference_results": [
    {
      "output": [
        {
          "name": "memory_id",
          "result": "xxxxx"
        },
        {
          "name": "response",
          "result": "Thought: To get the name field of documents in the .plugins-ml-agent index, I should search that index using the SearchIndexTool."
        },
        {
          "name": "response",
          "result": "\nObservation: \"{\\\"_index\\\":\\\".plugins-ml-agent\\\",\\\"_source\\\":{\\\"name\\\":\\\"Test_Agent_For_ReAct\\\"},\\\"_id\\\":\\\"xxxxx\\\",\\\"_score\\\":1.0}\\n\""
        },
        {
          "name": "response",
          "result": "Based on the information I was previously provided from searching the index, the name field of the documents in the .plugins-ml-agent index contains 'Test_Agent_For_ReAct'."
        },
        {
          "name": "response",
          "result": "The name field of the documents in the .plugins-ml-agent index is 'Test_Agent_For_ReAct'."
        }
      ]
    }
  ]
}

If there is error when parsing the input, the expected error response could be:

{
  "error": {
    "root_cause": [
      {
        "type": "illegal_argument_exception",
        "reason": "[query] is null or empty, can not process it."
      }
    ],
    "type": "illegal_argument_exception",
    "reason": "[query] is null or empty, can not process it."
  },
  "status": 400
}

Issues Resolved

  1. Implement search index tool
  2. Fix bugs in unit tests

Check List

  • New functionality includes testing.
    • All tests pass
  • New functionality has been documented.
    • New functionality has javadoc added
  • Commits are signed per the DCO using --signoff

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

Copy link

codecov bot commented Dec 26, 2023

Codecov Report

Attention: 18 lines in your changes are missing coverage. Please review.

Comparison is base (37bbd3b) 46.08% compared to head (724c33a) 80.38%.
Report is 2 commits behind head on main.

Files Patch % Lines
...va/org/opensearch/agent/tools/SearchIndexTool.java 72.58% 12 Missing and 5 partials ⚠️
src/main/java/org/opensearch/agent/ToolPlugin.java 0.00% 1 Missing ⚠️
Additional details and impacted files
@@              Coverage Diff              @@
##               main      #61       +/-   ##
=============================================
+ Coverage     46.08%   80.38%   +34.29%     
- Complexity       31       90       +59     
=============================================
  Files             6        8        +2     
  Lines           358      520      +162     
  Branches         42       70       +28     
=============================================
+ Hits            165      418      +253     
+ Misses          172       58      -114     
- Partials         21       44       +23     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@zane-neo
Copy link
Collaborator

Please check the codecov report and add more UTs.

});
contextBuilder.append(doc).append("\n");
}
listener.onResponse((T) StringUtils.gson.toJson(contextBuilder.toString()));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why we need a toJson here after calling toString?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sorry, there is no need for toJson. Thanks for pointing out.

Signed-off-by: yuye-aws <[email protected]>
@yuye-aws
Copy link
Member Author

yuye-aws commented Dec 26, 2023

Please check the codecov report and add more UTs.

I have added UT to cover more code. This PR is ready for review now.


public static final String TYPE = "SearchIndexTool";
private static final String DEFAULT_DESCRIPTION =
"Use this tool to search index with a query. You should pass in two parameters: index and query. Index is the index name and the query is an OpenSearch DSL query.";
Copy link
Collaborator

Choose a reason for hiding this comment

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

how about : Use this tool to search an index by providing two parameters: 'index' for the index name, and 'query' for the OpenSearch DSL formatted query.

@zane-neo zane-neo merged commit 2e05330 into opensearch-project:main Dec 28, 2023
13 checks passed
opensearch-trigger-bot bot pushed a commit that referenced this pull request Dec 28, 2023
* add search index tool

Signed-off-by: yuye-aws <[email protected]>

* run spotless apply

Signed-off-by: yuye-aws <[email protected]>

* remove unncessary string util operation

Signed-off-by: yuye-aws <[email protected]>

* add test cases

Signed-off-by: yuye-aws <[email protected]>

* spotless apply

Signed-off-by: yuye-aws <[email protected]>

* update tool description and add model group search

Signed-off-by: yuye-aws <[email protected]>

---------

Signed-off-by: yuye-aws <[email protected]>
(cherry picked from commit 2e05330)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
zane-neo pushed a commit that referenced this pull request Dec 29, 2023
* add search index tool



* run spotless apply



* remove unncessary string util operation



* add test cases



* spotless apply



* update tool description and add model group search



---------


(cherry picked from commit 2e05330)

Signed-off-by: yuye-aws <[email protected]>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
@yuye-aws yuye-aws deleted the feature/searchIndexTool branch January 2, 2024 08:06
yuye-aws pushed a commit to yuye-aws/skills that referenced this pull request Apr 26, 2024
* add search index tool

* run spotless apply

* remove unncessary string util operation

* add test cases

* spotless apply

* update tool description and add model group search

---------

(cherry picked from commit 2e05330)

Signed-off-by: yuye-aws <[email protected]>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Signed-off-by: yuye-aws <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants