ruby-openai 7.2.0 → 7.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7108bf76aa6f30cd7c38b41967b0162d6a4014698c1c8364b8116e5c665c044f
4
- data.tar.gz: 736587458668b4608e49fa8ec50e460da14eeda0dfb8a739c68806e3bd5d0a2c
3
+ metadata.gz: 81b77c3ad0c06f3dc20d9902e136e8f5cde83ab6073fd421d3cf8ba0fb84d7b1
4
+ data.tar.gz: e61927396de52b29f7de6fdaf4271715afaff51ef3c27f3bf9316673af31ad48
5
5
  SHA512:
6
- metadata.gz: 76cac4818b5941d5732becebc91675c1ebeaba4f27c1710888afbb1893f3f8ff4d18e24a3c90f4d4332eb89730b0f6195507cf99519e764876a614ca3927a0cb
7
- data.tar.gz: 4757d8e11b494a75d0ea839ae073610adfa77317ba0e13ddabdd21bf10fd34127a5747138644dba4d9073aa509341bfeaed69a3d00150d1ae90a42ddeadd53e4
6
+ metadata.gz: 4f7849cb0907df734b1f37fe5eac6c7353ce06dcf2ed31571e45548944d848ca87b7b1ab4916dc021216ed96c17b4d415d749981620bc65c1efc575aff084ab3
7
+ data.tar.gz: eaf47c514add119c4b1ee3f6efedc965f239f5fe57ef9232f834de62834364249ecd02beb86126ddc38c2a0757239c54f02c5a6839b9544214cec09ce0723690
data/CHANGELOG.md CHANGED
@@ -5,6 +5,18 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [7.3.1] - 2024-10-15
9
+
10
+ ### Fixed
11
+
12
+ - Fix 404 error when using Client#embeddings with Azure - thanks to [@ymtdzzz](https://github.com/ymtdzzz) for raising this in a really clear issue.
13
+
14
+ ## [7.3.0] - 2024-10-11
15
+
16
+ ### Added
17
+
18
+ - Add ability to (with the right incantations) retrieve the chunks used by an Assistant file search - thanks to [@agamble](https://github.com/agamble) for the addition!
19
+
8
20
  ## [7.2.0] - 2024-10-10
9
21
 
10
22
  ### Added
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ruby-openai (7.2.0)
4
+ ruby-openai (7.3.1)
5
5
  event_stream_parser (>= 0.3.0, < 2.0.0)
6
6
  faraday (>= 1)
7
7
  faraday-multipart (>= 1)
data/README.md CHANGED
@@ -50,6 +50,7 @@ Stream text with GPT-4o, transcribe and translate audio with Whisper, or create
50
50
  - [Runs](#runs)
51
51
  - [Create and Run](#create-and-run)
52
52
  - [Runs involving function tools](#runs-involving-function-tools)
53
+ - [Exploring chunks used in File Search](#exploring-chunks-used-in-file-search)
53
54
  - [Image Generation](#image-generation)
54
55
  - [DALL·E 2](#dalle-2)
55
56
  - [DALL·E 3](#dalle-3)
@@ -1111,6 +1112,113 @@ end
1111
1112
 
1112
1113
  Note that you have 10 minutes to submit your tool output before the run expires.
1113
1114
 
1115
+ #### Exploring chunks used in File Search
1116
+
1117
+ Take a deep breath. You might need a drink for this one.
1118
+
1119
+ It's possible for OpenAI to share what chunks it used in its internal RAG Pipeline to create its filesearch results.
1120
+
1121
+ An example spec can be found [here](https://github.com/alexrudall/ruby-openai/blob/main/spec/openai/client/assistant_file_search_spec.rb) that does this, just so you know it's possible.
1122
+
1123
+ Here's how to get the chunks used in a file search. In this example I'm using [this file](https://css4.pub/2015/textbook/somatosensory.pdf):
1124
+
1125
+ ```
1126
+ require "openai"
1127
+
1128
+ # Make a client
1129
+ client = OpenAI::Client.new(
1130
+ access_token: "access_token_goes_here",
1131
+ log_errors: true # Don't do this in production.
1132
+ )
1133
+
1134
+ # Upload your file(s)
1135
+ file_id = client.files.upload(
1136
+ parameters: {
1137
+ file: "path/to/somatosensory.pdf",
1138
+ purpose: "assistants"
1139
+ }
1140
+ )["id"]
1141
+
1142
+ # Create a vector store to store the vectorised file(s)
1143
+ vector_store_id = client.vector_stores.create(parameters: {})["id"]
1144
+
1145
+ # Vectorise the file(s)
1146
+ vector_store_file_id = client.vector_store_files.create(
1147
+ vector_store_id: vector_store_id,
1148
+ parameters: { file_id: file_id }
1149
+ )["id"]
1150
+
1151
+ # Check that the file is vectorised (wait for status to be "completed")
1152
+ client.vector_store_files.retrieve(vector_store_id: vector_store_id, id: vector_store_file_id)["status"]
1153
+
1154
+ # Create an assistant, referencing the vector store
1155
+ assistant_id = client.assistants.create(
1156
+ parameters: {
1157
+ model: "gpt-4o",
1158
+ name: "Answer finder",
1159
+ instructions: "You are a file search tool. Find the answer in the given files, please.",
1160
+ tools: [
1161
+ { type: "file_search" }
1162
+ ],
1163
+ tool_resources: {
1164
+ file_search: {
1165
+ vector_store_ids: [vector_store_id]
1166
+ }
1167
+ }
1168
+ }
1169
+ )["id"]
1170
+
1171
+ # Create a thread with your question
1172
+ thread_id = client.threads.create(parameters: {
1173
+ messages: [
1174
+ { role: "user",
1175
+ content: "Find the description of a nociceptor." }
1176
+ ]
1177
+ })["id"]
1178
+
1179
+ # Run the thread to generate the response. Include the "GIVE ME THE CHUNKS" incantation.
1180
+ run_id = client.runs.create(
1181
+ thread_id: thread_id,
1182
+ parameters: {
1183
+ assistant_id: assistant_id
1184
+ },
1185
+ query_parameters: { include: ["step_details.tool_calls[*].file_search.results[*].content"] } # incantation
1186
+ )["id"]
1187
+
1188
+ # Get the steps that happened in the run
1189
+ steps = client.run_steps.list(
1190
+ thread_id: thread_id,
1191
+ run_id: run_id,
1192
+ parameters: { order: "asc" }
1193
+ )
1194
+
1195
+ # Retrieve all the steps. Include the "GIVE ME THE CHUNKS" incantation again.
1196
+ steps = steps["data"].map do |step|
1197
+ client.run_steps.retrieve(
1198
+ thread_id: thread_id,
1199
+ run_id: run_id,
1200
+ id: step["id"],
1201
+ parameters: { include: ["step_details.tool_calls[*].file_search.results[*].content"] } # incantation
1202
+ )
1203
+ end
1204
+
1205
+ # Now we've got the chunk info, buried deep. Loop through the steps and find chunks if included:
1206
+ chunks = steps.flat_map do |step|
1207
+ included_results = step.dig("step_details", "tool_calls", 0, "file_search", "results")
1208
+
1209
+ next if included_results.nil? || included_results.empty?
1210
+
1211
+ included_results.flat_map do |result|
1212
+ result["content"].map do |content|
1213
+ content["text"]
1214
+ end
1215
+ end
1216
+ end.compact
1217
+
1218
+ # The first chunk will be the closest match to the prompt. Finally, if you want to view the completed message(s):
1219
+ client.messages.list(thread_id: thread_id)
1220
+ ```
1221
+
1114
1222
  ### Image Generation
1115
1223
 
1116
1224
  Generate images using DALL·E 2 or DALL·E 3!
data/lib/openai/http.rb CHANGED
@@ -18,9 +18,10 @@ module OpenAI
18
18
  end&.body)
19
19
  end
20
20
 
21
- def json_post(path:, parameters:)
21
+ def json_post(path:, parameters:, query_parameters: {})
22
22
  conn.post(uri(path: path)) do |req|
23
23
  configure_json_post_request(req, parameters)
24
+ req.params = req.params.merge(query_parameters)
24
25
  end&.body
25
26
  end
26
27
 
@@ -8,8 +8,8 @@ module OpenAI
8
8
  @client.get(path: "/threads/#{thread_id}/runs/#{run_id}/steps", parameters: parameters)
9
9
  end
10
10
 
11
- def retrieve(thread_id:, run_id:, id:)
12
- @client.get(path: "/threads/#{thread_id}/runs/#{run_id}/steps/#{id}")
11
+ def retrieve(thread_id:, run_id:, id:, parameters: {})
12
+ @client.get(path: "/threads/#{thread_id}/runs/#{run_id}/steps/#{id}", parameters: parameters)
13
13
  end
14
14
  end
15
15
  end
data/lib/openai/runs.rb CHANGED
@@ -12,8 +12,9 @@ module OpenAI
12
12
  @client.get(path: "/threads/#{thread_id}/runs/#{id}")
13
13
  end
14
14
 
15
- def create(thread_id:, parameters: {})
16
- @client.json_post(path: "/threads/#{thread_id}/runs", parameters: parameters)
15
+ def create(thread_id:, parameters: {}, query_parameters: {})
16
+ @client.json_post(path: "/threads/#{thread_id}/runs", parameters: parameters,
17
+ query_parameters: query_parameters)
17
18
  end
18
19
 
19
20
  def modify(id:, thread_id:, parameters: {})
@@ -1,3 +1,3 @@
1
1
  module OpenAI
2
- VERSION = "7.2.0".freeze
2
+ VERSION = "7.3.1".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-openai
3
3
  version: !ruby/object:Gem::Version
4
- version: 7.2.0
4
+ version: 7.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-10-10 00:00:00.000000000 Z
11
+ date: 2024-10-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: event_stream_parser