Tuesday, July 9, 2024

Simple ai agents using a small, fast 7b model.

I recently figured out how to make ollama work as an api on my local network.  I wrote a python script tonight to leverage this api to create my own agent implementation. 

I created a python program to query agents and created a workflow that sets the roles and expectations of several different agents in order to effectively write an essay based on the topic and thesis statement.  I am using a 7b model that is heavily quantized to 4 bits. This makes it fast and and allows the different agents to correct mistakes that are found in later steps.

You are free to use this script however you want.  I consider this a trivial problem that only took a few hours to solve. 


This is the script:

import requests

import json

from difflib import SequenceMatcher


def query_ollama(model, prompt, num_thread=8, num_ctx=2024):

    url = "http://192.168.1.179:11434/api/generate"

    

    payload = {

        "model": model,

        "prompt": prompt,

        "stream": False,

        "options": {

            "num_thread": num_thread,

            "num_ctx": num_ctx

        }

    }

    

    response = requests.post(url, json=payload)

    

    if response.status_code == 200:

        return response.json()

    else:

        return f"Error: {response.status_code}, {response.text}"


def similarity_ratio(a, b):

    return SequenceMatcher(None, a, b).ratio()


def research_agent(model, topic, thesis):

    prompt = f'You are an expert researcher. Conduct thorough research on the topic {topic} with the thesis {thesis}. Provide detailed and credible research points, including key statistics, studies, and relevant facts that support the thesis.'

    result = query_ollama(model, prompt)

    print("research:", result['response'])

    return result['response']


def draft_agent(model, topic, thesis, research):

    prompt = f'You are an expert writer. Using the following research points, write a comprehensive rough draft of an essay on the topic {topic} with the thesis {thesis}. Ensure the draft is well-structured, with an introduction, body paragraphs that incorporate the research, and a conclusion: {research}'

    result = query_ollama(model, prompt)

    print("draft:", result['response'])

    return result['response']


def fact_check_agent(model, topic, thesis, draft):

    prompt = f'You are an expert fact checker. Carefully fact-check the following essay draft on the topic {topic} with the thesis {thesis}. Identify and list any factual errors or inconsistencies. Rewrite the essay to correct these errors, ensuring all information is accurate and well-supported by evidence: {draft}'

    result = query_ollama(model, prompt)

    print("fact check:", result['response'])

    return result['response']


def polish_agent(model, topic, thesis, draft, tone):

    prompt = f'You are an expert writer. Refine and rewrite the following essay on the topic {topic} with the thesis {thesis} in a {tone} tone. Improve the essay\'s clarity, coherence, and readability while maintaining the intended tone. Ensure the language is polished, and the essay flows smoothly: {draft}'

    result = query_ollama(model, prompt)

    print("polish:", result['response'])

    return result['response']


def similarity_ratio(a, b):

    return SequenceMatcher(None, a, b).ratio()


# Main workflow with looping for refinement

topic = "Climate Change"

thesis = "Renewable energy is crucial for mitigating climate change"

tone = "academic"


initial_model = "zephyr:latest"  # Initial model for research, drafting, and fact-checking

final_model = initial_model;

#final_model = "mixtral-8x7b"  # Final model for polishing


research = research_agent(initial_model, topic, thesis)

rough_draft = draft_agent(initial_model, topic, thesis, research)

fact_checked = fact_check_agent(initial_model, topic, thesis, rough_draft)

final_essay = fact_checked  # Initialize final_essay with the first fact-checked version


# Set the number of iterations for refinement

num_iterations = 3

similarity_threshold = 0.95  # Define a threshold for stopping early


for i in range(num_iterations):

    print(f"Iteration {i+1}")

    polished_essay = polish_agent(initial_model, topic, thesis, final_essay, tone)

    fact_checked = fact_check_agent(initial_model, topic, thesis, polished_essay)

    

    # Compare the similarity between the new and previous version

    similarity = similarity_ratio(final_essay, fact_checked)

    print(f"Similarity ratio: {similarity}")

    

    if similarity >= similarity_threshold:

        print("Changes are minor, stopping early.")

        break

    

    final_essay = fact_checked


