에이전트 사용

에이전트를 쿼리하는 코드는 로컬에서 실행 중인지 또는 원격으로 배포되었는지와 관계없이 동일합니다. 따라서 이 페이지에서 용어 agentlocal_agent 또는 remote_agent를 같은 의미로 나타내는 데 사용됩니다. 지원되는 작업 집합은 프레임워크마다 다르므로 프레임워크별 템플릿 사용 안내를 제공합니다.

프레임워크 설명
LangChain 사전 정의된 구성 및 추상화 덕분에 기본 사용 사례에서 더 쉽게 사용할 수 있습니다.
LangGraph 고급 인간 참여형(Human-In-The-Loop) 및 되감기/재생 기능을 사용하여 워크플로를 정의하는 그래프 기반 접근 방식입니다.
AG2(이전의 AutoGen) AG2는 LLM 워크플로를 빌드하기 위한 고급 추상화로 멀티 에이전트 대화 프레임워크를 제공합니다.
LlamaIndex 쿼리 파이프라인 LlamaIndex의 쿼리 파이프라인은 검색 증강 생성(RAG) 워크플로를 만들기 위한 고급 인터페이스를 제공합니다.

프레임워크별 템플릿 중 하나를 기반으로 하지 않는 커스텀 에이전트의 경우 다음 단계를 따르세요.

  1. 사용자 인증
  2. 에이전트 인스턴스 가져오기
  3. 지원되는 작업 조회
  4. 에이전트 쿼리
  5. (해당하는 경우) 에이전트 응답 스트리밍

1단계: 사용자 인증

환경 설정과 동일한 안내를 따릅니다.

2단계: 에이전트 인스턴스 가져오기

에이전트를 쿼리하려면 먼저 에이전트의 인스턴스가 필요합니다. 에이전트의 새 인스턴스를 만들거나 기존 인스턴스를 가져올 수 있습니다.

특정 리소스 ID에 해당하는 에이전트를 가져오려면 다음 안내를 따르세요.

Python용 Vertex AI SDK

다음 코드를 실행합니다.

from vertexai import agent_engines

agent = agent_engines.get(RESOURCE_ID)

또는 에이전트의 전체 리소스 이름을 제공할 수 있습니다.

agent = agent_engines.get("projects/PROJECT_ID/locations/LOCATION/reasoningEngines/RESOURCE_ID")

요청

다음 코드를 실행합니다.

from google import auth as google_auth
from google.auth.transport import requests as google_requests
import requests

def get_identity_token():
    credentials, _ = google_auth.default()
    auth_request = google_requests.Request()
    credentials.refresh(auth_request)
    return credentials.token

response = requests.get(
f"https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/reasoningEngines/RESOURCE_ID",
    headers={
        "Content-Type": "application/json; charset=utf-8",
        "Authorization": f"Bearer {get_identity_token()}",
    },
)

REST

curl \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/reasoningEngines/RESOURCE_ID

이 섹션의 나머지 부분에서는 agent라는 이름의 인스턴스가 있다고 가정합니다.

3단계: 지원되는 작업

로컬에서 에이전트를 개발할 때는 에이전트가 지원하는 작업에 액세스하고 이를 알 수 있습니다. 배포된 에이전트를 사용하려면 에이전트에서 지원하는 작업을 열거하면 됩니다.

Python용 Vertex AI SDK

다음 코드를 실행합니다.

agent.operation_schemas()

요청

다음 코드를 실행합니다.

import json

json.loads(response.content).get("spec").get("classMethods")

REST

curl 요청에 대한 응답에서 spec.class_methods로 표시됩니다.

각 작업의 스키마는 호출할 수 있는 에이전트의 메서드 정보를 문서화하는 사전입니다. 다음은 동기 작업의 작업 스키마 예입니다.

다음 명령어는 JSON 형식으로 remote_app 객체의 작업에 해당하는 스키마 목록을 제공합니다.

agent.operation_schemas()

예를 들어 다음은 LangchainAgentquery 작업에 관한 스키마입니다.

