MCPcopy Create free account
hub / github.com/appleboy/CodeGPT / NewOpenAI

Function NewOpenAI

cmd/provider.go:31–77  ·  view source on GitHub ↗
(ctx context.Context)

Source from the content-addressed store, hash-verified

29}
30
31func NewOpenAI(ctx context.Context) (*openai.Client, error) {
32 var apiKey string
33
34 // Try to get API key from helper first, fallback to static config
35 if helper := viper.GetString("openai.api_key_helper"); helper != "" {
36 var refreshInterval time.Duration
37 if viper.IsSet("openai.api_key_helper_refresh_interval") {
38 // User explicitly set a value (could be 0 to disable cache)
39 refreshInterval = time.Duration(
40 viper.GetInt("openai.api_key_helper_refresh_interval"),
41 ) * time.Second
42 } else {
43 // Not set, use default
44 refreshInterval = util.DefaultRefreshInterval
45 }
46 key, err := util.GetAPIKeyFromHelperWithCache(ctx, helper, refreshInterval)
47 if err != nil {
48 return nil, err
49 }
50 apiKey = key
51 } else {
52 key, err := getAPIKey("openai.api_key")
53 if err != nil {
54 return nil, err
55 }
56 apiKey = key
57 }
58
59 return openai.New(
60 openai.WithToken(apiKey),
61 openai.WithModel(viper.GetString("openai.model")),
62 openai.WithOrgID(viper.GetString("openai.org_id")),
63 openai.WithProxyURL(viper.GetString("openai.proxy")),
64 openai.WithSocksURL(viper.GetString("openai.socks")),
65 openai.WithBaseURL(viper.GetString("openai.base_url")),
66 openai.WithTimeout(viper.GetDuration("openai.timeout")),
67 openai.WithMaxTokens(viper.GetInt("openai.max_tokens")),
68 openai.WithTemperature(float32(viper.GetFloat64("openai.temperature"))),
69 openai.WithProvider(core.Platform(viper.GetString("openai.provider"))),
70 openai.WithSkipVerify(viper.GetBool("openai.skip_verify")),
71 openai.WithHeaders(viper.GetStringSlice("openai.headers")),
72 openai.WithAPIVersion(viper.GetString("openai.api_version")),
73 openai.WithTopP(float32(viper.GetFloat64("openai.top_p"))),
74 openai.WithFrequencyPenalty(float32(viper.GetFloat64("openai.frequency_penalty"))),
75 openai.WithPresencePenalty(float32(viper.GetFloat64("openai.presence_penalty"))),
76 )
77}
78
79// NewGemini returns a new Gemini client
80func NewGemini(ctx context.Context) (*gemini.Client, error) {

Callers 1

GetClientFunction · 0.85

Calls 15

NewFunction · 0.92
WithTokenFunction · 0.92
WithModelFunction · 0.92
WithOrgIDFunction · 0.92
WithProxyURLFunction · 0.92
WithSocksURLFunction · 0.92
WithBaseURLFunction · 0.92
WithTimeoutFunction · 0.92
WithMaxTokensFunction · 0.92
WithTemperatureFunction · 0.92
WithProviderFunction · 0.92

Tested by

no test coverage detected