Get Started with Python
This guide will walk you through creating and deploying your first AI agent using the aiXplain Python SDK. You'll learn how to specify a Large Language Model (LLM), equip the agent with tools, and integrate the agent into your application.
Setup
Set an aiXplain API Key
Generate an API key from the API keys page. Once generated, you can use it in one of two ways:
- Export it as an environment variable in your terminal or
- Set it in your Python project using either environment variables or a
.env
file withpython-dotenv
.
- MacOS / Linux
- Windows
export AIXPLAIN_API_KEY="<YOUR_API_KEY>"
setx AIXPLAIN_API_KEY="<YOUR_API_KEY>"
- os module
- .env (via dotenv)
import os
os.environ["AIXPLAIN_API_KEY"] = "<YOUR_API_KEY>"
from dotenv import find_dotenv, load_dotenv # pip install python-dotenv
load_dotenv(find_dotenv()) # Load environment variables from .env file
Install the aiXplain SDK
pip install aixplain
Create an Agent
Agents are defined using a name
, description
, instructions
and any tools
you equip the agent with.
The agent below is eqipped with Open Weather API tool. Find more in aiXplain's marketplace.
from aixplain.factories import AgentFactory
from aixplain.modules.agent.tool.model_tool import ModelTool
Tool = "66f83c216eb563266175e201" #Open Weather API tool
weather_agent = AgentFactory.create(
name="Weather Agent",
description="Agent for weather updates.",
instructions="An agent that answers queries about the current weather.",
tools=[
ModelTool(model=Tool),
],
)
Only the instructions
field influences an agent's behavior. The name
and description
are metadata and do not affect execution.
aiXplain agents are currently optimized for OpenAI (GPT-4o Mini default) and Meta models. However, you can override the default llm
with your preferred model from a collection of 170+ LLMs in the aiXplain marketplace.
Run the agent
response = weather_agent.run("What is the weather in Liverpool, UK?")
response.data.output
Enable conversational memory
Retrieve the session_id
from the first query and include it in subsequent requests to maintain the agent’s conversation history.
session_id = response.data.session_id
response = weather_agent.run("Is that too cold for shorts?", session_id=session_id)
response.data.output
Deploy the agent
Creating an agent generates a draft agent with a temporary endpoint URL that expires after 24 hours. Draft agents are ideal for development and debugging.
To make an agent permanent, you need to deploy it. By default, agents are deployed on aiXplain’s multi-tenant AWS cloud environment.
weather_agent.deploy()
Create a Team Agent
A Team Agent orchestrates multiple agents to execute multi-step plans collaboratively.
Add tools
The agentic framework supports different types of tools. This section demonstrates how to list, try and add an AI model as an agent tool.
List models
from aixplain.factories import ModelFactory
from aixplain.enums import Function
# List text generation models
model_list = ModelFactory.list(function=Function.TEXT_GENERATION, page_size=50)["results"]
for model in model_list:
print(model.id, model.name, model.supplier)
Alternatively, browse models and other assets in the marketplace.
Try the model
In this example, we will use the Google Search by Scale SERP model.
from aixplain.factories import ModelFactory
scrape_model = ModelFactory.get("66f423426eb563fa213a3531") # Scraper Website Tool
result = scrape_model.run("https://www.bbc.com/news")
display(result.data)
Create a Scraper Agent
Create a scraper agent to scrape websites.
# Web scraper agent
from aixplain.factories import AgentFactory
scraper_agent = AgentFactory.create(
name="Scraper Agent",
description="An agent that answers queries using website scraping.",
instructions="Scrapes websites to extract information",
tools=[AgentFactory.create_model_tool(
model=scrape_model,
description="The input query of this tool must be of the form 'text': 'https://website.com'.")]
)
print(scraper_agent.name, scraper_agent.id)
Run and Deploy the Agent
from aixplain.modules.agent import OutputFormat
response = scraper_agent.run("What are the latest interesting news listed at https://www.cnn.com/'?", output_format=OutputFormat.MARKDOWN)
print(response.data["output"])
Deploy the agent
scraper_agent.deploy()
Create a Web Wiki Agent
Create a web wiki agent to answer questions using wikipedia
from aixplain.modules.agent.tool.model_tool import ModelTool
# Wiki agent
wiki_agent = AgentFactory.create(
name="Wiki Agent",
description="An agent that answers queries using wikipedia.",
instructions="Queries wikipedia to answer user questions",
tools=[ModelTool(model="6633fd59821ee31dd914e232")] # Wikipedia utility
)
print(wiki_agent.name, wiki_agent.id)
Run and Deploy the Agent
response = wiki_agent.run("How is the friendship paradox described?")
print(response.data["output"])
Deploy the agent
wiki_agent.deploy()
Create a Team Agent
Create a Web and Wiki Team Agent with the scraper agent
and wiki agent
.
from aixplain.factories import TeamAgentFactory
team = TeamAgentFactory.create(
name="Wiki and Web Team Agent",
description="You take user queries and search them using wiki or by web scraping URLs if appropriate.",
instructions="You take user queries and search them using wiki or by web scraping URLs if appropriate.",
agents=[scraper_agent, wiki_agent],
llm_id="6646261c6eb563165658bbb1" # GPT-4o
)
print(team.__dict__) # Team agent attributes
# Run the team agent for the first time
result = team.run("Tell me about OpenAI. They have a website, https://openai.com/.") # run team agent
print(result['data']['output'])
# Use the same `session ID` to maintain conversational context
session = result["data"]["session_id"]
result = team.run("Who is the founder?",session_id=session)
print(result['data']['output'])
# Deploy the team agent to make it production-ready
team.deploy()
Next – Customize your agent
Now that you’ve seen how agents can collaborate as a team, let’s dive deeper into how you can customize individual agents even further. From refining instructions and selecting tools to configuring memory, guardrails, and model behavior—agents on aiXplain are highly adaptable to your specific use case.