Hi all,
Given the latest changes to assistants (now OpenAI-beta => assistants=v2), I was able to follow this guide to get file search up and running with my assistants: https://platform.openai.com/docs/assistants/tools/file-search/quickstart?context=streaming&lang=python
The guide describes two ways to access files:
- Create a vector store, upload a file and attach to vector store; then modify you assistant to reference the vector store. Then do the thread and run creation.
or
- Upload file, create assistant, create a thread with a nested message which includes the file_id as a file_search attachment (will show an example), then create a run.
I’ve had success with #2, the assistant is able to access the file and I don’t need to handle vector stores or modifying assistants directly (2 fewer api calls).
Here’s some partial examples:
Create assistant:
34 def create_assistant
35 url = "#{@base_url}/assistants"
36
37 body = {
38 model: 'gpt-4-turbo-preview',
39 name: "My assistant",
40 instructions: "You will analyze documents",
41 tools: [
42 { type: 'file_search' }
43 ]
44 }.to_json
45
46 res = HTTParty.post(url, body: body, headers: headers)
47
48 res
49 end
Upload a file (body):
140 def upload_file_body
141 {
142 file: @file,
143 purpose: 'assistants'
144 }
145 end
Create a thread:
def create_thread(file_id:)
119 url = "#{@base_url}/threads"
120
121 body = {
122 messages: [
123 {
124 role: 'user',
125 content: content(file_id),
126 attachments: [
127 { file_id: file_id, tools: [{ type: 'file_search' }] }
128 ]
129 }
130 ]
131 }
132
133 res = HTTParty.post(url, body: body.to_json, headers: headers)
134
135 res
136 end
Headers for all api calls:
147 def headers
148 {
149 'Authorization' => "Bearer #{@api_key}",
150 'Content-Type' => "application/json",
151 "OpenAI-Beta" => "assistants=v2"
152 }
153 end
Creating the assistant, uploading the file, and creating a thread in this way allow my runs to succeed as the assistant is able to locate the file and run analysis.
Be sure to OpenAI-Beta header to v2 as well.
Hope this helps!