Skip to main content

A Python client library for interacting with the Lyzr Agent API

Project description

Lyzr Python SDK

PyPI - Python Version PyPI License: MIT

A comprehensive Python client library for interacting with the Lyzr Agent API. This SDK provides a convenient and structured way to access all Lyzr API functionalities, including managing agents, tools, providers, workflows, and handling inference requests.

Table of Contents

Installation

Install the package using pip:

pip install lyzr-python-sdk

Quick Start

from lyzr_python_sdk import LyzrAgentAPI
import os

# Initialize the client
api_key = os.environ.get("LYZR_AGENT_API_KEY")
client = LyzrAgentAPI(api_key=api_key)

# Get all agents
agents = client.agents.get_agents()
print(f"Found {len(agents)} agents")

# Chat with an agent
chat_response = client.inference.chat({
    "user_id": "user@example.com",
    "agent_id": "your-agent-id",
    "message": "Hello! How can you help me?",
    "session_id": "session-123"  # Optional for conversation continuity
})
print(chat_response)

Authentication

The SDK requires a Lyzr API key for authentication. You can obtain your API key from the Lyzr dashboard.

# Method 1: Environment variable (recommended)
import os
api_key = os.environ.get("LYZR_AGENT_API_KEY")
client = LyzrAgentAPI(api_key=api_key)

# Method 2: Direct initialization
client = LyzrAgentAPI(api_key="your-api-key-here")

## API Reference

### Agents

The `client.agents` provides methods to manage agents.

#### `get_agents()`
Get all agents associated with your API key.

