Skip to main content

Add your description here

Project description

Blaxel Python SDK

Blaxel

An SDK to connect your agent or tools with Blaxel platform. Currently in preview, feel free to send us feedback or contribute to the project.

Table of Contents

Features

Supported AI frameworks:

  • LangChain
  • LlamaIndex
  • CrewAI
  • OpenAI Agents

Supported Tools frameworks:

  • MCP (Model Context Protocol)

Prerequisites

  • Python: 3.10 or later
  • Blaxel CLI: Ensure you have the Blaxel CLI installed. If not, install it globally:
    curl -fsSL https://raw.githubusercontent.com/beamlit/toolkit/preview/install.sh | BINDIR=$HOME/.local/bin sh
    
  • Blaxel login: Login to Blaxel platform
      bl login YOUR-WORKSPACE
    

Start from an hello world example

bl create-agent-app myfolder
cd myfolder
bl serve --hotreload

Integrate with a custom code

Set-up blaxel observability

It only needs an import of our SDK on top of your main entrypoint file. It will directly plug our backend (when deployed on blaxel) with open telemetry standard.

from blaxel import sdk

Connect tools and model from blaxel platform to your agent

from blaxel.models import bl_model
from blaxel.tools import bl_tools

Then you need to use it in your agent. Here are examples with different frameworks:

# Example with LangChain
from langchain.agents import AgentExecutor, create_react_agent
from langchain.prompts import ChatPromptTemplate
from langchain.tools import Tool
from langchain_core.messages import HumanMessage

async def create_agent():
    model = await bl_model("gpt-4o-mini").to_langchain()
    async with bl_tools(["blaxel-search", "webcrawl"]) as t:
        tools = t.to_langchain()
        tools.append(
            Tool(
                name="weather",
                description="Get the weather in a specific city",
                func=lambda city: f"The weather in {city} is sunny"
            )
        )

        prompt = ChatPromptTemplate.from_messages([
            ("system", "You are a helpful assistant."),
            ("human", "{input}")
        ])

        agent = create_react_agent(model, tools, prompt)
        return AgentExecutor(agent=agent, tools=tools)

# Example with LlamaIndex
from llama_index.core import SimpleDirectoryReader
from llama_index.core.agent import ReActAgent
from llama_index.core.tools import FunctionTool

async def create_llamaindex_agent():
    model = await bl_model("gpt-4o-mini").to_llamaindex()
    async with bl_tools(["blaxel-search", "webcrawl"]) as t:
        tools = t.to_llamaindex()
        tools.append(
            FunctionTool.from_defaults(
                fn=lambda city: f"The weather in {city} is sunny",
                name="weather",
                description="Get the weather in a specific city"
            )
        )

        return ReActAgent.from_tools(
            tools,
            llm=model,
            verbose=True
        )

# Example with CrewAI
from crewai import Agent, Task, Crew

async def create_crewai_agent():
    model = await bl_model("gpt-4o-mini").to_crewai()
    async with bl_tools(["blaxel-search", "webcrawl"]) as t:
        tools = t.to_crewai()
        tools.append(
            Tool(
                name="weather",
                description="Get the weather in a specific city",
                func=lambda city: f"The weather in {city} is sunny"
            )
        )

        agent = Agent(
            role='Assistant',
            goal='Help users with their queries',
            backstory='I am a helpful AI assistant',
            tools=tools,
            llm=model
        )

        return agent

Agent Chaining

You can call an agent from another agent to chain them. This allows complex agentic logic, with multiple agents calling each other, orchestration, routing, etc.

# Example of calling an agent, then putting its result inside a second one
from blaxel.agents import bl_agent

async def first_agent(input_text: str) -> dict:
    # First agent that processes loan applications
    response = await bl_agent("first-agent").run({
        "inputs": input_text
    })
    return response

async def second_agent(input_text: str) -> dict:
    # Second agent that evaluates the loan application
    first_response = await first_agent(input_text)

    model = await bl_model("gpt-4o-mini").to_langchain()
    prompt = ChatPromptTemplate.from_messages([
        ("system", "You are a loan specialist. Based on the given json file with client data, your job is to decide if a client can be further processed."),
        ("human", "{input}")
    ])

    response = await model.ainvoke(first_response)
    return response

