google / generative-ai-go

Go SDK for Google Generative AI
Apache License 2.0
557 stars 57 forks source link

Error 400: Unsupported MIME type when trying to upload files to GenerateContent (.docx) #161

Open masihur1989 opened 3 months ago

masihur1989 commented 3 months ago

Description of the bug:

Using the below snippet getting error while trying to upload files to generate content. Using model gemini-1.5-flash

package main

import (
    "context"
    "fmt"
    "log"
    "os"

    "github.com/google/generative-ai-go/genai"
    "github.com/joho/godotenv"
    "google.golang.org/api/option"
)

func main() {
    fmt.Println("Hello World!")

    err := godotenv.Load()
    if err != nil {
        log.Fatal("Error loading .env key")
    }
    // Retrieve API key
    api_key := os.Getenv("GEMINI_API_KEY")
    // Initialize Gemini client
    ctx := context.Background()
    client, err := genai.NewClient(ctx, option.WithAPIKey(api_key))
    if err != nil {
        log.Fatal("Error Creatign Client: ", err)
    }
    defer client.Close()

    // Use Client.UploadFile to Upload a file to the service.
    // Pass it an io.Reader.
    f, err := os.Open("files/ResumeSample03.docx")
    if err != nil {
        log.Fatal("File Opening Error: ", err)
    }
    defer f.Close()
    // You can choose a name, or pass the empty string to generate a unique one.
    file, err := client.UploadFile(ctx, "", f, nil)
    if err != nil {
        log.Fatal("File Uploading Error: :", err)
    }

    // Connect to Gemini model
    model := client.GenerativeModel("gemini-1.5-flash")

    resp, err := model.GenerateContent(ctx, genai.FileData{URI: file.URI})
    if err != nil {
        log.Fatal("Error Getting Response: ", err)
    }

    printResponse(resp)
}

func printResponse(resp *genai.GenerateContentResponse) {
    for _, cand := range resp.Candidates {
        if cand.Content != nil {
            for _, part := range cand.Content.Parts {
                fmt.Println(part)
            }
        }
    }
    fmt.Println("---")
}

Actual vs expected behavior:

Getting Below errors.

For pdf format googleapi: Error 400: Unsupported MIME type: application/pdf

For docx format. googleapi: Error 400: Unsupported MIME type: application/zip

Any other information you'd like to share?

No response

moficodes commented 3 months ago

Looks like a dupe of #120

I looked in to it. And ai studio seem to generate code in Python and JS that looks very similar to this when a PDF is uploaded.

also look like an example for processing PDF file exist in the vertex sdk for go. https://cloud.google.com/vertex-ai/generative-ai/docs/samples/generativeaionvertexai-gemini-pdf

eliben commented 2 months ago

PDFs are supported now -- see examples

masihur1989 commented 1 month ago

@eliben thanks for the update. I have checked and pdf is working fine. Is there any timeline at this time for supporting docx?

eliben commented 1 month ago

This is a general API issue, not specific to the Go SDK. I suggest asking in https://discuss.ai.google.dev/