ruby-openai
ruby-openai copied to clipboard
When json_post is executed, is JSON.parse not executed on the response?
I noticed that in the recent commit (dc4d1c6b8d0d52ae5897e8203912b9b047ac981f) on https://github.com/alexrudall/ruby-openai, the processing for JSON.parse within the json_post method seems to have been removed. Could you please clarify the reason behind this change?
https://github.com/alexrudall/ruby-openai/commit/dc4d1c6b8d0d52ae5897e8203912b9b047ac981f
After creating a stub with WebMock as shown below, I encountered an error during the execution of dig('choices', 0, 'message', 'content') due to the absence of JSON.parse being executed.
response = {
"model" => "gpt-4-0613",
"choices" => [
{
"message" => {
"role" => "assistant",
"content" => "Response from the API",
},
"finish_reason" => "stop",
"index" => 0
}
]
}
stub_request(:post, "https://api.openai.com/v1/chat/completions")
.to_return(status: 200, body: response.to_json, headers: {})
ERROR -- : undefined method `dig' for "{\"model\":\"gpt-4-0613\",\"choices\":[{\"message\":{\"role\":\"assistant\",\"content\":\"Response from the API\"},\"finish_reason\":\"stop\",\"index\":0}]}":String
response.dig('choices', 0, 'message', 'content')
^^^^ (NoMethodError)
It seems like the removal of JSON.parse from json_post might be the cause of this issue. What are your thoughts on this?
I ran into this error too. In lieu for there being a solution, I came up with this monkey patch:
# workaround for https://github.com/alexrudall/ruby-openai/issues/416
if Rails.env.test?
module OpenAIClientExtensions
def chat(**kwargs)
rez = super
JSON.parse(rez)
end
end
module OpenAI
class Client
prepend OpenAIClientExtensions
end
end
end