Skip to content

Commit

Permalink
openai: WithEmbeddingModel option is incorrectly designating the desi…
Browse files Browse the repository at this point in the history
…red model to use for embedding (#731)

* chore: issue #729

* chore: issue #729
  • Loading branch information
devalexandre authored Mar 31, 2024
1 parent 4174692 commit 05ab264
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
27 changes: 27 additions & 0 deletions examples/openai-embeddings-example/openai-embeddings-example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"context"
"fmt"
"log"

"github.com/tmc/langchaingo/llms/openai"
)

func main() {
opts := []openai.Option{
openai.WithModel("gpt-3.5-turbo-0125"),
openai.WithEmbeddingModel("text-embedding-3-large"),
}
llm, err := openai.New(opts...)
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
embedings, err := llm.CreateEmbedding(ctx, []string{"ola", "mundo"})
if err != nil {
log.Fatal(err)
}

fmt.Println(embedings)
}
12 changes: 9 additions & 3 deletions llms/openai/internal/openaiclient/embeddings.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,19 @@ type embeddingResponsePayload struct {

// nolint:lll
func (c *Client) createEmbedding(ctx context.Context, payload *embeddingPayload) (*embeddingResponsePayload, error) {
if c.baseURL == "" {
c.baseURL = defaultBaseURL
}

if c.apiType == APITypeOpenAI {
payload.Model = c.embeddingsModel
}

payloadBytes, err := json.Marshal(payload)
if err != nil {
return nil, fmt.Errorf("marshal payload: %w", err)
}
if c.baseURL == "" {
c.baseURL = defaultBaseURL
}

req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.buildURL("/embeddings", c.embeddingsModel), bytes.NewReader(payloadBytes))
if err != nil {
return nil, fmt.Errorf("create request: %w", err)
Expand Down

0 comments on commit 05ab264

Please sign in to comment.