Skip to content

Improve docs and small code cleanups (Azure, union tags, request options) #475

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ $ ./scripts/lint

This will install all the required dependencies and build the SDK.

You can also [install go 1.18+ manually](https://go.dev/doc/install).
You can also [install Go 1.21+ manually](https://go.dev/doc/install).

## Modifying/Adding code

Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,7 @@ func Logger(req *http.Request, next option.MiddlewareNext) (res *http.Response,

// Handle stuff after the request
end := time.Now()
LogRes(res, err, start - end)
LogRes(res, err, end.Sub(start))

return res, err
}
Expand All @@ -895,13 +895,15 @@ middleware has been applied.

## Microsoft Azure OpenAI

To use this library with [Azure OpenAI]https://learn.microsoft.com/azure/ai-services/openai/overview),
To use this library with [Azure OpenAI](https://learn.microsoft.com/azure/ai-services/openai/overview),
use the option.RequestOption functions in the `azure` package.

```go
package main

import (
"fmt"
"os"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/openai/openai-go/v2"
"github.com/openai/openai-go/v2/azure"
Expand All @@ -911,7 +913,7 @@ func main() {
const azureOpenAIEndpoint = "https://<azure-openai-resource>.openai.azure.com"

// The latest API versions, including previews, can be found here:
// ttps://learn.microsoft.com/en-us/azure/ai-services/openai/reference#rest-api-versionng
// https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#rest-api-versioning
const azureOpenAIAPIVersion = "2024-06-01"

tokenCredential, err := azidentity.NewDefaultAzureCredential(nil)
Expand Down
4 changes: 2 additions & 2 deletions azure/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func WithTokenCredential(tokenCredential azcore.TokenCredential) option.RequestO
// WithAPIKey configures this client to authenticate using an API key.
// This function should be paired with a call to [WithEndpoint] to point to your Azure OpenAI instance.
func WithAPIKey(apiKey string) option.RequestOption {
// NOTE: there is an option.WithApiKey(), but that adds the value into
// NOTE: there is an option.WithAPIKey(), but that adds the value into
// the Authorization header instead so we're doing this instead.
return option.WithHeader("Api-Key", apiKey)
}
Expand Down Expand Up @@ -234,4 +234,4 @@ func (mp policyAdapter) Do(req *policy.Request) (*http.Response, error) {
return (option.MiddlewareNext)(mp)(req.Raw())
}

const version = "v.0.1.0"
const version = "v0.1.0"
6 changes: 3 additions & 3 deletions option/requestoption.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func WithBaseURL(base string) RequestOption {

return requestconfig.RequestOptionFunc(func(r *requestconfig.RequestConfig) error {
if err != nil {
return fmt.Errorf("requestoption: WithBaseURL failed to parse url %s", err)
return fmt.Errorf("requestoption: WithBaseURL failed to parse base URL %q: %v", base, err)
}

r.BaseURL = u
Expand Down Expand Up @@ -232,7 +232,7 @@ func WithResponseInto(dst **http.Response) RequestOption {
// WithRequestBody returns a RequestOption that provides a custom serialized body with the given
// content type.
//
// body accepts an io.Reader or raw []bytes.
// body accepts an io.Reader or raw []byte.
func WithRequestBody(contentType string, body any) RequestOption {
return requestconfig.RequestOptionFunc(func(r *requestconfig.RequestConfig) error {
if reader, ok := body.(io.Reader); ok {
Expand Down Expand Up @@ -270,7 +270,7 @@ func WithEnvironmentProduction() RequestOption {
func WithAPIKey(value string) RequestOption {
return requestconfig.RequestOptionFunc(func(r *requestconfig.RequestConfig) error {
r.APIKey = value
return r.Apply(WithHeader("authorization", fmt.Sprintf("Bearer %s", r.APIKey)))
return r.Apply(WithHeader("Authorization", fmt.Sprintf("Bearer %s", r.APIKey)))
})
}

Expand Down