Code Monkey home page Code Monkey logo

ruby-openai's Introduction

Ruby OpenAI

Gem Version GitHub license CircleCI Build Status

Use the OpenAI API with Ruby! 🤖❤️

Stream text with GPT-4o, transcribe and translate audio with Whisper, or create images with DALL·E...

📚 Rails AI (FREE Book) | 🎮 Ruby AI Builders Discord | 🐦 X | 🧠 Anthropic Gem | 🚂 Midjourney Gem

NEW: AI::Engine

Need to quickly integrate your Rails app with an OpenAI Assistant? Check out my new gem, built on top of ruby-openai - free trial available now at insertrobot.com!

Contents

Installation

Bundler

Add this line to your application's Gemfile:

gem "ruby-openai"

And then execute:

$ bundle install

Gem install

Or install with:

$ gem install ruby-openai

and require with:

require "openai"

Usage

Quickstart

For a quick test you can pass your token directly to a new client:

client = OpenAI::Client.new(
  access_token: "access_token_goes_here",
  log_errors: true # Highly recommended in development, so you can see what errors OpenAI is returning. Not recommended in production because it could leak private data to your logs.
)

With Config

For a more robust setup, you can configure the gem with your API keys, for example in an openai.rb initializer file. Never hardcode secrets into your codebase - instead use something like dotenv to pass the keys safely into your environments.

OpenAI.configure do |config|
  config.access_token = ENV.fetch("OPENAI_ACCESS_TOKEN")
  config.organization_id = ENV.fetch("OPENAI_ORGANIZATION_ID") # Optional
  config.log_errors = true # Highly recommended in development, so you can see what errors OpenAI is returning. Not recommended in production because it could leak private data to your logs.
end

Then you can create a client like this:

client = OpenAI::Client.new

You can still override the config defaults when making new clients; any options not included will fall back to any global config set with OpenAI.configure. e.g. in this example the organization_id, request_timeout, etc. will fallback to any set globally using OpenAI.configure, with only the access_token overridden:

client = OpenAI::Client.new(access_token: "access_token_goes_here")

Custom timeout or base URI

The default timeout for any request using this library is 120 seconds. You can change that by passing a number of seconds to the request_timeout when initializing the client. You can also change the base URI used for all requests, eg. to use observability tools like Helicone, and add arbitrary other headers e.g. for openai-caching-proxy-worker:

client = OpenAI::Client.new(
    access_token: "access_token_goes_here",
    uri_base: "https://oai.hconeai.com/",
    request_timeout: 240,
    extra_headers: {
      "X-Proxy-TTL" => "43200", # For https://github.com/6/openai-caching-proxy-worker#specifying-a-cache-ttl
      "X-Proxy-Refresh": "true", # For https://github.com/6/openai-caching-proxy-worker#refreshing-the-cache
      "Helicone-Auth": "Bearer HELICONE_API_KEY", # For https://docs.helicone.ai/getting-started/integration-method/openai-proxy
      "helicone-stream-force-format" => "true", # Use this with Helicone otherwise streaming drops chunks # https://github.com/alexrudall/ruby-openai/issues/251
    }
)

or when configuring the gem:

OpenAI.configure do |config|
    config.access_token = ENV.fetch("OPENAI_ACCESS_TOKEN")
    config.log_errors = true # Optional
    config.organization_id = ENV.fetch("OPENAI_ORGANIZATION_ID") # Optional
    config.uri_base = "https://oai.hconeai.com/" # Optional
    config.request_timeout = 240 # Optional
    config.extra_headers = {
      "X-Proxy-TTL" => "43200", # For https://github.com/6/openai-caching-proxy-worker#specifying-a-cache-ttl
      "X-Proxy-Refresh": "true", # For https://github.com/6/openai-caching-proxy-worker#refreshing-the-cache
      "Helicone-Auth": "Bearer HELICONE_API_KEY" # For https://docs.helicone.ai/getting-started/integration-method/openai-proxy
    } # Optional
end

Extra Headers per Client

You can dynamically pass headers per client object, which will be merged with any headers set globally with OpenAI.configure:

client = OpenAI::Client.new(access_token: "access_token_goes_here")
client.add_headers("X-Proxy-TTL" => "43200")

Logging

Errors

By default, ruby-openai does not log any Faraday::Errors encountered while executing a network request to avoid leaking data (e.g. 400s, 500s, SSL errors and more - see here for a complete list of subclasses of Faraday::Error and what can cause them).

If you would like to enable this functionality, you can set log_errors to true when configuring the client:

  client = OpenAI::Client.new(log_errors: true)
Faraday middleware

You can pass Faraday middleware to the client in a block, eg. to enable verbose logging with Ruby's Logger:

  client = OpenAI::Client.new do |f|
    f.response :logger, Logger.new($stdout), bodies: true
  end

Azure

To use the Azure OpenAI Service API, you can configure the gem like this:

    OpenAI.configure do |config|
        config.access_token = ENV.fetch("AZURE_OPENAI_API_KEY")
        config.uri_base = ENV.fetch("AZURE_OPENAI_URI")
        config.api_type = :azure
        config.api_version = "2023-03-15-preview"
    end

where AZURE_OPENAI_URI is e.g. https://custom-domain.openai.azure.com/openai/deployments/gpt-35-turbo

Ollama

Ollama allows you to run open-source LLMs, such as Llama 3, locally. It offers chat compatibility with the OpenAI API.

You can download Ollama here. On macOS you can install and run Ollama like this:

brew install ollama
ollama serve
ollama pull llama3:latest # In new terminal tab.

Create a client using your Ollama server and the pulled model, and stream a conversation for free:

client = OpenAI::Client.new(
  uri_base: "http://localhost:11434"
)

client.chat(
    parameters: {
        model: "llama3", # Required.
        messages: [{ role: "user", content: "Hello!"}], # Required.
        temperature: 0.7,
        stream: proc do |chunk, _bytesize|
            print chunk.dig("choices", 0, "delta", "content")
        end
    })

# => Hi! It's nice to meet you. Is there something I can help you with, or would you like to chat?

Groq

Groq API Chat is broadly compatible with the OpenAI API, with a few minor differences. Get an access token from here, then:

  client = OpenAI::Client.new(
    access_token: "groq_access_token_goes_here",
    uri_base: "https://api.groq.com/openai"
  )

  client.chat(
    parameters: {
        model: "llama3-8b-8192", # Required.
        messages: [{ role: "user", content: "Hello!"}], # Required.
        temperature: 0.7,
        stream: proc do |chunk, _bytesize|
            print chunk.dig("choices", 0, "delta", "content")
        end
    })

Counting Tokens

OpenAI parses prompt text into tokens, which are words or portions of words. (These tokens are unrelated to your API access_token.) Counting tokens can help you estimate your costs. It can also help you ensure your prompt text size is within the max-token limits of your model's context window, and choose an appropriate max_tokens completion parameter so your response will fit as well.

To estimate the token-count of your text:

OpenAI.rough_token_count("Your text")

If you need a more accurate count, try tiktoken_ruby.

Models

There are different models that can be used to generate text. For a full list and to retrieve information about a single model:

client.models.list
client.models.retrieve(id: "gpt-4o")

Chat

GPT is a model that can be used to generate text in a conversational style. You can use it to generate a response to a sequence of messages:

