OpenaiEx

repository·main·Indexed 42 days ago

https://github.com/cyberchitta/openai_ex

A production-ready Elixir library providing a client for the OpenAI API. It features streaming support, Finch-based transport, and compatibility with Azure OpenAI and local LLM proxies. The library supports a wide range of endpoints including Chat Completions, the Assistants API (Beta), Batch jobs, and Container APIs.

Tokens
9.8K
Snippets
58
Records
70
Agent score
53%

What's inside OpenaiEx

  1. Configure OpenaiEx for Azure OpenAI or Local LLMs

    main

    While designed for OpenAI, OpenaiEx is compatible with:

    • Azure OpenAI API
    • Local LLMs via OpenAI-compatible proxies
    • Third-party integrations like Portkey

    The library uses Finch for HTTP transport, allowing for fine-grained control over connection pools and timeouts.

  2. Explore OpenaiEx via Livebook User Guide

    main

    The primary way to learn and test OpenaiEx is through its Livebook-based User Guide. Because the library uses a Livebook-first design, you can run the guide directly to see working examples of almost every API call without setting up a local project.

    Detailed usage examples and the full user guide are available on hexdocs.

  3. Install OpenaiEx via Hex

    main

    To use OpenaiEx in your Elixir project, add it to your mix.exs dependencies. It is a community-maintained OpenAI API client that supports all current endpoints, including the Responses API and Container APIs.

    {:openai_ex, "~> 0.1.0"} # Replace with the latest version
  4. Use OpenaiEx for Streaming and Chatbot implementations

    main

    The library provides comprehensive streaming API implementations with request cancellation capabilities. You can find specialized Livebook examples for different patterns:

  5. Configure environment variables for development

    main

    When developing locally using the provided devcontainer, you can manage secrets and configuration by creating a .env file in the .devcontainer/ directory.

    Commonly used variables include:

    • OPENAI_API_KEY: Your OpenAI credentials.
    • LIVEBOOK_PASSWORD: The password required to access Livebook within the devcontainer.
    # Example .devcontainer/.env content
    OPENAI_API_KEY=sk-...
    LIVEBOOK_PASSWORD=your_password_here
  6. Prepare a file reference request with `new_reference/1`

    main

    If you want to reference an existing file by its ID instead of uploading a new one, use new_reference/1. This prepares a request containing the :file_id key.

    Example:

    OpenaiEx.ContainerFiles.new_reference(file_id: "file-123")
    # => %{file_id: "file-123"}
  7. List containers with `OpenaiEx.Containers.list!/2`

    main

    Use OpenaiEx.Containers.list!/2 to retrieve a list of all containers belonging to your organization. You can pass optional query parameters to filter the results. The bang version (!) raises an error on failure.

    # List all containers
    containers = OpenaiEx.Containers.list!(openai_client)
    
    # List containers with specific query parameters
    containers = OpenaiEx.Containers.list!(openai_client, %{limit: 10})
  8. Retrieve a container by ID with `OpenaiEx.Containers.retrieve!/2`

    main

    To fetch the details of a specific container, use OpenaiEx.Containers.retrieve!/2 and provide the container_id. The bang version (!) raises an error if the container is not found or the request fails.

    container = OpenaiEx.Containers.retrieve!(openai_client, "cont-123")
  9. Submit Tool Outputs to a Run

    main

    When a tool requires input from the user or an external system, use OpenaiEx.Beta.Threads.Runs.submit_tool_outputs/2 to provide the results back to the run.

    Required keys in the params map:

    • thread_id
    • run_id
    • tool_outputs

    Supports streaming via stream: true.

    params = %{
      thread_id: "thread_foo",
      run_id: "run_bar",
      tool_outputs: [%{tool_call_id: "call_123", output: "result_data"}]
    }
    OpenaiEx.Beta.Threads.Runs.submit_tool_outputs(openai_client, params)