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

feat: add azure openai support #214

Merged
merged 26 commits into from
Apr 4, 2023

Conversation

ttys3
Copy link
Contributor

@ttys3 ttys3 commented Mar 31, 2023

feat: add azure openai support

refactor config:
1. add WithXXX options, allow construct a config like openai.NewConfig(...Option)
2. make the config field naming more like the openai python pkg's, this will make us follow py's changes more easy
3. add new NewConfig method, which will check the config just like the py' pkg (the error message almost the same)

resolve #142

@codecov
Copy link

codecov bot commented Mar 31, 2023

Codecov Report

Merging #214 (59461ec) into master (bee0656) will increase coverage by 0.92%.
The diff coverage is 92.30%.

❗ Current head 59461ec differs from pull request most recent head 222aa40. Consider uploading reports for the commit 222aa40 to get more accurate results

@@            Coverage Diff             @@
##           master     #214      +/-   ##
==========================================
+ Coverage   72.51%   73.44%   +0.92%     
==========================================
  Files          21       21              
  Lines         593      625      +32     
==========================================
+ Hits          430      459      +29     
- Misses        124      125       +1     
- Partials       39       41       +2     
Impacted Files Coverage Δ
api.go 73.41% <86.36%> (+2.44%) ⬆️
config.go 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

Copy link

@tao12345666333 tao12345666333 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, there is one authorization that needs to be revised. Also the /chat/completions API does not exist for the current stable API version api-version

api.go Outdated Show resolved Hide resolved
@ttys3
Copy link
Contributor Author

ttys3 commented Mar 31, 2023

LGTM, there is one authorization that needs to be revised. Also the /chat/completions API does not exist for the current stable API version api-version

authorization for non stream request just fixed.

about the /chat/completions API, we are using the preview version. I did not aware that -_-

what can we do about the stable version? add a check and just return an not supported error ?

@tao12345666333
Copy link

stable API version 2022-12-01
https://github.com/Azure/azure-rest-api-specs/blob/main/specification/cognitiveservices/data-plane/AzureOpenAI/inference/stable/2022-12-01/inference.json

This API includes a prompt field, if we want to support it, we can just deal with this field.

Or we can just declare we only support 2023-03-15-preview api version

@ttys3
Copy link
Contributor Author

ttys3 commented Mar 31, 2023

currently, I follow the openai python package's logic just use the api type to detect the auth method like:

yes, this is simple and just works.

if c.config.APIType == APITypeAzure {
// use api-key method
} else {
// user bear token
}

I think maybe it is better to add a AuthMethod option to the config ?

like

type AuthMethod interface {
	SetupAuth(req *http.Request, apiKey string)
}

then impl two common :

type GenericApiKeyAuth struct {
	KeyName string
}

func (a *GenericApiKeyAuth) SetupAuth(req *http.Request, apiKey string) {
	req.Header.Set(a.KeyName, apiKey)
}

type BearTokenAuth struct{}

func (a *BearTokenAuth) SetupAuth(req *http.Request, apiKey string) {
	req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", apiKey))
}

and we can have the default:

var AzureAPIKeyAuth = &GenericApiKeyAuth{KeyName: "api-key"}

var OpenAITokenAuth = &BearTokenAuth{}

@ttys3
Copy link
Contributor Author

ttys3 commented Mar 31, 2023

declare we only support 2023-03-15-preview api version

maybe it 's simple just declare we only support 2023-03-15-preview api version ?

after all, /completions is usable ? ( I did not test it for 2022-12-01)

and, even 2023-03-15-preview does not support /audio api

@sashabaranov
Copy link
Owner

Thank you so much for the PR!

@sashabaranov
Copy link
Owner

sashabaranov commented Mar 31, 2023

A couple of general comments:

  1. add WithXXX options, allow construct a config like openai.NewConfig(...Option)

We are actively avoiding this style of configuration as discussed in #79.

  1. make the config field naming more like the openai python pkg's, this will make us follow py's changes more easy

While making this package close to python's would be a good thing, it is less of a priority than not breaking API for existing clients. I don't think renaming Config fields and NewConfig with a returned error is justified given the API non-breakage goal.

@ttys3
Copy link
Contributor Author

ttys3 commented Mar 31, 2023

A couple of general comments:

  1. add WithXXX options, allow construct a config like openai.NewConfig(...Option)

