RAG (Files & Search)

View as Markdown

RAG (Retrieval-Augmented Generation)

Upload documents, generate embeddings, and query them semantically — all through client.RAG.

Quick upload

The UploadFile convenience method handles both the init call and the PUT to the signed URL in one step:

1content, _ := os.ReadFile("handbook.pdf")
2
3upload, err := client.RAG.UploadFile(ctx, meshapi.UploadFileParams{
4 FileName: "handbook.pdf",
5 MimeType: "application/pdf",
6 Content: content,
7})
8if err != nil {
9 log.Fatal(err)
10}
11fmt.Println("File ID:", upload.FileID)

Two-step upload

Use InitUpload when you want to control the PUT yourself (e.g. streaming large files):

1embedFalse := false
2upload, err := client.RAG.InitUpload(ctx, meshapi.InitUploadRequest{
3 FileName: "handbook.pdf",
4 MimeType: "application/pdf",
5 Embed: &embedFalse,
6})
7
8// PUT the bytes directly to the signed URL
9req, _ := http.NewRequestWithContext(ctx, http.MethodPut, upload.SignedURL, bytes.NewReader(content))
10req.Header.Set("Content-Type", "application/pdf")
11http.DefaultClient.Do(req)

Trigger embedding

1resp, err := client.RAG.Embed(ctx, meshapi.BulkEmbedRequest{
2 FileIDs: []string{upload.FileID},
3})
4for _, r := range resp.Results {
5 fmt.Printf("%s: %s\n", r.FileID, r.EmbeddingStatus)
6}

Poll until ready

1for {
2 status, _ := client.RAG.Get(ctx, upload.FileID)
3 if status.EmbeddingStatus == "ready" {
4 break
5 }
6 if status.EmbeddingStatus == "failed" {
7 log.Fatalf("embedding failed: %v", status.LastErrorCode)
8 }
9 time.Sleep(3 * time.Second)
10}
1topK := 5
2results, err := client.RAG.Search(ctx, meshapi.SearchRequest{
3 Query: "What is the refund policy?",
4 TopK: &topK,
5 FileIDs: []string{upload.FileID}, // omit to search all files
6})
7
8for _, r := range results.Results {
9 fmt.Printf("[%.3f] %s\n", r.Score, r.Text)
10}

Search options

FieldTypeNotes
QuerystringPlain-language question
TopK*intResults to return (1–50, default 5)
FileIDs[]stringRestrict to specific files
Filtermap[string]anyMatch on metadata key-value pairs
DateFrom*int64Unix timestamp — only chunks created after
DateTo*int64Unix timestamp — only chunks created before

List files

1limit := 50
2list, err := client.RAG.List(ctx, meshapi.ListRagFilesParams{
3 Limit: &limit,
4})
5fmt.Printf("%d total files\n", list.Total)
6for _, f := range list.Files {
7 fmt.Printf("%s upload=%s embed=%s\n", f.FileID, f.UploadStatus, f.EmbeddingStatus)
8}

RAG chat

Combine search results with a chat completion:

1results, _ := client.RAG.Search(ctx, meshapi.SearchRequest{
2 Query: "What is the refund policy?",
3 TopK: &topK,
4})
5
6var chunks []string
7for _, r := range results.Results {
8 chunks = append(chunks, r.Text)
9}
10context := strings.Join(chunks, "\n\n")
11
12model := "openai/gpt-4o-mini"
13reply, _ := client.Chat.Completions.Create(ctx, meshapi.ChatCompletionParams{
14 Model: &model,
15 Messages: []meshapi.ChatMessage{
16 {Role: "system", Content: "Answer using only the context below.\n\n" + context},
17 {Role: "user", Content: "What is the refund policy?"},
18 },
19})
20fmt.Println(reply.Choices[0].Message.Content)