spectre_ai 1.1.2 → 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: 07e44eac15c5b58b13b2dbbb784f1ca4d5364d6aed7f2fe5a5d797c0ee5fd43d
4
- data.tar.gz: 3d33fb9b5c228eb4577f678d5e2f310d401671cd242b0ca02bcf9a80df55fad8
3
+ metadata.gz: e92299b643fbf7d928b9c45de5ad9a504528cbb246202d56cb24d36a32030030
4
+ data.tar.gz: 8dc5d19040a9cac2cc929a1a91b55112d0e78fa21ca8de05bb6c3544838af9ed
5
5
  SHA512:
6
- metadata.gz: 17c0de5cfb63e7d072e1c85d221abbdadabd2eb131276b22ceff834c7eaea05ce3a2a1a02c826887d39b54619c8841d651a7aaa3823a5eabf4f0f3faaf5c263e
7
- data.tar.gz: 14ffff5c963ea79c2360e9517c9906f36b3bc27494732c3f3bd640c0f492aacdd0bdaf190be2effd5bbb9bc875315afe441c8583c07f3ff2faf6f7a8cdcdcceb
6
+ metadata.gz: 8ea61b8b0a0e23d7a5c500fd99e147942fd33538e21d38be0030aff60dda20758a87651b9460d4b4de50f986ffe068da4dd29f172dc0a25a993595eadee34fe6
7
+ data.tar.gz: b4ef2e616f0143f677c635e6d999e155a1205a1ab90ac47fc2a8c322b95ee6f107522dc4c2d8a16e33a856f3387ffdb1e7ac85ca9f83b3a72960e9b8e6d52ca1
data/CHANGELOG.md CHANGED
@@ -93,4 +93,49 @@ This version enhances the flexibility and robustness of the Completions class, e
93
93
  * The `Spectre::Prompt.render` method now dynamically detects the project root based on the presence of project-specific markers, such as `Gemfile`, `.git`, or `config/application.rb`.
94
94
  * This change allows for greater flexibility when using spectre in different environments and projects, ensuring the prompt templates are found regardless of where spectre is used.
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
- * If no markers are found, the system falls back to the current working directory (`Dir.pwd`).
96
+ * If no markers are found, the system falls back to the current working directory (`Dir.pwd`).
97
+
98
+
99
+ # Changelog for Version 1.1.3
100
+
101
+ **Release Date:** [2nd Dec 2024]
102
+
103
+ **Fixes:**
104
+
105
+ * **Removed unnecessary validations in `Completions` class**
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
@@ -72,27 +72,6 @@ module Spectre
72
72
  if messages.empty?
73
73
  raise ArgumentError, "Messages cannot be empty."
74
74
  end
75
-
76
- # Iterate through each message and perform detailed validation.
77
- messages.each_with_index do |msg, index|
78
- # Check if each message hash contains the required keys: :role and :content.
79
- # These keys are necessary for defining the type of message and its content.
80
- unless msg.key?(:role) && msg.key?(:content)
81
- raise ArgumentError, "Message at index #{index} must contain both :role and :content keys."
82
- end
83
-
84
- # Check if the role is one of the allowed values: 'system', 'user', or 'assistant'.
85
- # This ensures that each message has a valid role identifier.
86
- unless %w[system user assistant].include?(msg[:role])
87
- raise ArgumentError, "Invalid role '#{msg[:role]}' at index #{index}. Valid roles are 'system', 'user', 'assistant'."
88
- end
89
-
90
- # Check if the content is a non-empty string.
91
- # This prevents empty or non-string content, which would be meaningless in a conversation.
92
- unless msg[:content].is_a?(String) && !msg[:content].strip.empty?
93
- raise ArgumentError, "Content for message at index #{index} must be a non-empty string."
94
- end
95
- end
96
75
  end
97
76
 
98
77
  # Helper method to generate the request body
@@ -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.2"
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.2
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-10-14 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