```python
agents = client.agents.get_agents()

create_agent(agent_config: dict)

Create a new agent.

agent_config = {
    "template_type": "single_task",
    "name": "My Assistant",
    "description": "A helpful AI assistant",
    "agent_role": "Assistant",
    "agent_instructions": "You are a helpful assistant that provides accurate information",
    "agent_goal": "Help users with their questions and tasks",
    "features": [],
    "tool": "",
    "tool_usage_description": "",
    "response_format": {
        "type": "text"
    },
    "provider_id": "OpenAI",
    "model": "gpt-4o",
    "top_p": "0.9",
    "temperature": "0.7",
    "managed_agents": [],
    "llm_credential_id": "lyzr_openai"
}
agent = client.agents.create_agent(agent_config)

create_single_task_agent(agent_config: dict)

Create a single-task agent using a template.

agent_config = {
    "template_type": "single_task",
    "name": "Task Specialist",
    "description": "Specialized agent for specific tasks",
    "agent_role": "Specialist",
    "agent_instructions": "Focus on completing the assigned task efficiently",
    "agent_goal": "Complete specific tasks with high accuracy",
    "features": [],
    "tool": "",
    "tool_usage_description": "",
    "response_format": {
        "type": "text"
    },
    "provider_id": "OpenAI",
    "model": "gpt-4o",
    "top_p": "0.9",
    "temperature": "0.7",
    "managed_agents": [],
    "llm_credential_id": "lyzr_openai"
}
agent = client.agents.create_single_task_agent(agent_config)

get_agent(agent_id: str)

Get details of a specific agent.

agent = client.agents.get_agent("agent-123")

update_agent(agent_id: str, agent_config: dict)

Update an existing agent.

update_config = {
    "name": "Updated Assistant",
    "description": "An updated helpful AI assistant",
    "agent_role": "Senior Assistant",
    "agent_instructions": "You are an experienced assistant with enhanced capabilities",
    "agent_goal": "Provide comprehensive help with complex tasks",
    "features": [],
    "tool": "",
    "tool_usage_description": "",
    "response_format": {
        "type": "text"
    },
    "provider_id": "OpenAI",
    "model": "gpt-4o",
    "top_p": "0.8",
    "temperature": "0.5",
    "managed_agents": [],
    "llm_credential_id": "lyzr_openai"
}
result = client.agents.update_agent("agent-123", update_config)

delete_agent(agent_id: str)

Delete an agent.

result = client.agents.delete_agent("agent-123")

list_agent_versions(agent_id: str)

List all versions of an agent.

versions = client.agents.list_agent_versions("agent-123")

get_agent_version(agent_id: str, version_id: str)

Get a specific version of an agent.

version = client.agents.get_agent_version("agent-123", "version_id")

activate_agent_version(agent_id: str, version_id: str)

Activate a specific agent version.

result = client.agents.activate_agent_version("agent-123", "version_id")

update_single_task_agent(agent_id: str, agent_config: dict)

Update a single-task agent.

update_config = {
    "name": "Updated Task Agent",
    "description": "Enhanced task-focused agent",
    "agent_role": "Task Expert",
    "agent_instructions": "You are an expert at handling specific tasks with precision",
    "agent_goal": "Execute tasks with maximum efficiency and accuracy",
    "features": [],
    "tool": "",
    "tool_usage_description": "",
    "response_format": {
        "type": "json"
    },
    "provider_id": "OpenAI",
    "model": "gpt-4o",
    "top_p": "0.9",
    "temperature": "0.3",
    "managed_agents": [],
    "llm_credential_id": "lyzr_openai"
}
result = client.agents.update_single_task_agent("agent-123", update_config)

Tools

The client.tools provides methods to manage tools and integrations.

get_tools()

Get all tools for the user.

tools = client.tools.get_tools()

create_tool(tool_data: dict)

Create a new tool from an OpenAPI schema.

tool_data = {
    "tool_set_name": "my-api-tools",
    "openapi_schema": {...},  # Your OpenAPI schema
    "default_headers": {"Authorization": "Bearer token"},
    "enhance_descriptions": True
}
tool = client.tools.create_tool(tool_data)

get_tool(tool_id: str)

Get details of a specific tool.

tool = client.tools.get_tool("tool-123")

get_tool_info(tool_id: str)

Get information about an OpenAPI tool.

info = client.tools.get_tool_info("tool-123")

toggle_tool(tool_id: str, enabled: bool)

Enable or disable a tool.

# Enable tool
result = client.tools.toggle_tool("tool-123", True)

# Disable tool
result = client.tools.toggle_tool("tool-123", False)

delete_tool(tool_id: str)

Delete a tool.

result = client.tools.delete_tool("tool-123")

Composio Integration Methods

get_composio_tools()

Get available Composio tools.

composio_tools = client.tools.get_composio_tools()

get_composio_connected_accounts()

Get connected Composio accounts.

accounts = client.tools.get_composio_connected_accounts()

delete_composio_connection(connection_id: str)

Delete a Composio connection.

result = client.tools.delete_composio_connection("conn-123")

Providers

The client.providers provides methods to manage AI model providers and credentials.

create_provider(provider_body: dict)

Create a new provider.

provider_body = {
    "vendor_id": "openai",
    "type": "llm",
    "form": {
        "api_key": "your-openai-key"
    }
}
provider = client.providers.create_provider(provider_body)

create_lyzr_provider(provider_body: dict)

Create a Lyzr provider.

provider_body = {
    "type": "llm",
    "metadata": {
        "model": "gpt-4"
    }
}
provider = client.providers.create_lyzr_provider(provider_body)

get_providers(provider_type: str)

Get providers by type.

llm_providers = client.providers.get_providers("llm")

get_provider(provider_id: str)

Get a specific provider.

provider = client.providers.get_provider("provider-123")

delete_provider(provider_id: str)

Delete a provider.

result = client.providers.delete_provider("provider-123")

Provider Credentials

create_provider_credential(credential_data: dict)

Create a provider credential.

credential_data = {
    "provider_id": "provider-123",
    "name": "OpenAI Credential",
    "config": {"api_key": "sk-..."}
}
credential = client.providers.create_provider_credential(credential_data)

create_bigquery_credential(credential_data: str, service_account_json_file_path: str)

Create a BigQuery credential with service account file.

credential_data = '{"project_id": "my-project"}'
credential = client.providers.create_bigquery_credential(
    credential_data,
    "/path/to/service-account.json"
)

create_file_upload_credential(credential_data: str, files: list)

Create a file upload credential.

credential_data = '{"name": "File Upload"}'
files = ["/path/to/file1.txt", "/path/to/file2.pdf"]
credential = client.providers.create_file_upload_credential(credential_data, files)

get_provider_credential(credential_id: str)

Get a provider credential.

credential = client.providers.get_provider_credential("cred-123")

update_provider_credential(credential_id: str, update_data: dict)

Update a provider credential.

update_data = {
    "config": {"api_key": "new-key"}
}
result = client.providers.update_provider_credential("cred-123", update_data)

delete_provider_credential(credential_id: str)

Delete a provider credential.

result = client.providers.delete_provider_credential("cred-123")

get_all_credentials(provider_type: str, provider_id: str)

Get all credentials for a provider.

credentials = client.providers.get_all_credentials("llm", "provider-123")

get_all_credentials_by_type(provider_type: str)

Get all credentials by type.

llm_credentials = client.providers.get_all_credentials_by_type("llm")

Inference

The client.inference provides methods for AI inference and chat operations.

chat(chat_request: dict)

Chat with an agent.

chat_request = {
    "user_id": "user@example.com",
    "agent_id": "agent-123",
    "message": "Hello, how can you help me?",
    "session_id": "session-456"
}
response = client.inference.chat(chat_request)

get_response(agent_id: str, request_body: dict)

Generate a response from an agent.

request_body = {
    "messages": [
        {"role": "user", "content": "What is AI?"}
    ],
    "response_format": "json"  # Optional
}
response = client.inference.get_response("agent-123", request_body)

task(chat_request: dict)

Create a chat task (asynchronous).

chat_request = {
    "user_id": "user@example.com",
    "agent_id": "agent-123",
    "message": "Hello, how can you help me?",
    "session_id": "session-456" 
}
task = client.inference.task(chat_request)
task_id = task["task_id"]

task_status(task_id: str)

Get the status of a task.

status = client.inference.task_status("task-123")
print(f"Status: {status['status']}")
if status['status'] == 'completed':
    print(f"Result: {status['result']}")

stream_chat(chat_request: dict)

Stream chat with an agent (returns raw response for streaming).

chat_request = {
    "user_id": "user@example.com",
    "agent_id": "agent-123",
    "message": "Hello, how can you help me?",
    "session_id": "session-456" 
}
response = client.inference.stream_chat(chat_request)

# Process streaming response
for line in response.iter_lines():
    if line:
        print(line.decode('utf-8'))

Workflows

The client.workflow provides methods to manage and execute workflows.

list_workflows()

List all workflows.

workflows = client.workflow.list_workflows()

create_workflow(workflow_create_data: dict)

Create a new workflow.

workflow_data = {
    "name": "Data Processing Workflow",
    "description": "Process and analyze data",
    "steps": [
        {"type": "data_input", "config": {...}},
        {"type": "analysis", "config": {...}}
    ]
}
workflow = client.workflow.create_workflow(workflow_data)

get_workflow(flow_id: str)

Get a specific workflow.

workflow = client.workflow.get_workflow("workflow-123")

update_workflow(flow_id: str, workflow_update_data: dict)

Update a workflow.

update_data = {
    "name": "Updated Workflow Name",
    "description": "Updated description"
}
result = client.workflow.update_workflow("workflow-123", update_data)

delete_workflow(flow_id: str)

Delete a workflow.

result = client.workflow.delete_workflow("workflow-123")

execute_workflow(flow_id: str, input_data: dict)

Execute a workflow.

input_data = {
    "data_source": "file.csv",
    "parameters": {"threshold": 0.8}
}
result = client.workflow.execute_workflow("workflow-123", input_data)

Examples

Complete Agent Interaction Example

from lyzr_python_sdk import LyzrAgentAPI
import os

# Initialize client
client = LyzrAgentAPI(api_key=os.environ.get("LYZR_AGENT_API_KEY"))

# Create an agent
agent_config = {
    "template_type": "single_task",
    "name": "Data Analyst",
    "description": "Expert data analysis assistant",
    "agent_role": "Data Analyst",
    "agent_instructions": "You are an expert data analyst who provides insights from data",
    "agent_goal": "Analyze data and provide actionable insights",
    "features": [],
    "tool": "",
    "tool_usage_description": "",
    "response_format": {
        "type": "text"
    },
    "provider_id": "OpenAI",
    "model": "gpt-4o",
    "top_p": "0.9",
    "temperature": "0.7",
    "managed_agents": [],
    "llm_credential_id": "lyzr_openai"
}
agent = client.agents.create_agent(agent_config)
agent_id = agent["agent_id"]

# Chat with the agent
chat_response = client.inference.chat({
    "user_id": "user@example.com",
    "agent_id": agent_id,
    "message": "Hello, how can you help me?",
    "session_id": "session-456" 
})

print(f"Agent response: {chat_response['response']}")

# Create and execute a task
task = client.inference.task({
    "user_id": "user@example.com",
    "agent_id": agent_id,
    "message": "Generate a comprehensive sales report for Q4",
    "session_id": "session-456" 
})

# Check task status
import time
while True:
    status = client.inference.task_status(task["task_id"])
    if status["status"] == "completed":
        print(f"Task completed: {status['result']}")
        break
    elif status["status"] == "failed":
        print(f"Task failed: {status['result']}")
        break
    time.sleep(2)

Tool Integration Example

# Create a tool from OpenAPI schema
tool_data = {
    "tool_set_name": "weather-api",
    "openapi_schema": {
        "openapi": "3.0.0",
        "info": {"title": "Weather API", "version": "1.0.0"},
        "paths": {
            "/weather": {
                "get": {
                    "summary": "Get weather data",
                    "parameters": [
                        {
                            "name": "city",
                            "in": "query",
                            "required": True,
                            "schema": {"type": "string"}
                        }
                    ]
                }
            }
        }
    },
    "default_headers": {"X-API-Key": "your-weather-api-key"}
}

tool = client.tools.create_tool(tool_data)
tool_id = tool["tool_ids"][0]

# Execute the tool
weather_data = client.tools.execute_tool(
    tool_id=tool_id,
    path="/weather",
    method="GET",
    params={"city": "New York"}
)

print(f"Weather data: {weather_data}")

Error Handling

The SDK raises exceptions for API errors. Always wrap API calls in try-catch blocks:

try:
    agents = client.agents.get_agents()
except Exception as e:
    print(f"Error fetching agents: {e}")

Contributing

We welcome contributions! Please see our Contributing Guide for details.

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests
  5. Submit a pull request

License

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

Support

Changelog

v0.1.1

  • Fixed package import structure
  • Improved error handling
  • Added comprehensive documentation

v0.1.0

  • Initial release
  • Core API client functionality
  • Support for Agents, Tools, Providers, Inference, and Workflows

Project details


Download files

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

Source Distribution

lyzr_python_sdk-0.1.5.tar.gz (18.8 kB view details)

Uploaded Source

Built Distribution

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

lyzr_python_sdk-0.1.5-py3-none-any.whl (19.7 kB view details)

Uploaded Python 3

File details

Details for the file lyzr_python_sdk-0.1.5.tar.gz.

File metadata

  • Download URL: lyzr_python_sdk-0.1.5.tar.gz
  • Upload date:
  • Size: 18.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for lyzr_python_sdk-0.1.5.tar.gz
Algorithm Hash digest
SHA256 01bd574135d592050fb8e2891cdcf901aff9cc99a1cc9b6b2b6e917b83501a45
MD5 6af93218b396329f78abef1f969c3394
BLAKE2b-256 3922bd22f7ad24855dcfc0e45ce92b32f32966e0a7058896ef8e6b53517d8011

See more details on using hashes here.

File details

Details for the file lyzr_python_sdk-0.1.5-py3-none-any.whl.

File metadata

File hashes

Hashes for lyzr_python_sdk-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 08ce0b13d3e34727904fef2c19a852de8667fd8f4e0ae438eda721c1328633fd
MD5 27ae5daf4997e7de301523d86d3278da
BLAKE2b-256 e9a071fc9735ab97a9f89821cba6ef34121b13517ab96eb22c4b84c3a4a767ac

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