langgraph / / 2024. 12. 3. 07:50

[langgraph] AutoGen, CrewAI 외 다른 프레임워크와 LangGraph 통합하는 방법

LangGraph 공식문서를 번역한 내용입니다. 필요한 경우 부연 설명을 추가하였고 이해하기 쉽게 예제를 일부 변경하였습니다. 문제가 되면 삭제하겠습니다.

https://langchain-ai.github.io/langgraph/how-tos/autogen-integration/

LangGraph는 에이전트 기반 및 다중 에이전트 애플리케이션을 구축하기 위한 프레임워크이다. 여기에는 다른 에이전트 프레임워크와의 통합도 포함된다.

이 가이드는 LangGraph를 다른 프레임워크와 통합하는 방법을 보여준다. 여기서 통합하는 프레임워크는 AutoGen이지만, 다른 프레임워크와도 쉽게 통합할 수 있다.

전체적인 흐름은 다른 에이전트를 LangGraph 노드 안에 래핑하는 방식이다. LangGraph의 노드는 임의의 코드일 수 있다. 이를 통해 AutoGen(또는 CrewAI, LlamaIndex, 또는 다른 프레임워크) 에이전트를 정의하고 이를 그래프 내에서 참조할 수 있다. 이 방법을 사용하면 일부 하위 에이전트가 다른 프레임워크에서 정의된 다중 에이전트 시스템을 만들 수 있다.

준비

우선, 필요한 패키지를 설치하자.

pip install autogen bs4 langgraph langchain-openai langchain-community

AutoGen 에이전트 정의

