[ReAct] agent 에 structured output 설정하기
create_react_agent의 response_format으로 출력 스키마를 지정해 ReAct 에이전트에서 구조화된 응답을 받는 방법을 정리한다.
개요
create_react_agent() 에 response_format 파라미터로 출력 스키마를 지정하면, ReAct 에이전트로부터 구조화된 응답을 받을 수 있다.
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')
Prompt 를 따라 대문자로 Return 해준다.
Reference
Google AdSense — Post Ad
이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.
Comments powered by Disqus.