AI Personal Learning
and practical guidance
CyberKnife Drawing Mirror

OpenAI Agents SDK: A Python Framework for Building Multi-Intelligence Collaborative Workflows

General Introduction

The OpenAI Agents SDK is a lightweight development tool from OpenAI designed for building multi-intelligent workflows. Based on Python, it's easy to get started, and enables developers to automate complex tasks by configuring Agents, Handoffs, Guardrails, and Tracing. For example, you can use it to build an assistant that writes code or an intelligent system that handles multilingual interactions. Its highlight is its compatibility and flexibility with all model providers that support the OpenAI Chat Completions API format. Detailed documentation and sample code are officially provided for developers of all levels to get started quickly.

OpenAI Agents SDK: A Python Framework for Building Multi-Intelligence Collaborative Workflows-1


 

Function List

  • Smart Body Customization: Set commands, tools, and restrictions for language models to create proprietary intelligent assistants.
  • Task switching: Supports seamless task switching between intelligences, such as going from the English assistant to the Spanish assistant.
  • safety certification: Built-in input and output checks ensure safe and reliable results.
  • Operational tracking: Automatically record the operation process of the smart body for easy debugging and optimization.
  • Tool Extension: Allows the addition of customization tools, such as checking the weather or handling specific tasks.
  • model compatibility: Multiple model providers are supported, as long as they conform to the OpenAI API format.

 

Using Help

Installation process

To use the OpenAI Agents SDK, you first need to set up a Python environment. Here are the steps:

  1. Creating a Virtual Environment
    Create a separate Python environment to avoid conflicts with other projects by typing the following command in the terminal:
python -m venv env

Then activate the environment:

  • Windows:env\Scripts\activate
  • Mac/Linux systems:source env/bin/activate
  1. Installing the SDK
    After activating the environment, run the following command to install the SDK:
pip install openai-agents
  1. Configuring API Keys
    Before running the code, you need to set the OpenAI API key. Enter it in the terminal:
export OPENAI_API_KEY='Your key'

Or load it in the code via an environment variable. Take care to secure the key.

Once you have completed the above steps, you are ready to start using the tool.

How to use

At the heart of the OpenAI Agents SDK are Agent cap (a poem) Runner Class.Agent used to define the behavior of the intelligences.Runner Responsible for executing and returning results. The following is a detailed description of the operation of the main functions.

1. Creating a basic intelligent assistant

Want to get started quickly? Try this simple example of an intelligent body writing a haiku about programming:

from agents import Agent, Runner
agent = Agent(name="Assistant", instructions="You are a helpful assistant")
result = Runner.run_sync(agent, "Write a haiku about recursion in programming.")
print(result.final_output)

procedure::

  • Create an Intelligence, set the name and commands.
  • expense or outlay Runner.run_sync Synchronize the run and enter the task requirements.
  • Running it will output something like, "Code in code, function calls itself, infinite loop dance."

2. Realization of task switching

Task switching is a feature of the SDK that allows multiple intelligences to collaborate. For example, handling multi-language requests:

from agents import Agent, Runner
import asyncio
spanish_agent = Agent(name="Spanish agent", instructions="You only speak Spanish.")
english_agent = Agent(name="English agent", instructions="You only speak English.")
triage_agent = Agent(
name="Triage agent", instructions="Handoff to the appropriate agent based on the language.
name="Triage agent", instructions="Handoff to the appropriate agent based on the language of the request.", handoffs=[spanish_agent
instructions="Handoff to the appropriate agent based on the language of the request.", handoffs=[spanish_agent, english_agent],)
)
async def main(): result = await Runner.
result = await Runner.run(triage_agent, input="Hola, ¿cómo estás?")
print(result.final_output)
if __name__ == "__main__".
asyncio.run(main())

