LangChain 에이전트 만들기

Agent Runtime을 사용하면 프레임워크별 LangChain 템플릿을 사용하여 에이전트를 개발하고 배포할 수 있습니다. Agent Platform SDKLangchainAgent 클래스를 사용하면 복잡한 작업을 실행하고 외부 도구와 통합되는 에이전트를 만들 수 있습니다.

이 문서에서는 모델 정의, 환율 검색과 같은 작업을 실행하는 도구 장착, 채팅 기록 관리 등 LangChain 에이전트를 개발하는 방법을 설명합니다.

배포된 에이전트 관리에 대한 자세한 내용은 배포된 에이전트 관리를 참조하세요.

시작하기 전에

환경 설정 단계에 따라 환경이 설정되었는지 확인합니다.

모델 정의 및 구성

사용할 모델을 지정합니다.

model = "gemini-3.5-flash"

(선택사항) 모델의 안전 설정을 구성합니다. Gemini에서 안전 설정에 사용할 수 있는 옵션에 대한 자세한 내용은 안전 속성 구성을 참조하세요. 다음은 안전 설정을 구성하는 방법의 예시입니다.

from langchain_google_vertexai import HarmBlockThreshold, HarmCategory

safety_settings = {
    HarmCategory.HARM_CATEGORY_UNSPECIFIED: HarmBlockThreshold.BLOCK_NONE,
    HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
    HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_ONLY_HIGH,
    HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
    HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_NONE,
}

(선택사항) 다음과 같이 모델 파라미터를 지정합니다.

model_kwargs = {
    # temperature (float): The sampling temperature controls the degree of
    # randomness in token selection.
    "temperature": 0.28,
    # max_output_tokens (int): The token limit determines the maximum amount of
    # text output from one prompt.
    "max_output_tokens": 1000,
    # top_p (float): Tokens are selected from most probable to least until
    # the sum of their probabilities equals the top-p value.
    "top_p": 0.95,
    # top_k (int): The next token is selected from among the top-k most
    # probable tokens. This is not supported by all model versions. See
    # https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/image-understanding#valid_parameter_values
    # for details.
    "top_k": None,
    # safety_settings (Dict[HarmCategory, HarmBlockThreshold]): The safety
    # settings to use for generating content.
    # (you must create your safety settings using the previous step first).
    "safety_settings": safety_settings,
}

모델 구성을 사용하여 LangchainAgent 를 만듭니다.

from vertexai import agent_engines

agent = agent_engines.LangchainAgent(
    model=model,                # Required.
    model_kwargs=model_kwargs,  # Optional.
)

터미널 또는 Colab 노트북과 같은 대화형 환경에서 실행 중이면 중간 테스트 단계로 쿼리를 실행할 수 있습니다.

response = agent.query(input="What is the exchange rate from US dollars to SEK today?")

print(response)

응답은 다음 예시와 유사한 Python 사전입니다.

{"input": "What is the exchange rate from US dollars to Swedish currency?",
 "output": """I cannot provide the live exchange rate from US dollars to Swedish currency (Swedish krona, SEK).

**Here's why:**

* **Exchange rates constantly fluctuate.** Factors like global economics, interest rates, and political events cause
  these changes throughout the day.
* **Providing inaccurate information would be misleading.**

**How to find the current exchange rate:**

1. **Use a reliable online converter:** Many websites specialize in live exchange rates. Some popular options include:
   * Google Finance (google.com/finance)
   * XE.com
   * Bank websites (like Bank of America, Chase, etc.)
2. **Contact your bank or financial institution:** They can give you the exact exchange rate they are using.

Remember to factor in any fees or commissions when exchanging currency.
"""}

(선택사항) 고급 맞춤설정

LangchainAgent 템플릿은 에서 사용 가능한 모든 파운데이션 모델에 대한 액세스를 제공하므로 기본적으로 ChatVertexAI를 사용합니다 Google Cloud. ChatVertexAI를 통해 사용할 수 없는 모델을 사용하려면 다음 서명이 있는 Python 함수와 함께 model_builder= 인수를 지정하면 됩니다.

