|
1 | 1 | require "faraday"
|
2 | 2 | require "faraday/multipart" if Gem::Version.new(Faraday::VERSION) >= Gem::Version.new("2.0")
|
3 |
| - |
4 | 3 | require_relative "openai/http"
|
5 | 4 | require_relative "openai/client"
|
6 | 5 | require_relative "openai/files"
|
@@ -31,13 +30,7 @@ def call(env)
|
31 | 30 | @app.call(env)
|
32 | 31 | rescue Faraday::Error => e
|
33 | 32 | raise e unless e.response.is_a?(Hash)
|
34 |
| - |
35 |
| - logger = Logger.new($stdout) |
36 |
| - logger.formatter = proc do |_severity, _datetime, _progname, msg| |
37 |
| - "\033[31mOpenAI HTTP Error (spotted in ruby-openai #{VERSION}): #{msg}\n\033[0m" |
38 |
| - end |
39 |
| - logger.error(e.response[:body]) |
40 |
| - |
| 33 | + OpenAI.log_message("OpenAI HTTP Error", e.response[:body], :error) |
41 | 34 | raise e
|
42 | 35 | end
|
43 | 36 | end
|
@@ -73,25 +66,37 @@ def initialize
|
73 | 66 |
|
74 | 67 | class << self
|
75 | 68 | attr_writer :configuration
|
76 |
| - end |
77 | 69 |
|
78 |
| - def self.configuration |
79 |
| - @configuration ||= OpenAI::Configuration.new |
80 |
| - end |
| 70 | + def configuration |
| 71 | + @configuration ||= OpenAI::Configuration.new |
| 72 | + end |
81 | 73 |
|
82 |
| - def self.configure |
83 |
| - yield(configuration) |
84 |
| - end |
| 74 | + def configure |
| 75 | + yield(configuration) |
| 76 | + end |
85 | 77 |
|
86 |
| - # Estimate the number of tokens in a string, using the rules of thumb from OpenAI: |
87 |
| - # https://help.openai.com/en/articles/4936856-what-are-tokens-and-how-to-count-them |
88 |
| - def self.rough_token_count(content = "") |
89 |
| - raise ArgumentError, "rough_token_count requires a string" unless content.is_a? String |
90 |
| - return 0 if content.empty? |
| 78 | + # Estimate the number of tokens in a string, using the rules of thumb from OpenAI: |
| 79 | + # https://help.openai.com/en/articles/4936856-what-are-tokens-and-how-to-count-them |
| 80 | + def rough_token_count(content = "") |
| 81 | + raise ArgumentError, "rough_token_count requires a string" unless content.is_a? String |
| 82 | + return 0 if content.empty? |
| 83 | + count_by_chars = content.size / 4.0 |
| 84 | + count_by_words = content.split.size * 4.0 / 3 |
| 85 | + estimate = ((count_by_chars + count_by_words) / 2.0).round |
| 86 | + [1, estimate].max |
| 87 | + end |
91 | 88 |
|
92 |
| - count_by_chars = content.size / 4.0 |
93 |
| - count_by_words = content.split.size * 4.0 / 3 |
94 |
| - estimate = ((count_by_chars + count_by_words) / 2.0).round |
95 |
| - [1, estimate].max |
| 89 | + # Log a message with appropriate formatting |
| 90 | + # @param prefix [String] Prefix to add to the message |
| 91 | + # @param message [String] The message to log |
| 92 | + # @param level [Symbol] The log level (:error, :warn, etc.) |
| 93 | + def log_message(prefix, message, level = :warn) |
| 94 | + color = level == :error ? "\033[31m" : "\033[33m" |
| 95 | + logger = Logger.new($stdout) |
| 96 | + logger.formatter = proc do |_severity, _datetime, _progname, msg| |
| 97 | + "#{color}#{prefix} (spotted in ruby-openai #{VERSION}): #{msg}\n\033[0m" |
| 98 | + end |
| 99 | + logger.send(level, message) |
| 100 | + end |
96 | 101 | end
|
97 | 102 | end
|
0 commit comments