zzet--gortex
a06f331eb8
CI / benchmark (push) Has been skipped
install-script / posix-syntax (push) Successful in 6m1s
CI / build-onnx (push) Failing after 6m43s
init-smoke / dry-run (push) Failing after 15m57s
security / govulncheck (push) Has been cancelled
security / trivy-fs (push) Has been cancelled
CI / test (1.26, ubuntu-latest) (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
CI / test (1.26, macos-latest) (push) Has been cancelled
CI / build-windows (push) Has been cancelled
CI / lint (push) Has been cancelled
install-script / powershell-syntax (push) Has been cancelled
install-script / install (macos-14) (push) Has been cancelled
install-script / install (ubuntu-latest) (push) Has been cancelled
53 行
1.7 KiB
Go
53 行
1.7 KiB
Go
// Package openai is the hosted OpenAI Chat Completions llm.Provider.
|
|
//
|
|
// It is pure Go — available in every build. The OpenAI wire format,
|
|
// the json_schema structured-output mechanism, and the hollow-200
|
|
// retry all live in the shared openaicompat.Client; this package is
|
|
// just the constructor that addresses api.openai.com with a Bearer
|
|
// key. Azure OpenAI and user-registered custom OpenAI-compatible
|
|
// endpoints reuse the same core through their own constructors.
|
|
package openai
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/zzet/gortex/internal/llm"
|
|
"github.com/zzet/gortex/internal/llm/provider/openaicompat"
|
|
)
|
|
|
|
// New constructs the OpenAI provider. The API key is read from the env
|
|
// var named by cfg.APIKeyEnv (default OPENAI_API_KEY); an unset key is
|
|
// a hard error.
|
|
func New(cfg llm.RemoteConfig) (llm.Provider, error) {
|
|
keyEnv := strings.TrimSpace(cfg.APIKeyEnv)
|
|
if keyEnv == "" {
|
|
keyEnv = "OPENAI_API_KEY"
|
|
}
|
|
key := strings.TrimSpace(os.Getenv(keyEnv))
|
|
if key == "" {
|
|
return nil, fmt.Errorf("openai: API key env %q is not set", keyEnv)
|
|
}
|
|
if strings.TrimSpace(cfg.Model) == "" {
|
|
return nil, errors.New("openai: llm.openai.model is empty")
|
|
}
|
|
base := strings.TrimRight(strings.TrimSpace(cfg.BaseURL), "/")
|
|
if base == "" {
|
|
base = "https://api.openai.com"
|
|
}
|
|
return &openaicompat.Client{
|
|
ProviderID: "openai",
|
|
Tag: "openai",
|
|
Model: cfg.Model,
|
|
URL: base + "/v1/chat/completions",
|
|
Headers: map[string]string{"authorization": "Bearer " + key},
|
|
HTTPClient: &http.Client{Timeout: 120 * time.Second},
|
|
SchemaMode: openaicompat.SchemaJSONSchema,
|
|
ReasoningEffort: strings.TrimSpace(cfg.Effort),
|
|
}, nil
|
|
}
|