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:
- Import necessary modules from the
Agentspackage. - Initialize the
OpenAIChatCompletionModelwithllama3:latestas the model name and the Ollama base URL (http://localhost:11434/v1). - Create an
agentRunnerinstance, providing a name, instructions, and the model. - Run the agent synchronously using
run_syncwith a specific prompt. - Print the result.
- Import necessary modules from the
- 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):
- Import
DDGSfromduckduckgo_searchandfunction_toolfromagents. - Define a function
get_news_articlethat takes a topic as input. - Use the
@function_tooldecorator to register the function as a tool. - Inside the function, use
DDGS().news()to search for news articles related to the topic, limiting the results to the current date. - Return the titles and bodies of the articles as a string.
- Import
- 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):
- Define two agents:
news_agentandeditor_agent. - The
news_agentis equipped with theget_news_articletool. - Create a
run_news_workflowfunction that takes a topic as input. - The workflow involves running the
news_agentto fetch news articles and then passing the raw news to theeditor_agentfor rewriting. - The final edited news article is returned.
- Define two agents:
- Workflow: The
run_news_workflowfunction orchestrates the interaction between the two agents. The output from thenews_agent(raw news articles) is fed as input to theeditor_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:
- Import
chainlitand therun_news_workflowfunction. - Use the
@cl.on_messagedecorator to define a function that runs when a message is received. - Inside the function, call
run_news_workflowwith the message content as the topic. - Send the result back to the user using
cl.Message(content=result).send(). - Use the
@cl.on_chat_startdecorator to define a function that runs when the chat starts, sending a welcome message.
- Import
- Functionality: The UI allows users to type a topic, which is then passed to the
run_news_workflowfunction. 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.