{'api_mode': '',
 'name': 'query',
 'description': """Queries the Agent with the given input and config.
    Args:
        input (Union[str, Mapping[str, Any]]):
            Required. The input to be passed to the Agent.
        config (langchain_core.runnables.RunnableConfig):
            Optional. The config (if any) to be used for invoking the Agent.
    Returns:
        The output of querying the Agent with the given input and config.
""",            '        ',
 'parameters': {'$defs': {'RunnableConfig': {'description': 'Configuration for a Runnable.',
                                             'properties': {'configurable': {...},
                                                            'run_id': {...},
                                                            'run_name': {...},
                                                            ...},
                                             'type': 'object'}},
                'properties': {'config': {'nullable': True},
                               'input': {'anyOf': [{'type': 'string'}, {'type': 'object'}]}},
                'required': ['input'],
                'type': 'object'}}

각 항목의 의미는 다음과 같습니다.

  • name은 작업 이름입니다(예: query라는 작업의 경우 agent.query).
  • api_mode는 작업의 API 모드입니다(동기의 경우 "", 스트리밍의 경우 "stream").
  • description은 메서드의 문서 문자열을 기반으로 한 작업 설명입니다.
  • parameters는 OpenAPI 스키마 형식의 입력 인수 스키마입니다.

4단계: 에이전트 쿼리

지원되는 작업(예: query) 중 하나를 사용하여 에이전트를 쿼리하려면 다음 안내를 따르세요.

Python용 Vertex AI SDK

agent.query(input={"messages": [
    ("user", "What is the exchange rate from US dollars to Swedish currency?")
]})

요청

from google import auth as google_auth
from google.auth.transport import requests as google_requests
import requests

def get_identity_token():
    credentials, _ = google_auth.default()
    auth_request = google_requests.Request()
    credentials.refresh(auth_request)
    return credentials.token

requests.post(
    f"https://{LOCATION}-aiplatform.googleapis.com/v1/projects/{PROJECT_ID}/locations/{LOCATION}/reasoningEngines/{RESOURCE_ID}:query",
    headers={
        "Content-Type": "application/json; charset=utf-8",
        "Authorization": f"Bearer {get_identity_token()}",
    },
    data=json.dumps({"input": {
        "input": {"messages": [
            ("user", "What is the exchange rate from US dollars to Swedish currency?")
        ]},
    }})
)

REST

curl \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/reasoningEngines/RESOURCE_ID:query -d '{
  "input": {
    "input": {"messages": [
      ("user", "What is the exchange rate from US dollars to Swedish currency?")
    ]},
  }
}'

쿼리 응답은 로컬 애플리케이션 테스트 출력과 비슷한 문자열입니다.

{"input": "What is the exchange rate from US dollars to Swedish currency?",
 # ...
 "output": "For 1 US dollar you will get 10.7345 Swedish Krona."}

5단계: 에이전트 응답 스트리밍

해당하는 경우 작업 중 하나(예: stream_query)를 사용하여 에이전트의 응답을 스트리밍할 수 있습니다.

Python용 Vertex AI SDK

agent = agent_engines.get("projects/PROJECT_ID/locations/LOCATION/reasoningEngines/RESOURCE_ID")

agent.stream_query(input={"messages": [
    ("user", "What is the exchange rate from US dollars to Swedish currency?")
]})

요청

from google import auth as google_auth
from google.auth.transport import requests as google_requests
import requests

def get_identity_token():
    credentials, _ = google_auth.default()
    auth_request = google_requests.Request()
    credentials.refresh(auth_request)
    return credentials.token

requests.post(
    f"https://{LOCATION}-aiplatform.googleapis.com/v1/projects/{PROJECT_ID}/locations/{LOCATION}/reasoningEngines/{RESOURCE_ID}:streamQuery",
    headers={
        "Content-Type": "application/json",
        "Authorization": f"Bearer {get_identity_token()}",
    },
    data=json.dumps({"input": {
        "input": {"messages": [
            ("user", "What is the exchange rate from US dollars to Swedish currency?")
        ]},
    }}),
    stream=True,
)

REST

curl \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/reasoningEngines/RESOURCE_ID:streamQuery?alt=sse -d '{
  "input": {
    "input": {"messages": [
      ("user", "What is the exchange rate from US dollars to Swedish currency?")
    ]},
  }
}'

Vertex AI Agent Engine은 반복적으로 생성된 객체의 시퀀스로 응답을 스트리밍합니다. 예를 들어 세 개의 응답 세트는 다음과 같이 표시될 수 있습니다.

{'actions': [{'tool': 'get_exchange_rate', ...}]}  # first response
{'steps': [{'action': {'tool': 'get_exchange_rate', ...}}]}  # second response
{'output': 'The exchange rate is 11.0117 SEK per USD as of 2024-12-03.'}  # final response

다음 단계