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.

Revisions

  1. dlo revised this gist Feb 24, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion main.go
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@ func main() {

    file, err := client.Files.New(context.TODO(), openai.FileNewParams{
    // IMPORTANT
    File: openai.FileParam(io.Reader(bytes.NewBuffer([]byte(s))), "context.txt", "application/pdf"),
    File: openai.FileParam(io.Reader(bytes.NewBuffer([]byte(s))), "context.txt", "text/plain"),
    Purpose: openai.F(openai.FilePurposeAssistants),
    })
    if err != nil {
  2. dlo created this gist Feb 24, 2025.
    47 changes: 47 additions & 0 deletions main.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    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", "application/pdf"),
    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)
    }
    }