sashabaranov / go-openai

OpenAI ChatGPT, GPT-3, GPT-4, DALL·E, Whisper API wrapper for Go
Apache License 2.0
9.25k stars 1.42k forks source link

Ask openai.GPT4o with a file request #809

Open mihaiav opened 3 months ago

mihaiav commented 3 months ago

Is your feature request related to a problem? Please describe. A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

I'm trying to ask openai.GPT4o to analyze a html document/ file and extract keywords but I cannot find how to attach the html document to the prompt request

Describe the solution you'd like A clear and concise description of what you want to happen. A File field on openai.ChatCompletionRequest

Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered.

I've added the html content itself in the prompt but it confuses chatGPT and returns gibberish responses. See the reduced code I've tried:

func main(){
   content,_ := ioutil.ReadFile("x.html")
   response, _ := fmt.Sprintf("analyze the html content below and return me 10 keywords: \n %s", content)
func chat(cx context.Context, text string) (string, error) {
    client := openai.NewClient(openAIKey)
    resp, err := client.CreateChatCompletion(
        cx,
        openai.ChatCompletionRequest{

            Temperature: 2,
            Model:       openai.GPT4o, //  openai.GPT3Dot5Turbo,
            //  FilePath:    "./x.html",
            Messages: []openai.ChatCompletionMessage{
                {
                    Role:    openai.ChatMessageRoleUser,
                    Content: text,
                },
            },
        },
    )

    if err != nil {
        return "", err

    }

    return resp.Choices[0].Message.Content, nil
}
WillMatthews commented 3 months ago

There's nowhere in the API that allows generic file uploads. It's only text and images, within the prompt structure. In order to achieve the effect you want, you will have to either insert all the HTML as text as part of your prompt, or find a way to parse it to extract portions of the HTML (so you can save on tokens).

WillMatthews commented 3 months ago

Also - your temperature is extremely high- which may explain some of the "gibberish". Start with 0.7 and maybe reduce/increase it incrementally.