from typing import Optional

def model_builder(
    *,
    model_name: str,                      # Required. The name of the model
    model_kwargs: Optional[dict] = None,  # Optional. The model keyword arguments.
    **kwargs,                             # Optional. The remaining keyword arguments to be ignored.
):

LangChain에서 지원되는 채팅 모델과 기능의 목록은 채팅 모델을 참조하세요. model=model_kwargs=에 지원되는 값 집합은 각 채팅 모델에 따라 다르므로 자세한 내용은 해당 문서를 참조하세요.

ChatVertexAI

기본적으로 설치되어 있습니다.

예를 들어 LangchainAgent 인수를 생략하면 model_builder 에서 사용됩니다.

from vertexai import agent_engines

agent = agent_engines.LangchainAgent(
    model=model,                # Required.
    model_kwargs=model_kwargs,  # Optional.
)

ChatAnthropic

먼저 문서에 따라 계정을 설정하고 패키지를 설치합니다.

다음으로 ChatAnthropic을 반환하는 model_builder를 정의합니다.

def model_builder(*, model_name: str, model_kwargs = None, **kwargs):
    from langchain_anthropic import ChatAnthropic
    return ChatAnthropic(model_name=model_name, **model_kwargs)

마지막으로 다음 코드를 사용하여 LangchainAgent에서 사용합니다.

from vertexai import agent_engines

agent = agent_engines.LangchainAgent(
    model="claude-3-opus-20240229",                       # Required.
    model_builder=model_builder,                          # Required.
    model_kwargs={
        "api_key": "ANTHROPIC_API_KEY",  # Required.
        "temperature": 0.28,                              # Optional.
        "max_tokens": 1000,                               # Optional.
    },
)

ChatOpenAI

ChatOpenAI를 Gemini의 ChatCompletions API와 함께 사용할 수 있습니다.

먼저 문서에 따라 패키지를 설치합니다.

다음으로 ChatOpenAI를 반환하는 model_builder를 정의합니다.

def model_builder(
    *,
    model_name: str,
    model_kwargs = None,
    project: str,   # Specified via vertexai.init
    location: str,  # Specified via vertexai.init
    **kwargs,
):
    import google.auth
    from langchain_openai import ChatOpenAI

    # Note: the credential lives for 1 hour by default.
    # After expiration, it must be refreshed.
    creds, _ = google.auth.default(scopes=["https://www.googleapis.com/auth/cloud-platform"])
    auth_req = google.auth.transport.requests.Request()
    creds.refresh(auth_req)

    if model_kwargs is None:
        model_kwargs = {}

    endpoint = f"https://{location}-aiplatform.googleapis.com"
    base_url = f'{endpoint}/v1beta1/projects/{project}/locations/{location}/endpoints/openapi'

    return ChatOpenAI(
        model=model_name,
        base_url=base_url,
        api_key=creds.token,
        **model_kwargs,
    )

마지막으로 다음 코드를 사용하여 LangchainAgent에서 사용합니다.

from vertexai import agent_engines

agent = agent_engines.LangchainAgent(
    model="google/gemini-3.5-flash",  # Or "meta/llama3-405b-instruct-maas"
    model_builder=model_builder,        # Required.
    model_kwargs={
        "temperature": 0,               # Optional.
        "max_retries": 2,               # Optional.
    },
)

도구 정의 및 사용

모델을 정의한 후 다음 단계는 추론을 위해 모델에 사용되는 도구를 정의하는 것입니다. 도구는 LangChain 도구 또는 Python 함수일 수 있습니다. 또한 정의된 Python 함수를 LangChain 도구로 변환할 수 있습니다.

함수를 정의할 때는 함수 매개변수, 함수 기능 및 함수 반환값을 완전히 명확하게 기술하는 설명을 포함하는 것이 중요합니다. 이 정보는 모델이 사용할 함수를 결정하기 위해 사용됩니다. 또한 함수를 로컬로 테스트하여 작동하는지 확인해야 합니다.

