Skip to content

Instantly share code, notes, and snippets.

@dlo
Last active February 24, 2025 21:08
Show Gist options
  • Save dlo/dd361e9e22ea5adb2cdf6e8456eda99f to your computer and use it in GitHub Desktop.
Save dlo/dd361e9e22ea5adb2cdf6e8456eda99f to your computer and use it in GitHub Desktop.
A little demonstration of how to provide a filename when uploading a file to a vector store using the OpenAI Assistants API.
package main
import (
"bytes"
"context"
"io"
"github.com/openai/openai-go"
)
func main() {
client := openai.NewClient()
s := "This is just random context"
file, err := client.Files.New(context.TODO(), openai.FileNewParams{
// IMPORTANT
File: openai.FileParam(io.Reader(bytes.NewBuffer([]byte(s))), "context.txt", "text/plain"),
Purpose: openai.F(openai.FilePurposeAssistants),
})
if err != nil {
panic(err)
}
vs, err := client.Beta.VectorStores.New(context.TODO(), openai.BetaVectorStoreNewParams{
Name: openai.F(" Vector Store"),
FileIDs: openai.F([]string{file.ID}),
})
if err != nil {
panic(err)
}
_, err = client.Beta.Assistants.New(context.TODO(), openai.BetaAssistantNewParams{
Model: openai.F(openai.ChatModelGPT4o),
Name: openai.F("Random Assistant"),
Instructions: openai.F("You're a helpful assistant."),
Tools: openai.F([]openai.AssistantToolUnionParam{openai.FileSearchToolParam{
Type: openai.F(openai.FileSearchToolTypeFileSearch),
}}),
ToolResources: openai.F(openai.BetaAssistantNewParamsToolResources{
FileSearch: openai.F(openai.BetaAssistantNewParamsToolResourcesFileSearch{
VectorStoreIDs: openai.F([]string{vs.ID}),
}),
}),
})
if err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment