Repository: sashabaranov/go-openai
Pull request #637 (2024-01-16 00:15:02)

Fix Azure embeddings model detection by passing string to `fullURL`
A [recent pull request](https://github.com/sashabaranov/go-openai/pull/629) broke support for the Azure OpenAI embeddings endpoint by [passing a variable of type `EmbeddingModel` to `client.fullURL`](https://github.com/sashabaranov/go-openai/pull/629/files#diff-0fdd78c64708f1e3c576725363d6d7accb8b43590810436765198e7a5522770fR231). This breaks the [Azure deployment name lookup](https://github.com/sashabaranov/go-openai/blob/09f6920ad04666f65dd86ed542e5ebf8bffc93a8/client.go#L223-L229).

This pull request fixes Azure embeddings support by casting the type to `string`.

**Tests**
Added tests for generating Azure embeddings 

Note: I'm not a great Go programmer. If there's a better way to do this I'm all ears!

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/sashabaranov/go-openai/pull/637?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Comparison is base [(`09f6920`)](https://app.codecov.io/gh/sashabaranov/go-openai/commit/09f6920ad04666f65dd86ed542e5ebf8bffc93a8?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) 98.43% compared to head [(`c72f9ec`)](https://app.codecov.io/gh/sashabaranov/go-openai/pull/637?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) 98.43%.


<details><summary>Additional details and impacted files</summary>


```diff
@@           Coverage Diff           @@
##           master     #637   +/-   ##
=======================================
  Coverage   98.43%   98.43%           
=======================================
  Files          24       24           
  Lines        1342     1342           
=======================================
  Hits         1321     1321           
  Misses         15       15           
  Partials        6        6           
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/sashabaranov/go-openai/pull/637?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None).

sashabaranov: @greysteil great catch, thank you!

For context: here's the exact part which breaks

https://github.com/sashabaranov/go-openai/blob/master/client.go#L224-L229

And a minimal code example to reproduce: https://go.dev/play/p/KakKIBplunp

There's an open issue to get rid of `args ...any` there and make this part type-safe https://github.com/sashabaranov/go-openai/issues/636


Patch:
diff --git a/embeddings.go b/embeddings.go
index f79df9d..c144119 100644
--- a/embeddings.go
+++ b/embeddings.go
@@ -228,7 +228,7 @@ func (c *Client) CreateEmbeddings(
 	conv EmbeddingRequestConverter,
 ) (res EmbeddingResponse, err error) {
 	baseReq := conv.Convert()
-	req, err := c.newRequest(ctx, http.MethodPost, c.fullURL("/embeddings", baseReq.Model), withBody(baseReq))
+	req, err := c.newRequest(ctx, http.MethodPost, c.fullURL("/embeddings", string(baseReq.Model)), withBody(baseReq))
 	if err != nil {
 		return
 	}


Test patch:
diff --git a/embeddings_test.go b/embeddings_test.go
index 846d199..ed6384f 100644
--- a/embeddings_test.go
+++ b/embeddings_test.go
@@ -158,6 +158,32 @@ func TestEmbeddingEndpoint(t *testing.T) {
 	checks.HasError(t, err, "CreateEmbeddings error")
 }
 
+func TestAzureEmbeddingEndpoint(t *testing.T) {
+	client, server, teardown := setupAzureTestServer()
+	defer teardown()
+
+	sampleEmbeddings := []openai.Embedding{
+		{Embedding: []float32{1.23, 4.56, 7.89}},
+		{Embedding: []float32{-0.006968617, -0.0052718227, 0.011901081}},
+	}
+
+	server.RegisterHandler(
+		"/openai/deployments/text-embedding-ada-002/embeddings",
+		func(w http.ResponseWriter, r *http.Request) {
+			resBytes, _ := json.Marshal(openai.EmbeddingResponse{Data: sampleEmbeddings})
+			fmt.Fprintln(w, string(resBytes))
+		},
+	)
+	// test create embeddings with strings (simple embedding request)
+	res, err := client.CreateEmbeddings(context.Background(), openai.EmbeddingRequest{
+		Model: openai.AdaEmbeddingV2,
+	})
+	checks.NoError(t, err, "CreateEmbeddings error")
+	if !reflect.DeepEqual(res.Data, sampleEmbeddings) {
+		t.Errorf("Expected %#v embeddings, got %#v", sampleEmbeddings, res.Data)
+	}
+}
+
 func TestEmbeddingResponseBase64_ToEmbeddingResponse(t *testing.T) {
 	type fields struct {
 		Object string