다음 코드를 사용하여 환율을 반환하는 함수를 정의합니다.

def get_exchange_rate(
    currency_from: str = "USD",
    currency_to: str = "EUR",
    currency_date: str = "latest",
):
    """Retrieves the exchange rate between two currencies on a specified date.

    Uses the Frankfurter API (https://api.frankfurter.app/) to obtain
    exchange rate data.

    Args:
        currency_from: The base currency (3-letter currency code).
            Defaults to "USD" (US Dollar).
        currency_to: The target currency (3-letter currency code).
            Defaults to "EUR" (Euro).
        currency_date: The date for which to retrieve the exchange rate.
            Defaults to "latest" for the most recent exchange rate data.
            Can be specified in YYYY-MM-DD format for historical rates.

    Returns:
        dict: A dictionary containing the exchange rate information.
            Example: {"amount": 1.0, "base": "USD", "date": "2023-11-24",
                "rates": {"EUR": 0.95534}}
    """
    import requests
    response = requests.get(
        f"https://api.frankfurter.app/{currency_date}",
        params={"from": currency_from, "to": currency_to},
    )
    return response.json()

에이전트에 사용하기 전에 함수를 테스트하기 위해 다음을 실행합니다.

get_exchange_rate(currency_from="USD", currency_to="SEK")

응답은 다음과 비슷하게 표시됩니다.

{'amount': 1.0, 'base': 'USD', 'date': '2024-02-22', 'rates': {'SEK': 10.3043}}

LangchainAgent 내에서 도구를 사용하려면 도구 목록에 tools= 인수 아래의 도구를 추가합니다.

from vertexai import agent_engines

agent = agent_engines.LangchainAgent(
    model=model,                # Required.
    tools=[get_exchange_rate],  # Optional.
    model_kwargs=model_kwargs,  # Optional.
)

테스트 쿼리를 수행하여 로컬에서 에이전트를 테스트할 수 있습니다. 다음 명령어를 실행하여 미국 달러와 스웨덴 크로나를 사용해 로컬에서 에이전트를 테스트합니다.

response = agent.query(
    input="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."}

(선택사항) 여러 도구

LangchainAgent 용 도구를 다른 방식으로 정의하고 인스턴스화할 수 있습니다.

그라운딩 도구

먼저 generate_models 패키지를 가져오고 도구를 만듭니다.

from vertexai.generative_models import grounding, Tool

grounded_search_tool = Tool.from_google_search_retrieval(
    grounding.GoogleSearchRetrieval()
)

다음으로 LangchainAgent 내에서 도구를 사용합니다.

from vertexai import agent_engines

agent = agent_engines.LangchainAgent(
    model=model,
    tools=[grounded_search_tool],
)
agent.query(input="When is the next total solar eclipse in US?")

응답은 다음과 유사한 사전입니다.

{"input": "When is the next total solar eclipse in US?",
 "output": """The next total solar eclipse in the U.S. will be on August 23, 2044.
 This eclipse will be visible from three states: Montana, North Dakota, and
 South Dakota. The path of totality will begin in Greenland, travel through
 Canada, and end around sunset in the United States."""}

자세한 내용은 그라운딩을 참조하세요.

LangChain 도구

먼저 도구를 정의하는 패키지를 설치합니다.

pip install langchain-google-community

다음으로 패키지를 가져오고 도구를 만듭니다.

from langchain_google_community import VertexAISearchRetriever
from langchain.tools.retriever import create_retriever_tool

retriever = VertexAISearchRetriever(
    project_id="PROJECT_ID",
    data_store_id="DATA_STORE_ID",
    location_id="DATA_STORE_LOCATION_ID",
    engine_data_type=1,
    max_documents=10,
)
movie_search_tool = create_retriever_tool(
    retriever=retriever,
    name="search_movies",
    description="Searches information about movies.",
)

