spectre_ai 1.1.3 → 1.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d6cce97e6ac3ab3cde8536f7d422609ff35756847c562769f21305a68421cb89
4
- data.tar.gz: cd092c3a8eef87550999630dc77c66eb8900ff24ed4129eff32f8050cb90a5dd
3
+ metadata.gz: e92299b643fbf7d928b9c45de5ad9a504528cbb246202d56cb24d36a32030030
4
+ data.tar.gz: 8dc5d19040a9cac2cc929a1a91b55112d0e78fa21ca8de05bb6c3544838af9ed
5
5
  SHA512:
6
- metadata.gz: e4b701d7447eabb48a5c82e0d1bcbf5585c68497a49c513a81a0091e409aa84fd44d3c45e39b2b88ff53ef4643c63e047f24e88a705c3358b08be408cd0f9ed1
7
- data.tar.gz: 639a433617ffc983cc078d26a22b516d415086c66e5091e42e1c85219f6885efe64daf924431182e1589db073a19bb8ab330c8a71793a0d5f7a95a2a2e722237
6
+ metadata.gz: 8ea61b8b0a0e23d7a5c500fd99e147942fd33538e21d38be0030aff60dda20758a87651b9460d4b4de50f986ffe068da4dd29f172dc0a25a993595eadee34fe6
7
+ data.tar.gz: b4ef2e616f0143f677c635e6d999e155a1205a1ab90ac47fc2a8c322b95ee6f107522dc4c2d8a16e33a856f3387ffdb1e7ac85ca9f83b3a72960e9b8e6d52ca1
data/CHANGELOG.md CHANGED
@@ -95,6 +95,7 @@ This version enhances the flexibility and robustness of the Completions class, e
95
95
  * **Example**: If you're using `spectre` inside a gem, the `detect_prompts_path` method will now correctly resolve the prompts path within the gem project root.
96
96
  * If no markers are found, the system falls back to the current working directory (`Dir.pwd`).
97
97
 
98
+
98
99
  # Changelog for Version 1.1.3
99
100
 
100
101
  **Release Date:** [2nd Dec 2024]
@@ -102,4 +103,39 @@ This version enhances the flexibility and robustness of the Completions class, e
102
103
  **Fixes:**
103
104
 
104
105
  * **Removed unnecessary validations in `Completions` class**