response = client.chat(
    parameters: {
        model: "gpt-4o", # Required.
        messages: [{ role: "user", content: "Hello!"}], # Required.
        temperature: 0.7,
    })
puts response.dig("choices", 0, "message", "content")
# => "Hello! How may I assist you today?"

Streaming Chat

Quick guide to streaming Chat with Rails 7 and Hotwire

You can stream from the API in realtime, which can be much faster and used to create a more engaging user experience. Pass a Proc (or any object with a #call method) to the stream parameter to receive the stream of completion chunks as they are generated. Each time one or more chunks is received, the proc will be called once with each chunk, parsed as a Hash. If OpenAI returns an error, ruby-openai will raise a Faraday error.

client.chat(
    parameters: {
        model: "gpt-4o", # Required.
        messages: [{ role: "user", content: "Describe a character called Anna!"}], # Required.
        temperature: 0.7,
        stream: proc do |chunk, _bytesize|
            print chunk.dig("choices", 0, "delta", "content")
        end
    })
# => "Anna is a young woman in her mid-twenties, with wavy chestnut hair that falls to her shoulders..."

Note: In order to get usage information, you can provide the stream_options parameter and OpenAI will provide a final chunk with the usage. Here is an example:

stream_proc = proc { |chunk, _bytesize| puts "--------------"; puts chunk.inspect; }
client.chat(
    parameters: {
        model: "gpt-4o",
        stream: stream_proc,
        stream_options: { include_usage: true },
        messages: [{ role: "user", content: "Hello!"}],
    })
# => --------------
# => {"id"=>"chatcmpl-7bbq05PiZqlHxjV1j7OHnKKDURKaf", "object"=>"chat.completion.chunk", "created"=>1718750612, "model"=>"gpt-4o-2024-05-13", "system_fingerprint"=>"fp_9cb5d38cf7", "choices"=>[{"index"=>0, "delta"=>{"role"=>"assistant", "content"=>""}, "logprobs"=>nil, "finish_reason"=>nil}], "usage"=>nil}
# => --------------
# => {"id"=>"chatcmpl-7bbq05PiZqlHxjV1j7OHnKKDURKaf", "object"=>"chat.completion.chunk", "created"=>1718750612, "model"=>"gpt-4o-2024-05-13", "system_fingerprint"=>"fp_9cb5d38cf7", "choices"=>[{"index"=>0, "delta"=>{"content"=>"Hello"}, "logprobs"=>nil, "finish_reason"=>nil}], "usage"=>nil}
# => --------------
# => ... more content chunks
# => --------------
# => {"id"=>"chatcmpl-7bbq05PiZqlHxjV1j7OHnKKDURKaf", "object"=>"chat.completion.chunk", "created"=>1718750612, "model"=>"gpt-4o-2024-05-13", "system_fingerprint"=>"fp_9cb5d38cf7", "choices"=>[{"index"=>0, "delta"=>{}, "logprobs"=>nil, "finish_reason"=>"stop"}], "usage"=>nil}
# => --------------
# => {"id"=>"chatcmpl-7bbq05PiZqlHxjV1j7OHnKKDURKaf", "object"=>"chat.completion.chunk", "created"=>1718750612, "model"=>"gpt-4o-2024-05-13", "system_fingerprint"=>"fp_9cb5d38cf7", "choices"=>[], "usage"=>{"prompt_tokens"=>9, "completion_tokens"=>9, "total_tokens"=>18}}

Vision

You can use the GPT-4 Vision model to generate a description of an image:

messages = [
  { "type": "text", "text": "What’s in this image?"},
  { "type": "image_url",
    "image_url": {
      "url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg",
    },
  }
]
response = client.chat(
    parameters: {
        model: "gpt-4-vision-preview", # Required.
        messages: [{ role: "user", content: messages}], # Required.
    })
puts response.dig("choices", 0, "message", "content")
# => "The image depicts a serene natural landscape featuring a long wooden boardwalk extending straight ahead"

JSON Mode

You can set the response_format to ask for responses in JSON:

  response = client.chat(
    parameters: {
        model: "gpt-4o",
        response_format: { type: "json_object" },
        messages: [{ role: "user", content: "Hello! Give me some JSON please."}],
        temperature: 0.7,
    })
    puts response.dig("choices", 0, "message", "content")
    {
      "name": "John",
      "age": 30,
      "city": "New York",
      "hobbies": ["reading", "traveling", "hiking"],
      "isStudent": false
    }

You can stream it as well!

  response = client.chat(
    parameters: {
      model: "gpt-4o",
      messages: [{ role: "user", content: "Can I have some JSON please?"}],
        response_format: { type: "json_object" },
        stream: proc do |chunk, _bytesize|
          print chunk.dig("choices", 0, "delta", "content")
        end
  })
  {
    "message": "Sure, please let me know what specific JSON data you are looking for.",
    "JSON_data": {
      "example_1": {
        "key_1": "value_1",
        "key_2": "value_2",
        "key_3": "value_3"
      },
      "example_2": {
        "key_4": "value_4",
        "key_5": "value_5",
        "key_6": "value_6"
      }
    }
  }

Functions

You can describe and pass in functions and the model will intelligently choose to output a JSON object containing arguments to call them - eg., to use your method get_current_weather to get the weather in a given location. Note that tool_choice is optional, but if you exclude it, the model will choose whether to use the function or not (see here).

def get_current_weather(location:, unit: "fahrenheit")
  # Here you could use a weather api to fetch the weather.
  "The weather in #{location} is nice 🌞 #{unit}"
end

messages = [
  {
    "role": "user",
    "content": "What is the weather like in San Francisco?",
  },
]

response =
  client.chat(
    parameters: {
      model: "gpt-4o",
      messages: messages,  # Defined above because we'll use it again
      tools: [
        {
          type: "function",
          function: {
            name: "get_current_weather",
            description: "Get the current weather in a given location",
            parameters: {  # Format: https://json-schema.org/understanding-json-schema
              type: :object,
              properties: {
                location: {
                  type: :string,
                  description: "The city and state, e.g. San Francisco, CA",
                },
                unit: {
                  type: "string",
                  enum: %w[celsius fahrenheit],
                },
              },
              required: ["location"],
            },
          },
        }
      ],
      tool_choice: "required"  # Optional, defaults to "auto"
                               # Can also put "none" or specific functions, see docs
    },
  )

message = response.dig("choices", 0, "message")

if message["role"] == "assistant" && message["tool_calls"]
  message["tool_calls"].each do |tool_call|
    tool_call_id = tool_call.dig("id")
    function_name = tool_call.dig("function", "name")
    function_args = JSON.parse(
      tool_call.dig("function", "arguments"),
      { symbolize_names: true },
    )
    function_response = case function_name
      when "get_current_weather"
        get_current_weather(**function_args)  # => "The weather is nice 🌞"
      else
        # decide how to handle
    end

    # For a subsequent message with the role "tool", OpenAI requires the preceding message to have a tool_calls argument.
    messages << message

    messages << {
      tool_call_id: tool_call_id,
      role: "tool",
      name: function_name,
      content: function_response
    }  # Extend the conversation with the results of the functions
  end

  second_response = client.chat(
    parameters: {
      model: "gpt-4o",
      messages: messages
  })

  puts second_response.dig("choices", 0, "message", "content")

  # At this point, the model has decided to call functions, you've called the functions
  # and provided the response back, and the model has considered this and responded.
end
# => "It looks like the weather is nice and sunny in San Francisco! If you're planning to go out, it should be a pleasant day."

Completions

Hit the OpenAI API for a completion using other GPT-3 models:

response = client.completions(
    parameters: {
        model: "gpt-4o",
        prompt: "Once upon a time",
        max_tokens: 5
    })
puts response["choices"].map { |c| c["text"] }
# => [", there lived a great"]

Embeddings

You can use the embeddings endpoint to get a vector of numbers representing an input. You can then compare these vectors for different inputs to efficiently check how similar the inputs are.

response = client.embeddings(
    parameters: {
        model: "text-embedding-ada-002",
        input: "The food was delicious and the waiter..."
    }
)

puts response.dig("data", 0, "embedding")
# => Vector representation of your embedding

Batches

The Batches endpoint allows you to create and manage large batches of API requests to run asynchronously. Currently, the supported endpoints for batches are /v1/chat/completions (Chat Completions API) and /v1/embeddings (Embeddings API).

To use the Batches endpoint, you need to first upload a JSONL file containing the batch requests using the Files endpoint. The file must be uploaded with the purpose set to batch. Each line in the JSONL file represents a single request and should have the following format:

{
  "custom_id": "request-1",
  "method": "POST",
  "url": "/v1/chat/completions",
  "body": {
    "model": "gpt-4o",
    "messages": [
      { "role": "system", "content": "You are a helpful assistant." },
      { "role": "user", "content": "What is 2+2?" }
    ]
  }
}

Once you have uploaded the JSONL file, you can create a new batch by providing the file ID, endpoint, and completion window:

response = client.batches.create(
  parameters: {
    input_file_id: "file-abc123",
    endpoint: "/v1/chat/completions",
    completion_window: "24h"
  }
)
batch_id = response["id"]

You can retrieve information about a specific batch using its ID:

batch = client.batches.retrieve(id: batch_id)

To cancel a batch that is in progress:

client.batches.cancel(id: batch_id)

You can also list all the batches:

client.batches.list

Once the batch["completed_at"] is present, you can fetch the output or error files:

batch = client.batches.retrieve(id: batch_id)
output_file_id = batch["output_file_id"]
output_response = client.files.content(id: output_file_id)
error_file_id = batch["error_file_id"]
error_response = client.files.content(id: error_file_id)

These files are in JSONL format, with each line representing the output or error for a single request. The lines can be in any order:

{
  "id": "response-1",
  "custom_id": "request-1",
  "response": {
    "id": "chatcmpl-abc123",
    "object": "chat.completion",
    "created": 1677858242,
    "model": "gpt-4o",
    "choices": [
      {
        "index": 0,
        "message": {
          "role": "assistant",
          "content": "2+2 equals 4."
        }
      }
    ]
  }
}

If a request fails with a non-HTTP error, the error object will contain more information about the cause of the failure.

Files

For fine-tuning purposes

Put your data in a .jsonl file like this:

{"prompt":"Overjoyed with my new phone! ->", "completion":" positive"}
{"prompt":"@lakers disappoint for a third straight night ->", "completion":" negative"}

and pass the path (or a StringIO object) to client.files.upload to upload it to OpenAI, and then interact with it:

client.files.upload(parameters: { file: "path/to/sentiment.jsonl", purpose: "fine-tune" })
client.files.list
client.files.retrieve(id: "file-123")
client.files.content(id: "file-123")
client.files.delete(id: "file-123")

For assistant purposes

You can send a file path:

client.files.upload(parameters: { file: "path/to/file.pdf", purpose: "assistants" })

or a File object

my_file = File.open("path/to/file.pdf", "rb")
client.files.upload(parameters: { file: my_file, purpose: "assistants" })

See supported file types on API documentation.

Finetunes

Upload your fine-tuning data in a .jsonl file as above and get its ID:

response = client.files.upload(parameters: { file: "path/to/sarcasm.jsonl", purpose: "fine-tune" })
file_id = JSON.parse(response.body)["id"]

You can then use this file ID to create a fine tuning job:

response = client.finetunes.create(
    parameters: {
    training_file: file_id,
    model: "gpt-4o"
})
fine_tune_id = response["id"]

That will give you the fine-tune ID. If you made a mistake you can cancel the fine-tune model before it is processed:

client.finetunes.cancel(id: fine_tune_id)

You may need to wait a short time for processing to complete. Once processed, you can use list or retrieve to get the name of the fine-tuned model:

client.finetunes.list
response = client.finetunes.retrieve(id: fine_tune_id)
fine_tuned_model = response["fine_tuned_model"]

This fine-tuned model name can then be used in chat completions:

response = client.chat(
    parameters: {
        model: fine_tuned_model,
        messages: [{ role: "user", content: "I love Mondays!"}]
    }
)
response.dig("choices", 0, "message", "content")

You can also capture the events for a job:

client.finetunes.list_events(id: fine_tune_id)

Vector Stores

Vector Store objects give the File Search tool the ability to search your files.

You can create a new vector store:

response = client.vector_stores.create(
  parameters: {
    name: "my vector store",
    file_ids: ["file-abc123", "file-def456"]
  }
)

vector_store_id = response["id"]

Given a vector_store_id you can retrieve the current field values:

client.vector_stores.retrieve(id: vector_store_id)

You can get a list of all vector stores currently available under the organization:

client.vector_stores.list

You can modify an existing vector store, except for the file_ids:

response = client.vector_stores.modify(
  id: vector_store_id,
  parameters: {
    name: "Modified Test Vector Store",
  }
)

You can delete vector stores:

client.vector_stores.delete(id: vector_store_id)

Vector Store Files

Vector store files represent files inside a vector store.

You can create a new vector store file by attaching a File to a vector store.

response = client.vector_store_files.create(
  vector_store_id: "vector-store-abc123",
  parameters: {
    file_id: "file-abc123"
  }
)

vector_store_file_id = response["id"]

Given a vector_store_file_id you can retrieve the current field values:

client.vector_store_files.retrieve(
  vector_store_id: "vector-store-abc123",
  id: vector_store_file_id
)

You can get a list of all vector store files currently available under the vector store:

client.vector_store_files.list(vector_store_id: "vector-store-abc123")

You can delete a vector store file:

client.vector_store_files.delete(
  vector_store_id: "vector-store-abc123",
  id: vector_store_file_id
)

Note: This will remove the file from the vector store but the file itself will not be deleted. To delete the file, use the delete file endpoint.

Vector Store File Batches

Vector store file batches represent operations to add multiple files to a vector store.

You can create a new vector store file batch by attaching multiple Files to a vector store.

response = client.vector_store_file_batches.create(
  vector_store_id: "vector-store-abc123",
  parameters: {
    file_ids: ["file-abc123", "file-def456"]
  }
)

file_batch_id = response["id"]

Given a file_batch_id you can retrieve the current field values:

client.vector_store_file_batches.retrieve(
  vector_store_id: "vector-store-abc123",
  id: file_batch_id
)

You can get a list of all vector store files in a batch currently available under the vector store:

client.vector_store_file_batches.list(
  vector_store_id: "vector-store-abc123",
  id: file_batch_id
)

You can cancel a vector store file batch (This attempts to cancel the processing of files in this batch as soon as possible):

client.vector_store_file_batches.cancel(
  vector_store_id: "vector-store-abc123",
  id: file_batch_id
)

Assistants

Assistants are stateful actors that can have many conversations and use tools to perform tasks (see Assistant Overview).

To create a new assistant:

response = client.assistants.create(
    parameters: {
        model: "gpt-4o",
        name: "OpenAI-Ruby test assistant",
        description: nil,
        instructions: "You are a Ruby dev bot. When asked a question, write and run Ruby code to answer the question",
        tools: [
            { type: "code_interpreter" },
            { type: "file_search" }
        ],
        tool_resources: {
          code_interpreter: {
            file_ids: [] # See Files section above for how to upload files
          },
          file_search: {
            vector_store_ids: [] # See Vector Stores section above for how to add vector stores
          }
        },
        "metadata": { my_internal_version_id: "1.0.0" }
    })
assistant_id = response["id"]

Given an assistant_id you can retrieve the current field values:

client.assistants.retrieve(id: assistant_id)

You can get a list of all assistants currently available under the organization:

client.assistants.list

You can modify an existing assistant using the assistant's id (see API documentation):

response = client.assistants.modify(
        id: assistant_id,
        parameters: {
            name: "Modified Test Assistant for OpenAI-Ruby",
            metadata: { my_internal_version_id: '1.0.1' }
        })

You can delete assistants:

client.assistants.delete(id: assistant_id)

Threads and Messages

Once you have created an assistant as described above, you need to prepare a Thread of Messages for the assistant to work on (see introduction on Assistants). For example, as an initial setup you could do:

# Create thread
response = client.threads.create # Note: Once you create a thread, there is no way to list it
                                 # or recover it currently (as of 2023-12-10). So hold onto the `id`
thread_id = response["id"]

# Add initial message from user (see https://platform.openai.com/docs/api-reference/messages/createMessage)
message_id = client.messages.create(
    thread_id: thread_id,
    parameters: {
        role: "user", # Required for manually created messages
        content: "Can you help me write an API library to interact with the OpenAI API please?"
    })["id"]

# Retrieve individual message
message = client.messages.retrieve(thread_id: thread_id, id: message_id)

# Review all messages on the thread
messages = client.messages.list(thread_id: thread_id)

To clean up after a thread is no longer needed:

# To delete the thread (and all associated messages):
client.threads.delete(id: thread_id)

client.messages.retrieve(thread_id: thread_id, id: message_id) # -> Fails after thread is deleted

Runs

To submit a thread to be evaluated with the model of an assistant, create a Run as follows:

# Create run (will use instruction/model/tools from Assistant's definition)
response = client.runs.create(thread_id: thread_id,
    parameters: {
        assistant_id: assistant_id,
        max_prompt_tokens: 256,
        max_completion_tokens: 16
    })
run_id = response['id']

You can stream the message chunks as they come through:

client.runs.create(thread_id: thread_id,
    parameters: {
        assistant_id: assistant_id,
        max_prompt_tokens: 256,
        max_completion_tokens: 16,
        stream: proc do |chunk, _bytesize|
          print chunk.dig("delta", "content", 0, "text", "value") if chunk["object"] == "thread.message.delta"
        end
    })

To get the status of a Run:

response = client.runs.retrieve(id: run_id, thread_id: thread_id)
status = response['status']

The status response can include the following strings queued, in_progress, requires_action, cancelling, cancelled, failed, completed, or expired which you can handle as follows:

while true do
    response = client.runs.retrieve(id: run_id, thread_id: thread_id)
    status = response['status']

    case status
    when 'queued', 'in_progress', 'cancelling'
      puts 'Sleeping'
      sleep 1 # Wait one second and poll again
    when 'completed'
      break # Exit loop and report result to user
    when 'requires_action'
      # Handle tool calls (see below)
    when 'cancelled', 'failed', 'expired'
      puts response['last_error'].inspect
      break # or `exit`
    else
      puts "Unknown status response: #{status}"
    end
end

If the status response indicates that the run is completed, the associated thread will have one or more new messages attached:

# Either retrieve all messages in bulk again, or...
messages = client.messages.list(thread_id: thread_id, parameters: { order: 'asc' })

# Alternatively retrieve the `run steps` for the run which link to the messages:
run_steps = client.run_steps.list(thread_id: thread_id, run_id: run_id, parameters: { order: 'asc' })
new_message_ids = run_steps['data'].filter_map { |step|
  if step['type'] == 'message_creation'
    step.dig('step_details', "message_creation", "message_id")
  end # Ignore tool calls, because they don't create new messages.
}

# Retrieve the individual messages
new_messages = new_message_ids.map { |msg_id|
  client.messages.retrieve(id: msg_id, thread_id: thread_id)
}

# Find the actual response text in the content array of the messages
new_messages.each { |msg|
    msg['content'].each { |content_item|
        case content_item['type']
        when 'text'
            puts content_item.dig('text', 'value')
            # Also handle annotations
        when 'image_file'
            # Use File endpoint to retrieve file contents via id
            id = content_item.dig('image_file', 'file_id')
        end
    }
}

You can also update the metadata on messages, including messages that come from the assistant.

metadata = {
  user_id: "abc123"
}
message = client.messages.modify(id: message_id, thread_id: thread_id, parameters: { metadata: metadata })

At any time you can list all runs which have been performed on a particular thread or are currently running:

client.runs.list(thread_id: thread_id, parameters: { order: "asc", limit: 3 })

Create and Run

You can also create a thread and run in one call like this:

response = client.runs.create_thread_and_run(parameters: { assistant_id: assistant_id })
run_id = response['id']
thread_id = response['thread_id']

Runs involving function tools

In case you are allowing the assistant to access function tools (they are defined in the same way as functions during chat completion), you might get a status code of requires_action when the assistant wants you to evaluate one or more function tools:

def get_current_weather(location:, unit: "celsius")
    # Your function code goes here
    if location =~ /San Francisco/i
        return unit == "celsius" ? "The weather is nice 🌞 at 27°C" : "The weather is nice 🌞 at 80°F"
    else
        return unit == "celsius" ? "The weather is icy 🥶 at -5°C" : "The weather is icy 🥶 at 23°F"
    end
end

if status == 'requires_action'

    tools_to_call = response.dig('required_action', 'submit_tool_outputs', 'tool_calls')

    my_tool_outputs = tools_to_call.map { |tool|
        # Call the functions based on the tool's name
        function_name = tool.dig('function', 'name')
        arguments = JSON.parse(
              tool.dig("function", "arguments"),
              { symbolize_names: true },
        )

        tool_output = case function_name
        when "get_current_weather"
            get_current_weather(**arguments)
        end

        { tool_call_id: tool['id'], output: tool_output }
    }

    client.runs.submit_tool_outputs(thread_id: thread_id, run_id: run_id, parameters: { tool_outputs: my_tool_outputs })
end

Note that you have 10 minutes to submit your tool output before the run expires.

Image Generation

Generate images using DALL·E 2 or DALL·E 3!

DALL·E 2

For DALL·E 2 the size of any generated images must be one of 256x256, 512x512 or 1024x1024 - if not specified the image will default to 1024x1024.

response = client.images.generate(parameters: { prompt: "A baby sea otter cooking pasta wearing a hat of some sort", size: "256x256" })
puts response.dig("data", 0, "url")
# => "https://oaidalleapiprodscus.blob.core.windows.net/private/org-Rf437IxKhh..."

Ruby

DALL·E 3

For DALL·E 3 the size of any generated images must be one of 1024x1024, 1024x1792 or 1792x1024. Additionally the quality of the image can be specified to either standard or hd.

response = client.images.generate(parameters: { prompt: "A springer spaniel cooking pasta wearing a hat of some sort", model: "dall-e-3", size: "1024x1792", quality: "standard" })
puts response.dig("data", 0, "url")
# => "https://oaidalleapiprodscus.blob.core.windows.net/private/org-Rf437IxKhh..."

Ruby

Image Edit

Fill in the transparent part of an image, or upload a mask with transparent sections to indicate the parts of an image that can be changed according to your prompt...

response = client.images.edit(parameters: { prompt: "A solid red Ruby on a blue background", image: "image.png", mask: "mask.png" })
puts response.dig("data", 0, "url")
# => "https://oaidalleapiprodscus.blob.core.windows.net/private/org-Rf437IxKhh..."

Ruby

Image Variations

Create n variations of an image.

response = client.images.variations(parameters: { image: "image.png", n: 2 })
puts response.dig("data", 0, "url")
# => "https://oaidalleapiprodscus.blob.core.windows.net/private/org-Rf437IxKhh..."

Ruby Ruby

Moderations

Pass a string to check if it violates OpenAI's Content Policy:

response = client.moderations(parameters: { input: "I'm worried about that." })
puts response.dig("results", 0, "category_scores", "hate")
# => 5.505014632944949e-05

Whisper

Whisper is a speech to text model that can be used to generate text based on audio files:

Translate

The translations API takes as input the audio file in any of the supported languages and transcribes the audio into English.

response = client.audio.translate(
    parameters: {
        model: "whisper-1",
        file: File.open("path_to_file", "rb"),
    })
puts response["text"]
# => "Translation of the text"

Transcribe

The transcriptions API takes as input the audio file you want to transcribe and returns the text in the desired output file format.

You can pass the language of the audio file to improve transcription quality. Supported languages are listed here. You need to provide the language as an ISO-639-1 code, eg. "en" for English or "ne" for Nepali. You can look up the codes here.

response = client.audio.transcribe(
    parameters: {
        model: "whisper-1",
        file: File.open("path_to_file", "rb"),
        language: "en" # Optional
    })
puts response["text"]
# => "Transcription of the text"

Speech

The speech API takes as input the text and a voice and returns the content of an audio file you can listen to.

response = client.audio.speech(
  parameters: {
    model: "tts-1",
    input: "This is a speech test!",
    voice: "alloy",
    response_format: "mp3", # Optional
    speed: 1.0 # Optional
  }
)
File.binwrite('demo.mp3', response)
# => mp3 file that plays: "This is a speech test!"

Errors

HTTP errors can be caught like this:

  begin
    OpenAI::Client.new.models.retrieve(id: "gpt-4o")
  rescue Faraday::Error => e
    raise "Got a Faraday error: #{e}"
  end

Development

After checking out the repo, run bin/setup to install dependencies. You can run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install.

To run all tests, execute the command bundle exec rake, which will also run the linter (Rubocop). This repository uses VCR to log API requests.

Warning

If you have an OPENAI_ACCESS_TOKEN in your ENV, running the specs will use this to run the specs against the actual API, which will be slow and cost you money - 2 cents or more! Remove it from your environment with unset or similar if you just want to run the specs against the stored VCR responses.

Release

First run the specs without VCR so they actually hit the API. This will cost 2 cents or more. Set OPENAI_ACCESS_TOKEN in your environment or pass it in like this:

OPENAI_ACCESS_TOKEN=123abc bundle exec rspec

Then update the version number in version.rb, update CHANGELOG.md, run bundle install to update Gemfile.lock, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/alexrudall/ruby-openai. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Ruby OpenAI project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

ruby-openai's People

Contributors

alexrudall avatar atesgoral avatar ch0ngxian avatar clemalfroy avatar codergeek121 avatar coezbek avatar dependabot-preview[bot] avatar dependabot[bot] avatar dlackty avatar drnic avatar gary-h9 avatar haegin avatar ignacio-chiazzo avatar jamiemccarthy avatar kmcphillips avatar krschacht avatar lalunamel avatar lancecarlson avatar maks112v avatar marckohlbrugge avatar mmmaia avatar mpallenjr avatar obie avatar petergoldstein avatar rmontgomery429 avatar rohitpaulk avatar shalecraig avatar simonx1 avatar steffansluis avatar willywg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ruby-openai's Issues

Support for additional parameters?

I don't believe this was covered explicitly in your documentation exactly, so just want to check.

If we want to pass an additional parameter, can we just add them as perhaps one would expect?

e.g. for adding temperature is this ok?:

        response = @client.chat(
          parameters: {
              model: "gpt-3.5-turbo",
              messages: messages,
              temperature: 0.7
          })

etc.

Streaming

is there a way to stream the response from the api like how chatgpt streams one token at a time?

Slow performance

Describe the bug

I am not sure why yet but our requests are taking like between 5 to 15 seconds most times.

I tried to log using httplog gem and it stops after the connecting step, after that for like 10 seconds goes fast.

May it be related to httparty gem using Net::HTTP? I think it doesn't support http2 nor keep-alives

CleanShot 2023-03-23 at 03 45 43@2x

No exception information returned in Stream mode

The api returns a JSON when error, eg.

{
  "error": {
    "message": "'user_role' is not one of ['system', 'assistant', 'user'] - 'messages.0.role'",
    "type": "invalid_request_error",
    "param": null,
    "code": null
  }
}

I use following code to fix

    def self.to_json_stream(user_proc:)

      proc do |chunk, overall_received_bytes, env|
        Rails.logger.debug("raw chunk: #{chunk}")

        if chunk.start_with?("data: {")
          chunk.each_line do |line|
            next unless line.start_with?("data: {")
            line.delete_prefix!("data: ")
            user_proc.call(JSON.parse(line))
          end
        elsif chunk.include?('"error": ')
          user_proc.call(JSON.parse(chunk))
        else
          Rails.logger.warn("unkown raw chunk: #{chunk}")
        end
      end
    end

How to use?

If there is no GUI, how to use this tool?
Do I have to modify the code in notepad every time? Maybe it's just an intermediate tool and I should look for something more?

Deleting models

Describe the bug
I noticed that the API supports deleting models, but this client does not. Oddly enough I tried to add deleting models into this client and tried to delete my model and it didn't work. Did you experience the same issue and that's why it's not supported? Also awkward that it's documented under the finetunes endpoint but its path looks more like it's under the Models CRUD.

https://beta.openai.com/docs/api-reference/fine-tunes/delete-model

Microsoft Azure support

Is your feature request related to a problem? Please describe.

We would like to use ruby-openai with Microsoft Azure OpenAI, however its endpoints and authentication are handled slightly differently, making it impossible currently to use this gem.

Describe the solution you'd like

I propose to support a configuration setting api_type, which can then be set to :azure, similar to how https://github.com/openai/openai-python supports Microsoft Azure.

Describe alternatives you've considered

I'm aware of the changes being worked on in #150, it might be possible to create a subclass of OpenAI::Client once this has been implemented.

Additional context

Happy to submit a PR if desired!

[Question] Does this gem support the 'system' role?

Describe the bug
Does this gem support the 'system' role? Whenever I use it, the instruction appears to be ignored.

https://platform.openai.com/docs/api-reference/chat

To Reproduce
Steps to reproduce the behavior:

response = @client.chat(
  parameters: {
    model: "gpt-3.5-turbo",
    temperature: 0.7,
    messages: [{
      role: "user", content: userText,
      role: "system", content: instructionText,
    }],
  },
)
response.dig("choices", 0, "message", "content")

Expected behavior
Apply system parameters.

Additional context
Stuffing the instruction into the prompt works ok but this isn't ideal.

add timeout option

Is your feature request related to a problem? Please describe.
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

Describe the solution you'd like
A clear and concise description of what you want to happen.

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

Additional context
Add any other context or screenshots about the feature request here.

Show "usage" for Image Generation/Variations

The text completion models contain a "usage" object that allows us to see how much the generation costs, but it seems the image generation is lacking the same object.

	"usage": {
		"prompt_tokens": 54,
		"completion_tokens": 23,
		"total_tokens": 77
	}

Any documentation around this credit cost or thoughts on adding this?

Streaming: callbacks

Is your feature request related to a problem? Please describe.

  • Problem: No way to respond to success and error when streaming

Describe the solution you'd like
Add new callbacks to stream:

  • on_data
  • on_success
  • on_error

Would love feedback on this. Thanks!

Include OpenAI-Organization in headers

Currently, I don't think this library has support to include organization id
curl https://api.openai.com/v1/models \ -H 'Authorization: Bearer YOUR_API_KEY' \ -H 'OpenAI-Organization: org-sgDVddZhv965GLodgIQVRoUD'

Can we also take this as an argument and include in header easily

Delete Fine Tune Model seems broken.

Describe the bug

Cannot delete a fine tune model.

To Reproduce

Steps to reproduce the behavior:

irb(main):001:0> fine_tuned_model="davinci:ft-personal-2023-01-24-03-13-55"
=> "davinci:ft-personal-2023-01-24-03-13-55"
irb(main):002:1*  Ruby::OpenAI.configure do |config|
irb(main):003:1*             config.access_token = ENV.fetch('OPENAI_API_KEY')
irb(main):004:0>         end
irb(main):005:0>         client = OpenAI::Client.new
=> #<OpenAI::Client:0x000000011553c7d8>
irb(main):006:0> client.finetunes.delete(fine_tuned_model:fine_tuned_model)
(irb):7:in `<main>': undefined method `delete' for #<OpenAI::Finetunes:0x0000000115497198> (NoMethodError)

Expected behavior

Per the docs:

 client.finetunes.delete(fine_tuned_model: fine_tuned_model)

But it does not work .....

However this does work:

 OpenAI::Client.delete(path: "/models/#{fine_tuned_model}")
=> {"id"=>"davinci:ft-personal-2023-01-24-03-13-55", "object"=>"model", "deleted"=>true}

Finetuning GTP3?

In the OpenAi documentation it says

Fine-tuning is currently only available for the following base models: davinci, curie, babbage, and ada. These are the original models that do not have any instruction following training (like text-davinci-003 does for example). You are also able to [continue fine-tuning a fine-tuned model](https://platform.openai.com/docs/guides/fine-tuning/continue-fine-tuning-from-a-fine-tuned-model) to add additional data without having to start from scratch.

However, when I use client.models.list it says "allow_create_engine"=>false, (my understanding was that that's the method for fine-tuning:) for all models and "allow_fine_tuning"=>true only for the "snapperm-" models. What are those?

Am I missing something?

Thanks!

JSON::ParserError with different response_format - Whisper

Describe the bug
Whisper supports a few different response types like json, text, art, verbose json or vtt, when you set vtt for example it can't parse to json.

To Reproduce
Steps to reproduce the behavior:

  1. Go to the terminal and to execute:
OpenAI::Client.new.transcribe(
        parameters: {
            model: 'whisper-1',
            file:  'your_file_path',
            response_format: 'vtt'
        })
  1. See error

Expected behavior
response in all formats available for openAI

Cannot download DALL-e image from provided URL

Describe the bug
When using the gem, I can get an URL of a DALL-e image that I can open in a browser. However, when trying to download it locally, I get an error. Is this because of missing SSL connections?

To Reproduce

require 'open-uri
client = OpenAI::Client.new
response = client.images.generate(parameters: { prompt: "Cat", size: "256x256" })
url = response["data"].first["url"]
open(url)

Desktop (please complete the following information):

  • OS: Mac Desktop (Rails backend)
Screenshot 2023-06-04 at 9 48 15 pm

Getting error message trying to use examples from Readme

Describe the bug
Hi .
Getting error messages from OpenAI endpoints trying to use simple examples from Readme verbatum

To Reproduce

client = OpenAI::Client.new(access_token: API_KEY_NG_PERSONAL)
response = client.images.generate(parameters: { prompt: "A baby sea otter cooking pasta wearing a hat of some sort" })

=> {"error"=>
       {"code"=>nil,
         "message"=>"'{\"prompt\":\"A baby sea otter cooking pasta wearing a hat of some sort\"}' is not of type 'object'",      
         "param"=>nil, 
         "type"=>"invalid_request_error"}}`

Expected behavior
Supposed to get a proper response as described in Readme :)

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • macosx - Ventura
  • Ruby 3.1.2 & Ruby 3.2.0

Extra Details

If I run:

client.embeddings(
       parameters: {
           model: "babbage-similarity",
           input: "The food was delicious and the waiter..."
       }
   )

works as expected. So I assume my setup is ok, the issues is with client.images.generate request itself?

Please Add : Retrieve file content

Is your feature request related to a problem? Please describe.

Yes, we need to retrieve fine-tune file contents. This API endpoint is missing from this gem.

Describe the solution you'd like

Per the API:

Retrieve file content

GET
 
https://api.openai.com/v1/files/{file_id}/content

Returns the contents of the specified file

Thanks.

Transcribe throwing exception, expecting UploadFile

Describe the bug
When using V4.0.0 of the gem and attempting to pass a file to the transcribe it receives the response below. Downgrading to version 3.7.0 works as a workaround.

{"error"=>{"message"=>"1 validation error for Request\nbody -> file\n  Expected UploadFile, received: <class 'str'> (type=value_error)", "type"=>"invalid_request_error", "param"=>nil, "code"=>nil}}

To Reproduce
Steps to reproduce the behavior:

  1. Attempt to pass a file to the transcribe methods file parameter
  2. See error above as the response

Expected behavior
It should allow the file to be passed to the service and not attempt to stringify it.

Code

recording.audio.attachment.blob.open do |file|
  response = client.transcribe(parameters: { model: "whisper-1", file: file })
  recording.update(transcription: response.parsed_response['text'])
end

Additional context
It seems to be coming from https://github.com/alexrudall/ruby-openai/blob/main/lib/openai/http.rb#L84 as StringIO, Tempfile and others is_a?(File) is false.

Readme whisper issue

Hey! I Really like this gem, I was looking for whisper using ruby and I had to create a separate app with python but now it's there :) amazing! Thanks!

I saw on the readme there is this:

response = client.transcribe(
parameters: {
model: "whisper-1",
file: File.open('path_to_file'),
})
puts response.parsed_body['text']
=> "Transcription of the text"

"parsed_body" -> But I think it's "parsed_response"

Configuration Object Pattern

Is your feature request related to a problem? Please describe.
Hey there, cool library. I'm trying to integrate into a Rails application which typically follows the initializers convention and Configuration pattern. I'm wondering if you'd be open to a PR refactoring the library a bit to support this pattern.

Describe the solution you'd like
Looking at the code as is, I think it would involve removing the dependency injection of the access_token into each of the sub classes like Finetuning and Engines, and instead pulling from a base library Config object, which is expected to contain the necessary credentials for access.

For example:

OpenAI::Configuration.configure do |c|
  c.access_token = "my_access_token"
end

Then the client could be instantiated without the access token. I started an implementation here but figured I'd get some input before I went too far forward.

Nice to have: Including custom headers on completion/chat

I was looking into using Helicone.ai's caching feature but it requires including a header in the openai request. Here's what it looks like in the python lib:

openai.api_base = "https://oai.hconeai.com/v1"

openai.Completion.create(
    model="text-davinci-003",
    prompt="How do I enable caching?",
    headers={
      "Helicone-Cache-Enabled": "true",
    }
)

For ruby-openai it could look like

response = client.completions(
    parameters: {
        model: "text-davinci-001",
        prompt: "Once upon a time",
        max_tokens: 5,
        headers: {
          "Helicone-Cache-Enabled": "true",
        }
    })

Make packaging consistent with a single top-level package structure

Is your feature request related to a problem? Please describe.

Mostly it's related to writing code, especially as items move around.

The gem doesn't have a single top-level package structure. Some classes are in the Ruby::OpenAI namespace, while others are in the OpenAI namespace. This is fairly confusing and I'm not clear why it's the case.

For example, the Ruby::OpenAI::ConfigurationError class is an error generated when an access_key is not specified. When moving to multi-tenant, this error should be moved from Ruby::OpenAI::Configuration to the initializer of OpenAI::Client. But now it would have inconsistent namespacing.

Describe the solution you'd like

I would suggest a single package structure be used across the gem. The OpenAI seems simpler to me than Ruby::OpenAI, but I don't have a strong preference.

This is pretty simple - it just involves some shuffling of the classes into different directories and an update to the require path in the gemspec. If necessary we could keep the Ruby::OpenAI::Configuration around for a version as an alias for OpenAI::Configuration to ease migration.

Switching to use of require_relative in most places would also simplify things.

Describe alternatives you've considered

I'm not sure alternatives apply here.

Request to be unbanned (Why though?)

What'd I do that deserved a ban? Coudn't you have at least discussed it with me first?

That was mean, I didn't know you guys were like that. Because I got drunk that one and talked about my dreams of an AI-designed rocket?

--Sad and confused

Support for Ruby 2.6

Hi, thanks for making this! I'd love if this was a little more flexible in terms of requirements e.g. Ruby version - is this something you are open to adding? We could use a matrix in the CI for test coverage.

Accessing to response headers and code in v4

Is your feature request related to a problem? Please describe.
In version 3 there was a possibility to access the response code. In the current version 4.0.0, it's not possible.

Describe the solution you'd like
It would be good to access the response headers and code. Sometimes openai API returns 429 response code. Need to be able to retry in 1 second once again. Now it's not possible to use a retry mechanism.

Describe alternatives you've considered
Stick with v3 for now, as v4 returns via to_json JSON hash.

Incomplete response with streaming

I have a chat system working perfectly. When I updated to version 4 of the gem, everything went well (it works normally). However, when I add the 'stream' option to the OpenAI API call, the content of the response is incomplete.

This code works normally:

response = openai_client.chat(
  parameters: {
    model: "gpt-4",
    messages: [
      {role: "system", content: system_prompt},
      {role: "user", content: user_prompt}
    ],
    temperature: 0.4,
    user: "user_#{user_id}"
  }
)
puts "RESPONSE: #{response}"
response.dig("choices", 0, "message", "content")

However, when adding the streaming option:

response = openai_client.chat(
  parameters: {
    model: "gpt-4",
    messages: [
      {role: "system", content: system_prompt},
      {role: "user", content: user_prompt}
    ],
    temperature: 0.4,
    stream: proc do |chunk, _bytesize|
      new_content = chunk.dig("choices", 0, "delta", "content")
      if new_content
        answer.content = (answer.content || "") + new_content
        answer.save!
      end
    end,
    user: "user_#{user_id}"
  }
)
puts "RESPONSE: #{response}"
response.dig("choices", 0, "message", "content")

Screenshot with responses:
Captura de pantalla 2023-04-28 a las 12 29 24

Any clue as to why it could happen?

  • Rails 7.0.4.3
  • Ruby 3.2.1
  • OS: macOS
  • Browser ARC/Chrome

Getting expected multipart/form-data when uploading jsonl fine tuning and training data

Describe the bug
Trying to upload fine tune data as described in the README.md but my dataset is about 42mb in size.

client = OpenAI::Client.new(access_token: "XXX")
response = client.files.upload(parameters: { file: "chases_training.jsonl", purpose: "fine-tune" })

This produces the following errors:

{"error"=>
  {"message"=>
    "Invalid Content-Type header (application/json), expected multipart/form-data. (HINT: If you're using curl, you can pass -H 'Content-Type: multipart/form-data')",
   "type"=>"invalid_request_error",
   "param"=>nil,
   "code"=>nil}}

To Reproduce
Steps to reproduce the behavior:

  1. Follow the same steps as the README file of this project
  2. Try using a larger file

Expected behavior
It should upload the file

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: OSX Ventura 13.1

Additional context
I prepared the training data using these instructions

ruby gem for dalle 2 API

Hello, I love your gem, I used it to install some crazy cool text based features in our web app.

OpenAI has a dalle 2 API coming out at some point soon. Do you plan to make a ruby gem that could interface with that? I would love to see another great gem from your guys using that new resource.

undefined method `configure' for OpenAI:Module (NoMethodError)

Describe the bug
README instructions for configuring results in error.

To Reproduce
Steps to reproduce the behavior:

require 'openai'

OpenAI.configure do |config|
  config.access_token = ENV.fetch('OPENAI_API_KEY')
end

Expected behavior
No errors.

Actual behavior

undefined method `configure' for OpenAI:Module (NoMethodError)

Inclusion of dotenv creates undesirable side effects

Describe the bug
As a library, it's undesirable to include new configuration methods. For us, including this in our Rails app made our ENV vars be set to values in .env instead of .env.<environment>. This is due to ruby-openai being loaded before Rails configuration is thereby loading .env first in a non-Rails context. Most anyone including this gem in Rails projects will run into this problem. It will be best to remove dotenv altogether.

Thank you for this gem!

To Reproduce
Steps to reproduce the behavior:

  1. Create a Rails app
  2. Have your Rails app use both dotenv-rails and ruby-openai in your Gemfile
  3. Create .env and .env.development files with different values
  4. Observe that values in .env are taking precedence over environment-specific values despite the ordering listed here.

Expected behavior
Environment-specific values should take precedence over .env per the dotenv README. It's highly desirable to let users of this gem decide the methods they want to use to set configuration.

gemspec problems: spec.add_dependency "httparty", ">= 0.18.1", "< 0.22.0"

Describe the bug
This gem spec entry is causing numerous gem conflicts with httparty in a larger application

 spec.add_dependency "httparty", ">= 0.18.1", "< 0.22.0"

The version of 0.22.0 version of httparty does not yet exist on ruby gems.

Having such a spec breaks applications which cannot resolve this gem conflict.

See Also:

Screenshot 2023-01-10 at 4 59 02 PM

Functions Calling capability from ChatGPT model gpt-3.5-turbo-0613 not working

Describe the bug
This function throws a 500 error

**ArgumentError (unknown keyword: :functions):

app/controllers/openai_controller.rb:120:in `generate_insight'**

Am i doing something wrong

To Reproduce
Steps to reproduce the behavior:
# ChatGPT Prompt
begin
@aiResponseSleep = ChatGPTclient.completions(
model: "gpt-3.5-turbo-0613",
messages: [
{
role: "user",
content:"Your role is to be a Data Analyst with expert knowledge on sleep"
},
{
role: "user",
content:"
Your goal is to
- Provide a natural language comparison between these two data sets attached below make it short and interesting.
- Provide a comparison of the summariest from Today's day and past 3 Day
- Use days instead of full dates and bold these days
- Provide insights based on notes and based on your trained knowlege.
- Use an encouraging tone to make sure I keep up these habits
- Use HTML formatting and mark up to bold dates, number of days, REM, Deep, Light, Total
- Avoid displaying the data tables
"
},
{
role: "user",
content: "Today's Data: #{ formatted_latest_sleep_record.to_json }"
},
{
role: "user",
content: "Past 3 Days Sleep Recorded Data: #{ formatted_3days_data.to_json }"
}
]
)
rescue OpenaiChatgpt::Error => e
puts e.message
end

RFC: first-class response objects

Is your feature request related to a problem? Please describe.

Right now the gem returns raw HTTParty response objects which requires are certain amount of processing, especially on success, since the response may be successful but have an error-like response like "please retry with smaller completion length or fewer completions". Having an (opt-in) first-class response-object in the gem would solve the processing problem for all users of the gem. I'd be happy to make this contribution.

This would also make it easier to change the underlying gem from HTTParty to something else without affecting end-users.

Describe the solution you'd like

e.g. Response.from(http_response) would be an object which responds to success? but also has more information about specific response details such as choices and soft-errors like "please retry with smaller completion length or fewer completions".

Describe alternatives you've considered

Right now we do this work in our app.

Additional context

Let me know if I missed anything. I'd be happy to work on this, as an opt-in feature so that it doesn't break existing releases.

Whisper transcribe other languages

When I use openai whisper transcribe using python it automatically detects the language but it looks like it's not handling other languages but only English. Maybe a good feature to add :)

Thanks for this gem!

Allow clients to have different OpenAI configurations

Is your feature request related to a problem? Please describe.

I want to build a platform such that I can use multiple OpenAI credentials and configurations. For example, I may have a SaaS that uses models from different organizations. Or I may want to tier my users, such that one tier of users gets access to OpenAI at a higher usage threshold. In these cases I want a single application to be able to define a Client object with a given configuration for a per-user context. Currently that's not possible with this library.

Describe the solution you'd like

I thought that the global configuration would be a default, that could be overridden on creation of a OpenAI::Client object. Instead, based on the code here, invocation of a client overwrites the global configuration.

Converting the code to a global default, overriddable on a per-client basis, would be fairly straightforward for everything except the organization_id (since you might have a non-null default, which could be overridden on a per-client basis, potentially with nil).

Leaving this problem aside, this change could be accomplished with the following steps:

  1. The initializer for OpenAI::Client would be rewritten to set per-instance values for access_token, organization_id, and api_version based on arguments and default to those stored in Ruby::OpenAI::configuration if nil (or a NullObject for organization_id)
  2. There'd be a general conversion of the OpenAI::Client class methods to instance methods, potentially leaving the existing public class methods in place as ones that use the 'default' client (one that uses the global configuration)

I recognize that this would be a pretty significant change, but as-is the gem can't support any real multi-tenant use case. My thoughts would be that this might make sense as a v4 change.

Describe alternatives you've considered

None really. This is how most libraries of this nature (including Dalli, which I maintain) handle multi-tenancy with default configurations. The organization_id makes it somewhat more ambiguous, but could probably be solved pretty easily with a NullObject pattern in the OpenAI::Client initializer.

Additional context

I'd be happy to put up a PR if there's interest.

It might also make sense to look at supporting connection pools as a further step towards supporting usage in a multi-tenant SaaS.

Helicone setup

Describe the bug

I've just tested Helicone setup as described in the README

To Reproduce
Steps to reproduce the behavior:

  1. Downliad bin/console and run it

  2. Set OpenAI client with the helicone URL:

    client = OpenAI::Client.new access_token: "sk-FOOBAR", uri_base: "https://oai.hconeai.com/", request_timeout: 240
  3. See error

    => #<OpenAI::Client:0x0000aaab0c9c3648>
    irb(main):002:0> client.models.list
    => 
    {"helicone-message"=>"Helicone ran into an error servicing your request: SyntaxError: Unexpected end of JSON input",
     "support"=>"Please reach out on our discord or email us at [email protected], we'd love to help!",
     "helicone-error"=>"{}"}
    

Additional context
Basically extra header should be provided to get it running (from their example doc --header 'Helicone-Auth: Bearer sk-HELICONE_API_KEY')

NoMethodError: undefined method `arity' for "application/json":String

Describe the bug
NoMethodError: undefined method `arity' for "application/json":String

To Reproduce
Steps to reproduce the behavior:

  1. client = OpenAI::Client.new(access_token: '')
  2. client.models.list

Expected behavior
No Error

Screenshots
Screenshot 2023-02-10 at 13 58 37

Desktop (please complete the following information):

  • OS: macOS
  • Browser chrome

Undefined method `completions' for #<OpenAI::Client:0x00007fe1a2e621a8>

Thank for you for the time creating this Gem. When I try to test it out with an example I get the error:

NoMethodError (undefined method completions' for #OpenAI::Client:0x00007fe1ab35e2d0):`

When I unpack the gem the client.rb looks like:
`module OpenAI
class Client
include HTTParty
base_uri "https://api.openai.com"

def initialize(access_token: nil)
  @access_token = access_token || ENV["OPENAI_ACCESS_TOKEN"]
end

def call(engine:, prompt:, max_tokens:, version: "v1")
  self.class.post(
    "/#{version}/engines/#{engine}/completions",
    headers: {
      "Content-Type" => "application/json",
      "Authorization" => "Bearer #{@access_token}"
    },
    body: {
      prompt: prompt, max_tokens: max_tokens
    }.to_json
  )
end

end
end
`

Could it be that the latest code hasn't been committed and can be retrieved by the gems?

Streaming: Cancel stream

Is your feature request related to a problem? Please describe.

  • Problem: wasting tokens continuing to stream in background jobs when no longer needed

Describe the solution you'd like

  • Make conn accessible in the stream: callback so that Faraday #close could be called whenever the stream needed to be cancelled

Would love feedback on this. Thanks!

create finetunes model error

response = client.finetunes.create(
parameters: {
training_file: file_id,
model: "xxi"
})

response body:

Invalid base model: xxi (model must be one of ada, babbage, curie, davinci) or a fine-tuned model created by your or

please help !

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.