Skip to content

Commit ecceb88

Browse files
committed
Improve README example
1 parent 6fb7e9b commit ecceb88

File tree

1 file changed

+35
-19
lines changed

1 file changed

+35
-19
lines changed

README.md

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,10 +1142,13 @@ file_id = client.files.upload(
11421142
vector_store_id = client.vector_stores.create(parameters: {})["id"]
11431143
11441144
# Vectorise the file(s)
1145-
client.vector_store_files.create(
1145+
vector_store_file_id = client.vector_store_files.create(
11461146
vector_store_id: vector_store_id,
11471147
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"]
11491152
11501153
# Create an assistant, referencing the vector store
11511154
assistant_id = client.assistants.create(
@@ -1165,21 +1168,21 @@ assistant_id = client.assistants.create(
11651168
)["id"]
11661169
11671170
# Create a thread with your question
1168-
client.threads.create(parameters: {
1171+
thread_id = client.threads.create(parameters: {
11691172
messages: [
11701173
{ role: "user",
11711174
content: "Find the description of a nociceptor." }
11721175
]
1173-
})
1176+
})["id"]
11741177
11751178
# Run the thread to generate the response. Include the "GIVE ME THE CHUNKS" incantation.
1176-
client.runs.create(
1179+
run_id = client.runs.create(
11771180
thread_id: thread_id,
1178-
query_parameters: { include: ["step_details.tool_calls[*].file_search.results[*].content"] }, # incantation
11791181
parameters: {
11801182
assistant_id: assistant_id
1181-
}
1182-
)
1183+
},
1184+
query_parameters: { include: ["step_details.tool_calls[*].file_search.results[*].content"] } # incantation
1185+
)["id"]
11831186
11841187
# Get the steps that happened in the run
11851188
steps = client.run_steps.list(
@@ -1189,20 +1192,33 @@ steps = client.run_steps.list(
11891192
)
11901193
11911194
# 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"]
11931196
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
12011206
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
12041219
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)
12061222
```
12071223

12081224
### Image Generation

0 commit comments

Comments
 (0)