마지막으로 LangchainAgent 내에서 도구를 사용합니다.

from vertexai import agent_engines

agent = agent_engines.LangchainAgent(
    model=model,
    tools=[movie_search_tool],
)
response = agent.query(
    input="List some sci-fi movies from the 1990s",
)

다음과 같은 응답이 반환되어야 합니다.

{"input": "List some sci-fi movies from the 1990s",
 "output": """Here are some sci-fi movies from the 1990s:
    * The Matrix (1999): A computer hacker learns from mysterious rebels about the true nature of his reality and his role in the war against its controllers.
    * Star Wars: Episode I - The Phantom Menace (1999): Two Jedi Knights escape a hostile blockade to find a queen and her protector, and come across a young boy [...]
    * Men in Black (1997): A police officer joins a secret organization that monitors extraterrestrial interactions on Earth.
    [...]
 """}

전체 예시를 보려면 노트북을 참조하세요.

LangChain에서 사용할 수 있는 도구의 추가 예시는 Google 도구를 참조하세요.

Agent Platform 확장 프로그램

먼저 확장 프로그램 패키지를 가져오고 도구를 만듭니다.

from typing import Optional

def generate_and_execute_code(
    query: str,
    files: Optional[list[str]] = None,
    file_gcs_uris: Optional[list[str]] = None,
) -> str:
    """Get the results of a natural language query by generating and executing
    a code snippet.

    Example queries: "Find the max in [1, 2, 5]" or "Plot average sales by
    year (from data.csv)". Only one of `file_gcs_uris` and `files` field
    should be provided.

    Args:
        query:
            The natural language query to generate and execute.
        file_gcs_uris:
            Optional. URIs of input files to use when executing the code
            snippet. For example, ["gs://input-bucket/data.csv"].
        files:
            Optional. Input files to use when executing the generated code.
            If specified, the file contents are expected be base64-encoded.
            For example: [{"name": "data.csv", "contents": "aXRlbTEsaXRlbTI="}].
    Returns:
        The results of the query.
    """
    operation_params = {"query": query}
    if files:
        operation_params["files"] = files
    if file_gcs_uris:
        operation_params["file_gcs_uris"] = file_gcs_uris

    from vertexai.preview import extensions

    # If you have an existing extension instance, you can get it here
    # i.e. code_interpreter = extensions.Extension(resource_name).
    code_interpreter = extensions.Extension.from_hub("code_interpreter")
    return extensions.Extension.from_hub("code_interpreter").execute(
        operation_id="generate_and_execute",
        operation_params=operation_params,
    )

다음으로 LangchainAgent 내에서 도구를 사용합니다.

from vertexai import agent_engines

agent = agent_engines.LangchainAgent(
    model=model,
    tools=[generate_and_execute_code],
)
agent.query(
    input="""Using the data below, construct a bar chart that includes only the height values with different colors for the bars:

    tree_heights_prices = {
      \"Pine\": {\"height\": 100, \"price\": 100},
      \"Oak\": {\"height\": 65, \"price\": 135},
      \"Birch\": {\"height\": 45, \"price\": 80},
      \"Redwood\": {\"height\": 200, \"price\": 200},
      \"Fir\": {\"height\": 180, \"price\": 162},
    }
    """
)

다음과 같은 응답이 반환되어야 합니다.

{"input": """Using the data below, construct a bar chart that includes only the height values with different colors for the bars:

 tree_heights_prices = {
    \"Pine\": {\"height\": 100, \"price\": 100},
    \"Oak\": {\"height\": 65, \"price\": 135},
    \"Birch\": {\"height\": 45, \"price\": 80},
    \"Redwood\": {\"height\": 200, \"price\": 200},
    \"Fir\": {\"height\": 180, \"price\": 162},
 }
 """,
 "output": """Here's the generated bar chart:
 ```python
 import matplotlib.pyplot as plt

 tree_heights_prices = {
    "Pine": {"height": 100, "price": 100},
    "Oak": {"height": 65, "price": 135},
    "Birch": {"height": 45, "price": 80},
    "Redwood": {"height": 200, "price": 200},
    "Fir": {"height": 180, "price": 162},
 }

 heights = [tree["height"] for tree in tree_heights_prices.values()]
 names = list(tree_heights_prices.keys())

 plt.bar(names, heights, color=['red', 'green', 'blue', 'purple', 'orange'])
 plt.xlabel('Tree Species')
 plt.ylabel('Height')
 plt.title('Tree Heights')
 plt.show()
 ```
 """}

