NexusAI-Client

Cookbooks & Recipes

Practical patterns and production blueprints for web frameworks, chat sessions, and agents.

Cookbooks & Recipes 🍳

Ready-to-use patterns for integrating NexusAI-Client into real-world applications.


1. Generic Single-Prompt Helper

A reusable asynchronous helper function for one-off completions:

import asyncio
from nexusai_client import AIGateway

async def ask_ai(
    prompt: str,
    provider: str = "groq",
    system_prompt: str | None = None,
    temperature: float = 0.3,
) -> str:
    """Send a prompt to any supported AI provider and return the generated text."""
    async with AIGateway(provider=provider) as client:
        response = await client.generate_text(
            prompt=prompt,
            system_prompt=system_prompt,
            temperature=temperature,
        )
        return response.text

# Example
async def main():
    reply = await ask_ai(
        prompt="Suggest 3 catchy domain names for an AI analytics platform.",
        system_prompt="You are a creative branding expert.",
        provider="groq",
    )
    print(reply)

if __name__ == "__main__":
    asyncio.run(main())

2. Stateful Multi-Turn Chat Session

Manage multi-turn dialogues with message history memory:

import asyncio
from nexusai_client import AIGateway, ChatMessage

class AIChatSession:
    """Manages an ongoing multi-turn conversation with memory."""

    def __init__(self, provider: str = "mistral", system_prompt: str = "You are a helpful Python tutor.") -> None:
        self.provider = provider
        self.history: list[ChatMessage] = [
            ChatMessage(role="system", content=system_prompt)
        ]

    async def send_message(self, user_text: str) -> str:
        """Append user message, query AI, and store assistant reply."""
        self.history.append(ChatMessage(role="user", content=user_text))

        async with AIGateway(self.provider) as client:
            response = await client.chat(messages=self.history)
            self.history.append(ChatMessage(role="assistant", content=response.text))
            return response.text

# Example
async def main():
    chat = AIChatSession(provider="mistral")
    
    rep1 = await chat.send_message("How do I initialize an empty dictionary in Python?")
    print(f"Assistant: {rep1}\n")

    rep2 = await chat.send_message("How do I add a key to it?")
    print(f"Assistant (with context): {rep2}")

if __name__ == "__main__":
    asyncio.run(main())

3. Specialized Code Generation (Codestral)

Targeting Mistral's Codestral model for clean, strictly-typed code generation:

import asyncio
from nexusai_client import AIGateway

async def generate_python_code(task_description: str) -> str:
    system = "You are a Principal Software Engineer. Provide clean, PEP 8 compliant, strictly-typed Python code."
    
    async with AIGateway("mistral") as client:
        response = await client.generate_text(
            prompt=f"Task: {task_description}",
            system_prompt=system,
            model="codestral-latest",
            temperature=0.1,
        )
        return response.text

async def main():
    code = await generate_python_code("an asynchronous rate limiter with leaky bucket algorithm")
    print(code)

if __name__ == "__main__":
    asyncio.run(main())

4. FastAPI Endpoint Integration

Deploy a zero-downtime AI microservice with FastAPI:

from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from nexusai_client import AIGateway, NexusAIError, RateLimitError

app = FastAPI(title="NexusAI Microservice")

class GenerationRequest(BaseModel):
    prompt: str
    provider: str = "gemini_free"
    system_prompt: str | None = None
    temperature: float = 0.5

class GenerationResponse(BaseModel):
    result: str
    provider: str
    model: str
    tokens_used: int | None

@app.post("/api/generate", response_model=GenerationResponse)
async def generate_text_endpoint(req: GenerationRequest):
    try:
        # Use auto_fallback for zero downtime
        async with AIGateway.auto_fallback() as client:
            res = await client.generate_text(
                prompt=req.prompt,
                system_prompt=req.system_prompt,
                temperature=req.temperature,
            )
            return GenerationResponse(
                result=res.text,
                provider=res.provider,
                model=res.model,
                tokens_used=res.usage.total_tokens if res.usage else None,
            )
    except RateLimitError as e:
        raise HTTPException(status_code=429, detail=f"Rate limit exceeded: {e.message}")
    except NexusAIError as e:
        raise HTTPException(status_code=500, detail=f"AI Provider error: {e.message}")

