socketry / async-websocket

Asynchronous WebSocket client and server, supporting HTTP/1 and HTTP/2 for Ruby.
MIT License
171 stars 17 forks source link

Unable to send authorization headers #75

Closed adenta closed 2 days ago

adenta commented 3 days ago

Couldn't find documentation either.

OPENAI_URL = 'wss://api.openai.com/v1/realtime?model=gpt-4o-realtime-preview-2024-10-01'
    OPENAI_HEADERS = {
      'Authorization' => "Bearer #{ENV['OPENAI_API_KEY']}",
      'OpenAI-Beta' => 'realtime=v1'
    }
    Async do
      openai_endpoint = Async::HTTP::Endpoint.parse(OPENAI_URL)
      openai_connection = Async::WebSocket::Client.connect(openai_endpoint, headers: OPENAI_HEADERS)
    end

gives me a Failed to negotiate connection! when a similar turn of phrase works with websocket-client-simple. I am fairly certain I am not passing in the headers properly, and I couldnt find any documentation about what exactly this library is looking for.

ioquatix commented 3 days ago

This looks like a missing feature.

michelson commented 2 days ago

Hi, I'm able to do it with async. example:

def ws_client
    require "async"
    require "async/http"
    require "async/websocket"

    url = "wss://api.openai.com/v1/realtime?model=gpt-4o-realtime-preview-2024-10-01"

    # Creating headers for the request
    headers = {
      "Authorization" => "Bearer #{ENV.fetch('OPENAI_API_KEY', nil)}",
      "OpenAI-Beta" => "realtime=v1"
    }

    Async do |task|
      endpoint = Async::HTTP::Endpoint.parse(url, alpn_protocols: Async::HTTP::Protocol::HTTP11.names)

      Async::WebSocket::Client.connect(endpoint, headers: headers) do |connection|
        input_task = task.async do
          while line = $stdin.gets

            text = {
              type: "response.create",
              response: {
                modalities: ["text"],
                instructions: "Please assist the user."
              }
            }
            message = Protocol::WebSocket::TextMessage.generate(text) # ({ text: line })
            message.send(connection)
            connection.flush
          end
        end

        puts "Connected..."
        while message = connection.read
          puts "> #{message.to_h}"
        end
      ensure
        input_task&.stop
      end
    end
  end
adenta commented 2 days ago

Are you seeing session.created events from the server? Would the above example work with function calling?

I was able to get a basic example working, but it would not call defined tools/functions

adenta commented 2 days ago

looks like this is the async-websocket repo, not the ruby-openai, so closing

ioquatix commented 2 days ago

Okay, just to confirm, this is now working for you, at least the websocket part?

adenta commented 2 days ago

It’s working for the other guy, I haven’t tested it yet. I went with Faye

On Thu, Oct 17, 2024 at 6:47 PM Samuel Williams @.***> wrote:

Okay, just to confirm, this is now working for you, at least the websocket part?

— Reply to this email directly, view it on GitHub https://github.com/socketry/async-websocket/issues/75#issuecomment-2420755339, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACGUU5VKM7DC4ER7KLWMQ2DZ4A475AVCNFSM6AAAAABQCXDRJGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDIMRQG42TKMZTHE . You are receiving this because you modified the open/close state.Message ID: @.***>