배포된 에이전트가 코드 인터프리터 확장 프로그램에 액세스하려면 AI Platform Reasoning Engine 서비스 에이전트 서비스 계정에 Agent Platform 사용자 역할 (roles/aiplatform.user)을 추가해야 합니다. 자세한 내용은 액세스 관리를 참조하세요.

LangchainAgent에서 만든 도구의 전부 또는 일부를 사용할 수 있습니다.

from vertexai import agent_engines

agent = agent_engines.LangchainAgent(
    model=model,
    tools=[
        get_exchange_rate,         # Optional (Python function)
        grounded_search_tool,      # Optional (Grounding Tool)
        movie_search_tool,         # Optional (Langchain Tool)
        generate_and_execute_code, # Optional (Vertex Extension)
    ],
)

agent.query(input="When is the next total solar eclipse in US?")

(선택사항) 도구 구성

Gemini를 사용하면 도구 사용에 제약조건을 적용할 수 있습니다. 예를 들어 모델에서 자연어 응답을 생성하도록 허용하는 대신 함수 호출만 생성하도록 강제할 수 있습니다('강제 함수 호출').

from vertexai import agent_engines
from vertexai.preview.generative_models import ToolConfig

agent = agent_engines.LangchainAgent(
    model="gemini-3.5-flash",
    tools=[search_arxiv, get_exchange_rate],
    model_tool_kwargs={
        "tool_config": {  # Specify the tool configuration here.
            "function_calling_config": {
                "mode": ToolConfig.FunctionCallingConfig.Mode.ANY,
                "allowed_function_names": ["search_arxiv", "get_exchange_rate"],
            },
        },
    },
)

agent.query(
    input="Explain the Schrodinger equation in a few sentences",
)

자세한 내용은 도구 구성을 참조하세요.

채팅 기록 저장

채팅 메시지를 추적하고 데이터베이스에 추가하려면 get_session_history 함수를 정의하고 에이전트를 만들 때 전달합니다. 이 함수는 session_id를 사용하고 BaseChatMessageHistory 객체를 반환해야 합니다.

  • session_id는 이러한 입력 메시지가 속한 세션의 식별자입니다. 이를 통해 여러 대화를 동시에 유지할 수 있습니다.
  • BaseChatMessageHistory는 메시지 객체를 로드하고 저장할 수 있는 클래스의 인터페이스입니다.

데이터베이스 설정

LangChain에서 지원되는 Google의 ChatMessageHistory 제공업체 목록은 메모리를 참조하세요.

먼저 LangChain 문서에 따라 관련 패키지를 설치 및 사용하여 원하는 데이터베이스(예: Firestore, Bigtable, Spanner)를 설정합니다.

다음으로 get_session_history 함수를 다음과 같이 정의합니다.

Firestore(네이티브 모드)

def get_session_history(session_id: str):
    from langchain_google_firestore import FirestoreChatMessageHistory
    from google.cloud import firestore

    client = firestore.Client(project="PROJECT_ID")
    return FirestoreChatMessageHistory(
        client=client,
        session_id=session_id,
        collection="TABLE_NAME",
        encode_message=False,
    )

Bigtable

def get_session_history(session_id: str):
    from langchain_google_bigtable import BigtableChatMessageHistory

    return BigtableChatMessageHistory(
        instance_id="INSTANCE_ID",
        table_id="TABLE_NAME",
        session_id=session_id,
    )

