opensearch-project / ml-commons

ml-commons provides a set of common machine learning algorithms, e.g. k-means, or linear regression, to help developers build ML related features within OpenSearch.
Apache License 2.0
99 stars 136 forks source link

[DOC] Build blueprint for Bedrock Converse API #2846

Open ylwu-amzn opened 3 months ago

ylwu-amzn commented 3 months ago

Build blueprint for Bedrock converse API https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Converse.html

austintlee commented 1 month ago

Here are some examples of RAG search pipeline requests that I used:

Text and document

{
  "_source": ["text"],
  "query" : {
    "match": {"text": "president"}
  },
   "ext": {
      "generative_qa_parameters": {
        "llm_model": "bedrock-converse/anthropic.claude-3-5-sonnet-20240620-v1:0",
        "system_prompt": "You are a helpful assistant",
        "context_size": 5,
        "message_size": 5,
        "timeout": 60,
        "llm_messages": [
          { "role": "user", "content": [{"type": "text", "text": "use the information from the attached document to tell me something interesting about lincoln"}, 
          {"document": {"format": "pdf", "name": "lincoln", "data": "<base64 encoded string>"}}] }
        ]
      }
  }
}

Text and image

{
  "_source": ["text"],
  "query" : {
    "match": {"text": "president"}
  },
   "ext": {
      "generative_qa_parameters": {
        "llm_model": "bedrock-converse/anthropic.claude-3-5-sonnet-20240620-v1:0",
        "context_size": 5,
        "message_size": 5,
        "timeout": 60,
        "llm_messages": [
          { "role": "user", "content": [
            {"image": {"format": "jpeg", "data": "<base64 encoded string"}},
          {"text": "Can you image Lincoln or any other figured mentioned in the attached document or search results in the scenary depicted in the image?"}
          ]}
        ]
      }
  }
}

Text, document and image

{
  "_source": ["text"],
  "query" : {
    "match": {"text": "president"}
  },
   "ext": {
      "generative_qa_parameters": {
        "llm_model": "bedrock-converse/anthropic.claude-3-5-sonnet-20240620-v1:0",
        "timeout": 60,
        "llm_messages": [
          { "role": "user", "content": [
            {"document": {"format": "pdf", "name": "lincoln", "data": "<...>"}},
            {"image": {"format": "jpeg", "data": "<...>"}},
          {"text": "Can you image Lincoln or any other figured mentioned in the attached document or search results in the scenary depicted in the image?"}
          ]}
        ]
      }
  }
}
austintlee commented 1 month ago

Connector

I tested with and without "system_prompt". If you include a system prompt, Bedrock (or Anthropic) includes some message about private (I guess it triggers some kind of guardrail). Otherwise, the same answer.

{
  "name": "Bedrock Connector: claude 3",
  "description": "The connector to bedrock claude 3 model",
  "version": 1,
  "protocol": "aws_sigv4",
  "parameters": {
    "region": "us-west-2",
    "service_name": "bedrock",
    "model": "anthropic.claude-3-sonnet-20240229-v1:0",
    "system_prompt": "You are a helpful assistant."
  },
  "credential": {
    "access_key": "...",
    "secret_key": "...",
    "session_token": "..."
  },
  "actions": [
        {
            "action_type": "predict",
            "method": "POST",
            "headers": {
                "content-type": "application/json"
            },
            "url": "https://bedrock-runtime.us-west-2.amazonaws.com/model/anthropic.claude-3-sonnet-20240229-v1:0/converse",
            "request_body": "{\"system\": [{\"text\": \"${parameters.system_prompt}\"}], \"messages\": ${parameters.messages} , \"inferenceConfig\": {\"temperature\": 0.0, \"topP\": 0.9, \"maxTokens\": 1000} }"
        }
    ]
}
austintlee commented 1 month ago

@ylwu-amzn ^^