이 글은 LangGraph 기반 데이터 분석 멀티 에이전트 만드는 방법을 간략히 설명한다.
LangChain Tool 기반 수학 에이전트 개발
다음 코드를 입력해 실행한다.
from langchain_openai import OpenAI
from langchain_community.chat_models import ChatOllama
from langchain.chains import LLMMathChain, LLMChain
from langchain.prompts import PromptTemplate
from langchain_community.utilities import WikipediaAPIWrapper
from langchain.agents.agent_types import AgentType
from langchain.agents import Tool, initialize_agent
from dotenv import load_dotenv
import chainlit as cl
load_dotenv()
@cl.on_chat_start
def math_chatbot():
	llm = ChatOllama(model='llama3', temperature=0.0)
  	# prompt for reasoning based tool
	word_problem_template = """You are a reasoning agent tasked with solving t he user's logic-based questions. Logically arrive at the solution, and be factual. In your answers, clearly detail the steps involved and give the final answer. Provide the response in bullet points. Question  {question} Answer"""
	math_assistant_prompt = PromptTemplate(
		input_variables=["question"],
		template=word_problem_template
	)
  	# chain for reasoning based tool
	word_problem_chain = LLMChain(llm=llm,
								  prompt=math_assistant_prompt)
	# reasoning based tool                              
	word_problem_tool = Tool.from_function(name="Reasoning Tool",
		                     func=word_problem_chain.run,
				description="Useful for when you need to answer logic-based/reasoning questions."
										   )
  	# calculator tool for arithmetics
	problem_chain = LLMMathChain.from_llm(llm=llm)
	math_tool = Tool.from_function(name="Calculator",
				     func=problem_chain.run,
			             description="Useful for when you need to answer numeric questions. This tool is only for math questions and nothing else. Only input math expressions, without text",
								   )
  	# Wikipedia Tool
	wikipedia = WikipediaAPIWrapper()
	wikipedia_tool = Tool(
		name="Wikipedia",
		func=wikipedia.run,
		description="A useful tool for searching the Internet to find information on world events, issues, dates, years, etc. Worth using for general topics. Use precise questions.")
  	# agent
	agent = initialize_agent(
		tools=[wikipedia_tool, math_tool, word_problem_tool],
		llm=llm,
		agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
		verbose=False,
		handle_parsing_errors=True
	)
	cl.user_session.set("agent", agent)
@cl.on_message
async def process_user_query(message: cl.Message):
	agent = cl.user_session.get("agent")
	response = await agent.acall(message.content,
				callbacks=[cl.AsyncLangchainCallbackHandler()])
	await cl.Message(response["output"]).send()	
결과는 다음과 같다.
레퍼런스
- Building a Math Application with LangChain Agents | by Tahreem Rasul | Towards Data Science
 - LangGraph From LangChain Explained In Simple Terms | by Cobus Greyling | Medium
 - How to Build AI Agents with LangGraph: A Step-by-Step Guide | by Lore Van Oudenhove | Sep, 2024 | Medium
 - LangGraph Studio: Visualizing and Testing AI Agents with LangChain | by Lore Van Oudenhove | Sep, 2024 | AI Advances (gopubby.com)
 - Customer Support (langchain-ai.github.io)
 - LangGraph Simplified (kaggle.com)
 - LangGraph Studio: The first agent IDE (langchain.dev)
 
댓글 없음:
댓글 쓰기