Spanner

def get_session_history(session_id: str):
    from langchain_google_spanner import SpannerChatMessageHistory

    return SpannerChatMessageHistory(
        instance_id="INSTANCE_ID",
        database_id="DATABASE_ID",
        table_name="TABLE_NAME",
        session_id=session_id,
    )

마지막으로 에이전트를 만들어 chat_history로 전달합니다.

from vertexai import agent_engines

agent = agent_engines.LangchainAgent(
    model=model,
    chat_history=get_session_history,  # <- new
)

에이전트를 쿼리할 때는 에이전트에 이전 질문과 답변의 '메모리'가 있도록 session_id를 전달해야 합니다.

agent.query(
    input="What is the exchange rate from US dollars to Swedish currency?",
    config={"configurable": {"session_id": "SESSION_ID"}},
)

후속 쿼리에 세션 메모리가 보존되는지 확인할 수 있습니다.

response = agent.query(
    input="How much is 100 USD?",
    config={"configurable": {"session_id": "SESSION_ID"}},
)

print(response)

프롬프트 템플릿 맞춤설정

프롬프트 템플릿은 사용자 입력을 모델의 안내로 변환하는 데 도움이 되며 모델의 응답을 안내하는 데 사용되므로 컨텍스트를 이해하고 관련성 높고 일관된 언어 기반 출력을 생성할 수 있습니다. 자세한 내용은 ChatPromptTemplates를 참조하세요.

기본 프롬프트 템플릿은 섹션별로 순차 구성됩니다.

섹션 설명
선택사항: 시스템 요청 사항 에이전트가 모든 쿼리에 적용할 안내입니다.
선택사항: 채팅 기록 이전 세션의 채팅 기록에 해당하는 메시지입니다.
사용자 입력 에이전트가 응답하는 데 사용하는 사용자의 쿼리입니다.
에이전트 스크래치패드 에이전트에서 도구를 사용하고 추론을 수행하여 사용자에게 응답할 때 생성하는 메시지 (예: 함수 호출)입니다.

기본 프롬프트 템플릿은 자체 프롬프트 템플릿을 지정하지 않고 에이전트를 만드는 경우에 생성되며 다음과 같이 전체가 표시됩니다.

from langchain_core.prompts import ChatPromptTemplate
from langchain.agents.format_scratchpad.tools import format_to_tool_messages

prompt_template = {
    "user_input": lambda x: x["input"],
    "history": lambda x: x["history"],
    "agent_scratchpad": lambda x: format_to_tool_messages(x["intermediate_steps"]),
} | ChatPromptTemplate.from_messages([
    ("system", "{system_instruction}"),
    ("placeholder", "{history}"),
    ("user", "{user_input}"),
    ("placeholder", "{agent_scratchpad}"),
])

다음 예시에서는 에이전트를 인스턴스화할 때 암시적으로 전체 프롬프트 템플릿을 사용합니다.

from vertexai import agent_engines

system_instruction = "I help look up the rate between currencies"

agent = agent_engines.LangchainAgent(
    model=model,
    system_instruction=system_instruction,
    chat_history=get_session_history,
    tools=[get_exchange_rate],
)

기본 프롬프트 템플릿을 자체 프롬프트 템플릿으로 재정의하고 에이전트를 구성할 때 사용할 수 있습니다. 예를 들면 다음과 같습니다.


from vertexai import agent_engines

custom_prompt_template = {
    "user_input": lambda x: x["input"],
    "history": lambda x: x["history"],
    "agent_scratchpad": lambda x: format_to_tool_messages(x["intermediate_steps"]),
} | ChatPromptTemplate.from_messages([
    ("placeholder", "{history}"),
    ("user", "{user_input}"),
    ("placeholder", "{agent_scratchpad}"),
])

agent = agent_engines.LangchainAgent(
    model=model,
    prompt=custom_prompt_template,
    chat_history=get_session_history,
    tools=[get_exchange_rate],
)

