ngrok or
similar to proxy the traffic.
Example code:
- files/autoSave.ts has a simple example of how to use the automatic file saving.
- files/addFile.ts has an example of how to save the file, submit a question, and generate a response in separate steps.
- files/generateImage.ts has an example of how to generate an image and save it in an assistant message.
- FilesImages.tsx has client-side code.
Running the example
Sending an image by uploading first and generating asynchronously
The standard approach is to:- Upload the file to the database (
uploadFileaction). Note: this can be in a regular action or in an httpAction, depending on what’s more convenient. - Send a message to the thread (
submitFileQuestionaction) - Send the file to the LLM to generate / stream text asynchronously
(
generateResponseaction) - Query for the messages from the thread (
listThreadMessagesquery)
1: Saving the file
2: Sending the message
3: Generating the response & querying the responses
This is done in the same way as text inputs.Inline saving approach
You can also pass in an image / file direction when generating text, if you’re in an action. Any image or file passed in themessage argument will
automatically be saved in file storage if it’s larger than 64k, and a fileId
will be saved to the message.
Example:
Under the hood
Saving to the files has 3 components:- Saving to file storage (in your app, not in the component’s storage). This
means you can access it directly with the
storageIdand generate URLs. - Saving a reference (the storageId) to the file in the component. This will automatically keep track of how many messages are referencing the file, so you can vacuum files that are no longer used (see files/vacuum.ts).
- Inserting a URL in place of the data in the message sent to the LLM, along
with the media type and other metadata provided. It will be inferred if not
provided in
guessMimeType.