본문 바로가기
WIKI 기술 지식 베이스

Conversational search using Anthropic Claude on Amazon Bedrock

원문 보기 위키 갱신

출처: 문서

본문

Amazon Bedrock에서 Anthropic Claude를 사용한 대화형 검색

이 튜토리얼에서는 Amazon Bedrock에서 호스팅되는 Anthropic Claude 모델을 사용해 검색 증강 생성(RAG)이 적용된 대화형 검색을 구성하는 방법을 보여 줘요. 자세한 내용은 대화형 검색을 참고하세요.

your_ 접두사로 시작하는 플레이스홀더는 여러분의 값으로 바꿔 주세요. {: .note}

또는 에이전트와 도구를 사용해 RAG/대화형 검색을 구축할 수도 있어요. 자세한 내용은 검색 증강 생성 챗봇을 참고하세요.

사전 준비

테스트 데이터를 수집해요:

POST _bulk
{"index": {"_index": "qa_demo", "_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": "qa_demo", "_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": "qa_demo", "_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": "qa_demo", "_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": "qa_demo", "_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": "qa_demo", "_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 %}

다음 Amazon Bedrock API를 사용해 대화형 검색을 구성할 수 있어요:

  1. Converse API
  2. Invoke API

옵션 1: Amazon Bedrock Converse API

Amazon Bedrock Converse API를 사용해 대화형 검색을 구성하려면 다음 단계를 따라 주세요.

단계 1.1: 커넥터 생성 및 모델 등록

먼저 Claude 모델용 커넥터를 만들어요. 이 예제에서는 Anthropic Claude 3.5 Sonnet을 사용해요:

POST _plugins/_ml/connectors/_create
{
    "name": "Amazon Bedrock claude v3",
    "description": "Test connector for Amazon Bedrock claude v3",
    "version": 1,
    "protocol": "aws_sigv4",
    "credential": {
        "access_key": "your_access_key",
        "secret_key": "your_secret_key",
        "session_token": "your_session_token"
    },
    "parameters": {
        "region": "your_aws_region",
        "service_name": "bedrock",
        "model": "anthropic.claude-3-5-sonnet-20240620-v1:0",
        "system_prompt": "you are a helpful assistant.",
        "temperature": 0.0,
        "top_p": 0.9,
        "max_tokens": 1000
    },
    "actions": [
        {
            "action_type": "predict",
            "method": "POST",
            "headers": {
                "content-type": "application/json"
            },
            "url": "https://bedrock-runtime.${parameters.region}.amazonaws.com/model/${parameters.model}/converse",
            "request_body": "{ \"system\": [{\"text\": \"${parameters.system_prompt}\"}], \"messages\": ${parameters.messages} , \"inferenceConfig\": {\"temperature\": ${parameters.temperature}, \"topP\": ${parameters.top_p}, \"maxTokens\": ${parameters.max_tokens}} }"
        }
    ]
}

{% include copy-curl.html %}

Claude 2를 사용하려면 model에 anthropic.claude-3-5-sonnet-20240620-v1:0 대신 anthropic.claude-v2를 지정해 주세요.

커넥터 ID를 기억해 두세요. 모델을 등록할 때 사용해요.

다음으로 모델을 등록해요:

POST /_plugins/_ml/models/_register?deploy=true
{
    "name": "Bedrock Claude3.5 model",
    "description": "Bedrock Claude3.5 model",
    "function_name": "remote",
    "connector_id": "your_connector_id"
}

{% include copy-curl.html %}

모델 ID를 기억해 두세요. 이후 단계에서 사용해요.

모델을 테스트해요:

POST /_plugins/_ml/models/your_model_id/_predict
{
  "parameters": {
    "messages": [
      {
        "role": "user",
        "content": [
          {
            "text": "hello"
          }
        ]
      }
    ]
  }
}

{% include copy-curl.html %}

응답에는 모델이 생성한 텍스트가 담겨 있어요:

{
  "inference_results": [
    {
      "output": [
        {
          "name": "response",
          "dataAsMap": {
            "metrics": {
              "latencyMs": 955.0
            },
            "output": {
              "message": {
                "content": [
                  {
                    "text": "Hello! How can I assist you today? Feel free to ask me any questions or let me know if you need help with anything."
                  }
                ],
                "role": "assistant"
              }
            },
            "stopReason": "end_turn",
            "usage": {
              "inputTokens": 14.0,
              "outputTokens": 30.0,
              "totalTokens": 44.0
            }
          }
        }
      ],
      "status_code": 200
    }
  ]
}

단계 1.2: RAG 구성

RAG를 구성하려면 RAG 프로세서가 포함된 검색 파이프라인을 만들어요:

PUT /_search/pipeline/my-conversation-search-pipeline-claude
{
  "response_processors": [
    {
      "retrieval_augmented_generation": {
        "tag": "Demo pipeline",
        "description": "Demo pipeline Using Bedrock Claude",
        "model_id": "your_model_id",
        "context_field_list": [
          "text"
        ],
        "system_prompt": "You are a helpful assistant",
        "user_instructions": "Generate a concise and informative answer in less than 100 words for the given question"
      }
    }
  ]
}

{% include copy-curl.html %}

대화 기록을 저장하지 않고 기본적인 RAG 검색을 실행해요:

GET /qa_demo/_search?search_pipeline=my-conversation-search-pipeline-claude
{
  "query": {
    "match": {
      "text": "What's the population increase of New York City from 2021 to 2023?"
    }
  },
  "size": 1,
  "_source": [
    "text"
  ],
  "ext": {
    "generative_qa_parameters": {
      "llm_model": "bedrock-converse/anthropic.claude-3-sonnet-20240229-v1:0",
      "llm_question": "What's the population increase of New York City from 2021 to 2023?",
      "context_size": 5
    }
  }
}

{% include copy-curl.html %}

응답에는 모델 답변과 관련 문서가 담겨 있어요:

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 6,
      "relation": "eq"
    },
    "max_score": 9.042081,
    "hits": [
      {
        "_index": "qa_demo",
        "_id": "2",
        "_score": 9.042081,
        "_source": {
          "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."
        }
      }
    ]
  },
  "ext": {
    "retrieval_augmented_generation": {
      "answer": "The population of the New York City metro area increased by 114,000 people from 2021 to 2023. In 2021, the population was 18,823,000. By 2023, it had grown to 18,937,000. This represents a total increase of about 0.61% over the two-year period, with growth rates of 0.23% from 2021 to 2022 and 0.37% from 2022 to 2023."
    }
  }
}

