포스트

[React] agent 에 structured output 설정하기

여기에 입력하면 된다.


개요

create_react_agent()response_format 파라미터로 출력 스키마를 지정하면, ReAct 에이전트로부터 구조화된 응답을 받을 수 있다.

  • pre-built 된 ReAct agent 는 ReAct 루프 종료 시, LLM을 한; 번 더 호출하여 구조화된 결과를 생성한다.

    Tools 정의

1
2
3
4
5
6
7
8
9
10
11
12
13
from langchain_core.tools import tool

@tool
def get_weather(location:str) -> str:
    """Use this to get weather information"""
    if any([city in location.lower() for city in ['nyc','new york city']]):
        return "It might be cloudy in nyc"
    elif any([city in location.lower() for city in ['sf', 'san francisco']]):
        return "It's always sunny in sf"
    else:
        return f"I am not sure what the weather is in {location}"

tools = [get_weather]

구조화된 출력 스키마 정의

1
2
3
4
5
from pydantic import BaseModel, Field

class WeatherResponse(BaseModel):
	"""Response This Format"""
	conditions: str = Field(description="Weather Conditions")

질문

1
2
3
inputs = {"messages": [("user", "What's the weather in NYC?")]}
response = graph.invoke(inputs)
response['structured_response']
1
WeatherResponse(conditions='cloudy')

Prompt 와 함께 사용하기

1
2
3
4
5
6
7
8
graph = create_react_agent(
	model,
	tools=tools,
	response_format=("Always return capitalized weather conditions", WeatherResponse)
)

inputs = {"messages": [("user", "What's the weather in NYC?")]}
response = graph.invoke(inputs)
1
WeatherResponse(conditions='CLOUDY')

Reference

Google AdSense — Post Ad
이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.

Comments powered by Disqus.