alexrudall / ruby-openai

OpenAI API + Ruby! 🤖❤️ NEW: Assistant Vector Stores
MIT License
2.61k stars 302 forks source link

Fix Maintain Unchanged Hash Parameter in Method json_post #351

Closed martinjaimem closed 8 months ago

martinjaimem commented 8 months ago

Description:

When calling the chat method, the parameters used when calling it are being altered, specifically the parameters[:stream] entry:

parameters = {
     model: "gpt-3.5-turbo", # 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
}
client.chat(parameters: parameters)

puts parameters[:stream]
# true

This can cause bugs as parameters should remain unchanged, we noticed it when we tried retrying the request on some OpenAi network failures, we called the method again with the same parameters and noticed streaming wouldn't work then.

Impact: This change does not affect the external behavior of the chat method but ensures parameter integrity across multiple calls.

This PR was made in collaboration with @vickymadrid03 and @nicastelo 👏