Ogx Client Python Library

repository·main·Indexed 41 days ago

https://github.com/ogx-ai/ogx-client-python

The official Python library for the ogx-client API, providing a type-safe interface for interacting with the REST API. Version 1.1.0-alpha.3 supports synchronous and asynchronous workflows (via OgxClient and AsyncOgxClient), SSE streaming, and automatic retries. It includes resources for managing chat completions, embeddings, prompt templates, conversations, and vector stores, as well as an interactive agent CLI for exploring server-side tool execution.

Tokens
23.9K
Snippets
98
Records
121
Agent score
52%

What's inside Ogx Client

  1. Observe Agent Turn and Step Events

    main

    The Interactive Agent CLI uses AgentEventLogger to visualize the lifecycle of an agent interaction. When running the CLI, you can observe:

    • Turn lifecycle: The transition from TurnStarted to TurnCompleted.
    • Inference steps: Visual indicators of when the model is actively thinking or generating text.
    • Tool execution steps: Real-time updates when server-side tools (e.g., knowledge_search or file_search) are being executed.
    • Step metadata: Information regarding whether tools are being executed server_side or client_side.
    • Real-time streaming: Text generation appearing as it happens.
  2. Handle paginated list results

    main

    List methods are paginated. The library provides two ways to handle this:

    1. Auto-pagination: Use the returned iterator to automatically fetch subsequent pages as you iterate.
    2. Manual pagination: Use .has_next_page(), .next_page_info(), or .get_next_page() for granular control over page fetching.
    # Auto-pagination (Sync)
    for response in client.responses.list():
        print(response)
    
    # Auto-pagination (Async)
    async for response in client.responses.list():
        print(response)
    
    # Manual pagination (Async)
    first_page = await client.responses.list()
    if first_page.has_next_page():
        next_page = await first_page.get_next_page()
  3. Differentiate between `null` and missing fields in API responses

    main

    In API responses, a field that is explicitly null or missing entirely will both result in None in the library. To distinguish between them, check if the field name exists in the .model_fields_set attribute of the response model.

    if response.my_field is None:
      if 'my_field' not in response.model_fields_set:
        print('Got json like {}, without a "my_field" key present at all.')
      else:
        print('Got json like {"my_field": null}.')
  4. Basic usage with OgxClient

    main

    Use OgxClient for synchronous API calls. You can provide an api_key as a keyword argument, but it is recommended to set the OGX_CLIENT_API_KEY environment variable in a .env file to avoid storing secrets in source control.

    from ogx_client import OgxClient
    
    client = OgxClient()
    
    models = client.models.list()
  5. Async usage with AsyncOgxClient

    main

    For asynchronous applications, import AsyncOgxClient and await your API calls. The functionality is identical to the synchronous client.

    import asyncio
    from ogx_client import AsyncOgxClient
    
    client = AsyncOgxClient()
    
    async def main() -> None:
        models = await client.models.list()
    
    asyncio.run(main())
  6. Enable logging for the Ogx Client

    main

    The library uses the standard Python logging module. You can enable logging by setting the OGX_CLIENT_LOG environment variable to info or debug (for more verbose output).

    $ export OGX_CLIENT_LOG=info
  7. Run the Interactive Agent CLI

    main

    The interactive_agent_cli.py script is an interactive command-line tool designed to explore agent turn and step events, specifically demonstrating server-side tool execution (like file_search) and real-time event streaming. It automatically sets up a vector store with a sample knowledge base and provides a chat-style REPL.

    Prerequisites

    1. Start a Llama Stack server with an OpenAI provider:

      cd ~/local/llama-stack
      source ../stack-venv/bin/activate
      export OPENAI_API_KEY=<your-key>
      llama stack run ci-tests --port 8321
    2. Install the client from the repository root:

      cd /Users/ashwin/local/new-stainless/llama-stack-client-python
      uv sync

    Usage

    Basic usage (uses defaults: openai/gpt-4o and http://localhost:8321):

    cd examples
    uv run python interactive_agent_cli.py

    With custom options:

    uv run python interactive_agent_cli.py --model openai/gpt-4o-mini --base-url http://localhost:8321
    uv run python interactive_agent_cli.py --model openai/gpt-4o-mini --base-url http://localhost:8321
  8. Manage HTTP client resources and lifecycle

    main

    The library closes connections automatically when the client is garbage collected. However, for better resource management, you should manually close the client using .close() or use the client as a context manager.

    from ogx_client import OgxClient
    
    with OgxClient() as client:
      # make requests here
      ...
    
    # HTTP client is now closed
  9. Use aiohttp as the async HTTP backend

    main

    By default, the async client uses httpx. To use aiohttp for improved concurrency performance, install the aiohttp extra and pass DefaultAioHttpClient() to the client constructor.

    pip install '--pre ogx_client[aiohttp]'
    import asyncio
    from ogx_client import DefaultAioHttpClient
    from ogx_client import AsyncOgxClient
    
    async def main() -> None:
        async with AsyncOgxClient(
            http_client=DefaultAioHttpClient(),
        ) as client:
            models = await client.models.list()
    
    asyncio.run(main())
  10. Configure the HTTP client and proxies

    main

    You can customize the underlying httpx client by passing a DefaultHttpxClient to the OgxClient constructor. This allows you to configure proxies, custom transports, and base URLs. You can also apply per-request configurations using .with_options().

    import httpx
    from ogx_client import OgxClient, DefaultHttpxClient
    
    client = OgxClient(
        # Or use the `OGX_CLIENT_BASE_URL` env var
        base_url="http://my.test.server.example.com:8083",
        http_client=DefaultHttpxClient(
            proxy="http://my.test.proxy.example.com",
            transport=httpx.HTTPTransport(local_address="0.0.0.0"),
        ),
    )
    
    # Per-request customization
    client.with_options(http_client=DefaultHttpxClient(...))
  11. Use text and image content parts in queries and items

    main

    When performing reranking, both Item and Query can be complex objects following the OpenAI-compatible chat completion format. This allows you to include multimodal data (text and images) in your ranking requests.

    Text Content Part:

    • type: Must be exactly "text".
    • text: The string content of the message.

    Image Content Part:

    • type: Must be exactly "image_url".
    • image_url: A dictionary containing:
      • url: The URL of the image.
      • detail (Optional): The level of detail for processing. Allowed values are "low", "high", or "auto".
    rerank_params = {
        "model": "multimodal-rerank-model",
        "query": {
            "type": "text",
            "text": "Describe this image"
        },
        "items": [
            {
                "type": "image_url",
                "image_url": {
                    "url": "https://example.com/image.jpg",
                    "detail": "high"
                }
            },
            "A simple text description of an image."
        ]
    }