Skip to content

Configuration improvements #27

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
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,9 @@ preferences:
models:
- name: gpt-4-1106-preview
endpoint: https://api.openai.com/v1/chat/completions
auth_env_var: OPENAI_API_KEY
org_env_var: OPENAI_ORG_ID
api_key: ${OPENAI_API_KEY}
org_id: ${OPENAI_ORG_ID}
project_id: ${OPENAI_PROJECT_ID}
prompt:
[
{
Expand All @@ -141,7 +142,7 @@ models:
config_format_version: "1"
````

**Note:** The `auth_env_var` is set to `OPENAI_API_KEY` verbatim, not the key itself, so as to not keep sensitive information in the config file.
**Note:** The `api_key` references an environment variable by default, not the key itself.

### Setting Up a Local Model

Expand All @@ -163,8 +164,9 @@ Here's what I did:
models:
- name: stablelm-zephyr-3b.Q8_0
endpoint: http://127.0.0.1:8080/v1/chat/completions
auth_env_var: OPENAI_API_KEY
org_env_var: OPENAI_ORG_ID
api_key: ${OPENAI_API_KEY}
org_id: ${OPENAI_ORG_ID}
project_id: ${OPENAI_PROJECT_ID}
prompt:
- role: system
content:
Expand Down Expand Up @@ -212,7 +214,7 @@ Define `AZURE_OPENAI_API_KEY` environment variable and make few changes to the c
models:
- name: azure-gpt-4
endpoint: https://<resource_name>.openai.azure.com/openai/deployments/<deployment_name>/chat/completions?api-version=<api_version>
auth_env_var: AZURE_OPENAI_API_KEY
api_key: ${AZURE_OPENAI_API_KEY}
```

### I Fucked Up The Config File
Expand Down
38 changes: 18 additions & 20 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
. "q/types"
"q/util"

"regexp"
"runtime"
"strings"

Expand Down Expand Up @@ -295,24 +296,25 @@ func initialModel(prompt string, client *llm.LLMClient) model {

// === Main === //

var envVarNameRegex = regexp.MustCompile(`\$\{?([a-zA-Z_][a-zA-Z0-9_]*)\}?`)

func printAPIKeyNotSetMessage(modelConfig ModelConfig) {
auth := modelConfig.Auth
auth := modelConfig.ApiKey.Raw()
r, _ := glamour.NewTermRenderer(
glamour.WithAutoStyle(),
)

profileScriptName := ".zshrc or.bashrc"
shellSyntax := "\n```bash\nexport OPENAI_API_KEY=[your key]\n```"
if runtime.GOOS == "windows" {
profileScriptName = "$profile"
shellSyntax = "\n```powershell\n$env:OPENAI_API_KEY = \"[your key]\"\n```"
}

styleRed := lipgloss.NewStyle().Foreground(lipgloss.Color("9"))

switch auth {
case "OPENAI_API_KEY":
msg1 := styleRed.Render("OPENAI_API_KEY environment variable not set.")
if envVarNameRegex.MatchString(auth) {
varName := envVarNameRegex.ReplaceAllString(auth, "$1")
profileScriptName := ".zshrc or.bashrc"
shellSyntax := fmt.Sprintf("\n```bash\nexport %s=[your key]\n```", varName)
if runtime.GOOS == "windows" {
profileScriptName = "$profile"
shellSyntax = fmt.Sprintf("\n```powershell\n$env:%s = \"[your key]\"\n```", varName)
}

msg1 := styleRed.Render(fmt.Sprintf("%s environment variable not set.", varName))

// make it platform agnostic
message_string := fmt.Sprintf(`
Expand All @@ -324,8 +326,8 @@ func printAPIKeyNotSetMessage(modelConfig ModelConfig) {

msg2, _ := r.Render(message_string)
fmt.Printf("\n %v%v\n", msg1, msg2)
default:
msg := styleRed.Render(auth + " environment variable not set.")
} else {
msg := styleRed.Render("api_key value not set in config.")
fmt.Printf("\n %v", msg)
}
}
Expand Down Expand Up @@ -357,23 +359,19 @@ func runQProgram(prompt string) {
}

modelConfig, err := getModelConfig(appConfig)

if err != nil {
config.PrintConfigErrorMessage(err)
os.Exit(1)
}
auth := os.Getenv(modelConfig.Auth)
if auth == "" || os.Getenv(modelConfig.Auth) == "" {
if modelConfig.ApiKey.Resolve() == "" {
printAPIKeyNotSetMessage(modelConfig)
os.Exit(1)
}
// everything checks out, save the config
// TODO: maybe add a validating function
config.SaveAppConfig(appConfig)

orgID := os.Getenv(modelConfig.OrgID)
modelConfig.Auth = auth
modelConfig.OrgID = orgID

c := llm.NewLLMClient(modelConfig)
p := tea.NewProgram(initialModel(prompt, c))
c.StreamCallback = streamHandler(p)
Expand Down
5 changes: 1 addition & 4 deletions config/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,10 +399,7 @@ func modelDetailsForModelMenu(appConfig AppConfig, modelConfig types.ModelConfig
title: "Endpoint: " + modelConfig.Endpoint,
},
{
title: "Auth: " + modelConfig.Auth,
},
{
title: "Auth: " + modelConfig.Auth,
title: "Auth: " + modelConfig.ApiKey.Raw(),
},
{
title: "Prompt",
Expand Down
6 changes: 6 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ func loadExistingConfig(filePath string) (AppConfig, error) {
if err != nil {
return config, fmt.Errorf("error unmarshalling config file: %s", err)
}
if config.Version == "1" {
for i, modelCfg := range config.Models {
config.Models[i] = modelCfg.Migrate()
}
config.Version = "2"
}
return config, nil
}

Expand Down
17 changes: 10 additions & 7 deletions config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ preferences:
models:
- name: gpt-4
endpoint: https://api.openai.com/v1/chat/completions
auth_env_var: OPENAI_API_KEY
org_env_var: OPENAI_ORG_ID
api_key: ${OPENAI_API_KEY}
org_id: ${OPENAI_ORG_ID}
project_id: ${OPENAI_PROJECT_ID}
prompt:
[
{
Expand All @@ -18,8 +19,9 @@ models:

- name: gpt-4-1106-preview
endpoint: https://api.openai.com/v1/chat/completions
auth_env_var: OPENAI_API_KEY
org_env_var: OPENAI_ORG_ID
api_key: ${OPENAI_API_KEY}
org_id: ${OPENAI_ORG_ID}
project_id: ${OPENAI_PROJECT_ID}
prompt:
[
{
Expand All @@ -32,8 +34,9 @@ models:

- name: gpt-3.5-turbo
endpoint: https://api.openai.com/v1/chat/completions
auth_env_var: OPENAI_API_KEY
org_env_var: OPENAI_ORG_ID
api_key: ${OPENAI_API_KEY}
org_id: ${OPENAI_ORG_ID}
project_id: ${OPENAI_PROJECT_ID}
prompt:
[
{
Expand All @@ -49,4 +52,4 @@ models:
{ role: "assistant", content: "```bash\necho \"hi\"\n```" },
]

config_format_version: "1"
config_format_version: "2"
14 changes: 10 additions & 4 deletions llm/llm.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,19 @@ func (c *LLMClient) createRequest(payload Payload) (*http.Request, error) {
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
apiKey := c.config.ApiKey.Resolve()
if strings.Contains(c.config.Endpoint, "openai.azure.com") {
req.Header.Set("Api-Key", c.config.Auth)
req.Header.Set("Api-Key", apiKey)
} else {
req.Header.Set("Authorization", "Bearer "+c.config.Auth)
req.Header.Set("Authorization", "Bearer "+apiKey)
}
if c.config.OrgID != "" {
req.Header.Set("OpenAI-Organization", c.config.OrgID)
orgID := c.config.OrgID.Resolve()
if orgID != "" {
req.Header.Set("OpenAI-Organization", orgID)
}
projectID := c.config.ProjectID.Resolve()
if projectID != "" {
req.Header.Set("OpenAI-Project", projectID)
}
req.Header.Set("Content-Type", "application/json")
return req, nil
Expand Down
42 changes: 37 additions & 5 deletions types/types.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,43 @@
package types

import (
"fmt"
"os"
)

type ValueOrVar string

func (v ValueOrVar) Raw() string {
return string(v)
}

func (v ValueOrVar) Resolve() string {
return os.ExpandEnv(string(v))
}

type ModelConfig struct {
ModelName string `yaml:"name"`
Endpoint string `yaml:"endpoint"`
Auth string `yaml:"auth_env_var"`
OrgID string `yaml:"org_env_var,omitempty"`
Prompt []Message `yaml:"prompt"`
ModelName string `yaml:"name"`
Endpoint string `yaml:"endpoint"`
ApiKey ValueOrVar `yaml:"api_key,omitempty"`
OrgID ValueOrVar `yaml:"org_id,omitempty"`
ProjectID ValueOrVar `yaml:"project_id,omitempty"`
Prompt []Message `yaml:"prompt"`
// deprecated var-only keys
V1_Auth string `yaml:"auth_env_var,omitempty"`
V1_OrgID string `yaml:"org_env_var,omitempty"`
V1_ProjectID string `yaml:"project_env_var,omitempty"`
}

func (c ModelConfig) Migrate() ModelConfig {
c.ApiKey = ValueOrVar(fmt.Sprintf("${%s}", c.V1_Auth))
c.V1_Auth = ""

c.OrgID = ValueOrVar(fmt.Sprintf("${%s}", c.V1_OrgID))
c.V1_OrgID = ""

c.ProjectID = ValueOrVar("${OPENAI_PROJECT_ID}")

return c
}

type Message struct {
Expand Down