@@ -1142,10 +1142,13 @@ file_id = client.files.upload(
1142
1142
vector_store_id = client.vector_stores.create(parameters: {})["id"]
1143
1143
1144
1144
# Vectorise the file(s)
1145
- client.vector_store_files.create(
1145
+ vector_store_file_id = client.vector_store_files.create(
1146
1146
vector_store_id: vector_store_id,
1147
1147
parameters: { file_id: file_id }
1148
- )
1148
+ )["id"]
1149
+
1150
+ # Check that the file is vectorised (wait for status to be "completed")
1151
+ client.vector_store_files.retrieve(vector_store_id: vector_store_id, id: vector_store_file_id)["status"]
1149
1152
1150
1153
# Create an assistant, referencing the vector store
1151
1154
assistant_id = client.assistants.create(
@@ -1165,21 +1168,21 @@ assistant_id = client.assistants.create(
1165
1168
)["id"]
1166
1169
1167
1170
# Create a thread with your question
1168
- client.threads.create(parameters: {
1171
+ thread_id = client.threads.create(parameters: {
1169
1172
messages: [
1170
1173
{ role: "user",
1171
1174
content: "Find the description of a nociceptor." }
1172
1175
]
1173
- })
1176
+ })["id"]
1174
1177
1175
1178
# Run the thread to generate the response. Include the "GIVE ME THE CHUNKS" incantation.
1176
- client.runs.create(
1179
+ run_id = client.runs.create(
1177
1180
thread_id: thread_id,
1178
- query_parameters: { include: ["step_details.tool_calls[*].file_search.results[*].content"] }, # incantation
1179
1181
parameters: {
1180
1182
assistant_id: assistant_id
1181
- }
1182
- )
1183
+ },
1184
+ query_parameters: { include: ["step_details.tool_calls[*].file_search.results[*].content"] } # incantation
1185
+ )["id"]
1183
1186
1184
1187
# Get the steps that happened in the run
1185
1188
steps = client.run_steps.list(
@@ -1189,20 +1192,33 @@ steps = client.run_steps.list(
1189
1192
)
1190
1193
1191
1194
# Get the last step ID (or whichever one you want to look at)
1192
- step_id = steps["data"].last ["id"]
1195
+ step_id = steps["data"].first ["id"]
1193
1196
1194
- # Get the last step (or whichever one you need). Include the "GIVE ME THE CHUNKS" incantation again.
1195
- step = client.run_steps.retrieve(
1196
- thread_id: thread_id,
1197
- run_id: run_id,
1198
- id: step_id,
1199
- parameters: { include: ["step_details.tool_calls[*].file_search.results[*].content"] } # incantation
1200
- )
1197
+ # Retrieve all the steps. Include the "GIVE ME THE CHUNKS" incantation again.
1198
+ steps = steps["data"].map do |step|
1199
+ client.run_steps.retrieve(
1200
+ thread_id: thread_id,
1201
+ run_id: run_id,
1202
+ id: step["id"],
1203
+ parameters: { include: ["step_details.tool_calls[*].file_search.results[*].content"] } # incantation
1204
+ )
1205
+ end
1201
1206
1202
- # Now we've got the chunk info, buried deep:
1203
- print result.dig("step_details", "tool_calls", 0, "file_search", "results", 0, "content", 0, "text")
1207
+ # Now we've got the chunk info, buried deep. Loop through the steps and find chunks if included:
1208
+ chunks = steps.flat_map do |step|
1209
+ included_results = step.dig("step_details", "tool_calls", 0, "file_search", "results")
1210
+
1211
+ next if included_results.nil? || included_results.empty?
1212
+
1213
+ included_results.flat_map do |result|
1214
+ result["content"].map do |content|
1215
+ content["text"]
1216
+ end
1217
+ end
1218
+ end.compact
1204
1219
1205
- # And if you just want to see the actual result (not the chunk):
1220
+ # The first chunk will be the closest match to the prompt. Finally, if you want to view the completed message(s):
1221
+ client.messages.list(thread_id: thread_id)
1206
1222
```
1207
1223
1208
1224
### Image Generation
0 commit comments