여기서 AutoGen 에이전트 정의한다. (https://github.com/microsoft/autogen/blob/0.2/notebook/agentchat_web_info.ipynb)

import autogen
import os

config_list = [{"model": "gpt-4o", "api_key": os.environ["OPENAI_API_KEY"]}]

llm_config = {
    "timeout": 600,
    "cache_seed": 42,
    "config_list": config_list,
    "temperature": 0,
}

autogen_agent = autogen.AssistantAgent(
    name="assistant",
    llm_config=llm_config,
)

user_proxy = autogen.UserProxyAgent(
    name="user_proxy",
    human_input_mode="NEVER",
    max_consecutive_auto_reply=10,
    is_termination_msg=lambda x: x.get("content", "").rstrip().endswith("TERMINATE"),
    code_execution_config={
        "work_dir": "web",
        "use_docker": False,
    },  # Please set use_docker=True if docker is available to run the generated code. Using docker is safer than running the generated code directly.
    llm_config=llm_config,
    system_message="Reply TERMINATE if the task has been solved at full satisfaction. Otherwise, reply CONTINUE, or the reason why the task is not solved yet.",
)

LangGraph 에이전트 정의

이제 LangGraph 에이전트를 정의한다. 웹 검색 도구를 사용하는 간단한 ReAct 스타일의 에이전트를 생성할 것이다.

from langgraph.graph import StateGraph, START, MessagesState
from langgraph.prebuilt import ToolNode, create_react_agent
from langchain_community.tools.tavily_search import TavilySearchResults
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage, AIMessage

model = ChatOpenAI(model="gpt-4o")
tools = [TavilySearchResults(max_results=1)]
web_search_agent = create_react_agent(
    model, tools, state_modifier="You are an agent specializing in web search"
)

멀티 에이전트 그래프 생성

이제 AutoGen 에이전트와 LangGraph 에이전트를 결합한 멀티 에이전트 시스템을 생성할 것이다. 이를 위해 사용자 쿼리를 적절한 에이전트로 라우팅하고 에이전트를 실행하는 그래프를 생성할 수 있다.

from typing import Literal, TypedDict


class Route(TypedDict):
    """Decide where to go next"""

    goto: Literal["web_search_assistant", "coding_assistant"]


def route(state: MessagesState) -> Literal["web_search_assistant", "coding_assistant"]:
    messages = [
        {
            "role": "system",
            "content": "Based on the conversation so far, decide who to call next: web search assistant or coding assistant.",
        }
    ] + state["messages"]
    response = model.with_structured_output(Route).invoke(messages)
    return response["goto"]


def call_autogen_agent(state: MessagesState):
    last_message = state["messages"][-1]
    response = user_proxy.initiate_chat(autogen_agent, message=last_message.content)
    # get the final response from the agent
    content = response.chat_history[-1]["content"]
    return {"messages": AIMessage(content=content)}


builder = StateGraph(MessagesState)
builder.add_conditional_edges(START, route)
builder.add_node("coding_assistant", call_autogen_agent)
builder.add_node("web_search_assistant", web_search_agent)
graph = builder.compile()
from IPython.display import display, Image

display(Image(graph.get_graph().draw_mermaid_png()))

그래프 실행

이제 그래프를 실행할 수 있다. 아래 예시에서 어떻게 먼저 적절한 에이전트로 라우팅한 후, 서브 에이전트로 응답하는지 확인할 수 있다.

for chunk in graph.stream(
    {
        "messages": [
            {
                "role": "user",
                "content": "Find numbers between 10 and 30 in fibonacci sequence",
            }
        ]
    }
):
    print(chunk)
[33muser_proxy[0m (to assistant):

Find numbers between 10 and 30 in fibonacci sequence

--------------------------------------------------------------------------------
[33massistant[0m (to user_proxy):

To find numbers between 10 and 30 in the Fibonacci sequence, we can generate the Fibonacci sequence and check which numbers fall within this range. Here's a plan:

1. Generate Fibonacci numbers starting from 0.
2. Continue generating until the numbers exceed 30.
3. Collect and print the numbers that are between 10 and 30.

Let's implement this in Python:

\`\`\`python
# filename: fibonacci_range.py

def fibonacci_sequence():
    a, b = 0, 1
    while a <= 30:
        if 10 <= a <= 30:
            print(a)
        a, b = b, a + b

fibonacci_sequence()
\`\`\`

Save this code in a file named `fibonacci_range.py` and execute it. It will print the Fibonacci numbers between 10 and 30. TERMINATE

--------------------------------------------------------------------------------
{'coding_assistant': {'messages': AIMessage(content="To find numbers between 10 and 30 in the Fibonacci sequence, we can generate the Fibonacci sequence and check which numbers fall within this range. Here's a plan:\n\n1. Generate Fibonacci numbers starting from 0.\n2. Continue generating until the numbers exceed 30.\n3. Collect and print the numbers that are between 10 and 30.\n\nLet's implement this in Python:\n\n\`\`\`python\n# filename: fibonacci_range.py\n\ndef fibonacci_sequence():\n    a, b = 0, 1\n    while a <= 30:\n        if 10 <= a <= 30:\n            print(a)\n        a, b = b, a + b\n\nfibonacci_sequence()\n\`\`\`\n\nSave this code in a file named `fibonacci_range.py` and execute it. It will print the Fibonacci numbers between 10 and 30. TERMINATE", additional_kwargs={}, response_metadata={}, id='e95a8aa1-5aa8-4ff2-ba74-2b2993ea0a5a')}}
for chunk in graph.stream(
    {"messages": [{"role": "user", "content": "what's the weather in nyc?"}]},
    subgraphs=True,
):
    print(chunk)
(('web_search_assistant:d08ae326-b6b2-1749-e8ea-4d308f22d819',), {'agent': {'messages': [AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_wZ5w5uO733Cc4CvWbc4Axq5F', 'function': {'arguments': '{"query":"current weather in New York City"}', 'name': 'tavily_search_results_json'}, 'type': 'function'}], 'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 23, 'prompt_tokens': 96, 'total_tokens': 119, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}}, 'model_name': 'gpt-4o-2024-08-06', 'system_fingerprint': 'fp_45cf54deae', 'finish_reason': 'tool_calls', 'logprobs': None}, id='run-0658af11-b90b-407c-a6a5-6a3b4a9f61e0-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': 'current weather in New York City'}, 'id': 'call_wZ5w5uO733Cc4CvWbc4Axq5F', 'type': 'tool_call'}], usage_metadata={'input_tokens': 96, 'output_tokens': 23, 'total_tokens': 119, 'input_token_details': {'audio': 0, 'cache_read': 0}, 'output_token_details': {'audio': 0, 'reasoning': 0}})]}})
(('web_search_assistant:d08ae326-b6b2-1749-e8ea-4d308f22d819',), {'tools': {'messages': [ToolMessage(content='[{"url": "https://www.weatherapi.com/", "content": "{\'location\': {\'name\': \'New York\', \'region\': \'New York\', \'country\': \'United States of America\', \'lat\': 40.714, \'lon\': -74.006, \'tz_id\': \'America/New_York\', \'localtime_epoch\': 1732021037, \'localtime\': \'2024-11-19 07:57\'}, \'current\': {\'last_updated_epoch\': 1732020300, \'last_updated\': \'2024-11-19 07:45\', \'temp_c\': 8.3, \'temp_f\': 46.9, \'is_day\': 1, \'condition\': {\'text\': \'Sunny\', \'icon\': \'//cdn.weatherapi.com/weather/64x64/day/113.png\', \'code\': 1000}, \'wind_mph\': 7.2, \'wind_kph\': 11.5, \'wind_degree\': 332, \'wind_dir\': \'NNW\', \'pressure_mb\': 1016.0, \'pressure_in\': 29.99, \'precip_mm\': 0.0, \'precip_in\': 0.0, \'humidity\': 60, \'cloud\': 0, \'feelslike_c\': 6.3, \'feelslike_f\': 43.4, \'windchill_c\': 4.3, \'windchill_f\': 39.8, \'heatindex_c\': 7.0, \'heatindex_f\': 44.5, \'dewpoint_c\': 2.7, \'dewpoint_f\': 36.8, \'vis_km\': 16.0, \'vis_miles\': 9.0, \'uv\': 0.0, \'gust_mph\': 10.0, \'gust_kph\': 16.2}}"}]', name='tavily_search_results_json', id='e955ebe9-631f-4dd1-b0a4-ee6a3caeba9f', tool_call_id='call_wZ5w5uO733Cc4CvWbc4Axq5F', artifact={'query': 'current weather in New York City', 'follow_up_questions': None, 'answer': None, 'images': [], 'results': [{'title': 'Weather in New York City', 'url': 'https://www.weatherapi.com/', 'content': "{'location': {'name': 'New York', 'region': 'New York', 'country': 'United States of America', 'lat': 40.714, 'lon': -74.006, 'tz_id': 'America/New_York', 'localtime_epoch': 1732021037, 'localtime': '2024-11-19 07:57'}, 'current': {'last_updated_epoch': 1732020300, 'last_updated': '2024-11-19 07:45', 'temp_c': 8.3, 'temp_f': 46.9, 'is_day': 1, 'condition': {'text': 'Sunny', 'icon': '//cdn.weatherapi.com/weather/64x64/day/113.png', 'code': 1000}, 'wind_mph': 7.2, 'wind_kph': 11.5, 'wind_degree': 332, 'wind_dir': 'NNW', 'pressure_mb': 1016.0, 'pressure_in': 29.99, 'precip_mm': 0.0, 'precip_in': 0.0, 'humidity': 60, 'cloud': 0, 'feelslike_c': 6.3, 'feelslike_f': 43.4, 'windchill_c': 4.3, 'windchill_f': 39.8, 'heatindex_c': 7.0, 'heatindex_f': 44.5, 'dewpoint_c': 2.7, 'dewpoint_f': 36.8, 'vis_km': 16.0, 'vis_miles': 9.0, 'uv': 0.0, 'gust_mph': 10.0, 'gust_kph': 16.2}}", 'score': 0.9997275, 'raw_content': None}], 'response_time': 3.24})]}})
(('web_search_assistant:d08ae326-b6b2-1749-e8ea-4d308f22d819',), {'agent': {'messages': [AIMessage(content='The current weather in New York City is sunny with a temperature of 8.3°C (46.9°F). The wind is coming from the north-northwest at 7.2 mph (11.5 kph), and the humidity level is 60%. The weather feels slightly cooler at 6.3°C (43.4°F) due to the wind chill.', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 79, 'prompt_tokens': 535, 'total_tokens': 614, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}}, 'model_name': 'gpt-4o-2024-08-06', 'system_fingerprint': 'fp_159d8341cc', 'finish_reason': 'stop', 'logprobs': None}, id='run-43d275f1-aacb-44f4-bdd0-8233c3765699-0', usage_metadata={'input_tokens': 535, 'output_tokens': 79, 'total_tokens': 614, 'input_token_details': {'audio': 0, 'cache_read': 0}, 'output_token_details': {'audio': 0, 'reasoning': 0}})]}})
((), {'web_search_assistant': {'messages': [HumanMessage(content="what's the weather in nyc?", additional_kwargs={}, response_metadata={}, id='756466d3-18ce-4b8e-b4fc-ee59932ce9f4'), AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_wZ5w5uO733Cc4CvWbc4Axq5F', 'function': {'arguments': '{"query":"current weather in New York City"}', 'name': 'tavily_search_results_json'}, 'type': 'function'}], 'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 23, 'prompt_tokens': 96, 'total_tokens': 119, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}}, 'model_name': 'gpt-4o-2024-08-06', 'system_fingerprint': 'fp_45cf54deae', 'finish_reason': 'tool_calls', 'logprobs': None}, id='run-0658af11-b90b-407c-a6a5-6a3b4a9f61e0-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': 'current weather in New York City'}, 'id': 'call_wZ5w5uO733Cc4CvWbc4Axq5F', 'type': 'tool_call'}], usage_metadata={'input_tokens': 96, 'output_tokens': 23, 'total_tokens': 119, 'input_token_details': {'audio': 0, 'cache_read': 0}, 'output_token_details': {'audio': 0, 'reasoning': 0}}), ToolMessage(content='[{"url": "https://www.weatherapi.com/", "content": "{\'location\': {\'name\': \'New York\', \'region\': \'New York\', \'country\': \'United States of America\', \'lat\': 40.714, \'lon\': -74.006, \'tz_id\': \'America/New_York\', \'localtime_epoch\': 1732021037, \'localtime\': \'2024-11-19 07:57\'}, \'current\': {\'last_updated_epoch\': 1732020300, \'last_updated\': \'2024-11-19 07:45\', \'temp_c\': 8.3, \'temp_f\': 46.9, \'is_day\': 1, \'condition\': {\'text\': \'Sunny\', \'icon\': \'//cdn.weatherapi.com/weather/64x64/day/113.png\', \'code\': 1000}, \'wind_mph\': 7.2, \'wind_kph\': 11.5, \'wind_degree\': 332, \'wind_dir\': \'NNW\', \'pressure_mb\': 1016.0, \'pressure_in\': 29.99, \'precip_mm\': 0.0, \'precip_in\': 0.0, \'humidity\': 60, \'cloud\': 0, \'feelslike_c\': 6.3, \'feelslike_f\': 43.4, \'windchill_c\': 4.3, \'windchill_f\': 39.8, \'heatindex_c\': 7.0, \'heatindex_f\': 44.5, \'dewpoint_c\': 2.7, \'dewpoint_f\': 36.8, \'vis_km\': 16.0, \'vis_miles\': 9.0, \'uv\': 0.0, \'gust_mph\': 10.0, \'gust_kph\': 16.2}}"}]', name='tavily_search_results_json', id='e955ebe9-631f-4dd1-b0a4-ee6a3caeba9f', tool_call_id='call_wZ5w5uO733Cc4CvWbc4Axq5F', artifact={'query': 'current weather in New York City', 'follow_up_questions': None, 'answer': None, 'images': [], 'results': [{'title': 'Weather in New York City', 'url': 'https://www.weatherapi.com/', 'content': "{'location': {'name': 'New York', 'region': 'New York', 'country': 'United States of America', 'lat': 40.714, 'lon': -74.006, 'tz_id': 'America/New_York', 'localtime_epoch': 1732021037, 'localtime': '2024-11-19 07:57'}, 'current': {'last_updated_epoch': 1732020300, 'last_updated': '2024-11-19 07:45', 'temp_c': 8.3, 'temp_f': 46.9, 'is_day': 1, 'condition': {'text': 'Sunny', 'icon': '//cdn.weatherapi.com/weather/64x64/day/113.png', 'code': 1000}, 'wind_mph': 7.2, 'wind_kph': 11.5, 'wind_degree': 332, 'wind_dir': 'NNW', 'pressure_mb': 1016.0, 'pressure_in': 29.99, 'precip_mm': 0.0, 'precip_in': 0.0, 'humidity': 60, 'cloud': 0, 'feelslike_c': 6.3, 'feelslike_f': 43.4, 'windchill_c': 4.3, 'windchill_f': 39.8, 'heatindex_c': 7.0, 'heatindex_f': 44.5, 'dewpoint_c': 2.7, 'dewpoint_f': 36.8, 'vis_km': 16.0, 'vis_miles': 9.0, 'uv': 0.0, 'gust_mph': 10.0, 'gust_kph': 16.2}}", 'score': 0.9997275, 'raw_content': None}], 'response_time': 3.24}), AIMessage(content='The current weather in New York City is sunny with a temperature of 8.3°C (46.9°F). The wind is coming from the north-northwest at 7.2 mph (11.5 kph), and the humidity level is 60%. The weather feels slightly cooler at 6.3°C (43.4°F) due to the wind chill.', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 79, 'prompt_tokens': 535, 'total_tokens': 614, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}}, 'model_name': 'gpt-4o-2024-08-06', 'system_fingerprint': 'fp_159d8341cc', 'finish_reason': 'stop', 'logprobs': None}, id='run-43d275f1-aacb-44f4-bdd0-8233c3765699-0', usage_metadata={'input_tokens': 535, 'output_tokens': 79, 'total_tokens': 614, 'input_token_details': {'audio': 0, 'cache_read': 0}, 'output_token_details': {'audio': 0, 'reasoning': 0}})]}})

LangGraph 참고 자료

반응형
  • 네이버 블로그 공유
  • 네이버 밴드 공유
  • 페이스북 공유
  • 카카오스토리 공유