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: knowledge base tool (do not review now) #353

Closed

Conversation

yuye-aws
Copy link
Member

@yuye-aws yuye-aws commented Jul 16, 2024

Description

POC for knowledge base tool. You can use the knowledge base tool with the following steps:

# update connector settings
PUT _cluster/settings
{
  "persistent": {
    "plugins.ml_commons.trusted_connector_endpoints_regex": [
      """^https://runtime\.sagemaker\..*[a-z0-9-]\.amazonaws\.com/.*$""",
      """^https://api\.openai\.com/.*$""",
      """^https://api\.cohere\.ai/.*$""",
      """^https://bedrock-runtime\..*[a-z0-9-]\.amazonaws\.com/.*$""",
      """^https://bedrock-agent-runtime\..*[a-z0-9-]\.amazonaws\.com/.*$"""
    ]
  }
}

# register bedrock knowledge base connector
POST /_plugins/_ml/connectors/_create
{
  "name": "Amazon Bedrock Connector: knowledge",
  "description": "The connector to the Bedrock knowledge base",
  "version": 1,
  "protocol": "aws_sigv4",
  "parameters": {
    "region": "...",
    "service_name": "bedrock"
  },
  "credential": {
    "access_key": "...",
    "secret_key": "..."
  },
  "actions": [
    {
      "action_type": "predict",
      "method": "POST",
      "url": "...",
      "headers": {
        "content-type": "application/json",
        "x-amz-content-sha256": "required"
      },
      "request_body": "{\"retrievalQuery\": {\"text\": \"${parameters.text}\"}}"
    }
  ]
}

# register remote model given knowledge base connector
POST /_plugins/_ml/models/_register?deploy=true
{
  "name": "bedrock: knowledge base",
  "function_name": "remote",
  "description": "Test connector for bedrock knowledge base",
  "connector_id": "..."
}

# test your remote knowledge base model
POST /_plugins/_ml/models/<model_id>/_predict/
{
  "parameters": {
    "text": "..."
  }
}

# register a flow agent with knowledge base tool
POST /_plugins/_ml/agents/_register
{
  "name": "Test_Agent_For_KB_TOOL",
  "type": "flow",
  "description": "this is a test agent for knowledge base tool",
  "tools": [
    {
      "type": "KnowledgeBaseTool",
      "parameters": {
        "model_id": "..."
      }
    }
  ]
} 

# test your knowledge base agent
POST /_plugins/_ml/agents/<agent_id>/_execute
{
  "parameters": {
    "text": "..."
  }
}

Related Issues

POC for knowledge base tool

Check List

  • New functionality includes testing.
  • New functionality has been documented.
  • API changes companion pull request created.
  • Commits are signed per the DCO using --signoff.
  • Public documentation issue/PR created.

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.

@xinyual
Copy link
Collaborator

xinyual commented Jul 18, 2024

Can you please add UT and IT?

}
ArrayList<Map<String, Object>> retrievedDocuments = (ArrayList<Map<String, Object>>) dataAsMap.get("retrievalResults");
StringBuilder formattedDocuments = new StringBuilder();
for (Map<String, Object> retrievedDocument : retrievedDocuments) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