105
- * Removed redundant validations in the `Completions` class that were causing unnecessary errors in specific edge cases. LLM providers returns a proper errors messages now.
106
+ * Removed redundant validations in the `Completions` class that were causing unnecessary errors in specific edge cases. LLM providers returns a proper errors messages now.
107
+
108
+
109
+ # Changelog for Version 1.1.4
110
+
111
+ **Release Date:** [5th Dec 2024]
112
+
113
+ **New Features:**
114
+
115
+ * Customizable Timeout for API Requests
116
+ * Introduced DEFAULT_TIMEOUT constant (set to 60 seconds) for managing request timeouts across the Completions and Embeddings classes.
117
+ * Added optional arguments (args) to create methods, allowing users to override read_timeout and open_timeout dynamically.
118
+ * This change ensures greater flexibility when dealing with varying network conditions or API response times.
119
+
120
+ **Example Usage:**
121
+
122
+ ```ruby
123
+ Spectre::Openai::Completions.create(
124
+ messages: messages,
125
+ read_timeout: 30,
126
+ open_timeout: 20
127
+ )
128
+ ```
129
+
130
+ **Key Changes:**
131
+
132
+ * **Updated Completions class:**
133
+ * http.read_timeout = args.fetch(:read_timeout, DEFAULT_TIMEOUT)
134
+ * http.open_timeout = args.fetch(:open_timeout, DEFAULT_TIMEOUT)
135
+ * Updated Embeddings class with the same timeout handling logic.
136
+
137
+ **Fixes:**
138
+
139
+ * Simplified Exception Handling for Timeouts
140
+ * Removed explicit handling of Net::OpenTimeout and Net::ReadTimeout exceptions in both Completions and Embeddings classes.
141
+ * Letting these exceptions propagate ensures clearer and more consistent error messages for timeout issues.
data/README.md CHANGED
@@ -1,4 +1,6 @@
1
- # Spectre [![Gem Version](https://badge.fury.io/rb/spectre_ai.svg)](https://badge.fury.io/rb/spectre_ai)
1
+ # <img src='logo.svg' height='120' alt='Spectre Logo' />
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/spectre_ai.svg)](https://badge.fury.io/rb/spectre_ai)
2
4
 
3
5
  **Spectre** is a Ruby gem that makes it easy to AI-enable your Ruby on Rails application. Currently, Spectre focuses on helping developers create embeddings, perform vector-based searches, create chat completions, and manage dynamic prompts — ideal for applications that are featuring RAG (Retrieval-Augmented Generation), chatbots and dynamic prompts.
4
6
 
@@ -9,6 +9,7 @@ module Spectre
9
9
  class Completions
10
10
  API_URL = 'https://api.openai.com/v1/chat/completions'
11
11
  DEFAULT_MODEL = 'gpt-4o-mini'
12
+ DEFAULT_TIMEOUT = 60
12
13
 
13
14
  # Class method to generate a completion based on user messages and optional tools
14
15
  #
@@ -17,10 +18,11 @@ module Spectre
17
18
  # @param json_schema [Hash, nil] An optional JSON schema to enforce structured output
18
19
  # @param max_tokens [Integer] The maximum number of tokens for the completion (default: 50)
19
20
  # @param tools [Array<Hash>, nil] An optional array of tool definitions for function calling
21
+ # @param args [Hash] Optional arguments like timeouts
20
22
  # @return [Hash] The parsed response including any function calls or content
21
23
  # @raise [APIKeyNotConfiguredError] If the API key is not set
22
24
  # @raise [RuntimeError] For general API errors or unexpected issues
23
- def self.create(messages:, model: DEFAULT_MODEL, json_schema: nil, max_tokens: nil, tools: nil)
25
+ def self.create(messages:, model: DEFAULT_MODEL, json_schema: nil, max_tokens: nil, tools: nil, **args)
24
26
  api_key = Spectre.api_key
25
27
  raise APIKeyNotConfiguredError, "API key is not configured" unless api_key
26
28
 
@@ -29,8 +31,8 @@ module Spectre
29
31
  uri = URI(API_URL)
30
32
  http = Net::HTTP.new(uri.host, uri.port)
31
33
  http.use_ssl = true
32
- http.read_timeout = 10 # seconds
33
- http.open_timeout = 10 # seconds
34
+ http.read_timeout = args.fetch(:read_timeout, DEFAULT_TIMEOUT)
35
+ http.open_timeout = args.fetch(:open_timeout, DEFAULT_TIMEOUT)
34
36
 
35
37
  request = Net::HTTP::Post.new(uri.path, {
36
38
  'Content-Type' => 'application/json',
@@ -49,8 +51,6 @@ module Spectre
49
51
  handle_response(parsed_response)
50
52
  rescue JSON::ParserError => e
51
53
  raise "JSON Parse Error: #{e.message}"
52
- rescue Net::OpenTimeout, Net::ReadTimeout => e
53
- raise "Request Timeout: #{e.message}"
54
54
  end
55
55
 
56
56
  private
@@ -9,23 +9,25 @@ module Spectre
9
9
  class Embeddings
10
10
  API_URL = 'https://api.openai.com/v1/embeddings'
11
11
  DEFAULT_MODEL = 'text-embedding-3-small'
12
+ DEFAULT_TIMEOUT = 60
12
13
 
13
14
  # Class method to generate embeddings for a given text
14
15
  #
15
16
  # @param text [String] the text input for which embeddings are to be generated
16
17
  # @param model [String] the model to be used for generating embeddings, defaults to DEFAULT_MODEL
18
+ # # @param args [Hash] Optional arguments like timeouts
17
19
  # @return [Array<Float>] the generated embedding vector
18
20
  # @raise [APIKeyNotConfiguredError] if the API key is not set
19
21
  # @raise [RuntimeError] for general API errors or unexpected issues
20
- def self.create(text, model: DEFAULT_MODEL)
22
+ def self.create(text, model: DEFAULT_MODEL, **args)
21
23
  api_key = Spectre.api_key
22
24
  raise APIKeyNotConfiguredError, "API key is not configured" unless api_key
23
25
 
24
26
  uri = URI(API_URL)
25
27
  http = Net::HTTP.new(uri.host, uri.port)
26
28
  http.use_ssl = true
27
- http.read_timeout = 10 # seconds
28
- http.open_timeout = 10 # seconds
29
+ http.read_timeout = args.fetch(:read_timeout, DEFAULT_TIMEOUT)
30
+ http.open_timeout = args.fetch(:open_timeout, DEFAULT_TIMEOUT)
29
31
 
30
32
  request = Net::HTTP::Post.new(uri.path, {
31
33
  'Content-Type' => 'application/json',
@@ -42,8 +44,6 @@ module Spectre
42
44
  JSON.parse(response.body).dig('data', 0, 'embedding')
43
45
  rescue JSON::ParserError => e
44
46
  raise "JSON Parse Error: #{e.message}"
45
- rescue Net::OpenTimeout, Net::ReadTimeout => e
46
- raise "Request Timeout: #{e.message}"
47
47
  end
48
48
  end
49
49
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Spectre # :nodoc:all
4
- VERSION = "1.1.3"
4
+ VERSION = "1.1.4"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spectre_ai
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.3
4
+ version: 1.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ilya Klapatok
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2024-12-02 00:00:00.000000000 Z
12
+ date: 2024-12-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec-rails