How to Use the Superagent Python SDK

Jun 11, 2024 | Educational

The Superagent Python SDK is an open-source framework that empowers developers to seamlessly integrate production-ready AI assistants into their applications. Within minutes, you can have a sophisticated AI assistant at your disposal, transforming the user experience of your application.

Installation

To begin your journey with Superagent, you’ll need to add the dependency to your project’s build file. This can be done by executing one of the following commands in your terminal:

pip install superagent-py
poetry add superagent-py

Using Superagent

Once installed, you can start utilizing the SDK in your projects. Think of creating an AI assistant as crafting a new friend who understands your needs. Here’s how you can summon your new assistant:

  • First, import the necessary module:
  • from superagent.client import Superagent
  • Next, initialize the client:
  • client = Superagent(token=API_TOKEN, base_url="https://api.beta.superagent.sh")
  • Then, create an agent by giving it a name and purpose:
  • agent = client.agent.create(request={
        "name": "My Agent",
        "description": "My awesome agent",
        "isActive": True,
        "llmModel": "GPT_4_1106_PREVIEW",
        "prompt": "You are a helpful assistant"
    })
  • Finally, invoke the AI assistant with input:
  • output = client.agent.invoke(
        agent_id=agent.data.id,
        input="Hi there!",
        enable_streaming=False,
        session_id=123
    )
    print("Received response from superagent:", agent.data)

Asynchronous Handling

To manage requests asynchronously, use a similar approach; just initialize with AsyncSuperagent:

from superagent.client import AsyncSuperagent

agent = await client.agent.create(request={
    "name": "My Agent",
    "description": "My awesome agent",
    "isActive": True,
    "llmModel": "GPT_4_1106_PREVIEW",
    "prompt": "You are a helpful assistant"
})
output = await client.agent.invoke(
    agent_id=agent.data.id,
    input="Hi there!",
    enable_streaming=False,
    session_id=123
)
print("Received response from superagent:", agent.data)

Handling Exceptions

When working with the Superagent SDK, exceptions can occur, much like stepping on a twig while hiking. It’s important to handle those gently:

from superagent.core import ApiError

try:
    client.agents.get(agent_id=12312)
except ApiError as e:
    # handle any API related error

Troubleshooting Tips

If you encounter issues, consider the following:

  • Ensure you’ve installed the package correctly using the provided installation commands.
  • Double-check your API token and base URL to make sure they are correct.
  • If you encounter an ApiError, consult the error status codes provided in the documentation. For instance:
    • 422 – Unprocessable Entity Error

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Additional Notes

This SDK is currently in beta, which means it may undergo changes without prior notice. It is advisable to pin the package version in your pyproject.toml file to avoid breaking updates.

Final Thoughts

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox