Add configurable auth header and token prefix

This commit is contained in:
kardolus
2023-11-10 12:35:41 -05:00
parent 2e0b5445b6
commit 773c8bf6db
5 changed files with 176 additions and 808 deletions

View File

@@ -182,6 +182,8 @@ Configuration variables:
| `url` | The base URL for the OpenAI API. | 'https://api.openai.com' |
| `completions_path` | The API endpoint for completions. | '/v1/chat/completions' |
| `models_path` | The API endpoint for accessing model information. | '/v1/models' |
| `auth_header` | The header used for authorization in API requests. | 'Authorization' |
| `auth_token_prefix` | The prefix to be added before the token in the `auth_header`. | 'Bearer ' |
The defaults can be overridden by providing your own values in the user configuration file,
named `.chatgpt-cli/config.yaml`, located in your home directory.

View File

@@ -15,6 +15,8 @@ const (
openAIURL = "https://api.openai.com"
openAICompletionsPath = "/v1/chat/completions"
openAIModelsPath = "/v1/models"
openAIAuthHeader = "Authorization"
openAIAuthTokenPrefix = "Bearer "
openAIRole = "You are a helpful assistant."
openAIThread = "default"
openAITemperature = 1.0
@@ -61,6 +63,8 @@ func (f *FileIO) ReadDefaults() types.Config {
URL: openAIURL,
CompletionsPath: openAICompletionsPath,
ModelsPath: openAIModelsPath,
AuthHeader: openAIAuthHeader,
AuthTokenPrefix: openAIAuthTokenPrefix,
Thread: openAIThread,
Temperature: openAITemperature,
TopP: openAITopP,

File diff suppressed because it is too large Load Diff

View File

@@ -13,14 +13,12 @@ import (
)
const (
bearer = "Bearer %s"
contentType = "application/json"
errFailedToRead = "failed to read response: %w"
errFailedToCreateRequest = "failed to create request: %w"
errFailedToMakeRequest = "failed to make request: %w"
errHTTP = "http status %d: %s"
errHTTPStatus = "http status: %d"
headerAuthorization = "Authorization"
headerContentType = "Content-Type"
)
@@ -139,7 +137,7 @@ func (r *RestCaller) newRequest(method, url string, body []byte) (*http.Request,
}
if r.config.APIKey != "" {
req.Header.Set(headerAuthorization, fmt.Sprintf(bearer, r.config.APIKey))
req.Header.Set(r.config.AuthHeader, r.config.AuthTokenPrefix+r.config.APIKey)
}
req.Header.Set(headerContentType, contentType)

View File

@@ -15,4 +15,6 @@ type Config struct {
URL string `yaml:"url"`
CompletionsPath string `yaml:"completions_path"`
ModelsPath string `yaml:"models_path"`
AuthHeader string `yaml:"auth_header"`
AuthTokenPrefix string `yaml:"auth_token_prefix"`
}