100% Local OpenAI Agents with Ollama in 8 mins!

Mervin PraisonAbout 5 min readMar 25, 2025Watch original
THE SUMMARYAI-generated

Key Concepts

  • OpenAI Agents SDK: A framework for building AI agents.
  • Ollama: A tool for running large language models locally.
  • Llama 3.2: A specific large language model used in the examples.
  • Custom Tool: A function integrated into an agent to perform specific tasks (e.g., internet search).
  • Multi-Agent Workflow: A sequence of agents working together to achieve a goal.
  • Chainlit: A Python library for creating chatbot user interfaces.
  • DuckDuckGo Search: An internet search engine used as a custom tool.

Single Agent Creation

  • The initial example demonstrates creating a single agent using the OpenAI Agents SDK with Ollama.
  • Code Snippet:
    from Agents import agentRunner, OpenAIChatCompletionModel
    import os
    
    os.environ["OPENAI_API_KEY"] = "DO_NOT_USE_THIS_IN_PRODUCTION"
    
    model = OpenAIChatCompletionModel(
        model_name="llama3:latest",
        base_url="http://localhost:11434/v1"
    )
    
    agent = agentRunner(
        name="Meal Planner",
        instructions="Create a meal plan for a week.",
        model=model
    )
    
    result = agent.run_sync("Create a meal plan for a week")
    print(result)
    
  • Key Steps:
    1. Import necessary modules from the Agents package.
    2. Initialize the OpenAIChatCompletionModel with llama3:latest as the model name and the Ollama base URL (http://localhost:11434/v1).
    3. Create an agentRunner instance, providing a name, instructions, and the model.
    4. Run the agent synchronously using run_sync with a specific prompt.
    5. Print the result.
  • Technical Details:
    • OpenAIChatCompletionModel: A class that allows the agent to use a chat completion model, in this case, Llama 3.2.
    • base_url: Specifies the endpoint for the Ollama server, enabling local execution.
    • agentRunner: A class that manages the execution of an agent.
    • run_sync: Executes the agent and waits for the result.
  • Example: The agent is instructed to "Create a meal plan for a week," and the output is a detailed meal plan for each day of the week.

Custom Tool and Multiple Agents

  • This section explains how to create a custom tool for internet searching and integrate it into a multi-agent workflow.
  • Tools Used:
    • DuckDuckGo Search (ddgs): A Python library for performing searches on DuckDuckGo.
  • Code Snippet (Custom Tool):
    from duckduckgo_search import DDGS
    from agents import function_tool
    from datetime import date
    
    @function_tool
    def get_news_article(topic: str) -> str:
        """Searches the internet for the latest news articles on a given topic."""
        current_date = date.today().strftime("%Y-%m-%d")
        ddgs = DDGS()
        results = ddgs.news(topic, max_results=5, timelimit=current_date)
        articles = [r['title'] + " " + r['body'] for r in results]
        return str(articles)
    
  • Key Steps (Custom Tool):
    1. Import DDGS from duckduckgo_search and function_tool from agents.
    2. Define a function get_news_article that takes a topic as input.
    3. Use the @function_tool decorator to register the function as a tool.
    4. Inside the function, use DDGS().news() to search for news articles related to the topic, limiting the results to the current date.
    5. Return the titles and bodies of the articles as a string.
  • Code Snippet (Multiple Agents):
    from Agents import agentRunner, OpenAIChatCompletionModel
    import os
    
    os.environ["OPENAI_API_KEY"] = "DO_NOT_USE_THIS_IN_PRODUCTION"
    
    model = OpenAIChatCompletionModel(
        model_name="llama3:latest",
        base_url="http://localhost:11434/v1"
    )
    
    news_agent = agentRunner(
        name="News Agent",
        instructions="You provide the latest news articles.",
        tools=[get_news_article],
        model=model
    )
    
    editor_agent = agentRunner(
        name="Editor Agent",
        instructions="Rewrite and give me a news article ready for publishing.",
        model=model
    )
    
    def run_news_workflow(topic: str) -> str:
        """Runs the news workflow, fetching news and then editing it."""
        raw_news = news_agent.run_sync(topic)
        edited_news = editor_agent.run_sync(raw_news)
        return edited_news
    
    topic = "AI"
    result = run_news_workflow(topic)
    print(result)
    
  • Key Steps (Multiple Agents):
    1. Define two agents: news_agent and editor_agent.
    2. The news_agent is equipped with the get_news_article tool.
    3. Create a run_news_workflow function that takes a topic as input.
    4. The workflow involves running the news_agent to fetch news articles and then passing the raw news to the editor_agent for rewriting.
    5. The final edited news article is returned.
  • Workflow: The run_news_workflow function orchestrates the interaction between the two agents. The output from the news_agent (raw news articles) is fed as input to the editor_agent.
  • Example: The topic "AI" is used to fetch news articles, which are then processed by the editor agent to produce a publishable news article.

User Interface Creation

  • This section details how to create a simple chatbot user interface using Chainlit to interact with the multi-agent workflow.
  • Library Used:
    • Chainlit: A Python library for building conversational AI applications.
  • Code Snippet:
    import chainlit as cl
    from news import run_news_workflow
    
    @cl.on_message
    async def main(topic: str):
        """Runs the news workflow when a message is received."""
        result = run_news_workflow(topic)
        await cl.Message(content=result).send()
    
    @cl.on_chat_start
    async def start():
        """Sends a welcome message when the chat starts."""
        await cl.Message(content="Welcome to the news assistant!").send()
    
  • Key Steps:
    1. Import chainlit and the run_news_workflow function.
    2. Use the @cl.on_message decorator to define a function that runs when a message is received.
    3. Inside the function, call run_news_workflow with the message content as the topic.
    4. Send the result back to the user using cl.Message(content=result).send().
    5. Use the @cl.on_chat_start decorator to define a function that runs when the chat starts, sending a welcome message.
  • Functionality: The UI allows users to type a topic, which is then passed to the run_news_workflow function. The resulting news article is displayed in the chat interface.
  • Execution: The UI is launched using the command chainlit run ui.py.

Synthesis/Conclusion

The video demonstrates how to leverage the OpenAI Agents SDK with Ollama to create AI agents that run locally and for free. It covers the creation of a single agent, the integration of custom tools (like DuckDuckGo Search), the development of multi-agent workflows, and the creation of a user interface using Chainlit. The key takeaway is the ability to build and deploy AI agents without relying on external APIs or cloud services, providing greater control and privacy. The example of a news assistant showcases a practical application of these techniques, highlighting the potential for creating customized AI solutions.

AI summaries can miss context or contain errors. Check important details against the original video.

Go a little deeper.

Have a question about this video? Load its transcript to open the video chat.