이 페이지에서는 에이전트 사용을 위한 일반 안내 외에도 AG2Agent
와 관련된 기능을 설명합니다.
시작하기 전에
이 튜토리얼에서는 사용자가 다음 안내를 읽고 따랐다고 가정합니다.
- AG2 에이전트 개발:
agent
를AG2Agent
의 인스턴스로 개발합니다. - 사용자 인증: 에이전트 쿼리를 위해 사용자로 인증을 수행합니다.
- SDK 가져오기 및 초기화: 필요한 경우 배포된 인스턴스를 가져올 수 있도록 클라이언트를 초기화합니다.
에이전트 인스턴스 가져오기
AG2Agent
를 쿼리하려면 먼저 새 인스턴스를 만들거나 기존 인스턴스를 가져와야 합니다.
특정 리소스 ID에 해당하는 AG2Agent
를 가져오려면 다음 안내를 따르세요.
Python용 Vertex AI SDK
다음 코드를 실행합니다.
import vertexai
client = vertexai.Client( # For service interactions via client.agent_engines
project="PROJECT_ID",
location="LOCATION",
)
agent = client.agent_engines.get(name="projects/PROJECT_ID/locations/LOCATION/reasoningEngines/RESOURCE_ID")
print(agent)
각 항목의 의미는 다음과 같습니다.
PROJECT_ID
는 에이전트를 개발하고 배포하는 데 사용되는 Google Cloud 프로젝트 ID입니다.LOCATION
: 지원되는 리전 중 하나입니다.RESOURCE_ID
는 배포된 에이전트의 ID이며reasoningEngine
리소스로 등록되어 있습니다.
Python 요청 라이브러리
다음 코드를 실행합니다.
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 API
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
Python용 Vertex AI SDK를 사용하는 경우 agent
객체는 다음을 포함하는 AgentEngine
클래스에 해당합니다.
- 배포된 에이전트에 관한 정보가 포함된
agent.api_resource
agent.operation_schemas()
를 호출하여 에이전트가 지원하는 작업 목록을 반환할 수도 있습니다. 자세한 내용은 지원되는 작업을 참고하세요. - 동기 서비스 상호작용을 허용하는
agent.api_client
- 비동기 서비스 상호작용을 허용하는
agent.async_api_client
이 섹션의 나머지 부분에서는 agent
라는 이름의 AgentEngine
인스턴스가 있다고 가정합니다.
지원되는 작업
AG2Agent
에 지원되는 작업은 다음과 같습니다.
query
: 쿼리에 대한 응답을 동기식으로 가져옵니다.
query
메서드는 다음 인수를 지원합니다.
input
: 에이전트에게 전송할 메시지입니다.max_turns
: 허용되는 최대 대화 턴 수입니다. 도구를 사용할 때는 최소max_turns=2
가 필요합니다. 도구 인수를 생성하는 데 한 번, 도구를 실행하는 데 한 번입니다.
에이전트 쿼리
query()
메서드는 에이전트와 상호작용하는 간소화된 방법을 제공합니다. 일반적인 호출은 다음과 같습니다.
response = agent.query(input="What is the exchange rate from US dollars to Swedish currency?", max_turns=2)
이 메서드는 에이전트와의 기본 통신을 처리하고 에이전트의 최종 응답을 사전으로 반환합니다. 다음과 동일합니다(전체 형식).
from autogen import ConversableAgent
import dataclasses
import json
input_message: str = "What is the exchange rate from US dollars to Swedish currency?"
max_turns: int = 2
with agent._runnable._create_or_get_executor(
tools=agent._ag2_tool_objects, # Use the agent's existing tools
agent_name="user", # Default
agent_human_input_mode="NEVER", # query() enforces this
) as executor:
chat_result = executor.initiate_chat(
agent._runnable,
message=input_message,
max_turns=max_turns,
clear_history=False, # Default
summary_method="last_msg" # Default
)
response = json.loads(
json.dumps(dataclasses.asdict(chat_result)) # query() does this conversion
)
query()
에 추가 키워드 인수를 전달하여 input
및 max_turns
외에 에이전트의 동작을 맞춤설정할 수 있습니다.
response = agent.query(
input="What is the exchange rate from US dollars to Swedish currency?",
max_turns=2,
msg_to="user" # Start the conversation with the "user" agent
)
print(response)
사용 가능한 파라미터의 전체 목록은 ConversableAgent.run
문서를 참조하세요. 하지만 user_input
은 항상 AG2Agent
템플릿에 의해 False
로 재정의됩니다.