-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat: add azure openai support #214
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
Merged
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
35ca745
feat: add azure openai support
ttys3 4ef2708
chore: refine config
ttys3 350e961
chore: make config options like the python one
ttys3 61287b2
chore: adjust config struct field order
ttys3 9f06a7b
test: fix tests
ttys3 41a5ae5
style: make the linter happy
ttys3 8cdf9a8
fix: support Azure API Key authentication in sendRequest
ttys3 f5157ea
chore: check error in CreateChatCompletionStream
ttys3 a8a6b7e
chore: pass tests
ttys3 ca1315e
chore: try pass tests again
ttys3 5da05cf
chore: change ClientConfig back due to this lib does not like WithXxx…
ttys3 94438d4
chore: revert fix to CreateChatCompletionStream() due to cause tests …
ttys3 37eecb8
chore: at least add some comment about the required fields
ttys3 60cafde
chore: re order ClientConfig fields
ttys3 006ac90
chore: add DefaultAzure()
ttys3 84ea881
chore: set default api_version the same as py one "2023-03-15-preview"
ttys3 becc1bb
style: fixup typo
ttys3 f5c822d
test: add api_internal_test.go
ttys3 ee4bd5d
style: make lint happy
ttys3 416dc26
chore: add constant AzureAPIKeyHeader
ttys3 27be572
chore: use AzureAPIKeyHeader for api-key header, fix azure base url a…
ttys3 ebef9a9
test: add TestAzureFullURL, TestRequestAuthHeader and TestOpenAIFullURL
ttys3 862ce64
test: simplify TestRequestAuthHeader
ttys3 115595b
test: refine TestOpenAIFullURL
ttys3 59461ec
chore: refine comments
ttys3 222aa40
feat: DefaultAzureConfig
ttys3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| package openai | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestOpenAIFullURL(t *testing.T) { | ||
| cases := []struct { | ||
| Name string | ||
| Suffix string | ||
| Expect string | ||
| }{ | ||
| { | ||
| "ChatCompletionsURL", | ||
| "/chat/completions", | ||
| "https://api.openai.com/v1/chat/completions", | ||
| }, | ||
| { | ||
| "CompletionsURL", | ||
| "/completions", | ||
| "https://api.openai.com/v1/completions", | ||
| }, | ||
| } | ||
|
|
||
| for _, c := range cases { | ||
| t.Run(c.Name, func(t *testing.T) { | ||
| az := DefaultConfig("dummy") | ||
| cli := NewClientWithConfig(az) | ||
| actual := cli.fullURL(c.Suffix) | ||
| if actual != c.Expect { | ||
| t.Errorf("Expected %s, got %s", c.Expect, actual) | ||
| } | ||
| t.Logf("Full URL: %s", actual) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestRequestAuthHeader(t *testing.T) { | ||
| cases := []struct { | ||
| Name string | ||
| APIType APIType | ||
| HeaderKey string | ||
| Token string | ||
| Expect string | ||
| }{ | ||
| { | ||
| "OpenAIDefault", | ||
| "", | ||
| "Authorization", | ||
| "dummy-token-openai", | ||
| "Bearer dummy-token-openai", | ||
| }, | ||
| { | ||
| "OpenAI", | ||
| APITypeOpenAI, | ||
| "Authorization", | ||
| "dummy-token-openai", | ||
| "Bearer dummy-token-openai", | ||
| }, | ||
| { | ||
| "AzureAD", | ||
| APITypeAzureAD, | ||
| "Authorization", | ||
| "dummy-token-azure", | ||
| "Bearer dummy-token-azure", | ||
| }, | ||
| { | ||
| "Azure", | ||
| APITypeAzure, | ||
| AzureAPIKeyHeader, | ||
| "dummy-api-key-here", | ||
| "dummy-api-key-here", | ||
| }, | ||
| } | ||
|
|
||
| for _, c := range cases { | ||
| t.Run(c.Name, func(t *testing.T) { | ||
| az := DefaultConfig(c.Token) | ||
| az.APIType = c.APIType | ||
|
|
||
| cli := NewClientWithConfig(az) | ||
| req, err := cli.newStreamRequest(context.Background(), "POST", "/chat/completions", nil) | ||
| if err != nil { | ||
| t.Errorf("Failed to create request: %v", err) | ||
| } | ||
| actual := req.Header.Get(c.HeaderKey) | ||
| if actual != c.Expect { | ||
| t.Errorf("Expected %s, got %s", c.Expect, actual) | ||
| } | ||
| t.Logf("%s: %s", c.HeaderKey, actual) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestAzureFullURL(t *testing.T) { | ||
| cases := []struct { | ||
| Name string | ||
| BaseURL string | ||
| Engine string | ||
| Expect string | ||
| }{ | ||
| { | ||
| "AzureBaseURLWithSlashAutoStrip", | ||
| "https://httpbin.org/", | ||
| "chatgpt-demo", | ||
| "https://httpbin.org/" + | ||
| "openai/deployments/chatgpt-demo" + | ||
| "/chat/completions?api-version=2023-03-15-preview", | ||
| }, | ||
| { | ||
| "AzureBaseURLWithoutSlashOK", | ||
| "https://httpbin.org", | ||
| "chatgpt-demo", | ||
| "https://httpbin.org/" + | ||
| "openai/deployments/chatgpt-demo" + | ||
| "/chat/completions?api-version=2023-03-15-preview", | ||
| }, | ||
| } | ||
|
|
||
| for _, c := range cases { | ||
| t.Run(c.Name, func(t *testing.T) { | ||
| az := DefaultAzureConfig("dummy", c.BaseURL, c.Engine) | ||
| cli := NewClientWithConfig(az) | ||
| // /openai/deployments/{engine}/chat/completions?api-version={api_version} | ||
| actual := cli.fullURL("/chat/completions") | ||
| if actual != c.Expect { | ||
| t.Errorf("Expected %s, got %s", c.Expect, actual) | ||
| } | ||
| t.Logf("Full URL: %s", actual) | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.