agent.query(
    input="What is the exchange rate from US dollars to Swedish currency?",
    config={"configurable": {"session_id": "SESSION_ID"}},
)

조정 맞춤설정

모든 LangChain 구성요소는 조정의 입력 및 출력 스키마를 제공하는 Runnable interface, 를 구현합니다. LangchainAgent 클래스가 쿼리에 응답하면 실행 파일을 빌드해야 합니다. 기본적으로, LangchainAgent도구로 모델을 바인딩하여 이러한 실행 파일을 빌드하고 채팅 기록이 사용 설정된 경우 AgentExecutor 로 래핑된 RunnableWithMessageHistory 를 사용합니다.

(i) 개방형 추론을 수행하는 대신 결정론적 단계 집합을 수행하는 에이전트를 구현하거나 (ii) ReAct와 같은 방식으로 에이전트에게 해당 단계를 수행한 이유에 대한 사고로 각 단계에 주석으로 추가하도록 프롬프트하려는 경우에 조정을 맞춤설정하는 것이 좋습니다. 이렇게 하려면 다음 서명의 Python 함수로 runnable_builder= 인수를 지정하여 LangchainAgent를 만들 때 기본 실행 파일을 재정의해야 합니다.

from typing import Optional
from langchain_core.language_models import BaseLanguageModel

def runnable_builder(
    model: BaseLanguageModel,
    *,
    system_instruction: Optional[str] = None,
    prompt: Optional["RunnableSerializable"] = None,
    tools: Optional[Sequence["_ToolLike"]] = None,
    chat_history: Optional["GetSessionHistoryCallable"] = None,
    model_tool_kwargs: Optional[Mapping[str, Any]] = None,
    agent_executor_kwargs: Optional[Mapping[str, Any]] = None,
    runnable_kwargs: Optional[Mapping[str, Any]] = None,
    **kwargs,
):

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

  • modelmodel_builder에서 반환되는 채팅 모델에 해당합니다(모델 정의 및 구성 참조).
  • toolsmodel_tool_kwargs는 사용할 도구와 구성에 해당합니다(도구 정의 및 사용 참조).
  • chat_history: 채팅 메시지를 저장하는 데이터베이스에 해당합니다(채팅 기록 저장 참조).
  • system_instructionprompt는 프롬프트 구성에 해당합니다(프롬프트 템플릿 맞춤설정 참조).
  • agent_executor_kwargsrunnable_kwargs는 빌드할 실행 파일을 맞춤설정하는 데 사용할 수 있는 키워드 인수입니다.

이를 통해 조정 로직을 맞춤설정할 수 있는 다양한 옵션이 제공됩니다.

ChatModel

가장 간단한 경우 조정 없이 에이전트를 만들려면 runnable_builderLangchainAgent에 대해 재정의하여 model 를 직접 반환하면 됩니다.

from vertexai import agent_engines
from langchain_core.language_models import BaseLanguageModel

def llm_builder(model: BaseLanguageModel, **kwargs):
    return model

agent = agent_engines.LangchainAgent(
    model=model,
    runnable_builder=llm_builder,
)

ReAct

자체 ReAct 에이전트를 사용하여 기본 도구 호출 동작을 재정의하려면 자체 prompt (프롬프트 템플릿 맞춤설정 참조)를 기반으로 LangchainAgentrunnable_builder를 재정의해야 합니다.

from typing import Sequence
from langchain_core.language_models import BaseLanguageModel
from langchain_core.prompts import BasePromptTemplate
from langchain_core.tools import BaseTool
from langchain import hub

from vertexai import agent_engines

def react_builder(
    model: BaseLanguageModel,
    *,
    tools: Sequence[BaseTool],
    prompt: BasePromptTemplate,
    agent_executor_kwargs = None,
    **kwargs,
):
    from langchain.agents.react.agent import create_react_agent
    from langchain.agents import AgentExecutor

    agent = create_react_agent(model, tools, prompt)
    return AgentExecutor(agent=agent, tools=tools, **agent_executor_kwargs)