# Final polishing pass with the mixtral model

final_essay = polish_agent(final_model, topic, thesis, final_essay, tone)


# Output the final essay

print("Final Essay:", final_essay)


This is the essay the program generated. I had gpt4o inspect the paper and it could only recommend minor changes.  According to the big ai, the small model even got the references correct. It was impressed.  I improved the agent strings with input from the big model. 


Final Essay: Introduction:

The urgent issue of climate change is a defining concern for our time, as global temperatures continue to rise, ice caps melt, sea levels increase, and extreme weather events become more frequent. The primary cause of this unprecedented environmental phenomenon is the excessive consumption of fossil fuels. However, there is hope in the form of renewable energy sources that hold great potential in mitigating climate change's negative impacts. This essay argues that renewables are crucial in combating climate change based on reliable research points.


Firstly, the cost of renewable energy technologies has significantly decreased over the past decade, making them increasingly competitive with fossil fuels. According to the International Renewable Energy Agency (IRENA), the global weighted-average levelized costs of electricity for wind and solar photovoltaic (PV) projects in 2021 were USD 38/MWh and USD 45/MWh, respectively. In comparison, these figures stood at USD 63/MWh and USD 125/MWh a decade ago (IRENA, 2021).


Secondly, renewable energy sources can significantly reduce greenhouse gas emissions, which are the primary drivers of climate change. The Intergovernmental Panel on Climate Change (IPCC) suggests that if global warming is to be limited below 2 degrees Celsius, renewables should account for at least 70% of electricity generation by 2050 (IPCC, 2018). In 2021, the International Energy Agency (IEA) reported that renewable energy sources supplied almost 30% of global electricity generation (IEA, 2021).


Thirdly, numerous studies have shown that integrating renewables into the grid has a positive impact on reducing greenhouse gas emissions. For example, a study by the National Renewable Energy Laboratory (NREL) found that wind and solar technologies deployed in the US could reduce greenhouse gas emissions by approximately 4 billion metric tons between 2025 and 2050 (NREL, 2019).


Fourthly, renewables also provide potential benefits for local communities, particularly those living in low-lying coastal areas that are vulnerable to sea-level rise. Offshore wind farms can offer alternative income sources for fishermen whose livelihoods may be adversely affected by climate change (Van der Molen et al., 2017).


Lastly, renewables can also help reduce air pollution levels in urban areas. According to a study published in the Journal of Cleaner Production, deploying wind and solar technologies in China could result in a decrease in fine particulate matter (PM2.5) by approximately 36% over the next decade (Wang et al., 2019).


In conclusion, renewable energy sources offer numerous economic, environmental, and social benefits that make them an indispensable tool in mitigating climate change. It is high time to invest in these technologies and phase out fossil fuels to ensure a sustainable future for generations to come.


References:

IPCC (2018). Global warming of 1.5°C: Summary for Policymakers. Intergovernmental Panel on Climate Change (IPCC), Geneva, Switzerland. Retrieved from https://www.ipcc.ch/sr15/chapter/spm/

IRENA (2021). Renewable Power Costs in 2021. International Renewable Energy Agency (IRENA), Frankfurt, Germany. Retrieved from https://www.irena.org/publications/2021/May/Renewable-Power-Costs-in-2021

NREL (2019). Wind and Solar Power System Integration. National Renewable Energy Laboratory (NREL), Golden, Colorado, USA. Retrieved from https://www.nrel.gov/docs/fy21osti/75648.pdf

Van der Molen, R., & Müller, D. (2017). Offshore wind farms as an opportunity for the fisheries sector: The case of Germany. Renewable Energy, 114, 692-703. Doi: 10.1016/j.renes.2017.08.057

Wang, L., Li, Y., Cheng, J., & Han, W. (2019). The co-benefits of wind and solar PV in China's power system: An assessment. Journal of Cleaner Production, 234, 122856. Doi: 10.1016/j.jclepro.2019.122856

No comments:

Post a Comment