Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for OpenAI Organization ID header #27

Merged
merged 1 commit into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions openai/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,29 @@ func (c *CreateEmbeddingResponse) String() string {
type OpenAIClient struct {
BaseURL string
APIKey string
OrgID string
Client *http.Client
}

func NewOpenAIClient(apiKey string) *OpenAIClient {
return &OpenAIClient{
func NewOpenAIClient(apiKey string, opts ...Option) *OpenAIClient {
client := &OpenAIClient{
BaseURL: "https://api.openai.com/v1/",
Client: &http.Client{},
APIKey: apiKey,
}
applyClientOptions(client, opts...)

return client
}

func (c *OpenAIClient) SetAPIKey(apiKey string) {
c.APIKey = apiKey
}

func (c *OpenAIClient) SetOrgID(orgID string) {
c.OrgID = orgID
}

func (c *OpenAIClient) SetBaseURL(baseURL string) {
c.BaseURL = baseURL
}
Expand All @@ -118,6 +126,11 @@ func (c *OpenAIClient) CreateEmbedding(ctx context.Context, req *CreateEmbedding
httpReq.Header.Set("Content-Type", "application/json")
httpReq.Header.Set("Authorization", "Bearer "+c.getAPIKey())

// OpenAI Organization ID (Optional)
if c.OrgID != "" {
httpReq.Header.Set("OpenAI-Organization", c.OrgID)
}

resp, err := c.Client.Do(httpReq)
if err != nil {
return nil, err
Expand Down Expand Up @@ -147,9 +160,9 @@ type OpenAIEmbeddingFunction struct {
apiClient *OpenAIClient
}

func NewOpenAIEmbeddingFunction(apiKey string) *OpenAIEmbeddingFunction {
func NewOpenAIEmbeddingFunction(apiKey string, opts ...Option) *OpenAIEmbeddingFunction {
cli := &OpenAIEmbeddingFunction{
apiClient: NewOpenAIClient(apiKey),
apiClient: NewOpenAIClient(apiKey, opts...),
}

return cli
Expand Down
16 changes: 15 additions & 1 deletion openai/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ func Test_openai_client(t *testing.T) {
}
apiKey = os.Getenv("OPENAI_API_KEY")
}
ef := NewOpenAIEmbeddingFunction(apiKey)

t.Run("Test DefaultApiService Add", func(t *testing.T) {
ef := NewOpenAIEmbeddingFunction(apiKey)

documents := []string{
"Document 1 content here",
"Document 2 content here",
Expand All @@ -33,5 +34,18 @@ func Test_openai_client(t *testing.T) {
require.NotNil(t, resp)
fmt.Printf("resp: %v\n", resp)
// assert.Equal(t, 201, httpRes.StatusCode)
require.Empty(t, ef.apiClient.OrgID)
})

t.Run("Test Adding Organization Id with NewOpenAIClient", func(t *testing.T) {
apiClient := NewOpenAIClient(apiKey, WithOpenAIOrganizationID("org-123"))

require.Equal(t, "org-123", apiClient.OrgID)
})

t.Run("Test Adding Organization Id with NewOpenAIEmbeddingFunction", func(t *testing.T) {
ef := NewOpenAIEmbeddingFunction(apiKey, WithOpenAIOrganizationID("org-123"))

require.Equal(t, "org-123", ef.apiClient.OrgID)
})
}
17 changes: 17 additions & 0 deletions openai/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package openai

// Option is a function type that can be used to modify the client.
type Option func(c *OpenAIClient)

// WithOpenAIOrganizationID is an option for setting the OpenAI org id.
func WithOpenAIOrganizationID(openAiAPIKey string) Option {
return func(c *OpenAIClient) {
c.SetOrgID(openAiAPIKey)
}
}

func applyClientOptions(c *OpenAIClient, opts ...Option) {
for _, opt := range opts {
opt(c)
}
}
Loading