LangChain 에이전트 사용

이 페이지에서는 상담사 사용에 관한 일반적인 안내 외에도 LangchainAgent에만 해당하는 기능을 설명합니다.

시작하기 전에

이 튜토리얼에서는 다음 안내를 읽고 따랐다고 가정합니다.

지원되는 작업

LangchainAgent에 지원되는 작업은 다음과 같습니다.

  • query: 쿼리에 대한 응답을 동기식으로 가져옵니다.
  • stream_query: 쿼리에 대한 응답을 스트리밍합니다.

query 메서드와 stream_query 메서드 모두 동일한 유형의 인수를 지원합니다.

  • input: 상담사에게 전송할 메시지입니다.
  • config: 쿼리 컨텍스트의 구성 (해당하는 경우)입니다.

에이전트 쿼리

명령어:

agent.query(input="What is the exchange rate from US dollars to Swedish currency?")

다음과 동일합니다 (전체 형식).

agent.query(input={
    "input": [ # The input is represented as a list of messages (each message as a dict)
        {
            # The role (e.g. "system", "user", "assistant", "tool")
            "role": "user",
            # The type (e.g. "text", "tool_use", "image_url", "media")
            "type": "text",
            # The rest of the message (this varies based on the type)
            "text": "What is the exchange rate from US dollars to Swedish currency?",
        },
    ]
})

역할은 모델이 응답할 때 다양한 유형의 메시지를 구분하는 데 사용됩니다. 입력에서 role를 생략하면 기본값은 "user"입니다.

역할 설명
system 채팅 모델에 동작 방식을 알리고 추가 컨텍스트를 제공하는 데 사용됩니다. 일부 채팅 모델 제공업체에서는 지원하지 않습니다.
user 일반적으로 텍스트 또는 기타 대화형 입력의 형태로 모델과 상호작용하는 사용자의 입력을 나타냅니다.
assistant 모델의 응답을 나타냅니다. 여기에는 텍스트 또는 도구 호출 요청이 포함될 수 있습니다.
tool 외부 데이터 또는 처리가 검색된 후 도구 호출 결과를 모델에 다시 전달하는 데 사용되는 메시지입니다.

메시지의 type에 따라 나머지 메시지가 해석되는 방식도 결정됩니다 (다중 모드 콘텐츠 처리 참고).

멀티모달 콘텐츠로 상담사 쿼리

다음 상담사 (입력을 모델에 전달하고 도구를 사용하지 않음)를 사용하여 멀티모달 입력을 상담사에게 전달하는 방법을 설명합니다.

agent = agent_engines.LangchainAgent(
    model="gemini-1.5-pro-preview-0409",
    runnable_builder=lambda model, **kwargs: model,
)

멀티모달 메시지는 type 및 해당 데이터를 지정하는 콘텐츠 블록을 통해 표현됩니다. 일반적으로 멀티모달 콘텐츠의 경우 type"media"로, file_uri를 Cloud Storage URI로, mime_type를 파일 해석용으로 지정합니다.

이미지

agent.query(input={"input": [
    {"type": "text", "text": "Describe the attached media in 5 words!"},
    {"type": "media", "mime_type": "image/jpeg", "file_uri": "gs://cloud-samples-data/generative-ai/image/cricket.jpeg"},
]})

동영상

agent.query(input={"input": [
    {"type": "text", "text": "Describe the attached media in 5 words!"},
    {"type": "media", "mime_type": "video/mp4", "file_uri": "gs://cloud-samples-data/generative-ai/video/pixel8.mp4"},
]})

오디오

agent.query(input={"input": [
    {"type": "text", "text": "Describe the attached media in 5 words!"},
    {"type": "media", "mime_type": "audio/mp3", "file_uri": "gs://cloud-samples-data/generative-ai/audio/pixel.mp3"},
]})

Gemini에서 지원하는 MIME 유형 목록은 다음 문서를 참고하세요.

실행 가능한 구성으로 에이전트 쿼리

에이전트를 쿼리할 때 에이전트의 config (RunnableConfig의 스키마를 따름)를 지정할 수도 있습니다. 일반적인 두 가지 시나리오는 다음과 같습니다.

  • 기본 구성 매개변수:
  • 맞춤 구성 매개변수 (configurable를 통해):

예를 들면 다음과 같습니다.

import uuid

run_id = uuid.uuid4()  # Generate an ID for tracking the run later.

response = agent.query(
    input="What is the exchange rate from US dollars to Swedish currency?",
    config={  # Specify the RunnableConfig here.
        "run_id": run_id                               # Optional.
        "tags": ["config-tag"],                        # Optional.
        "metadata": {"config-key": "config-value"},    # Optional.
        "configurable": {"session_id": "SESSION_ID"}   # Optional.
    },
)

print(response)

다음 단계