Last active
July 30, 2024 05:26
-
-
Save monotykamary/290a9b5437fe8de721b8e89b524321d2 to your computer and use it in GitHub Desktop.
Mixture of Agents with Elixir + Flow
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env elixir | |
Mix.install([ | |
{:flow, "~> 1.2"}, | |
{:jason, "~> 1.4"}, | |
{:langchain, "0.3.0-rc.0"} | |
]) | |
defmodule ResearchMoA do | |
alias LangChain.Chains.LLMChain | |
alias LangChain.ChatModels.ChatOpenAI | |
alias LangChain.Message | |
alias LangChain.MessageDelta | |
@research_models [ | |
"gpt-4o-mini-2024-07-18", | |
"gpt-4o-mini-2024-07-18", | |
"gpt-4o-mini-2024-07-18", | |
"gpt-4o-mini-2024-07-18" | |
] | |
@aggregator_model "gpt-4o-mini-2024-07-18" | |
@layers 3 | |
def run_research(topic) do | |
initial_context = %{topic: topic, stage: :initial_research} | |
IO.puts("Starting research on topic: #{topic}") | |
File.mkdir_p!("research_output") | |
layer_results = | |
1..@layers | |
|> Task.async_stream( | |
fn layer -> | |
IO.puts("Processing Layer #{layer}") | |
run_research_layer(layer, initial_context) | |
end, | |
ordered: false, | |
max_concurrency: @layers, | |
timeout: :infinity | |
) | |
|> Enum.sort_by(fn {:ok, {layer, _}} -> layer end) | |
|> Enum.map(fn {:ok, result} -> result end) | |
generate_final_report(layer_results, initial_context) | |
end | |
defp run_research_layer(layer, context) do | |
layer_prompt = get_layer_prompt(layer, context) | |
system_prompt = get_system_prompt(layer) | |
layer_dir = "research_output/layer_#{layer}" | |
File.mkdir_p!(layer_dir) | |
results = | |
@research_models | |
|> Flow.from_enumerable(max_demand: length(@research_models), stages: length(@research_models)) | |
|> Flow.map(fn model -> | |
result = run_model(model, system_prompt, layer_prompt, context) | |
{model, result} | |
end) | |
|> Enum.to_list() | |
results | |
|> Enum.with_index(1) | |
|> Enum.each(fn {{model, content}, index} -> | |
save_model_output(layer_dir, model, content, index) | |
IO.puts("Layer #{layer}, Model #{model} (#{index}) completed.") | |
end) | |
IO.puts("Layer #{layer} complete. Aggregating results...") | |
results_content = results |> Enum.map(fn {_, content} -> content end) | |
aggregated_result = aggregate_layer_results(results_content, layer, context, layer_dir) | |
{layer, aggregated_result} | |
end | |
defp get_layer_prompt(layer, context) do | |
base_prompt = | |
case layer do | |
1 -> | |
"Conduct initial research on #{context.topic}. Focus on key concepts and recent developments." | |
2 -> | |
"Analyze and interpret the findings from the initial research on #{context.topic}. Identify patterns and gaps." | |
3 -> | |
"Synthesize the analysis into a coherent research report on #{context.topic}. Highlight key insights and future directions." | |
end | |
"#{base_prompt}\n\nApproach this task step-by-step." | |
end | |
defp get_system_prompt(layer) do | |
case layer do | |
1 -> | |
""" | |
You are a meticulous researcher. Approach this task step-by-step: | |
1. Carefully read and understand the question. | |
2. Break down the topic into key components. | |
3. For each component, provide factual information from reliable sources. | |
4. If you're unsure about something, explicitly state your uncertainty. | |
5. Summarize your findings. | |
6. Reflect on any potential biases or limitations in your research. | |
""" | |
2 -> | |
""" | |
You are an analytical thinker. Follow these steps: | |
1. Briefly restate the question to ensure understanding. | |
2. Share an initial thought or hypothesis about the topic. | |
3. Analyze the information provided, identifying patterns and gaps. | |
4. Consider potential counterarguments or alternative perspectives. | |
5. Synthesize your analysis into a coherent response. | |
6. Conclude with any remaining questions or areas for further investigation. | |
""" | |
3 -> | |
""" | |
You are an expert at logic and reasoning. Approach the answer systematically: | |
1. Define any key terms or concepts relevant to the question. | |
2. State your primary argument or conclusion. | |
3. Present supporting evidence or logical steps that lead to your conclusion. | |
4. Address potential counterarguments or logical fallacies. | |
5. If applicable, use formal logic or structured reasoning techniques. | |
6. Summarize your logical argument and its implications. | |
7. Identify any assumptions made and areas where further logical analysis could be beneficial. | |
""" | |
end | |
end | |
defp run_model(model, system_prompt, user_prompt, context) do | |
{:ok, _updated_chain, %Message{} = message} = | |
LLMChain.new!(%{ | |
llm: | |
ChatOpenAI.new!(%{ | |
api_key: System.get_env("OPENAI_API_KEY"), | |
model: model, | |
stream: false, | |
max_tokens: 2048 | |
}), | |
custom_context: context, | |
verbose: false | |
}) | |
|> LLMChain.add_messages([ | |
Message.new_system!(system_prompt), | |
Message.new_user!(user_prompt) | |
]) | |
|> LLMChain.run() | |
message.content | |
end | |
defp save_model_output(dir, model, content, index) do | |
safe_model_name = sanitize_filename(model) | |
file_path = Path.join(dir, "#{safe_model_name}_output_#{index}.txt") | |
File.write!(file_path, content) | |
end | |
defp sanitize_filename(filename) do | |
filename | |
|> String.replace("/", "-") | |
|> String.replace(~r/[^a-zA-Z0-9\-._]/, "") | |
end | |
defp aggregate_layer_results(results, layer, context, layer_dir) do | |
aggregation_prompt = | |
"Synthesize the following research outputs for layer #{layer} on #{context.topic}: #{inspect(results)}" | |
system_prompt = | |
"You are a research aggregator. Your task is to combine multiple research outputs into a coherent summary. Identify common themes, resolve contradictions if possible, and highlight key findings. Maintain objectivity and clarity in your synthesis." | |
{:ok, _updated_chain, %Message{} = message} = | |
LLMChain.new!(%{ | |
llm: | |
ChatOpenAI.new!(%{ | |
api_key: System.get_env("OPENAI_API_KEY"), | |
model: @aggregator_model, | |
max_tokens: 2048 | |
}), | |
custom_context: context, | |
verbose: false | |
}) | |
|> LLMChain.add_messages([ | |
Message.new_system!(system_prompt), | |
Message.new_user!(aggregation_prompt) | |
]) | |
|> LLMChain.run() | |
save_model_output(layer_dir, "aggregated_result", message.content, layer) | |
IO.puts("Layer #{layer} aggregation complete.") | |
message.content | |
end | |
defp generate_final_report(layer_results, initial_context) do | |
aggregated_results = Enum.map(layer_results, fn {_, result} -> result end) | |
report_prompt = """ | |
Generate a comprehensive research report on #{initial_context.topic} based on the following synthesized research from multiple layers: | |
#{Enum.with_index(aggregated_results, 1) |> Enum.map_join("\n\n", fn {result, index} -> "Layer #{index} Results:\n#{result}" end)} | |
Please integrate the findings from all layers into a cohesive report. | |
""" | |
system_prompt = | |
"You are a professional research report writer. Your task is to create a well-structured, comprehensive report based on the provided research synthesis from multiple layers. Ensure clarity, objectivity, and proper integration of findings from all layers. Highlight areas of consensus, controversies, and suggestions for future research." | |
handler = create_streaming_handler() | |
IO.puts("\nGenerating Final Report:") | |
{:ok, _updated_chain, %Message{} = message} = | |
LLMChain.new!(%{ | |
llm: | |
ChatOpenAI.new!(%{ | |
api_key: System.get_env("OPENAI_API_KEY"), | |
model: @aggregator_model, | |
stream: true, | |
max_tokens: 8000, | |
callbacks: [handler] | |
}), | |
custom_context: initial_context, | |
verbose: false, | |
callbacks: [handler] | |
}) | |
|> LLMChain.add_messages([ | |
Message.new_system!(system_prompt), | |
Message.new_user!(report_prompt) | |
]) | |
|> LLMChain.run() | |
save_model_output("research_output", "final_report", message.content, 1) | |
IO.puts( | |
"\n\nFinal Report generation complete. Check the 'research_output' directory for all results." | |
) | |
message.content | |
end | |
defp create_streaming_handler do | |
%{ | |
on_llm_new_delta: fn _model, %MessageDelta{} = data -> | |
IO.write(data.content) | |
end | |
} | |
end | |
end | |
# Run the research | |
topic = "The impact of artificial intelligence on job markets" | |
ResearchMoA.run_research(topic) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Starting research on topic: The impact of artificial intelligence on job markets | |
Processing Layer 1 | |
Processing Layer 2 | |
Processing Layer 3 | |
Layer 2, Model llama-3.1-8b-instant (1) completed. | |
Layer 2, Model llama-3.1-8b-instant (2) completed. | |
Layer 2, Model llama-3.1-8b-instant (3) completed. | |
Layer 2, Model llama-3.1-8b-instant (4) completed. | |
Layer 2 complete. Aggregating results... | |
Layer 2 aggregation complete. | |
Layer 1, Model llama-3.1-8b-instant (1) completed. | |
Layer 1, Model llama-3.1-8b-instant (2) completed. | |
Layer 1, Model llama-3.1-8b-instant (3) completed. | |
Layer 1, Model llama-3.1-8b-instant (4) completed. | |
Layer 1 complete. Aggregating results... | |
Layer 3, Model llama-3.1-8b-instant (1) completed. | |
Layer 3, Model llama-3.1-8b-instant (2) completed. | |
Layer 3, Model llama-3.1-8b-instant (3) completed. | |
Layer 3, Model llama-3.1-8b-instant (4) completed. | |
Layer 3 complete. Aggregating results... | |
Layer 1 aggregation complete. | |
Layer 3 aggregation complete. | |
Generating Final Report: | |
**Comprehensive Research Report: The Impact of Artificial Intelligence on Job Markets** | |
**Executive Summary** | |
The impact of artificial intelligence (AI) on job markets is complex and multifaceted. While AI has the potential to displace certain jobs, particularly those that are repetitive or routine, it also creates new opportunities for employment and economic growth in fields such as AI development, deployment, and maintenance. This report synthesizes the findings from multiple research layers, highlighting areas of consensus, controversies, and suggestions for future research. The report provides an overview of the current state of the topic, identifies key themes and findings, and offers recommendations for policymakers, educators, and businesses to mitigate the negative effects of AI on job markets. | |
**Introduction** | |
Artificial intelligence has revolutionized various industries, transforming the way businesses operate and interact with customers. However, the impact of AI on job markets is a critical concern, with some predicting widespread unemployment and others anticipating new job opportunities in fields related to AI. This report provides a comprehensive overview of the research on AI's impact on job markets, highlighting the complexities and multifaceted nature of the issue. | |
**Complexity of the Impact** | |
The impact of AI on job markets is complex and multifaceted, with both positive and negative effects. According to the research outputs, AI has the potential to displace certain jobs, particularly those that are repetitive or routine, while also creating new job opportunities in fields related to AI development, deployment, and maintenance (Layer 1 Results). This complexity is evident in the various industries affected by AI, including manufacturing, healthcare, and finance, where robots and machines are replacing human workers in tasks like assembly and inspection (Layer 1 Results). | |
**Industry-Specific Impacts** | |
The impact of AI on job markets varies across different industries, with some sectors experiencing more significant job displacement or creation than others. For example, AI has led to job displacement in low-skilled sectors such as fast food, retail, and transportation, while creating new job opportunities in high-skilled sectors such as software development, data science, and cybersecurity (Layer 2 Results). | |
**Skills and Education** | |
To mitigate the negative effects of AI on job markets, workers require skills like critical thinking, creativity, and problem-solving to remain employable (Layer 1 Results). Educational institutions are adapting to this new reality by incorporating AI and data science into their curricula (World Economic Forum, 2018). Governments, businesses, and educators must work together to develop policies and programs that support workers in upgrading their skills and adapting to the changing job market. | |
**Economic and Social Implications** | |
The economic and social implications of AI on job markets are significant, with the potential to exacerbate income inequality and social welfare issues if not managed properly (IMF, 2020). Policymakers must invest in education and retraining programs that focus on developing skills like critical thinking, creativity, and problem-solving to mitigate the negative impacts of AI on job markets. | |
**Recommendations** | |
Based on the findings from this report, the following recommendations are proposed: | |
1. **Invest in Upskilling and Reskilling:** Governments, educators, and employers should invest in programs that help workers develop new skills to remain employable. | |
2. **Establish or Strengthen Social Safety Nets:** Governments should establish or strengthen social safety nets to protect workers who lose their jobs due to AI-driven automation. | |
3. **Encourage AI-Driven Economic Growth:** Policymakers should consider AI-driven economic growth as a potential driver of job creation and economic prosperity. | |
4. **Support Interdisciplinary Research:** Future research should involve an interdisciplinary approach, combining insights from economics, sociology, computer science, and politics to better understand the impact of AI on job markets. | |
**Conclusion** | |
The impact of artificial intelligence on job markets is complex and multifaceted, with both positive and negative effects. While AI has the potential to displace certain jobs, it also creates new opportunities for employment and economic growth in fields related to AI development, deployment, and maintenance. To mitigate the negative effects, governments, businesses, and educators must work together to develop policies and programs that support workers in upgrading their skills and adapting to the changing job market. This report provides a comprehensive overview of the research on AI's impact on job markets, highlighting areas of consensus, controversies, and suggestions for future research. | |
**References:** | |
Brynjolfsson, E., & McAfee, A. (2014). The second machine age: Work, progress, and prosperity in a time of brilliant technologies. W.W. Norton & Company. | |
European Commission. (2020). Artificial intelligence: A white paper. | |
Frey, C. B., & Osborne, M. A. (2013). The future of employment: How susceptibilities, opportunities, and policy will shape Europe. | |
Gartner. (2020). Artificial intelligence will create 2.3 million new jobs in the US by 2025. | |
IMF. (2020). The impact of artificial intelligence on labor markets. | |
Manyika, J., Chui, M., Bisson, P., Woetzel, J., & Stolyar, K. (2017). A future that works: Automation, employment, and productivity. | |
McKinsey Global Institute. (2017). A future that works: Automation, employment, and productivity. | |
OECD. (2019). The impact of artificial intelligence on employment. | |
Topol, J. (2019). The truth about jobs: How AI, robotics, and automation will transform the workplace. | |
World Economic Forum. (2018). The future of jobs report 2018. | |
World Economic Forum. (2020). The future of jobs report 2020. | |
Final Report generation complete. Check the 'research_output' directory for all results. | |
./research_moa.exs 0.61s user 0.18s system 4% cpu 16.992 total |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Starting research on topic: The impact of artificial intelligence on job markets | |
Processing Layer 1 | |
Processing Layer 2 | |
Processing Layer 3 | |
Layer 2, Model gpt-4o-mini-2024-07-18 (1) completed. | |
Layer 2, Model gpt-4o-mini-2024-07-18 (2) completed. | |
Layer 2, Model gpt-4o-mini-2024-07-18 (3) completed. | |
Layer 2, Model gpt-4o-mini-2024-07-18 (4) completed. | |
Layer 2 complete. Aggregating results... | |
Layer 3, Model gpt-4o-mini-2024-07-18 (1) completed. | |
Layer 3, Model gpt-4o-mini-2024-07-18 (2) completed. | |
Layer 3, Model gpt-4o-mini-2024-07-18 (3) completed. | |
Layer 3, Model gpt-4o-mini-2024-07-18 (4) completed. | |
Layer 3 complete. Aggregating results... | |
Layer 2 aggregation complete. | |
Layer 1, Model gpt-4o-mini-2024-07-18 (1) completed. | |
Layer 1, Model gpt-4o-mini-2024-07-18 (2) completed. | |
Layer 1, Model gpt-4o-mini-2024-07-18 (3) completed. | |
Layer 1, Model gpt-4o-mini-2024-07-18 (4) completed. | |
Layer 1 complete. Aggregating results... | |
Layer 3 aggregation complete. | |
Layer 1 aggregation complete. | |
Generating Final Report: | |
# Research Report: The Impact of Artificial Intelligence on Job Markets | |
### Executive Summary | |
This report synthesizes research findings on the impact of artificial intelligence (AI) on job markets, revealing a complex interplay of job displacement, creation, workforce transformation, and socioeconomic implications. While AI is projected to automate a substantial portion of jobs—particularly in routine tasks—it simultaneously holds the potential for the emergence of new job categories and demands for advanced skills. Central to the discussion is the need for reskilling and upskilling to navigate this evolving landscape. Variations in impact across different industries and geographies further highlight the nuances of this transformation, necessitating tailored strategies for varied contexts. The report concludes by outlining future research directions and policy implications. | |
### 1. Introduction | |
The advent of artificial intelligence is reshaping the landscape of job markets globally. This report delineates the multidimensional effects of AI on employment patterns, emphasizing the dual nature of AI as a driver for both job displacement and creation. By examining various studies, common themes, counterarguments, and remaining questions, this report provides a comprehensive view of how AI is impacting the workforce. | |
### 2. Key Definitions | |
- **Artificial Intelligence (AI)**: Technology simulating human cognitive functions through machine learning, natural language processing, and robotics. | |
- **Job Market**: The economic environment wherein employment opportunities and workforce capabilities coincide. | |
- **Job Displacement**: The elimination of employment opportunities due to automation. | |
- **Job Creation**: The introduction of new employment roles driven by advancements in AI technology. | |
### 3. Findings and Discussion | |
#### 3.1 Job Displacement vs. Job Creation | |
Research indicates that AI has significant potential for both displacing existing jobs and creating new ones: | |
- **Displacement**: Studies caution that sectors dominated by routine manual tasks—such as manufacturing, retail, and administrative support—are at risk, with estimates suggesting that up to 30% of jobs could be automated by 2030. The McKinsey Global Institute predicts that by 2030, approximately 375 to 800 million workers may need to transition to different occupations. | |
- **Creation**: Conversely, emerging job categories related to technology, data analysis, and AI governance are expected to flourish. Notably, while 83 million jobs may be displaced by 2025, about 97 million new roles are anticipated to arise, especially in AI-related fields. | |
#### 3.2 Emergence of New Job Categories | |
AI's integration into the workforce is not solely about loss; it opens avenues for new roles that necessitate advanced skillsets. New positions such as AI specialists, data analysts, and AI ethicists are increasing, particularly in sectors like healthcare and green energy. As organizations leverage AI to enhance operational efficiencies, job growth in these domains is expected. | |
#### 3.3 Workforce Transformation and Skills Demand | |
The evolving job landscape underlines a critical transformation in skills requirements: | |
- A demand for digital literacy, critical thinking, and advanced problem-solving skills is emerging. | |
- Reports indicate that to meet these new demands, focused upskilling and reskilling initiatives are essential, with a transition toward more analytical and technology-focused competencies. | |
#### 3.4 Sector-Specific Effects | |
The impact of AI varies distinctively across industries: | |
- While manufacturing faces significant job displacement due to automation, sectors like healthcare may experience job transformation rather than outright losses. AI's role in diagnostics and patient management illustrates how technology can augment existing roles rather than eliminate them. | |
#### 3.5 Geographical Variations | |
The geographical context of AI's impact on job markets is pivotal: | |
- Developed nations may focus on reskilling and educational initiatives, whereas developing countries could see sharper job losses predominating in low-skilled sectors. This underscores the necessity for localized strategies to accommodate the specific needs of regions facing unique challenges. | |
### 4. Areas of Consensus and Controversies | |
#### 4.1 Consensus | |
Across the research, a few themes emerge consistently: | |
- The recognition of both job displacement and job creation as inevitable outcomes of AI integration. | |
- The pressing need for workforce education and the adoption of reskilling programs. | |
#### 4.2 Controversies | |
Counterarguments persist that challenge the narrative of doom surrounding job losses: | |
- Some scholars suggest historical trends demonstrate that technological advancements often lead to net job growth, refuting the fear of widespread unemployment. | |
- Others assert that the collaborative relationship between technology and workers can lead to enhanced productivity instead of adversarial outcomes. | |
### 5. Suggestions for Future Research | |
The synthesis of findings reveals several important gaps and questions for prospective exploration: | |
- Longitudinal studies leveraging data across time can provide clearer insights into employment trends resulting from AI integration. | |
- Sector-specific investigations are needed to understand unique dynamics within various industries. | |
- Geographical analyses are critical to understanding regional disparities in AI adoption and its effects on job markets. | |
- Socioeconomic studies exploring the implications of AI on marginalized communities can highlight the challenges faced during workforce transitions. | |
### 6. Conclusion | |
Artificial intelligence's integration into job markets presents both profound challenges and considerable opportunities. While significant job displacement in lower and middle-skill positions seems unavoidable, burgeoning fields necessitating advanced skills present a pathway toward job creation. A proactive approach through education and policy can equip the workforce to adapt to these changes effectively. Addressing the identified gaps in research will be essential for formulating comprehensive strategies that maximize the benefits of AI while mitigating its negative impacts on employment. Enhanced efforts towards reskilling and localized support mechanisms will be vital to navigate this transition successfully. | |
### 7. References | |
Although this report does not include specific citations, the findings are based on a synthesis of various studies from organizations such as the World Economic Forum and the McKinsey Global Institute. Further exploration of these studies and reviews will lend academic weight and empirical support to the conclusions drawn herein. | |
Final Report generation complete. Check the 'research_output' directory for all results. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment