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

API key header default way #326

Closed
Bercof opened this issue Jul 16, 2023 · 1 comment
Closed

API key header default way #326

Bercof opened this issue Jul 16, 2023 · 1 comment
Assignees
Labels
enhancement New feature or request Work In Progress
Milestone

Comments

@Bercof
Copy link

Bercof commented Jul 16, 2023

Is your feature request related to a problem? Please describe.
The default way to set httpClient header is openai-style Authorization: Bearer ... now. But comparing to official python-openai library the default way is Azure-style api_key ... (here is source)
There is only one option to influence api key header: ProviderType.

Describe the solution you'd like
To avoid breaking changes my idea is to introduce new option AuthorizationHeaderType with explicit default value Undefined. At OpenAIService constructor AuthorizationHeaderType may be checked and may be set depending on ProviderType value.

Create enum:

public enum AuthorizationHeaderType
{
    Undefined = 0,
    AuthorizationHeader = 1,
    ApiKey = 2
}

Add new attribute to class OpenAiOptions:

public AuthorizationHeaderType AuthorizationHeaderType { get; set; } = AuthorizationHeaderType.Undefined;

Replace lines switch (settings.ProviderType) {...} at OpenAIService constructor:

if (settings.AuthorizationHeaderType == AuthorizationHeaderType.Undefined)
{
  switch (settings.ProviderType)
  case ProviderType.Azure:
      settings.AuthorizationHeaderType =  AuthorizationHeaderType.ApiKey;
      break;
  case ProviderType.OpenAi:
  default:
      settings.AuthorizationHeaderType =  AuthorizationHeaderType.AuthorizationHeader;
      break;

}

switch (settings.AuthorizationHeaderType)
{
  case AuthorizationHeaderType.ApiKey:
      _httpClient.DefaultRequestHeaders.Add("api-key", settings.ApiKey);
      break;
  case AuthorizationHeaderType.AuthorizationHeader:
  default:
      _httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {settings.ApiKey}");
      break;
}

Describe alternatives you've considered
AuthorizationHeaderType attribute at OpenAiOptions may be nullable.

@kayhantolga kayhantolga added the enhancement New feature or request label Jul 24, 2023
@kayhantolga kayhantolga self-assigned this Apr 3, 2024
@kayhantolga kayhantolga added this to the 8.0.1 milestone Apr 6, 2024
@kayhantolga kayhantolga modified the milestones: 8.0.1, 8.0.3 Apr 15, 2024
@kayhantolga
Copy link
Member

https://platform.openai.com/docs/api-reference/authentication I think I am confused with this issue. I am not sure if they updated the code, but in OpenAI documentation, they suggest using a Bearer token, which the library is already doing. Currently, the library already sets the x-api-key for Azure connection and Bearer token for OpenAI connection. The Python library also does the same thing, and your suggestion is also the same. Am I missing something? I am not sure. Could you help me to understand what is wrong?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request Work In Progress
Projects
None yet
Development

No branches or pull requests

2 participants