We are actively avoiding this style of configuration as discussed in #79.

  1. make the config field naming more like the openai python pkg's, this will make us follow py's changes more easy

While making this package close to python's would be a good thing, it is less of a priority than not breaking API for existing clients. I don't think renaming Config fields and NewConfig with a returned error is justified given the API non-breakage goal.

  1. it's just a kind of common practice, many project use this style. I can accept that, if you don't like it.

  2. if we do not check the error in the Config constructor, then it seems inappropriate to check for configuration errors elsewhere. we may need just ignore the error checking, Leave the blame to the server and have the user check for errors returned by the server ?

@ttys3
Copy link
Contributor Author

ttys3 commented Mar 31, 2023

just add error check for CreateChatCompletionStream,

seems there's other thing need fix:

=== RUN   TestCompletionsStreamWrongModel
    stream_test.go:29: CreateCompletion should return ErrCompletionUnsupportedModel, but returned: Post "http://localhost/v1/completions": dial tcp [::1]:80: connect: connection refused
--- FAIL: TestCompletionsStreamWrongModel (0.00s)

@sashabaranov
Copy link
Owner

  1. it's just a kind of common practice, many project use this style. I can accept that, if you don't like it.

Exactly, there are multiple common ways to pass the configuration, this library prefers using a single config struct.

  1. if we do not check the error in the Config constructor, then it seems inappropriate to check for configuration errors elsewhere. we may need just ignore the error checking, Leave the blame to the server and have the user check for errors returned by the server ?

This PR checks for three errors:

  • Empty API key — I think it's fine not to check this during the configuration
  • Whether API type is supported – just type checking should be enough here if we use iota-enum for APIType
  • Empty APIVersion — it might make sense to have a DefaultAzureConfig(...) that ensures all the nitty-gritty details are specified
if cfg.APIKey == "" {
	return ClientConfig{}, fmt.Errorf("api key is required")
}

if _, ok := supportedAPIType[cfg.APIType]; !ok {
	return ClientConfig{}, fmt.Errorf("unsupported API type %s", cfg.APIType)
}

if cfg.APIType == APITypeAzure || cfg.APIType == APITypeAzureAD {
	if cfg.APIVersion == "" {
		return ClientConfig{}, fmt.Errorf("an API version is required for the Azure API type")
	}
}

@ttys3
Copy link
Contributor Author

ttys3 commented Apr 3, 2023

OK.
I updated commits. and it is a mininal patch now. it is non-breaking.

try to fix the CreateChatCompletionStream not detect http error code issue, but which cause ErrorAccumulatorWriteErrors tests fail. (I think it's better deal this with other PR. so reverted that commit)

PTAL

@ttys3 ttys3 changed the title feat: add azure openai support, refactor config feat: add azure openai support Apr 3, 2023
Copy link
Owner

@sashabaranov sashabaranov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for re-working this PR, it is much more manageable now! I've added a couple of comments — I wonder what your thoughts are

config.go Show resolved Hide resolved
config.go Outdated Show resolved Hide resolved
config.go Show resolved Hide resolved
config.go Outdated

HTTPClient *http.Client
APIType APIType
APIVersion string // required for APITypeAzure or APITypeAzureAD
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can also pack Azure-related fields into a sub-structure inside the config? That way, it would be more straightforward during configuration:

config.APIVersion = ...
config.Engine = ...

vs

config.Azure.APIVersion = ...
config.Azure.Engine = ...

Copy link
Contributor Author

@ttys3 ttys3 Apr 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we do this to Azure, do we have to make a special openai config for openai?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the py default the version to 2023-03-15-preview, we do this the same ?

api_version = (
    "2023-03-15-preview" if api_type in ("azure", "azure_ad", "azuread") else None
)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done via 84ea881

since this func called DefaultXXX, I think is OK to do this.

@sashabaranov
Copy link
Owner

Excellent, ready to merge once codecov issues are fixed 🙌🏻

@ttys3 ttys3 requested a review from sashabaranov April 3, 2023 18:12
config.go Outdated Show resolved Hide resolved
@ttys3 ttys3 requested a review from sashabaranov April 3, 2023 18:30
Copy link
Owner

@sashabaranov sashabaranov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feature request: Add support for Azure OpenAI Service
4 participants