Skip to main content

2. Install the SDK

pip install haliosai

Decorator Pattern

Use the @guarded_chat_completion decorator for automatic guardrail evaluation:
from haliosai import guarded_chat_completion
from openai import AsyncOpenAI

@guarded_chat_completion(agent_id="your-agent-id")
async def call_llm(messages):
    """Decorator automatically handles guardrail evaluation"""
    client = AsyncOpenAI()
    return await client.chat.completions.create(
        model="gpt-4o-mini",
        messages=messages,
        max_tokens=100
    )

Processing Modes

Parallel Processing (Default) - Guardrails run concurrently with LLM:
@guarded_chat_completion(agent_id="your-agent-id")
async def parallel_call(messages):
    # Guardrails run at the same time as LLM call
    # Faster but less conservative
Sequential Processing - Guardrails complete before LLM:
@guarded_chat_completion(agent_id="your-agent-id", concurrent_guardrail_processing=False)
async def sequential_call(messages):
    # Guardrails run before LLM call
    # Safer but potentially slower

Context Manager Pattern

Use HaliosGuard for manual control over guardrail evaluation:
from haliosai import HaliosGuard
from openai import AsyncOpenAI

async def process_with_guardrails():
    async with HaliosGuard(agent_id="your-agent-id") as guard:
        client = AsyncOpenAI()
        messages = [{"role": "user", "content": "Hello!"}]
        
        # Evaluate request before LLM call
        req_result = await guard.evaluate(messages, "request")
        if req_result.get("guardrails_triggered", 0) > 0:
            return "Request blocked by guardrails"
        
        # Proceed with LLM call
        response = await client.chat.completions.create(
            model="gpt-4o-mini",
            messages=messages,
            max_tokens=200
        )
        
        assistant_message = response.choices[0].message.content
        
        # Evaluate response after LLM call
        resp_messages = messages + [{"role": "assistant", "content": assistant_message}]
        resp_result = await guard.evaluate(resp_messages, "response")
        if resp_result.get("guardrails_triggered", 0) > 0:
            return "Response blocked by guardrails"
        
        return assistant_message

Key Features

  • Flexible Processing: Parallel (faster) or sequential (safer) modes
  • Async Support: Full asyncio compatibility with OpenAI client
  • Minimal Changes: Decorator requires almost no code modification
  • Manual Control: Context manager gives full evaluation control
  • Error Handling: Comprehensive error management and logging

Examples

See the SDK examples for different implementation patterns:
I