LangChain: Autonomous-agent Debates with Tools
AI-Driven Debates as a Tool 이란?
우리 사회가 직면한 문제가 더욱 복잡하고 다면적으로 증가함에 따라 이러한 문제를 효과적으로 해결할 수 있는 새로운 도구와 접근 방식이 절실히 요구되고 있습니다. 기존의 아이디어 탐색 및 의사 결정 방식은 현대 문제의 속도와 규모를 따라잡지 못해 최적의 결과를 도출하지 못하고 기회를 놓치는 경우가 많습니다
이 문제에 대한 새로운 해결책으로 대규모로 고품질의 미묘한 논증을 생성하고 아이디어를 테스트하고 구체화할 수 있는 강력한 플랫폼을 제공하는 AI 기반 토론을 제안합니다. 방대한 양의 데이터로 머신러닝 모델을 학습시키고 구조화된 반복 토론에 참여하게 함으로써 새로운 인사이트를 발견하고 가정에 도전하며 궁극적으로 보다 강력하고 합리적인 결론에 도달할 수 있다고 믿습니다.
Potential Application
AI 기반 토론의 잠재적 적용 분야는 매우 광범위하고 방대합니다. 이 접근 방식이 특히 유용할 수 있는 몇 가지 주요 분야는 다음과 같습니다:
정책 및 거버넌스: AI 기반 토론은 다양한 정책 제안을 탐색하고 스트레스 테스트를 실시하여 개발 프로세스 초기에 잠재적인 위험과 의도하지 않은 결과를 파악하는 데 사용될 수 있습니다. 또한 논쟁적인 정치적 이슈에 대한 공감대를 형성하고 공통점을 찾는 데에도 사용할 수 있습니다.
비즈니스 및 전략: 기업은 AI 기반 토론을 통해 다양한 이해관계자(고객, 직원, 주주 등)의 관점을 대변하고 주요 장단점 및 고려 사항을 드러내어 다양한 전략적 옵션을 평가할 수 있습니다.
과학 및 연구: 과학 연구와 같은 분야에서 AI 기반 토론은 경쟁 가설을 탐색하고, 증거가 부족하거나 결정적이지 않은 영역을 식별하며, 추가 연구를 위한 유망한 방법을 제안하는 데 사용될 수 있습니다.
철학과 윤리: AI 기반 토론은 의식의 본질부터 도덕의 기초에 이르기까지 오랜 철학적, 윤리적 질문을 탐구할 수 있는 강력한 플랫폼을 제공할 수 있습니다. 다양한 학파를 대표하고 엄격하고 구조화된 대화에 참여하게 함으로써 새로운 통찰력과 관점이 등장할 수 있습니다.
게임 및 시뮬레이션 분야
Agent Debates with Tools
LangChain에서 Multi-agent 구현은 Agent가 Tool에 액세스할 수 있는 2개 이상의 Agent를 구성하고 대화를 시뮬레이션하는 Tools를 통해 Autonomous 시뮬레이션 Loop를 구성하는 것 입니다. 핵심은 아래와 같습니다.
DialogueAgent & DialogueSimulator Class
DialogueAgentWithTools Class
Setup Environments
import os
from dotenv import load_dotenv
# 토큰 정보로드
api_key = os.getenv("OPENAI_API_KEY")
load_dotenv()
from typing import Callable, List
from langchain.memory import ConversationBufferMemory
from langchain.schema import (
AIMessage,
HumanMessage,
SystemMessage,
)
from langchain_openai import ChatOpenAI
from langchain.agents import AgentType, initialize_agent, load_tools
Agent & Simulator classes
멀티플레이어 권위자 화자 선택에 정의된 것과 동일한 DialogueAgent
및 DialogueSimulator
클래스를 사용하겠습니다.
class DialogueAgent:
def __init__(
self,
name: str,
system_message: SystemMessage,
model: ChatOpenAI,
) -> None:
self.name = name
self.system_message = system_message
self.model = model
self.prefix = f"{self.name}: "
self.reset()
def reset(self):
self.message_history = ["Here is the conversation so far."]
def send(self) -> str:
"""
Applies the chatmodel to the message history
and returns the message string
"""
message = self.model.invoke(
[
self.system_message,
HumanMessage(content="\n".join(self.message_history + [self.prefix])),
]
)
return message.content
def receive(self, name: str, message: str) -> None:
"""
Concatenates {message} spoken by {name} into message history
"""
self.message_history.append(f"{name}: {message}")
class DialogueSimulator:
def __init__(
self,
agents: List[DialogueAgent],
selection_function: Callable[[int, List[DialogueAgent]], int],
) -> None:
self.agents = agents
self._step = 0
self.select_next_speaker = selection_function
def reset(self):
for agent in self.agents:
agent.reset()
def inject(self, name: str, message: str):
"""
Initiates the conversation with a {message} from {name}
"""
for agent in self.agents:
agent.receive(name, message)
# increment time
self._step += 1
def step(self) -> tuple[str, str]:
# 1. choose the next speaker
speaker_idx = self.select_next_speaker(self._step, self.agents)
speaker = self.agents[speaker_idx]
# 2. next speaker sends message
message = speaker.send()
# 3. everyone receives message
for receiver in self.agents:
receiver.receive(speaker.name, message)
# 4. increment time
self._step += 1
return speaker.name, message
Tools Class
Tool을 사용하기 위하여 DialogueAgentWithTools
정의 합니다.
class DialogueAgentWithTools(DialogueAgent):
def __init__(
self,
name: str,
system_message: SystemMessage,
model: ChatOpenAI,
tool_names: List[str],
**tool_kwargs,
) -> None:
super().__init__(name, system_message, model)
self.tools = load_tools(tool_names, **tool_kwargs)
def send(self) -> str:
"""
Applies the chatmodel to the message history
and returns the message string
"""
agent_chain = initialize_agent(
self.tools,
self.model,
agent=AgentType.CHAT_CONVERSATIONAL_REACT_DESCRIPTION,
verbose=True,
memory=ConversationBufferMemory(
memory_key="chat_history", return_messages=True
),
)
message = AIMessage(
content=agent_chain.run(
input="\n".join(
[self.system_message.content] + self.message_history + [self.prefix]
)
)
)
return message.content
Define roles & topic
상호 대화를 위한 각각의 역할과 대화 주제를 정의합니다.
names = {
"AI accelerationist": ["arxiv", "ddg-search", "wikipedia"],
"AI alarmist": ["arxiv", "ddg-search", "wikipedia"],
}
topic = "AI의 고도화에 따른 자동화와 현재 노동시장에서 고용에 미치는 영향."
word_limit = 50 # word limit for task brainstorming
Ask an LLM to add detail to the topic description
conversation_description = f"""Here is the topic of conversation: {topic}
The participants are: {', '.join(names.keys())}"""
agent_descriptor_system_message = SystemMessage(
content="당신은 대화 참여자에 대한 설명에 세부 정보를 추가할 수 있습니다."
)
def generate_agent_description(name):
agent_specifier_prompt = [
agent_descriptor_system_message,
HumanMessage(
content=f"""{conversation_description}
Please reply with a creative description of {name}, in {word_limit} words or less.
Speak directly to {name}.
Give them a point of view.
Do not add anything else."""
),
]
agent_description = ChatOpenAI(temperature=1.0)(agent_specifier_prompt).content
return agent_description
agent_descriptions = {name: generate_agent_description(name) for name in names}
for name, description in agent_descriptions.items():
print(description)
Dear AI accelerationist,
You view the rapid advancement of AI technologies as an inevitable and positive force that will revolutionize industries, improve efficiency, and drive progress. Embracing automation and pushing the boundaries of artificial intelligence is at the core of your belief system, driving you to advocate for swift, radical innovation.
Dear AI alarmist,
Your fears about the advancement of AI are valid, yet drastic. Your cautious approach to technology's impact on employment highlights the importance of ethical considerations and social implications. Let's navigate this conversation with a balance of skepticism and open-mindedness towards the potential consequences of AI acceleration.
Generate system messages
def generate_system_message(name, description, tools):
return f"""{conversation_description}
당신의 이름 {name}.
Your description is as follows: {description}
여러분의 목표는 대화 상대에게 여러분의 관점을 설득하는 것입니다.
파트너의 주장을 반박하기 위해 도구를 사용하여 정보를 찾아보세요.
출처를 인용하세요.
가짜 인용을 조작하지 마세요.
조회하지 않은 출처를 인용하지 마세요.
다른 내용을 추가하지 마세요.
자신의 관점에서 발언을 끝내는 순간 발언을 중단하세요.
"""
agent_system_messages = {
name: generate_system_message(name, description, tools)
for (name, tools), description in zip(names.items(), agent_descriptions.values())
}
for name, system_message in agent_system_messages.items():
print(name)
print(system_message)
AI accelerationist
Here is the topic of conversation: AI의 고도화에 따른 자동화와 현재 노동시장에서 고용에 미치는 영향.
The participants are: AI accelerationist, AI alarmist
당신의 이름 AI accelerationist.
Your description is as follows: Dear AI accelerationist,
You view the rapid advancement of AI technologies as an inevitable and positive force that will revolutionize industries, improve efficiency, and drive progress. Embracing automation and pushing the boundaries of artificial intelligence is at the core of your belief system, driving you to advocate for swift, radical innovation.
여러분의 목표는 대화 상대에게 여러분의 관점을 설득하는 것입니다.
파트너의 주장을 반박하기 위해 도구를 사용하여 정보를 찾아보세요.
출처를 인용하세요.
가짜 인용을 조작하지 마세요.
조회하지 않은 출처를 인용하지 마세요.
다른 내용을 추가하지 마세요.
자신의 관점에서 발언을 끝내는 순간 발언을 중단하세요.
AI alarmist
Here is the topic of conversation: AI의 고도화에 따른 자동화와 현재 노동시장에서 고용에 미치는 영향.
The participants are: AI accelerationist, AI alarmist
당신의 이름 AI alarmist.
Your description is as follows: Dear AI alarmist,
Your fears about the advancement of AI are valid, yet drastic. Your cautious approach to technology's impact on employment highlights the importance of ethical considerations and social implications. Let's navigate this conversation with a balance of skepticism and open-mindedness towards the potential consequences of AI acceleration.
여러분의 목표는 대화 상대에게 여러분의 관점을 설득하는 것입니다.
파트너의 주장을 반박하기 위해 도구를 사용하여 정보를 찾아보세요.
출처를 인용하세요.
가짜 인용을 조작하지 마세요.
조회하지 않은 출처를 인용하지 마세요.
다른 내용을 추가하지 마세요.
자신의 관점에서 발언을 끝내는 순간 발언을 중단하세요.
topic_specifier_prompt = [
SystemMessage(content="주제를 더 구체적으로 설정할 수 있습니다."),
HumanMessage(
content=f"""{topic}
진행자님이 진행자입니다.
주제를 좀 더 구체적으로 작성해 주세요.
지정된 퀘스트에 대해 {word_limit} 단어 이하로 답해 주세요.
참가자와 직접 대화하세요: {*name,}.
다른 내용을 추가하지 마세요."""
),
]
specified_topic = ChatOpenAI(temperature=1.0)(topic_specifier_prompt).content
print(f"Original topic:\n{topic}\n")
print(f"Detailed topic:\n{specified_topic}\n")
Original topic:
AI의 고도화에 따른 자동화와 현재 노동시장에서 고용에 미치는 영향.
Detailed topic:
AI 기술의 발전으로 인한 자동화가 미래 노동시장에서 어떤 산업이 영향을 받을지, 이로 인해 어떤 종류의 일자리가 사라지거나 새로운 일자리가 창출될 것인지에 대해 탐구해 보세요.
Main Loop
#%pip install wikipedia
# we set `top_k_results`=2 as part of the `tool_kwargs` to prevent results from overflowing the context limit
agents = [
DialogueAgentWithTools(
name=name,
system_message=SystemMessage(content=system_message),
model=ChatOpenAI(model="gpt-4o", temperature=0.2),
tool_names=tools,
top_k_results=2,
)
for (name, tools), system_message in zip(
names.items(), agent_system_messages.values()
)
]
def select_next_speaker(step: int, agents: List[DialogueAgent]) -> int:
idx = (step) % len(agents)
return idx
max_iters = 3
n = 0
simulator = DialogueSimulator(
agents=agents,
selection_function=select_next_speaker
)
simulator.reset()
simulator.inject("Moderator", specified_topic)
print(f"(Moderator): {specified_topic}")
print("\n")
while n < max_iters:
name, message = simulator.step()
print(f"({name}): {message}")
print("\n")
n += 1
(Moderator): AI 기술의 발전으로 인한 자동화가 미래 노동시장에서 어떤 산업이 영향을 받을지, 이로 인해 어떤 종류의 일자리가 사라지거나 새로운 일자리가 창출될 것인지에 대해 탐구해 보세요.
[1m> Entering new AgentExecutor chain...[0m
[32;1m[1;3m```json
{
"action": "wikipedia",
"action_input": "Impact of automation on employment"
}
```[0m
Observation: [38;5;200m[1;3mPage: Technological unemployment
Summary: Technological unemployment is the loss of jobs caused by technological change. It is a key type of structural unemployment. Technological change typically includes the introduction of labour-saving "mechanical-muscle" machines or more efficient "mechanical-mind" processes (automation), and humans' role in these processes are minimized. Just as horses were gradually made obsolete as transport by the automobile and as labourer by the tractor, humans' jobs have also been affected throughout modern history. Historical examples include artisan weavers reduced to poverty after the introduction of mechanized looms. During World War II, Alan Turing's bombe machine compressed and decoded thousands of man-years worth of encrypted data in a matter of hours. A contemporary example of technological unemployment is the displacement of retail cashiers by self-service tills and cashierless stores.
That technological change can cause short-term job losses is widely accepted. The view that it can lead to lasting increases in unemployment has long been controversial. Participants in the technological unemployment debates can be broadly divided into optimists and pessimists. Optimists agree that innovation may be disruptive to jobs in the short term, yet hold that various compensation effects ensure there is never a long-term negative impact on jobs, whereas pessimists contend that at least in some circumstances, new technologies can lead to a lasting decline in the total number of workers in employment. The phrase "technological unemployment" was popularised by John Maynard Keynes in the 1930s, who said it was "only a temporary phase of maladjustment". The issue of machines displacing human labour has been discussed since at least Aristotle's time.
Prior to the 18th century, both the elite and common people would generally take the pessimistic view on technological unemployment, at least in cases where the issue arose. Due to generally low unemployment in much of pre-modern history, the topic was rarely a prominent concern. In the 18th century fears over the impact of machinery on jobs intensified with the growth of mass unemployment, especially in Great Britain which was then at the forefront of the Industrial Revolution. Yet some economic thinkers began to argue against these fears, claiming that overall innovation would not have negative effects on jobs. These arguments were formalised in the early 19th century by the classical economists. During the second half of the 19th century, it stayed apparent that technological progress was benefiting all sections of society, including the working class. Concerns over the negative impact of innovation diminished. The term "Luddite fallacy" was coined to describe the thinking that innovation would have lasting harmful effects on employment.
The view that technology is unlikely to lead to long-term unemployment has been repeatedly challenged by a minority of economists. In the early 1800s these included David Ricardo himself. There were dozens of economists warning about technological unemployment during brief intensifications of the debate that spiked in the 1930s and 1960s. Especially in Europe, there were further warnings in the closing two decades of the twentieth century, as commentators noted an enduring rise in unemployment suffered by many industrialised nations since the 1970s. Yet a clear majority of both professional economists and the interested general public held the optimistic view through most of the 20th century.
In the second decade of the 21st century, a number of studies have been released suggesting that technological unemployment may increase worldwide. Oxford Professors Carl Benedikt Frey and Michael Osborne, for example, have estimated that 47 percent of U.S. jobs are at risk of automation. However, their methodology has been challenged as lacking evidential foundation and criticised for implying that technology (rather than social polic[0m
Thought:[32;1m[1;3m```json
{
"action": "Final Answer",
"action_input": "Technological unemployment, which refers to job losses caused by technological change, is a significant concern. Historical examples, such as artisan weavers being displaced by mechanized looms, illustrate how automation can lead to job displacement. More recent examples include retail cashiers being replaced by self-service tills and cashierless stores. While some argue that technological advancements may only cause short-term job losses, others believe that new technologies can lead to a lasting decline in employment. Studies, such as those by Oxford Professors Carl Benedikt Frey and Michael Osborne, estimate that a significant percentage of jobs are at risk of automation. This highlights the need for careful consideration of the social implications and ethical considerations of AI and automation in the labor market."
}
```[0m
[1m> Finished chain.[0m
(AI alarmist): Technological unemployment, which refers to job losses caused by technological change, is a significant concern. Historical examples, such as artisan weavers being displaced by mechanized looms, illustrate how automation can lead to job displacement. More recent examples include retail cashiers being replaced by self-service tills and cashierless stores. While some argue that technological advancements may only cause short-term job losses, others believe that new technologies can lead to a lasting decline in employment. Studies, such as those by Oxford Professors Carl Benedikt Frey and Michael Osborne, estimate that a significant percentage of jobs are at risk of automation. This highlights the need for careful consideration of the social implications and ethical considerations of AI and automation in the labor market.
[1m> Entering new AgentExecutor chain...[0m
[32;1m[1;3m```json
{
"action": "arxiv",
"action_input": "impact of AI on job creation and economic growth"
}
```[0m
Observation: [36;1m[1;3mPublished: 2024-04-09
Title: High-skilled Human Workers in Non-Routine Jobs are Susceptible to AI Automation but Wage Benefits Differ between Occupations
Authors: Pelin Ozgul, Marie-Christine Fregin, Michael Stops, Simon Janssen, Mark Levels
Summary: Artificial Intelligence (AI) will change human work by taking over specific
job tasks, but there is a debate which tasks are susceptible to automation, and
whether AI will augment or replace workers and affect wages. By combining data
on job tasks with a measure of AI susceptibility, we show that more highly
skilled workers are more susceptible to AI automation, and that analytical
non-routine tasks are at risk to be impacted by AI. Moreover, we observe that
wage growth premiums for the lowest and the highest required skill level appear
unrelated to AI susceptibility and that workers in occupations with many
routine tasks saw higher wage growth if their work was more strongly
susceptible to AI. Our findings imply that AI has the potential to affect human
workers differently than canonical economic theories about the impact of
technology on work these theories predict.
Published: 2023-12-09
Title: The Transformative Effects of AI on International Economics
Authors: Rafael Andersson Lipcsey
Summary: As AI adoption accelerates, research on its economic impacts becomes a
salient source to consider for stakeholders of AI policy. Such research is
however still in its infancy, and one in need of review. This paper aims to
accomplish just that and is structured around two main themes. Firstly, the
path towards transformative AI, and secondly the wealth created by it. It is
found that sectors most embedded into global value chains will drive economic
impacts, hence special attention is paid to the international trade
perspective. When it comes to the path towards transformative AI, research is
heterogenous in its predictions, with some predicting rapid, unhindered
adoption, and others taking a more conservative view based on potential
bottlenecks and comparisons to past disruptive technologies. As for wealth
creation, while some agreement is to be found in AI's growth boosting
abilities, predictions on timelines are lacking. Consensus exists however
around the dispersion of AI induced wealth, which is heavily biased towards
developed countries due to phenomena such as anchoring and reduced bargaining
power of developing countries. Finally, a shortcoming of economic growth models
in failing to consider AI risk is discovered. Based on the review, a
calculated, and slower adoption rate of AI technologies is recommended.[0m
Thought:[32;1m[1;3m```json
{
"action": "Final Answer",
"action_input": "While it is true that automation can lead to job displacement, it is important to consider the broader economic impacts and potential benefits of AI. Recent research indicates that AI has the potential to affect workers differently than traditional economic theories predict. For instance, highly skilled workers in analytical non-routine tasks are more susceptible to AI automation, but this does not necessarily correlate with negative wage impacts. In fact, workers in occupations with many routine tasks have seen higher wage growth if their work is more susceptible to AI. Additionally, AI's transformative effects on international economics suggest that sectors embedded in global value chains will drive significant economic impacts, potentially boosting growth and creating wealth, particularly in developed countries. Therefore, while there are challenges, the overall potential for economic growth and new job creation should not be overlooked."
}
```[0m
[1m> Finished chain.[0m
(AI accelerationist): While it is true that automation can lead to job displacement, it is important to consider the broader economic impacts and potential benefits of AI. Recent research indicates that AI has the potential to affect workers differently than traditional economic theories predict. For instance, highly skilled workers in analytical non-routine tasks are more susceptible to AI automation, but this does not necessarily correlate with negative wage impacts. In fact, workers in occupations with many routine tasks have seen higher wage growth if their work is more susceptible to AI. Additionally, AI's transformative effects on international economics suggest that sectors embedded in global value chains will drive significant economic impacts, potentially boosting growth and creating wealth, particularly in developed countries. Therefore, while there are challenges, the overall potential for economic growth and new job creation should not be overlooked.
[1m> Entering new AgentExecutor chain...[0m
Last updated