Build your own chatbot
출처: 문서
본문
나만의 챗봇 만들기
때로는 대규모 언어 모델(LLM)이 질문에 바로 답변하지 못할 수 있어요. 예를 들어, LLM은 지난주 로그 인덱스에 오류가 몇 개 있는지 알려줄 수 없어요. 그 이유는 LLM의 지식 베이스에 사용자의 독점 데이터가 포함되어 있지 않기 때문이에요. 이런 경우에는 후속 호출에서 LLM에 추가 정보를 제공해야 해요. 에이전트를 사용하면 이러한 복잡한 문제를 해결할 수 있어요. 에이전트는 도구를 실행하여 구성된 데이터 소스에서 추가 정보를 가져오고, 그 정보를 컨텍스트로 LLM에 보낼 수 있어요.
이 튜토리얼에서는 OpenSearch에서 conversational 에이전트를 사용하여 나만의 챗봇을 구축하는 방법을 설명해요. 에이전트에 대한 자세한 내용은 에이전트 및 도구를 참조하세요.
your_ 접두사로 시작하는 플레이스홀더는 자신의 값으로 바꾸세요.
{: .note}
사전 준비사항
OpenSearch Dashboards 홈 페이지에 로그인하여 Add sample data를 선택하고 Sample eCommerce orders 데이터를 추가하세요.
1단계: 지식 베이스 구성하기
사전 준비사항을 충족하고 RAG with a conversational flow agent tutorial의 1단계를 따라 미국 도시 인구 데이터가 포함된 test_population_data 지식 베이스 인덱스를 구성하세요.
수집 파이프라인을 생성하세요:
PUT /_ingest/pipeline/test_stock_price_data_pipeline
{
"description": "text embedding pipeline",
"processors": [
{
"text_embedding": {
"model_id": "your_text_embedding_model_id",
"field_map": {
"stock_price_history": "stock_price_history_embedding"
}
}
}
]
}
{% include copy-curl.html %}
과거 주가 데이터가 포함된 test_stock_price_data 인덱스를 생성하세요:
PUT test_stock_price_data
{
"mappings": {
"properties": {
"stock_price_history": {
"type": "text"
},
"stock_price_history_embedding": {
"type": "knn_vector",
"dimension": 384
}
}
},
"settings": {
"index": {
"knn.space_type": "cosinesimil",
"default_pipeline": "test_stock_price_data_pipeline",
"knn": "true"
}
}
}
{% include copy-curl.html %}
인덱스에 데이터를 수집하세요:
POST _bulk
{"index": {"_index": "test_stock_price_data"}}
{"stock_price_history": "This is the historical montly stock price record for Amazon.com, Inc. (AMZN) with CSV format.\nDate,Open,High,Low,Close,Adj Close,Volume\n2023-03-01,93.870003,103.489998,88.120003,103.290001,103.290001,1349240300\n2023-04-01,102.300003,110.860001,97.709999,105.449997,105.449997,1224083600\n2023-05-01,104.949997,122.919998,101.150002,120.580002,120.580002,1432891600\n2023-06-01,120.690002,131.490005,119.930000,130.360001,130.360001,1242648800\n2023-07-01,130.820007,136.649994,125.919998,133.679993,133.679993,1058754800\n2023-08-01,133.550003,143.630005,126.410004,138.009995,138.009995,1210426200\n2023-09-01,139.460007,145.860001,123.040001,127.120003,127.120003,1120271900\n2023-10-01,127.279999,134.479996,118.349998,133.089996,133.089996,1224564700\n2023-11-01,133.960007,149.259995,133.710007,146.089996,146.089996,1025986900\n2023-12-01,146.000000,155.630005,142.809998,151.940002,151.940002,931128600\n2024-01-01,151.539993,161.729996,144.050003,155.199997,155.199997,953344900\n2024-02-01,155.869995,175.000000,155.619995,174.449997,174.449997,437720800\n"}
{"index": {"_index": "test_stock_price_data"}}
{"stock_price_history": "This is the historical montly stock price record for Apple Inc. (AAPL) with CSV format.\nDate,Open,High,Low,Close,Adj Close,Volume\n2023-03-01,146.830002,165.000000,143.899994,164.899994,164.024475,1520266600\n2023-04-01,164.270004,169.850006,159.779999,169.679993,168.779099,969709700\n2023-05-01,169.279999,179.350006,164.309998,177.250000,176.308914,1275155500\n2023-06-01,177.699997,194.479996,176.929993,193.970001,193.207016,1297101100\n2023-07-01,193.779999,198.229996,186.600006,196.449997,195.677261,996066400\n2023-08-01,196.240005,196.729996,171.960007,187.869995,187.130997,1322439400\n2023-09-01,189.490005,189.979996,167.619995,171.210007,170.766846,1337586600\n2023-10-01,171.220001,182.339996,165.669998,170.770004,170.327972,1172719600\n2023-11-01,171.000000,192.929993,170.119995,189.949997,189.458313,1099586100\n2023-12-01,190.330002,199.619995,187.449997,192.529999,192.284637,1062774800\n2024-01-01,187.149994,196.380005,180.169998,184.399994,184.164993,1187219300\n2024-02-01,183.990005,191.050003,179.250000,188.850006,188.609329,420063900\n"}
{"index": {"_index": "test_stock_price_data"}}
{"stock_price_history": "This is the historical montly stock price record for NVIDIA Corporation (NVDA) with CSV format.\nDate,Open,High,Low,Close,Adj Close,Volume\n2023-03-01,231.919998,278.339996,222.970001,277.769989,277.646820,1126373100\n2023-04-01,275.089996,281.100006,262.200012,277.489990,277.414032,743592100\n2023-05-01,278.399994,419.380005,272.399994,378.339996,378.236420,1169636000\n2023-06-01,384.890015,439.899994,373.559998,423.019989,422.904175,1052209200\n2023-07-01,425.170013,480.880005,413.459991,467.290009,467.210449,870489500\n2023-08-01,464.600006,502.660004,403.109985,493.549988,493.465942,1363143600\n2023-09-01,497.619995,498.000000,409.799988,434.989990,434.915924,857510100\n2023-10-01,440.299988,476.089996,392.299988,407.799988,407.764130,1013917700\n2023-11-01,408.839996,505.480011,408.690002,467.700012,467.658905,914386300\n2023-12-01,465.250000,504.329987,450.100006,495.220001,495.176453,740951700\n2024-01-01,492.440002,634.929993,473.200012,615.270020,615.270020,970385300\n2024-02-01,621.000000,721.849976,616.500000,721.330017,721.330017,355346500\n"}
{"index": {"_index": "test_stock_price_data"}}
{"stock_price_history": "This is the historical montly stock price record for Meta Platforms, Inc. (META) with CSV format.\n\nDate,Open,High,Low,Close,Adj Close,Volume\n2023-03-01,174.589996,212.169998,171.429993,211.940002,211.940002,690053000\n2023-04-01,208.839996,241.690002,207.130005,240.320007,240.320007,446687900\n2023-05-01,238.619995,268.649994,229.850006,264.720001,264.720001,486968500\n2023-06-01,265.899994,289.790009,258.880005,286.980011,286.980011,480979900\n2023-07-01,286.700012,326.200012,284.850006,318.600006,318.600006,624605100\n2023-08-01,317.540009,324.140015,274.380005,295.890015,295.890015,423147800\n2023-09-01,299.369995,312.869995,286.790009,300.209991,300.209991,406686600\n2023-10-01,302.739990,330.540009,279.399994,301.269989,301.269989,511307900\n2023-11-01,301.850006,342.920013,301.850006,327.149994,327.149994,329270500\n2023-12-01,325.480011,361.899994,313.660004,353.959991,353.959991,332813800\n2024-01-01,351.320007,406.359985,340.010010,390.140015,390.140015,347020200\n2024-02-01,393.940002,485.959991,393.049988,473.279999,473.279999,294260900\n"}
{"index": {"_index": "test_stock_price_data"}}
{"stock_price_history": "This is the historical montly stock price record for Microsoft Corporation (MSFT) with CSV format.\n\nDate,Open,High,Low,Close,Adj Close,Volume\n2023-03-01,250.759995,289.269989,245.610001,288.299988,285.953064,747635000\n2023-04-01,286.519989,308.929993,275.369995,307.260010,304.758759,551497100\n2023-05-01,306.970001,335.940002,303.399994,328.390015,325.716766,600807200\n2023-06-01,325.929993,351.470001,322.500000,340.540009,338.506226,547588700\n2023-07-01,339.190002,366.779999,327.000000,335.920013,333.913818,666764400\n2023-08-01,335.190002,338.540009,311.549988,327.760010,325.802582,479456700\n2023-09-01,331.309998,340.859985,309.450012,315.750000,314.528809,416680700\n2023-10-01,316.279999,346.200012,311.209991,338.109985,336.802307,540907000\n2023-11-01,339.790009,384.299988,339.649994,378.910004,377.444519,563880300\n2023-12-01,376.760010,378.160004,362.899994,376.040009,375.345886,522003700\n2024-01-01,373.859985,415.320007,366.500000,397.579987,396.846130,528399000\n2024-02-01,401.829987,420.820007,401.799988,409.489990,408.734131,237639700\n"}
{"index": {"_index": "test_stock_price_data"}}
{"stock_price_history": "This is the historical montly stock price record for Alphabet Inc. (GOOG) with CSV format.\n\nDate,Open,High,Low,Close,Adj Close,Volume\n2023-03-01,90.160004,107.510002,89.769997,104.000000,104.000000,725477100\n2023-04-01,102.669998,109.629997,102.379997,108.220001,108.220001,461670700\n2023-05-01,107.720001,127.050003,104.500000,123.370003,123.370003,620317400\n2023-06-01,123.500000,129.550003,116.910004,120.970001,120.970001,521386300\n2023-07-01,120.320000,134.070007,115.830002,133.110001,133.110001,525456900\n2023-08-01,130.854996,138.399994,127.000000,137.350006,137.350006,463482000\n2023-09-01,138.429993,139.929993,128.190002,131.850006,131.850006,389593900\n2023-10-01,132.154999,142.380005,121.459999,125.300003,125.300003,514877100\n2023-11-01,125.339996,141.100006,124.925003,133.919998,133.919998,405635900\n2023-12-01,133.320007,143.945007,129.399994,140.929993,140.929993,482059400\n2024-01-01,139.600006,155.199997,136.850006,141.800003,141.800003,428771200\n2024-02-01,143.690002,150.695007,138.169998,147.139999,147.139999,231934100\n"}
{% include copy-curl.html %}
2단계: LLM 준비하기
이 튜토리얼에서는 Amazon Bedrock Claude model을 사용해요. 다른 LLM을 사용할 수도 있어요. 자세한 내용은 외부 호스팅 모델에 연결을 참조하세요.
모델용 커넥터를 생성하세요:
POST /_plugins/_ml/connectors/_create
{
"name": "Bedrock Claude Sonnet 5",
"description": "The connector to BedRock service for claude model",
"version": 1,
"protocol": "aws_sigv4",
"parameters": {
"region": "us-east-1",
"service_name": "bedrock",
"model": "us.anthropic.claude-sonnet-5",
"response_filter": "$.output.message.content[0].text"
},
"credential": {
"access_key": "your_aws_access_key",
"secret_key": "your_aws_secret_key",
"session_token": "your_aws_session_token"
},
"actions": [{
"action_type": "predict",
"method": "POST",
"url": "https://bedrock-runtime.${parameters.region}.amazonaws.com/model/${parameters.model}/converse",
"headers": { "content-type": "application/json" },
"request_body": "{\"messages\": [${parameters._chat_history:-}{\"role\":\"user\",\"content\":[{\"text\":\"${parameters.prompt:-}\"}]}${parameters._interactions:-}]${parameters.tool_configs:-}}"
}]
}
{% include copy-curl.html %}
커넥터 ID를 기록해 두세요. 모델을 등록할 때 사용할 거예요.
모델을 등록하세요:
POST /_plugins/_ml/models/_register
{
"name": "Bedrock Claude Instant model",
"function_name": "remote",
"description": "Bedrock Claude instant-v1 model",
"connector_id": "your_connector_id"
}
{% include copy-curl.html %}
LLM 모델 ID를 기록해 두세요. 다음 단계에서 사용할 거예요.
모델을 배포하세요:
POST /_plugins/_ml/models/your_LLM_model_id/_deploy
{% include copy-curl.html %}
모델을 테스트하세요:
POST /_plugins/_ml/models/your_LLM_model_id/_predict
{
"parameters": {
"prompt": "\n\nHuman: how are you? \n\nAssistant:"
}
}
{% include copy-curl.html %}
3단계: 기본 프롬프트로 에이전트 생성하기
다음으로, 에이전트를 생성하고 테스트해요.
에이전트 생성하기
conversational 유형의 에이전트를 생성하세요.
에이전트는 다음 정보로 구성돼요:
- 메타 정보:
name,type,description. - LLM 정보: 에이전트는 LLM을 사용하여 추론하고 다음 단계를 선택해요. 여기에는 적절한 도구를 선택하고 도구 입력을 준비하는 것이 포함돼요.
- 도구: 도구는 에이전트가 실행할 수 있는 함수예요. 각 도구는 자체
name,description,parameters를 정의할 수 있어요. - 메모리: 채팅 메시지를 저장해요. OpenSearch는
conversation_index라는 하나의 메모리 유형을 지원해요.
에이전트에는 다음 매개변수가 포함돼요:
-
conversational: 이 에이전트 유형에는 기본 제공 프롬프트가 있어요. 자신의 프롬프트로 재정의하려면 4단계를 참조하세요. -
app_type: 여러 에이전트를 구분하기 위해 참조용으로 이 매개변수를 지정하세요. -
llm: LLM 구성을 정의해요:"max_iteration": 5: 에이전트가 LLM을 최대 5회 실행해요."response_filter": "$.completion": Bedrock Claude 모델 응답에서 LLM 답변을 검색하는 데 필요해요."message_history_limit": 5: 에이전트가 최대 5개의 최근 기록 메시지를 검색하여 LLM 컨텍스트에 추가해요. 이 매개변수를0으로 설정하면 컨텍스트에서 메시지 기록이 생략돼요.disable_trace:true로 설정하면 에이전트가 메모리에 추적 데이터를 저장하지 않아요. 추적 데이터는 각 메시지에 포함되며 메시지 생성 중 수행된 단계에 대한 자세한 설명을 제공해요.
-
memory: 메시지 저장 방식을 정의해요. OpenSearch는 메모리 인덱스에 메시지를 저장하는conversation_index메모리만 지원해요. -
도구:
- LLM은 추론하여 실행할 도구를 결정하고 도구의 입력을 준비해요.
- 도구의 출력을 응답에 포함하려면
"include_output_in_agent_response": true를 지정하세요. 이 튜토리얼에서는 응답에PPLTool출력을 포함할 거예요(에이전트 테스트의 예제 응답 참조). - 기본적으로 도구의
name은 도구의type과 동일하며, 각 도구에는 기본 설명이 있어요. 도구의name과description을 재정의할 수 있어요. tools목록의 각 도구는 고유한 이름을 가져야 해요. 예를 들어, 다음 데모 에이전트는VectorDBTool유형의 두 도구를 서로 다른 이름(population_data_knowledge_base및stock_price_data_knowledge_base)으로 정의해요. 각 도구에는 LLM이 도구의 기능을 쉽게 이해할 수 있도록 사용자 지정 설명이 있어요.
도구에 대한 자세한 내용은 도구를 참조하세요.
이 예제 요청은 에이전트에 여러 샘플 도구를 구성해요. 필요에 따라 사용 사례와 관련된 다른 도구를 구성할 수 있어요. {: .note}
에이전트를 등록하세요:
POST _plugins/_ml/agents/_register
{
"name": "Chat Agent with Claude",
"type": "conversational",
"description": "this is a test agent",
"app_type": "os_chat",
"llm": {
"model_id": "your_llm_model_id_from_step2",
"parameters": {
"max_iteration": 5,
"response_filter": "$.completion",
"message_history_limit": 5,
"disable_trace": false
}
},
"memory": {
"type": "conversation_index"
},
"tools": [
{
"type": "PPLTool",
"parameters": {
"model_id": "your_llm_model_id_from_step2",
"model_type": "CLAUDE",
"execute": true
},
"include_output_in_agent_response": true
},
{
"type": "VisualizationTool",
"parameters": {
"index": ".kibana"
},
"include_output_in_agent_response": true
},
{
"type": "VectorDBTool",
"name": "population_data_knowledge_base",
"description": "This tool provide population data of US cities.",
"parameters": {
"input": "${parameters.question}",
"index": "test_population_data",
"source_field": [
"population_description"
],
"model_id": "your_embedding_model_id_from_step1",
"embedding_field": "population_description_embedding",
"doc_size": 3
}
},
{
"type": "VectorDBTool",
"name": "stock_price_data_knowledge_base",
"description": "This tool provide stock price data.",
"parameters": {
"input": "${parameters.question}",
"index": "test_stock_price_data",
"source_field": [
"stock_price_history"
],
"model_id": "your_embedding_model_id_from_step1",
"embedding_field": "stock_price_history_embedding",
"doc_size": 3
}
},
{
"type": "ListIndexTool",
"description": "Use this tool to get OpenSearch index information: (health, status, index, uuid, primary count, replica count, docs.count, docs.deleted, store.size, primary.store.size). \nIt takes 2 optional arguments named `index` which is a comma-delimited list of one or more indices to get information from (default is an empty list meaning all indices), and `local` which means whether to return information from the local node only instead of the cluster manager node (default is false)."
},
{
"type": "SearchAnomalyDetectorsTool"
},
{
"type": "SearchAnomalyResultsTool"
},
{
"type": "SearchMonitorsTool"
},
{
"type": "SearchAlertsTool"
}
]
}
{% include copy-curl.html %}
에이전트 ID를 기록해 두세요. 다음 단계에서 사용할 거예요.
에이전트 테스트하기
다음 테스트 팁을 참고하세요:
-
에이전트 실행의 자세한 단계는 다음 방법 중 하나로 확인할 수 있어요:
- 상세 모드 활성화:
"verbose": true. - Get Trace API 호출:
GET _plugins/_ml/memory/message/your_message_id/traces.
- 상세 모드 활성화:
-
LLM이 환각을 일으킬 수 있어요. 특히 많은 도구를 구성한 경우 문제 해결에 잘못된 도구를 선택할 수 있어요. 환각을 방지하려면 다음 옵션을 시도해 보세요:
- 에이전트에 많은 도구를 구성하지 마세요.
- 도구가 수행할 수 있는 작업을 명확히 설명하는 자세한 도구 설명을 제공하세요.
- LLM 질문에서 사용할 도구를 지정하세요. 예:
Can you use the PPLTool to query the opensearch_dashboards_sample_data_ecommerce index so it can calculate how many orders were placed last week?. - 에이전트를 실행할 때 사용할 도구를 지정하세요. 예를 들어, 현재 요청을 처리하는 데
PPLTool과ListIndexTool만 사용하도록 지정하세요.
에이전트를 테스트하세요:
POST _plugins/_ml/agents/your_agent_id/_execute
{
"parameters": {
"question": "Can you query with index opensearch_dashboards_sample_data_ecommerce to calculate how many orders in last week?",
"verbose": false,
"selected_tools": ["PPLTool", "ListIndexTool"]
}
}
{% include copy-curl.html %}
PPLTool 테스트하기
POST _plugins/_ml/agents/your_agent_id/_execute
{
"parameters": {
"question": "Can you query with index opensearch_dashboards_sample_data_ecommerce to calculate how many orders in last week?",
"verbose": false
}
}
{% include copy-curl.html %}
PPLTool에 대해 "include_output_in_agent_response": true를 지정했기 때문에 응답의 additional_info 객체에 PPLTool.output이 포함돼요:
{
"inference_results": [
{
"output": [
{
"name": "memory_id",
"result": "TkJwyI0Bn3OCesyvzuH9"
},
{
"name": "parent_interaction_id",
"result": "T0JwyI0Bn3OCesyvz-EI"
},
{
"name": "response",
"dataAsMap": {
"response": "The tool response from the PPLTool shows that there were 3812 orders in the opensearch_dashboards_sample_data_ecommerce index within the last week.",
"additional_info": {
"PPLTool.output": [
"""{"ppl":"source\u003dopensearch_dashboards_sample_data_ecommerce| where order_date \u003e DATE_SUB(NOW(), INTERVAL 1 WEEK) | stats COUNT() AS count","executionResult":"{\n \"schema\": [\n {\n \"name\": \"count\",\n \"type\": \"integer\"\n }\n ],\n \"datarows\": [\n [\n 3812\n ]\n ],\n \"total\": 1,\n \"size\": 1\n}"}"""
]
}
}
}
]
}
]
}
{% include copy-curl.html %}
추적 데이터 가져오기:
GET _plugins/_ml/memory/message/T0JwyI0Bn3OCesyvz-EI/traces
{% include copy-curl.html %}
population_data_knowledge_base VectorDBTool 테스트하기
자세한 단계를 보려면 에이전트를 실행할 때 verbose를 true로 설정하세요:
POST _plugins/_ml/agents/your_agent_id/_execute
{
"parameters": {
"question": "What's the population increase of Seattle from 2021 to 2023?",
"verbose": true
}
}
{% include copy-curl.html %}
응답에 실행 단계가 포함돼요:
{
"inference_results": [
{
"output": [
{
"name": "memory_id",
"result": "LkJuyI0Bn3OCesyv3-Ef"
},
{
"name": "parent_interaction_id",
"result": "L0JuyI0Bn3OCesyv3-Er"
},
{
"name": "response",
"result": """{
"thought": "Let me check the population data tool",
"action": "population_data_knowledge_base",
"action_input": "{'question': 'What is the population increase of Seattle from 2021 to 2023?', 'cities': ['Seattle']}"
}"""
},
{
"name": "response",
"result": """{"_index":"test_population_data","_source":{"population_description":"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."},"_id":"9EJsyI0Bn3OCesyvU-B7","_score":0.75154537}
{"_index":"test_population_data","_source":{"population_description":"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."},"_id":"80JsyI0Bn3OCesyvU-B7","_score":0.6689899}
{"_index":"test_population_data","_source":{"population_description":"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."},"_id":"8EJsyI0Bn3OCesyvU-B7","_score":0.66782206}
"""
},
{
"name": "response",
"result": "According to the population data tool, the population of Seattle increased by approximately 28,000 people from 2021 to 2023, which is a 0.82% increase from 2021 to 2022 and a 0.86% increase from 2022 to 2023."
}
]
}
]
}
추적 데이터 가져오기:
GET _plugins/_ml/memory/message/L0JuyI0Bn3OCesyv3-Er/traces
{% include copy-curl.html %}
대화 메모리 테스트하기
동일한 대화를 계속하려면 에이전트를 실행할 때 대화의 memory_id를 지정하세요:
POST _plugins/_ml/agents/your_agent_id/_execute
{
"parameters": {
"question": "What's the population of Austin 2023, compare with Seattle",
"memory_id": "LkJuyI0Bn3OCesyv3-Ef",
"verbose": true
}
}
{% include copy-curl.html %}
응답에서 population_data_knowledge_base가 시애틀의 인구를 반환하지 않는 것을 확인할 수 있어요. 대신 에이전트는 기록 메시지를 참조하여 시애틀의 인구를 학습해요:
{
"inference_results": [
{
"output": [
{
"name": "memory_id",
"result": "LkJuyI0Bn3OCesyv3-Ef"
},
{
"name": "parent_interaction_id",
"result": "00J6yI0Bn3OCesyvIuGZ"
},
{
"name": "response",
"result": """{
"thought": "Let me check the population data tool first",
"action": "population_data_knowledge_base",
"action_input": "{\"city\":\"Austin\",\"year\":2023}"
}"""
},
{
"name": "response",
"result": """{"_index":"test_population_data","_source":{"population_description":"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."},"_id":"BhF5vo0BubpYKX5ER0fT","_score":0.69129956}
{"_index":"test_population_data","_source":{"population_description":"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."},"_id":"6zrZvo0BVR2NrurbRIAE","_score":0.69129956}
{"_index":"test_population_data","_source":{"population_description":"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."},"_id":"AxF5vo0BubpYKX5ER0fT","_score":0.61015373}
"""
},
{
"name": "response",
"result": "According to the population data tool, the population of Austin in 2023 is approximately 2,228,000 people, a 2.39% increase from 2022. This is lower than the population of Seattle in 2023 which is approximately 3,519,000 people, a 0.86% increase from 2022."
}
]
}
]
}
모든 메시지 보기:
GET _plugins/_ml/memory/LkJuyI0Bn3OCesyv3-Ef/messages
{% include copy-curl.html %}
추적 데이터 가져오기:
GET _plugins/_ml/memory/message/00J6yI0Bn3OCesyvIuGZ/traces
{% include copy-curl.html %}
4단계(선택 사항): 사용자 지정 프롬프트로 에이전트 생성하기
모든 에이전트에는 다음과 같은 기본 프롬프트가 있어요:
"prompt": """
Human:${parameters.prompt.prefix}
${parameters.prompt.suffix}
Human: follow RESPONSE FORMAT INSTRUCTIONS
Assistant:"""
프롬프트는 두 부분으로 구성돼요:
${parameters.prompt.prefix}: AI 어시스턴트가 수행할 수 있는 작업을 설명하는 프롬프트 접두사예요. 사용 사례에 따라 이 매개변수를 변경할 수 있어요. 예:You are a professional data analyst. You will always answer questions based on the tool response first. If you don't know the answer, just say you don't know.${parameters.prompt.suffix}: 도구, 채팅 기록, 프롬프트 형식 지침, 질문 및 스크래치패드를 정의하는 프롬프트의 주요 부분이에요.
기본 prompt.suffix는 다음과 같아요:
"prompt.suffix": """Human:TOOLS
------
Assistant can ask Human to use tools to look up information that may be helpful in answering the users original question. The tool response will be listed in "TOOL RESPONSE of {tool name}:". If TOOL RESPONSE is enough to answer human's question, Assistant should avoid rerun the same tool.
Assistant should NEVER suggest run a tool with same input if it's already in TOOL RESPONSE.
The tools the human can use are:
${parameters.tool_descriptions}
${parameters.chat_history}
${parameters.prompt.format_instruction}
Human:USER'S INPUT
--------------------
Here is the user's input :
${parameters.question}
${parameters.scratchpad}"""
prompt.suffix는 다음 플레이스홀더로 구성돼요:
${parameters.tool_descriptions}: 이 플레이스홀더는 에이전트의 도구 정보(도구 이름 및 설명)로 채워져요. 이 플레이스홀더를 생략하면 에이전트가 도구를 사용하지 않아요.${parameters.prompt.format_instruction}: 이 플레이스홀더는 LLM 응답 형식을 정의해요. 이 플레이스홀더는 중요하므로 제거하지 않는 것이 좋아요.${parameters.chat_history}: 이 플레이스홀더는 현재 메모리의 메시지 기록으로 채워져요. 에이전트를 실행할 때memory_id를 설정하지 않거나 기록 메시지가 없으면 이 플레이스홀더는 비어 있어요. 채팅 기록이 필요하지 않으면 이 플레이스홀더를 제거할 수 있어요.${parameters.question}: 이 플레이스홀더는 사용자 질문으로 채워져요.${parameters.scratchpad}: 이 플레이스홀더는 자세한 에이전트 실행 단계로 채워져요. 이 단계는 상세 모드를 지정하거나 추적 데이터를 가져와서 볼 수 있는 단계와 동일해요(에이전트 테스트의 예제 참조). 이 플레이스홀더는 LLM이 이전 단계의 결과를 기반으로 추론하고 다음 단계를 선택하는 데 중요하므로 제거하지 않는 것이 좋아요.
사용자 지정 프롬프트 예제
다음 예제에서는 프롬프트를 사용자 지정하는 방법을 보여줘요.
예제 1: prompt.prefix 사용자 지정
사용자 지정 prompt.prefix로 에이전트를 등록하세요:
POST _plugins/_ml/agents/_register
{
"name": "Chat Agent with Custom Prompt",
"type": "conversational",
"description": "this is a test agent",
"app_type": "os_chat",
"llm": {
"model_id": "P0L8xI0Bn3OCesyvPsif",
"parameters": {
"max_iteration": 3,
"response_filter": "$.completion",
"prompt.prefix": "Assistant is a professional data analyst. You will always answer question based on the tool response first. If you don't know the answer, just say don't know.\n"
}
},
"memory": {
"type": "conversation_index"
},
"tools": [
{
"type": "VectorDBTool",
"name": "population_data_knowledge_base",
"description": "This tool provide population data of US cities.",
"parameters": {
"input": "${parameters.question}",
"index": "test_population_data",
"source_field": [
"population_description"
],
"model_id": "xkJLyI0Bn3OCesyvf94S",
"embedding_field": "population_description_embedding",
"doc_size": 3
}
},
{
"type": "VectorDBTool",
"name": "stock_price_data_knowledge_base",
"description": "This tool provide stock price data.",
"parameters": {
"input": "${parameters.question}",
"index": "test_stock_price_data",
"source_field": [
"stock_price_history"
],
"model_id": "xkJLyI0Bn3OCesyvf94S",
"embedding_field": "stock_price_history_embedding",
"doc_size": 3
}
}
]
}
{% include copy-curl.html %}
에이전트를 테스트하세요:
POST _plugins/_ml/agents/o0LDyI0Bn3OCesyvr-Zq/_execute
{
"parameters": {
"question": "What's the stock price increase of Amazon from May 2023 to Feb 2023?",
"verbose": true
}
}
{% include copy-curl.html %}
예제 2: 사용자 지정 프롬프트가 있는 OpenAI 모델
OpenAI gpt-4o-mini 모델용 커넥터를 생성하세요:
POST _plugins/_ml/connectors/_create
{
"name": "My openai connector: gpt-4o-mini",
"description": "The connector to openai chat model",
"version": 1,
"protocol": "http",
"parameters": {
"model": "gpt-4o-mini",
"response_filter": "$.choices[0].message.content",
"stop": ["\n\nHuman:","\nObservation:","\n\tObservation:","\n\tObservation","\n\nQuestion"],
"system_instruction": "You are an Assistant which can answer kinds of questions."
},
"credential": {
"openAI_key": "your_openAI_key"
},
"actions": [
{
"action_type": "predict",
"method": "POST",
"url": "https://api.openai.com/v1/chat/completions",
"headers": {
"Authorization": "Bearer ${credential.openAI_key}"
},
"request_body": "{ \"model\": \"${parameters.model}\", \"messages\": [{\"role\":\"system\",\"content\":\"${parameters.system_instruction}\"},{\"role\":\"user\",\"content\":\"${parameters.prompt}\"}] }"
}
]
}
{% include copy-curl.html %}
응답의 커넥터 ID를 사용하여 모델을 생성하세요:
POST /_plugins/_ml/models/_register?deploy=true
{
"name": "My OpenAI model",
"function_name": "remote",
"description": "test model",
"connector_id": "your_connector_id"
}
{% include copy-curl.html %}
모델 ID를 기록하고 Predict API를 호출하여 모델을 테스트하세요:
POST /_plugins/_ml/models/your_openai_model_id/_predict
{
"parameters": {
"system_instruction": "You are an Assistant which can answer kinds of questions.",
"prompt": "hello"
}
}
{% include copy-curl.html %}
사용자 지정 system_instruction 및 prompt로 에이전트를 생성하세요. prompt는 tool_descriptions, chat_history, format_instruction, question 및 scratchpad 플레이스홀더를 사용자 지정해요:
POST _plugins/_ml/agents/_register
{
"name": "My Chat Agent with OpenAI gpt-4o-mini",
"type": "conversational",
"description": "this is a test agent",
"app_type": "os_chat",
"llm": {
"model_id": "your_openai_model_id",
"parameters": {
"max_iteration": 3,
"response_filter": "$.choices[0].message.content",
"system_instruction": "You are an assistant which is designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics.",
"prompt": "Assistant can ask Human to use tools to look up information that may be helpful in answering the users original question.\n${parameters.tool_descriptions}\n\n${parameters.chat_history}\n\n${parameters.prompt.format_instruction}\n\nHuman: ${parameters.question}\n\n${parameters.scratchpad}\n\nHuman: follow RESPONSE FORMAT INSTRUCTIONS\n\nAssistant:",
"disable_trace": true
}
},
"memory": {
"type": "conversation_index"
},
"tools": [
{
"type": "VectorDBTool",
"name": "population_data_knowledge_base",
"description": "This tool provide population data of US cities.",
"parameters": {
"input": "${parameters.question}",
"index": "test_population_data",
"source_field": [
"population_description"
],
"model_id": "your_embedding_model_id_from_step1",
"embedding_field": "population_description_embedding",
"doc_size": 3
}
},
{
"type": "VectorDBTool",
"name": "stock_price_data_knowledge_base",
"description": "This tool provide stock price data.",
"parameters": {
"input": "${parameters.question}",
"index": "test_stock_price_data",
"source_field": [
"stock_price_history"
],
"model_id": "your_embedding_model_id_from_step1",
"embedding_field": "stock_price_history_embedding",
"doc_size": 3
}
}
]
}
{% include copy-curl.html %}
응답에서 에이전트 ID를 기록하고 에이전트를 실행하여 모델을 테스트하세요:
POST _plugins/_ml/agents/your_agent_id/_execute
{
"parameters": {
"question": "What's the stock price increase of Amazon from May 2023 to Feb 2023?",
"verbose": true
}
}
{% include copy-curl.html %}
에이전트가 구성된 두 도구를 모두 사용하도록 요구하는 질문을 하여 에이전트를 테스트하세요:
POST _plugins/_ml/agents/your_agent_id/_execute
{
"parameters": {
"question": "What's the population increase of Seattle from 2021 to 2023? Then check what's the stock price increase of Amazon from May 2023 to Feb 2023?",
"verbose": true
}
}
{% include copy-curl.html %}
응답은 에이전트가 population_data_knowledge_base와 stock_price_data_knowledge_base 도구를 모두 실행하여 답변을 얻는다는 것을 보여줘요:
{
"inference_results": [
{
"output": [
{
"name": "memory_id",
"result": "_0IByY0Bn3OCesyvJenb"
},
{
"name": "parent_interaction_id",
"result": "AEIByY0Bn3OCesyvJerm"
},
{
"name": "response",
"result": """{
"thought": "I need to use a tool to find the population increase of Seattle from 2021 to 2023",
"action": "population_data_knowledge_base",
"action_input": "{\"city\": \"Seattle\", \"start_year\": 2021, \"end_year\": 2023}"
}"""
},
{
"name": "response",
"result": """{"_index":"test_population_data","_source":{"population_description":"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."},"_id":"9EJsyI0Bn3OCesyvU-B7","_score":0.6542084}
{"_index":"test_population_data","_source":{"population_description":"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."},"_id":"8EJsyI0Bn3OCesyvU-B7","_score":0.5966786}
{"_index":"test_population_data","_source":{"population_description":"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."},"_id":"80JsyI0Bn3OCesyvU-B7","_score":0.5883104}
"""
},
{
"name": "response",
"result": """{
"thought": "I need to use a tool to find the stock price increase of Amazon from May 2023 to Feb 2023",
"action": "stock_price_data_knowledge_base",
"action_input": "{\"company\": \"Amazon\", \"start_date\": \"May 2023\", \"end_date\": \"Feb 2023\"}"
}"""
},
{
"name": "response",
"result": """{"_index":"test_stock_price_data","_source":{"stock_price_history":"This is the historical montly stock price record for Amazon.com, Inc. (AMZN) with CSV format.\nDate,Open,High,Low,Close,Adj Close,Volume\n2023-03-01,93.870003,103.489998,88.120003,103.290001,103.290001,1349240300\n2023-04-01,102.300003,110.860001,97.709999,105.449997,105.449997,1224083600\n2023-05-01,104.949997,122.919998,101.150002,120.580002,120.580002,1432891600\n2023-06-01,120.690002,131.490005,119.930000,130.360001,130.360001,1242648800\n2023-07-01,130.820007,136.649994,125.919998,133.679993,133.679993,1058754800\n2023-08-01,133.550003,143.630005,126.410004,138.009995,138.009995,1210426200\n2023-09-01,139.460007,145.860001,123.040001,127.120003,127.120003,1120271900\n2023-10-01,127.279999,134.479996,118.349998,133.089996,133.089996,1224564700\n2023-11-01,133.960007,149.259995,133.710007,146.089996,146.089996,1025986900\n2023-12-01,146.000000,155.630005,142.809998,151.940002,151.940002,931128600\n2024-01-01,151.539993,161.729996,144.050003,155.199997,155.199997,953344900\n2024-02-01,155.869995,175.000000,155.619995,174.449997,174.449997,437720800\n"},"_id":"BUJsyI0Bn3OCesyvveHo","_score":0.63949186}
{"_index":"test_stock_price_data","_source":{"stock_price_history":"This is the historical montly stock price record for Alphabet Inc. (GOOG) with CSV format.\n\nDate,Open,High,Low,Close,Adj Close,Volume\n2023-03-01,90.160004,107.510002,89.769997,104.000000,104.000000,725477100\n2023-04-01,102.669998,109.629997,102.379997,108.220001,108.220001,461670700\n2023-05-01,107.720001,127.050003,104.500000,123.370003,123.370003,620317400\n2023-06-01,123.500000,129.550003,116.910004,120.970001,120.970001,521386300\n2023-07-01,120.320000,134.070007,115.830002,133.110001,133.110001,525456900\n2023-08-01,130.854996,138.399994,127.000000,137.350006,137.350006,463482000\n2023-09-01,138.429993,139.929993,128.190002,131.850006,131.850006,389593900\n2023-10-01,132.154999,142.380005,121.459999,125.300003,125.300003,514877100\n2023-11-01,125.339996,141.100006,124.925003,133.919998,133.919998,405635900\n2023-12-01,133.320007,143.945007,129.399994,140.929993,140.929993,482059400\n2024-01-01,139.600006,155.199997,136.850006,141.800003,141.800003,428771200\n2024-02-01,143.690002,150.695007,138.169998,147.139999,147.139999,231934100\n"},"_id":"CkJsyI0Bn3OCesyvveHo","_score":0.6056718}
{"_index":"test_stock_price_data","_source":{"stock_price_history":"This is the historical montly stock price record for Apple Inc. (AAPL) with CSV format.\nDate,Open,High,Low,Close,Adj Close,Volume\n2023-03-01,146.830002,165.000000,143.899994,164.899994,164.024475,1520266600\n2023-04-01,164.270004,169.850006,159.779999,169.679993,168.779099,969709700\n2023-05-01,169.279999,179.350006,164.309998,177.250000,176.308914,1275155500\n2023-06-01,177.699997,194.479996,176.929993,193.970001,193.207016,1297101100\n2023-07-01,193.779999,198.229996,186.600006,196.449997,195.677261,996066400\n2023-08-01,196.240005,196.729996,171.960007,187.869995,187.130997,1322439400\n2023-09-01,189.490005,189.979996,167.619995,171.210007,170.766846,1337586600\n2023-10-01,171.220001,182.339996,165.669998,170.770004,170.327972,1172719600\n2023-11-01,171.000000,192.929993,170.119995,189.949997,189.458313,1099586100\n2023-12-01,190.330002,199.619995,187.449997,192.529999,192.284637,1062774800\n2024-01-01,187.149994,196.380005,180.169998,184.399994,184.164993,1187219300\n2024-02-01,183.990005,191.050003,179.250000,188.850006,188.609329,420063900\n"},"_id":"BkJsyI0Bn3OCesyvveHo","_score":0.5960163}
"""
},
{
"name": "response",
"result": "The population increase of Seattle from 2021 to 2023 is 0.86%. The stock price increase of Amazon from May 2023 to Feb 2023 is from $120.58 to $174.45, which is a percentage increase."
}
]
}
]
}
5단계: OpenSearch Dashboards에서 루트 챗봇 에이전트 구성하기
OpenSearch Assistant for OpenSearch Dashboards를 사용하려면 루트 챗봇 에이전트를 구성해야 해요.
루트 챗봇 에이전트는 다음 부분으로 구성돼요:
conversational에이전트:AgentTool내에서 이전 단계에서 생성한 모든conversational에이전트를 사용할 수 있어요.MLModelTool: 이 도구는 현재 질문과 모델 응답을 기반으로 새 질문을 제안하는 데 사용돼요.
루트 에이전트를 구성하세요:
POST /_plugins/_ml/agents/_register
{
"name": "Chatbot agent",
"type": "flow",
"description": "this is a test chatbot agent",
"tools": [
{
"type": "AgentTool",
"name": "LLMResponseGenerator",
"parameters": {
"agent_id": "your_conversational_agent_created_in_prevous_steps"
},
"include_output_in_agent_response": true
},
{
"type": "MLModelTool",
"name": "QuestionSuggestor",
"description": "A general tool to answer any question",
"parameters": {
"model_id": "your_llm_model_id_created_in_previous_steps",
"prompt": "Human: You are an AI that only speaks JSON. Do not write normal text. Output should follow example JSON format: \n\n {\"response\": [\"question1\", \"question2\"]}\n\n. \n\nHuman:You will be given a chat history between OpenSearch Assistant and a Human.\nUse the context provided to generate follow up questions the Human would ask to the Assistant.\nThe Assistant can answer general questions about logs, traces and metrics.\nAssistant can access a set of tools listed below to answer questions given by the Human:\nQuestion suggestions generator tool\nHere's the chat history between the human and the Assistant.\n${parameters.LLMResponseGenerator.output}\nUse the following steps to generate follow up questions Human may ask after the response of the Assistant:\nStep 1. Use the chat history to understand what human is trying to search and explore.\nStep 2. Understand what capabilities the assistant has with the set of tools it has access to.\nStep 3. Use the above context and generate follow up questions.Step4:You are an AI that only speaks JSON. Do not write normal text. Output should follow example JSON format: \n\n {\"response\": [\"question1\", \"question2\"]} \n \n----------------\n\nAssistant:"
},
"include_output_in_agent_response": true
}
],
"memory": {
"type": "conversation_index"
}
}
{% include copy-curl.html %}
루트 챗봇 에이전트 ID를 기록하고, OpenSearch 서버에 로그인하여 OpenSearch 구성 폴더($OS_HOME/config)로 이동한 후 다음 명령을 실행하세요:
curl -k --cert ./kirk.pem --key ./kirk-key.pem -X PUT https://localhost:9200/.plugins-ml-config/_doc/os_chat -H 'Content-Type: application/json' -d'
{
"type":"os_chat_root_agent",
"configuration":{
"agent_id": "your_root_chatbot_agent_id"
}
}'
{% include copy.html %}
OpenSearch Dashboards 구성 폴더($OSD_HOME/config)로 이동하여 opensearch_dashboards.yml을 편집하고 파일 끝에 다음 줄을 추가하세요: assistant.chat.enabled: true.
OpenSearch Dashboards를 다시 시작한 후 오른쪽 상단 모서리에 있는 채팅 아이콘을 선택하세요. 다음 이미지에 표시되어 있어요.
{: width="300" }
이제 OpenSearch Dashboards에서 채팅할 수 있어요. 다음 이미지와 같아요.