단계 1.3: 대화형 검색 구성

대화 기록을 메모리에 저장하는 방식으로 대화형 검색을 구성하려면 다음 단계를 따라 주세요.

  1. 메모리를 만들어요:

    POST /_plugins/_ml/memory/
    {
    "name": "Conversation about NYC population"
    }
    

    {% include copy-curl.html %}

    응답에는 메모리 ID가 담겨 있어요:

    {
    "memory_id": "sBAqY5UBSzdNxlHvrSJK"
    }
    
  2. 대화 기록을 저장하려면 검색 요청에 메모리 ID를 포함해 주세요:

    GET /qa_demo/_search?search_pipeline=my-conversation-search-pipeline-claude
    {
    "query": {
        "match": {
        "text": "What's the population increase of New York City from 2021 to 2023?"
        }
    },
    "size": 1,
    "_source": [
        "text"
    ],
    "ext": {
        "generative_qa_parameters": {
        "llm_model": "bedrock-converse/anthropic.claude-3-sonnet-20240229-v1:0",
        "llm_question": "What's the population increase of New York City from 2021 to 2023?",
        "context_size": 5,
        "memory_id": "sBAqY5UBSzdNxlHvrSJK"
        }
    }
    }
    

    {% include copy-curl.html %}

    응답에는 모델 답변과 관련 문서가 담겨 있어요:

    {
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
        "value": 6,
        "relation": "eq"
        },
        "max_score": 9.042081,
        "hits": [
        {
            "_index": "qa_demo",
            "_id": "2",
            "_score": 9.042081,
            "_source": {
            "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."
            }
        }
        ]
    },
    "ext": {
        "retrieval_augmented_generation": {
        "answer": "The population of the New York City metro area increased by 114,000 people from 2021 to 2023. In 2021, the population was 18,823,000. By 2023, it had grown to 18,937,000. This represents a total increase of about 0.61% over the two-year period, with growth rates of 0.23% from 2021 to 2022 and 0.37% from 2022 to 2023.",
        "message_id": "sRAqY5UBSzdNxlHvzCIL"
        }
    }
    }
    
  3. 대화를 이어 가려면 다음 검색에서 동일한 메모리 ID를 제공해 주세요:

    GET /qa_demo/_search?search_pipeline=my-conversation-search-pipeline-claude
    {
    "query": {
        "match": {
        "text": "What's the population increase of Chicago from 2021 to 2023?"
        }
    },
    "size": 1,
    "_source": [
        "text"
    ],
    "ext": {
        "generative_qa_parameters": {
        "llm_model": "bedrock-converse/anthropic.claude-3-sonnet-20240229-v1:0",
        "llm_question": "can you compare the population increase of Chicago with New York City",
        "context_size": 5,
        "memory_id": "sBAqY5UBSzdNxlHvrSJK"
        }
    }
    }
    

    {% include copy-curl.html %}

    모델은 메모리의 대화 기록을 활용해 시카고의 인구 데이터를 앞서 다룬 뉴욕시 통계와 비교해요:

    {
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
        "value": 6,
        "relation": "eq"
        },
        "max_score": 3.6660428,
        "hits": [
        {
            "_index": "qa_demo",
            "_id": "3",
            "_score": 3.6660428,
            "_source": {
            "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."
            }
        }
        ]
    },
    "ext": {
        "retrieval_augmented_generation": {
        "answer": "Based on the provided data for Chicago, we can compare its population increase to New York City from 2021 to 2023:\n\nChicago's population increased from 8,877,000 in 2021 to 8,937,000 in 2023, a total increase of 60,000 people or about 0.68%.\n\nNew York City's population increased by 114,000 people or 0.61% in the same period.\n\nWhile New York City had a larger absolute increase, Chicago experienced a slightly higher percentage growth rate during this two-year period.",
        "message_id": "shArY5UBSzdNxlHvQyL-"
        }
    }
    }
    

옵션 2: Amazon Bedrock Invoke API

Amazon Bedrock Invoke API를 사용해 대화형 검색을 구성하려면 다음 단계를 따라 주세요.

Anthropic Claude 3.x 모델은 서로 다른 인터페이스를 필요로 하기 때문에 Amazon Bedrock Invoke API에서는 지원되지 않아요. {: .important}

단계 2.1: 커넥터 생성 및 모델 등록

먼저 Claude 모델용 커넥터를 만들어요. 이 예제에서는 Anthropic Claude v2를 사용해요:

POST _plugins/_ml/connectors/_create
{
    "name": "Bedrock Claude2",
    "description": "Connector for Bedrock Claude2",
    "version": 1,
    "protocol": "aws_sigv4",
    "credential": {
        "access_key": "your_access_key",
        "secret_key": "your_secret_key",
        "session_token": "your_session_token"
    },
    "parameters": {
        "region": "your_aws_region",
        "service_name": "bedrock",
        "model": "anthropic.claude-v2"
    },
    "actions": [
        {
            "action_type": "predict",
            "method": "POST",
            "headers": {
                "content-type": "application/json"
            },
            "url": "https://bedrock-runtime.${parameters.region}.amazonaws.com/model/${parameters.model}/invoke",
            "request_body": "{\"prompt\":\"\\n\\nHuman: ${parameters.inputs}\\n\\nAssistant:\",\"max_tokens_to_sample\":300,\"temperature\":0.5,\"top_k\":250,\"top_p\":1,\"stop_sequences\":[\"\\\\n\\\\nHuman:\"]}"
        }
    ]
}

{% include copy-curl.html %}

커넥터 ID를 기억해 두세요. 모델을 등록할 때 사용해요.

다음으로 모델을 등록해요:

POST /_plugins/_ml/models/_register?deploy=true
{
    "name": "Bedrock Claude2 model",
    "function_name": "remote",
    "description": "Bedrock Claude2 model",
    "connector_id": "your_connector_id"
}

{% include copy-curl.html %}

모델 ID를 기억해 두세요. 이후 단계에서 사용해요.

모델을 테스트해요:

POST /_plugins/_ml/models/your_model_id/_predict
{
    "parameters": {
      "inputs": "Who won the world series in 2020?"
    }
}

{% include copy-curl.html %}

응답에는 모델이 생성한 텍스트가 담겨 있어요:

{
  "inference_results": [
    {
      "output": [
        {
          "name": "response",
          "dataAsMap": {
            "type": "completion",
            "completion": " The Los Angeles Dodgers won the 2020 World Series, defeating the Tampa Bay Rays 4 games to 2. The World Series was played at a neutral site in Arlington, Texas due to the COVID-19 pandemic. It was the Dodgers' first World Series championship since 1988.",
            "stop_reason": "stop_sequence",
            "stop": "\n\nHuman:"
          }
        }
      ],
      "status_code": 200
    }
  ]
}

단계 2.2: RAG 구성

RAG를 구성하려면 RAG 프로세서가 포함된 검색 파이프라인을 만들어요:

PUT /_search/pipeline/my-conversation-search-pipeline-claude2
{
  "response_processors": [
    {
      "retrieval_augmented_generation": {
        "tag": "Demo pipeline",
        "description": "Demo pipeline Using Bedrock Claude2",
        "model_id": "your_model_id",
        "context_field_list": [
          "text"
        ],
        "system_prompt": "You are a helpful assistant",
        "user_instructions": "Generate a concise and informative answer in less than 100 words for the given question"
      }
    }
  ]
}

{% include copy-curl.html %}

대화 기록을 저장하지 않고 기본적인 RAG 검색을 실행해요:

GET /qa_demo/_search?search_pipeline=my-conversation-search-pipeline-claude2
{
  "query": {
    "match": {
      "text": "What's the population increase of New York City from 2021 to 2023?"
    }
  },
  "size": 1,
  "_source": [
    "text"
  ],
  "ext": {
    "generative_qa_parameters": {
      "llm_model": "bedrock/claude",
      "llm_question": "What's the population increase of New York City from 2021 to 2023?",
      "context_size": 5,
      "timeout": 15
    }
  }
}

{% include copy-curl.html %}

응답은 단계 1.2의 응답과 비슷해요.

단계 2.3: 대화형 검색 구성

대화형 검색을 구성하려면 단계 1.3으로 계속 진행해 주세요.

더 알아보기 (Learn more)