sashabaranov / go-openai

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

Openai Supported AI Providers [fireworks.ai] #779

Open satya-500 opened 4 days ago

satya-500 commented 4 days ago

Describe the change

I was testing fireworks AI to create the embedding, and it threw an error due to an extra parameter (user) being included in the request body even when it was an empty string. This PR modifies the code to ensure the user parameter is omitted when it is empty, aligning with fireworks AI's requirements.

Provide OpenAI documentation link

Relevant API documentation can be found here.

Describe your solution

The solution involves adding the omitempty JSON tag to the user field in the EmbeddingRequest struct. This ensures that if the user field is empty, it will not be included in the JSON payload sent to the API. This change allows the request to be accepted by fireworks AI without triggering an error due to an empty user field.

Tests

I tested the change by making a request to the fireworks AI API using the modified code and verified that the request was successful. The test involved creating an embedding with the following details:

{
  "input": ["hi satya"],
  "model": "nomic-ai/nomic-embed-text-v1.5",
  "encoding_format": "float",
  "dimensions": 100
}

Additional context

Example error response before the fix:

curl --request POST \
  --url https://api.fireworks.ai/inference/v1/embeddings \
  --header 'accept: application/json' \
  --header 'authorization: Bearer token' \
  --header 'content-type: application/json' \
  --data '{
    "input": [
        "hi satya"
    ],
    "model": "nomic-ai/nomic-embed-text-v1.5",
    "encoding_format": "float",
    "dimensions": 10,
    "user": ""
}'
{
  "error": {
    "object": "error",
    "type": "internal_server_error",
    "message": "server had an error while processing your request, please retry again after a brief wait"
  },
  "raw_output": null
}
Screenshot 2024-06-27 at 1 50 58 AM Screenshot 2024-06-27 at 1 51 46 AM

Example successful response after the fix:

curl --request POST \
  --url https://api.fireworks.ai/inference/v1/embeddings \
  --header 'accept: application/json' \
  --header 'authorization: Bearer token' \
  --header 'content-type: application/json' \
  --data '{
    "input": [
        "hi satya"
    ],
    "model": "nomic-ai/nomic-embed-text-v1.5",
    "encoding_format": "float",
    "dimensions": 10
}'
{
  "data": [
    {
      "index": 0,
      "embedding": [
        -0.1552734375,
        0.09490966796875,
        -0.90283203125,
        0.1776123046875,
        0.11700439453125,
        -0.00719451904296875,
        -0.119873046875,
        -0.1888427734375,
        -0.22216796875,
        0.08270263671875
      ],
      "object": "embedding"
    }
  ],
  "model": "nomic-ai/nomic-embed-text-v1.5",
  "object": "list",
  "usage": {
    "prompt_tokens": 3,
    "total_tokens": 3
  }
}
Screenshot 2024-06-27 at 1 52 14 AM Screenshot 2024-06-27 at 1 53 03 AM

go code

package main

import (
    "context"
    "log"

    openai "github.com/sashabaranov/go-openai"
)

func main() {

    config := openai.DefaultConfig("token")
    config.BaseURL = "https://api.fireworks.ai/inference/v1"

    openaiClient := openai.NewClientWithConfig(config)

    resp, err := openaiClient.CreateEmbeddings(context.Background(), openai.EmbeddingRequest{
        Dimensions:     100,
        Model:          openai.EmbeddingModel("nomic-ai/nomic-embed-text-v1.5"),
        Input:          []string{"hi satya"},
        EncodingFormat: openai.EmbeddingEncodingFormatFloat,
    })

    if err != nil {
        log.Fatal("Error creating target embedding:", err)
    } else {
        log.Println("-->", resp.Data)
    }
}