If knowledge base endpoint returns an error like exceed limitation (I don't know, we may need a survey), we will return empty result, does this meet expectation?

Copy link
Member Author

Choose a reason for hiding this comment

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

Any idea to mock an error exception from bedrock knowledge base?

Choose a reason for hiding this comment

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

Any idea to mock an error exception from bedrock knowledge base?

We can make a easy real environment exception by remove the model access to the embedding model and then call the API.
Maybe return the origin exception information is enough?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good idea. I can start testing once we have plan to merge this PR. I will mark this PR as draft.

@yuye-aws
Copy link
Member Author

Can you please add UT and IT?

It's a POC for now. We are not in a hurry to merge this PR, so I will ad UT and IT later.

Copy link

@conggguan conggguan left a comment

Choose a reason for hiding this comment

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

How should we assure that the input parameter model_id is a valid knowledge base connector?

@yuye-aws yuye-aws marked this pull request as draft July 18, 2024 07:13
@yuye-aws
Copy link
Member Author

How should we assure that the input parameter model_id is a valid knowledge base connector?

A simple idea is: fetch model and connector configuration from the model_id. Then, check the url from the connector. Just wondering whether PPLTool or other tools has this kind of model_id validation @xinyual

yuye-aws added 2 commits July 23, 2024 10:14
Signed-off-by: yuye-aws <yuyezhu@amazon.com>
Signed-off-by: yuye-aws <yuyezhu@amazon.com>
yuye-aws added 2 commits July 23, 2024 10:14
Signed-off-by: yuye-aws <yuyezhu@amazon.com>
Signed-off-by: yuye-aws <yuyezhu@amazon.com>
@yuye-aws yuye-aws force-pushed the feat/KnowledgeBaseTool branch from ed36bed to 521e2b9 Compare July 23, 2024 02:14
@Hailong-am
Copy link
Contributor

can we leverage existing MLModelTool or ConnectorTool

Signed-off-by: yuye-aws <yuyezhu@amazon.com>
Copy link

codecov bot commented Jul 23, 2024

Codecov Report

Attention: Patch coverage is 0% with 62 lines in your changes missing coverage. Please review.

Project coverage is 77.02%. Comparing base (2b76e3c) to head (4033781).
Report is 6 commits behind head on main.

Files Patch % Lines
.../org/opensearch/agent/tools/KnowledgeBaseTool.java 0.00% 59 Missing ⚠️
src/main/java/org/opensearch/agent/ToolPlugin.java 0.00% 3 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main     #353      +/-   ##
============================================
- Coverage     81.78%   77.02%   -4.77%     
- Complexity      193      222      +29     
============================================
  Files            11       16       +5     
  Lines           961     1197     +236     
  Branches        137      171      +34     
============================================
+ Hits            786      922     +136     
- Misses          121      211      +90     
- Partials         54       64      +10     

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

yuye-aws added 4 commits July 30, 2024 16:25
Signed-off-by: yuye-aws <yuyezhu@amazon.com>
Signed-off-by: yuye-aws <yuyezhu@amazon.com>
Signed-off-by: yuye-aws <yuyezhu@amazon.com>
Signed-off-by: yuye-aws <yuyezhu@amazon.com>
@yuye-aws yuye-aws changed the title feat: knowledge base tool feat: knowledge base tool (do not review now) Aug 1, 2024
@yuye-aws
Copy link
Member Author

yuye-aws commented Aug 1, 2024

No need to review this PR now. This PR is just a POC for alert analysis workflow.

yuye-aws and others added 16 commits August 2, 2024 10:56
Signed-off-by: yuye-aws <yuyezhu@amazon.com>
Signed-off-by: yuye-aws <yuyezhu@amazon.com>
Signed-off-by: yuye-aws <yuyezhu@amazon.com>
Signed-off-by: yuye-aws <yuyezhu@amazon.com>
Signed-off-by: yuye-aws <yuyezhu@amazon.com>
Signed-off-by: yuye-aws <yuyezhu@amazon.com>
Signed-off-by: yuye-aws <yuyezhu@amazon.com>
Signed-off-by: yuye-aws <yuyezhu@amazon.com>
Signed-off-by: Songkan Tang <songkant@amazon.com>
Signed-off-by: Songkan Tang <songkant@amazon.com>
Signed-off-by: Songkan Tang <songkant@amazon.com>
Signed-off-by: Songkan Tang <songkant@amazon.com>
Signed-off-by: yuye-aws <yuyezhu@amazon.com>
Signed-off-by: yuye-aws <yuyezhu@amazon.com>
Signed-off-by: yuye-aws <yuyezhu@amazon.com>
@yuye-aws
Copy link
Member Author

yuye-aws commented Jan 3, 2025

Closing this PR as knowledge base tool is no longer needed.

@yuye-aws yuye-aws closed this Jan 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants