Ruby on Rails调用ruby-openAI gem遇Net::ReadTimeout,如何延长超时?
解决ruby-openAI gem调用OpenAI时的超时问题
全局设置超时(所有请求生效)
在初始化OpenAI客户端时,直接指定timeout参数(单位:秒),比如设置为60秒:
@client = OpenAI::Client.new( access_token: "你的API密钥", timeout: 60 # 自定义超时时长 )
单个请求单独设置超时
如果只需要给特定请求延长超时,可在调用completions时额外传入timeout参数,覆盖全局设置:
response = @client.completions( parameters: { model: "text-davinci-003", prompt: "In the style of #{@as_written_by}, write a longer article in HTML of at least 750 words using the #{article} as the primary source and basis for the new article, and include interesting facts from the #{secondary_sources}, with a tags around the source of the information pointing to the original URLs", max_tokens: 3000 }, timeout: 60 # 仅对当前请求生效的超时设置 )
额外建议
你设置的max_tokens为3000,属于较大的生成量,OpenAI处理这类请求耗时会更长,建议将超时设置在30-60秒区间,根据实际情况调整。如果仍出现超时,可尝试拆分prompt,减少单次生成的token数量,分多次请求拼接结果。
内容的提问来源于stack exchange,提问作者Ammar khalid
相关产品推荐
相关产品推荐