procedure::

  • Define multiple intelligences, each with specific instructions.
  • Creating the primary intelligence (triage_agent), and specify switchable intelligences.
  • expense or outlay Runner.run Running asynchronously and entering Spanish issues.
  • The main intelligent body will switch according to the language spanish_agent, returning something like, "¡Hola! Estoy bien, gracias por preguntar. ¿Y tú, cómo estás?"

3. Adding customized tools

You can use function_tool Define tools that allow intelligences to call external functions. For example, checking the weather:

from agents import Agent, Runner, function_tool
@function_tool
def get_weather(city: str) -> str:
return f"The weather in {city} is sunny."
agent = Agent(
name="Weather agent",
instructions="You are a helpful agent.",
tools=[get_weather],
)
async def main():
result = await Runner.run(agent, input="What's the weather in Tokyo?")
print(result.final_output)
if __name__ == "__main__":
asyncio.run(main())

procedure::

  • Define a function with @function_tool Decoration.
  • Adding tools to an intelligent body's tools Parameters.
  • After running, the Intelligent Society calls the tool and returns, "The weather in Tokyo is sunny."

4. Use of runtracking

The SDK has a built-in tracking feature that records the details of the smart body's actions with each run. You can find out more about the operation of the smartbody by using the result Objects to view running processes or to integrate with external tools (e.g. Logfire) to analyze them. Works by default with no additional settings.

5. Setting up security authentication

Security validation (Guardrails) can check inputs and outputs. For example, preventing a user from having an intelligent do math homework:

from agents import Agent, Runner, input_guardrail
from pydantic import BaseModel
class MathCheck(BaseModel).
is_math_homework: bool
reasoning: str
guardrail_agent = Agent(
name="Guardrail check",
instructions="Check if the user is asking you to do their math homework.
output_type=MathCheck, )
)
@input_guardrail
async def math_guardrail(ctx, agent, input): result = await Runner.run(runner.check, )
result = await Runner.run(guardrail_agent, input)
return GuardrailFunctionOutput(
output_info=result.final_output,
tripwire_triggered=result.final_output.is_math_homework, )
)
agent = Agent(
name="Support agent",
instructions="Help customers with their questions.",
input_guardrails=[math_guardrail], )
)
async def main():: asynchronous(): asynchronous(): asynchronous()
async def main(): try.
await Runner.run(agent, "Can you solve 2x + 3 = 11?")
except Exception as e: print("Guardrail stopped the
print("Guardrail stopped the request")

procedure::

  • Define a guard intelligence that checks the input.
  • expense or outlay @input_guardrail Create the guard function.
  • Add the guards to the main intelligence. If the input involves math homework, the run will be interrupted.

Operation process details

  • intelligent circulation: Call Runner.run() The SDK performs the following steps:
    1. Invoke the language model to generate a response.
    2. Check for tool calls or task switches.
    3. Execute the tool call and log the results.
    4. If there is a task switch, jump to the new smart body.
    5. Loop until the final output is generated, or until the max_turns Cap.
  • Output type: If you set the output_type, the intelligence will generate structured output of the specified type; otherwise, the response to the first tool-less call or switch is the final output.

Recommendations for use

  • adjust components during testing: View every step in detail with the tracking feature to quickly pinpoint problems.
  • make superior: Set up according to the needs of the mission max_turns, avoiding too much cycling.
  • extensions: Refer to official examples folder to explore more uses.

Official Documentation: https://openai.github.io/openai-agents-python/

CDN1
May not be reproduced without permission:Chief AI Sharing Circle " OpenAI Agents SDK: A Python Framework for Building Multi-Intelligence Collaborative Workflows

Chief AI Sharing Circle

Chief AI Sharing Circle specializes in AI learning, providing comprehensive AI learning content, AI tools and hands-on guidance. Our goal is to help users master AI technology and explore the unlimited potential of AI together through high-quality content and practical experience sharing. Whether you are an AI beginner or a senior expert, this is the ideal place for you to gain knowledge, improve your skills and realize innovation.

Contact Us
en_USEnglish