# Run with: uvicorn main:app --reload

5. Autonomous AI Agent with Tool Calling

Build full autonomous agent execution loops (ReAct pattern) that query local tools, database functions, or web APIs, and feed the results back to the LLM until reaching a final conclusion:

import asyncio
import json
from nexusai_client import (
    AIGateway,
    ChatMessage,
    FunctionDefinition,
    ToolCall,
    ToolDefinition,
)

# 1. Define your local Python functions
def execute_database_query(sql_query: str) -> str:
    """Simulated local database query execution."""
    print(f"⚡ [Executing SQL]: {sql_query}")
    return json.dumps({
        "status": "success",
        "rows": [
            {"id": 101, "user": "Alice", "plan": "Enterprise", "balance_usd": 4500.00},
            {"id": 102, "user": "Bob", "plan": "Free", "balance_usd": 0.00},
        ]
    })

# 2. Define standard ToolDefinition schema
sql_tool = ToolDefinition(
    function=FunctionDefinition(
        name="execute_database_query",
        description="Execute a read-only SQL query on the PostgreSQL customer database.",
        parameters={
            "type": "object",
            "properties": {
                "sql_query": {
                    "type": "string",
                    "description": "Valid PostgreSQL query, e.g. SELECT * FROM users WHERE plan = 'Enterprise'",
                }
            },
            "required": ["sql_query"],
        },
    )
)

TOOL_REGISTRY = {
    "execute_database_query": execute_database_query,
}

# 3. Autonomous ReAct Agent Loop
async def run_sql_agent(user_question: str) -> str:
    messages: list[ChatMessage] = [
        ChatMessage(
            role="system",
            content="You are an expert data analyst assistant. Use the provided tools to answer user database questions.",
        ),
        ChatMessage(role="user", content=user_question),
    ]

    # Auto-fallback ensures your agent stays alive even if one provider hits rate limits
    async with AIGateway.auto_fallback() as client:
        # Step 1: Initial model call with tools
        response = await client.chat(messages=messages, tools=[sql_tool])

        # Step 2: Handle model tool requests
        if response.has_tool_calls:
            # Append assistant message with requested tool calls to dialogue history
            messages.append(
                ChatMessage(
                    role="assistant",
                    content=response.text,
                    tool_calls=response.tool_calls,
                )
            )

            # Execute each requested tool locally
            for tool_call in response.tool_calls:
                fn = TOOL_REGISTRY.get(tool_call.name)
                if fn:
                    tool_output = fn(**tool_call.arguments)
                    
                    # Feed tool execution output back as a 'tool' role message
                    messages.append(
                        ChatMessage(
                            role="tool",
                            name=tool_call.name,
                            tool_call_id=tool_call.id,
                            content=tool_output,
                        )
                    )

            # Step 3: Second model call to summarize the final answer with tool outputs
            final_response = await client.chat(messages=messages)
            return final_response.text

        return response.text

# --- Example Run ---
async def main():
    result = await run_sql_agent("What is the account balance and subscription plan for Alice?")
    print("\n🤖 Final Agent Answer:\n" + result)

if __name__ == "__main__":
    asyncio.run(main())

6. CLI Diagnostic Utilities

NexusAI-Client includes CLI diagnostic tools to audit your accesses and explore live models:

Real-Time Access & Latency Verification

uv run python verify_access.py

Validates .env keys, inspects real-time balances, tests inference, and measures network latency in milliseconds.

Live Catalog Explorer (670+ Models)

# List free-tier models only
uv run python list_all_models.py --free

# Search by keyword (e.g., llama, r1, command, sonnet)
uv run python list_all_models.py --search llama

# Export complete catalog with pricing to JSON
uv run python list_all_models.py --export models_catalog.json

On this page