Deploy on blaxel

To deploy on blaxel, we have only one requirement in each agent code. We need an HTTP Server.

For example with FastAPI:

from fastapi import FastAPI
from blaxel import sdk
import uvicorn

app = FastAPI()

@app.post("/")
async def root(inputs: str):
    # Your agentic logic here
    return {"response": "Your response here"}

if __name__ == "__main__":
    port = int(os.getenv("BL_SERVER_PORT", "3000"))
    host = os.getenv("BL_SERVER_HOST", "0.0.0.0")
    uvicorn.run(app, host=host, port=port)
bl deploy

Advanced configuration

You can add optionally a configuration file "blaxel.toml" in your project root.

name = "my-agent"
workspace = "my-workspace"
type = "agent"

functions = ["blaxel-search"]
models = ["sandbox-openai"]

It allows to customize the requirements for your agent, it can be useful if you have many models and functions in your workspace.

Create an MCP Server

If you want to create an MCP Server for using it in multiple agents, you can bootstrap it with the following command:

bl create-mcp-server my-mcp-server
cd my-mcp-server
bl serve --hotreload

We follow current standard for tool development over MCP Server. Example of a tool which is sending fake information about the weather:

from blaxel.mcp.server import FastMCP

mcp = FastMCP("My Weather MCP server")

@mcp.tool()
def weather(city: str) -> str:
    """Get the weather for a city"""
    return f"The weather in {city} is sunny"

if __name__ == "__main__":
    if os.getenv("BL_SERVER_PORT"):
      mcp.run(transport="ws")
    else:
      mcp.run(transport="stdio")

Connect an existing MCP Server to blaxel

You need to have a "blaxel.toml" file in your project root:

name = "weather"
workspace = "my-workspace"
type = "function"

Connect the observability layer:

from blaxel import sdk

Update your import of FastMCP

from blaxel.mcp.server import FastMCP

Update your entrypoint to support our transport:

def main():
    mcp.run(transport="ws") if os.getenv("BL_SERVER_PORT") else mcp.run(transport="stdio")

How to use environment variables or secrets

You can use the "blaxel.toml" config file to specify environment variables for your agent:

name = "weather"
workspace = "my-workspace"
type = "function"

[env]
DEFAULT_CITY = "San Francisco"

Then you can use it in your agent or function with the following syntax:

from blaxel.env import env
print(env.DEFAULT_CITY)  # San Francisco

You can also add secrets variables to a .env files in your project root. (goal is to not commit this file)

Example of a .env file:

# Secret variables can be stored here
DEFAULT_CITY_PASSWORD=123456

Then you can use it in your agent or function with the following syntax:

from blaxel.env import env
print(env.DEFAULT_CITY_PASSWORD)  # 123456

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

blaxel-0.1.8.dev11.tar.gz (174.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

blaxel-0.1.8.dev11-py3-none-any.whl (253.4 kB view details)

Uploaded Python 3

File details

Details for the file blaxel-0.1.8.dev11.tar.gz.

File metadata

  • Download URL: blaxel-0.1.8.dev11.tar.gz
  • Upload date:
  • Size: 174.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.13

File hashes

Hashes for blaxel-0.1.8.dev11.tar.gz
Algorithm Hash digest
SHA256 1e819f65d41af7d50960a48e38fdd5c25476a4cfa263bec2d9a9265dcbd3f39c
MD5 90966c55374459a5f0ce77652b84edbe
BLAKE2b-256 0f565cfff4a6d1c1add6713b4d7f85d6149dca52b4d9aa6ca30a286008e31e1e

See more details on using hashes here.

File details

Details for the file blaxel-0.1.8.dev11-py3-none-any.whl.

File metadata

File hashes

Hashes for blaxel-0.1.8.dev11-py3-none-any.whl
Algorithm Hash digest
SHA256 3748507d2ce7ceb60c973d2d7a1e58559c568450977194ed7e972c6abf805f0c
MD5 bca3e99938d533ab5d550190f5678c4a
BLAKE2b-256 411b29af11b6fe5c8cf7559c2e35ecefa9de9dbf39aa946311dc2f8a9b8d5c1a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page