openai-go icon indicating copy to clipboard operation
openai-go copied to clipboard

Go client doesn't allow embedding response to be base64 encoded string

Open aliok opened this issue 9 months ago • 1 comments

Similar to https://github.com/openai/openai-openapi/issues/424

The embedding field of the Embedding object will be a string, if encoding_format is set to base64.

E.g.

curl -v https://api.openai.com/v1/embeddings \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"input": "The food was delicious",
"model": "text-embedding-3-small",
"encoding_format": "base64",
"dimensions":10
}'
{
  "object": "list",
  "data": [
    {
      "object": "embedding",
      "index": 0,
      "embedding": "nMcovm6gOb7ldwO/AESlvk0Avz2nZIq+3c+EvZvdFT9HsZa9oSy+vg=="    <------ THIS
    }
  ],
  "model": "text-embedding-3-small",
  "usage": {
    "prompt_tokens": 4,
    "total_tokens": 4
  }
}

However, the client mandates that the embedding field is an array of floats.

https://github.com/openai/openai-go/blob/7ad3660c4678e9723d831ac84b53588c25b7f2fb/embedding.go#L115-L124

aliok avatar Feb 26 '25 12:02 aliok

It's interesting that it's not in the OpenAPI specification, I'll get back to you on that.

In the meantime, the base64 encoded embedding is accessible via the .JSON metadata field

resp, _ := client.Embeddings.New(ctx, openai.EmbeddingNewParams{
	Model: openai.EmbeddingModelTextEmbedding3Small,
	Input: openai.EmbeddingNewParamsInputUnion{
		OfString: openai.String(question),
	},
	EncodingFormat: openai.EmbeddingNewParamsEncodingFormatBase64,
})

var b64embedding string
json.Unmarshal([]byte(resp.Data[0].JSON.Embedding.Raw()), &b64embedding)
println(b64embedding)

jacobzim-stl avatar Mar 26 '25 22:03 jacobzim-stl