agent = agent_engines.LangchainAgent(
    model=model,
    tools=[get_exchange_rate],
    prompt=hub.pull("hwchase17/react"),
    agent_executor_kwargs={"verbose": True}, # Optional. For illustration.
    runnable_builder=react_builder,
)

LCEL 구문

LangChain Expression Language(LCEL)를 사용하여 다음 그래프를 구성하려면

   Input
   /   \
 Pros  Cons
   \   /
  Summary

LangchainAgentrunnable_builder를 재정의해야 합니다.

from vertexai import agent_engines

def lcel_builder(*, model, **kwargs):
    from operator import itemgetter
    from langchain_core.prompts import ChatPromptTemplate
    from langchain_core.runnables import RunnablePassthrough
    from langchain_core.output_parsers import StrOutputParser

    output_parser = StrOutputParser()

    planner = ChatPromptTemplate.from_template(
        "Generate an argument about: {input}"
    ) | model | output_parser | {"argument": RunnablePassthrough()}

    pros = ChatPromptTemplate.from_template(
        "List the positive aspects of {argument}"
    ) | model | output_parser

    cons = ChatPromptTemplate.from_template(
        "List the negative aspects of {argument}"
    ) | model | output_parser

    final_responder = ChatPromptTemplate.from_template(
        "Argument:{argument}\nPros:\n{pros}\n\nCons:\n{cons}\n"
        "Generate a final response given the critique",
    ) | model | output_parser

    return planner | {
        "pros": pros,
        "cons": cons,
        "argument": itemgetter("argument"),
    } | final_responder

agent = agent_engines.LangchainAgent(
    model=model,
    runnable_builder=lcel_builder,
)

LangGraph

LangGraph를 사용하여 다음 그래프를 구성하려면

   Input
   /   \
 Pros  Cons
   \   /
  Summary

LangchainAgentrunnable_builder를 재정의해야 합니다.

from vertexai import agent_engines

def langgraph_builder(*, model, **kwargs):
    from langchain_core.prompts import ChatPromptTemplate
    from langchain_core.output_parsers import StrOutputParser
    from langgraph.graph import END, MessageGraph

    output_parser = StrOutputParser()

    planner = ChatPromptTemplate.from_template(
        "Generate an argument about: {input}"
    ) | model | output_parser

    pros = ChatPromptTemplate.from_template(
        "List the positive aspects of {input}"
    ) | model | output_parser

    cons = ChatPromptTemplate.from_template(
        "List the negative aspects of {input}"
    ) | model | output_parser

    summary = ChatPromptTemplate.from_template(
        "Input:{input}\nGenerate a final response given the critique",
    ) | model | output_parser

    builder = MessageGraph()
    builder.add_node("planner", planner)
    builder.add_node("pros", pros)
    builder.add_node("cons", cons)
    builder.add_node("summary", summary)

    builder.add_edge("planner", "pros")
    builder.add_edge("planner", "cons")
    builder.add_edge("pros", "summary")
    builder.add_edge("cons", "summary")
    builder.add_edge("summary", END)
    builder.set_entry_point("planner")
    return builder.compile()

agent = agent_engines.LangchainAgent(
    model=model,
    runnable_builder=langgraph_builder,
)

# Example query
agent.query(input={"role": "user", "content": "scrum methodology"})

다음 단계

가이드

개발 요구사항에 따라 Agent Platform Runtime에 에이전트를 배포하는 다섯 가지 방법을 알아보세요.

가이드

Agent Platform Runtime에서 LangChain 에이전트를 사용합니다.

가이드

기본 에이전트를 만들고 배포한 후 Gen AI Evaluation Service를 사용하여 에이전트를 평가합니다.

문제 해결

맞춤 에이전트를 만들 때 발생하는 일반적인 오류를 해결하는 방법을 알아보세요.

리소스

Google Agent Platform의 리소스와 지원을 찾아보세요.