ModelFromConfig resolves a provider/model pair and constructs a fantasy language model client using the provided provider credentials. The userAgent is sent as the User-Agent header on every outgoing LLM API request. extraHeaders, when non-nil, are sent as additional HTTP headers on every request. h
( providerHint string, modelName string, providerKeys ProviderAPIKeys, userAgent string, extraHeaders map[string]string, httpClient *http.Client, )
| 1166 | // HTTP headers on every request. httpClient, when non-nil, is used for |
| 1167 | // all provider HTTP requests. |
| 1168 | func ModelFromConfig( |
| 1169 | providerHint string, |
| 1170 | modelName string, |
| 1171 | providerKeys ProviderAPIKeys, |
| 1172 | userAgent string, |
| 1173 | extraHeaders map[string]string, |
| 1174 | httpClient *http.Client, |
| 1175 | ) (fantasy.LanguageModel, error) { |
| 1176 | provider, modelID, err := ResolveModelWithProviderHint(modelName, providerHint) |
| 1177 | if err != nil { |
| 1178 | return nil, err |
| 1179 | } |
| 1180 | |
| 1181 | apiKey := providerKeys.APIKey(provider) |
| 1182 | if apiKey == "" && |
| 1183 | !(ProviderAllowsAmbientCredentials(provider) && providerKeys.HasProvider(provider)) { |
| 1184 | return nil, missingProviderAPIKeyError(provider) |
| 1185 | } |
| 1186 | baseURL := providerKeys.BaseURL(provider) |
| 1187 | |
| 1188 | var providerClient fantasy.Provider |
| 1189 | switch provider { |
| 1190 | case fantasyanthropic.Name: |
| 1191 | options := []fantasyanthropic.Option{ |
| 1192 | fantasyanthropic.WithAPIKey(apiKey), |
| 1193 | fantasyanthropic.WithUserAgent(userAgent), |
| 1194 | } |
| 1195 | if len(extraHeaders) > 0 { |
| 1196 | options = append(options, fantasyanthropic.WithHeaders(extraHeaders)) |
| 1197 | } |
| 1198 | if baseURL != "" { |
| 1199 | options = append(options, fantasyanthropic.WithBaseURL(baseURL)) |
| 1200 | } |
| 1201 | if httpClient != nil { |
| 1202 | options = append(options, fantasyanthropic.WithHTTPClient(httpClient)) |
| 1203 | } |
| 1204 | providerClient, err = fantasyanthropic.New(options...) |
| 1205 | case fantasyazure.Name: |
| 1206 | if baseURL == "" { |
| 1207 | return nil, xerrors.New("AZURE_OPENAI_BASE_URL is not set") |
| 1208 | } |
| 1209 | azureOpts := []fantasyazure.Option{ |
| 1210 | fantasyazure.WithAPIKey(apiKey), |
| 1211 | fantasyazure.WithBaseURL(baseURL), |
| 1212 | fantasyazure.WithUseResponsesAPI(), |
| 1213 | fantasyazure.WithUserAgent(userAgent), |
| 1214 | } |
| 1215 | if len(extraHeaders) > 0 { |
| 1216 | azureOpts = append(azureOpts, fantasyazure.WithHeaders(extraHeaders)) |
| 1217 | } |
| 1218 | if httpClient != nil { |
| 1219 | azureOpts = append(azureOpts, fantasyazure.WithHTTPClient(httpClient)) |
| 1220 | } |
| 1221 | providerClient, err = fantasyazure.New(azureOpts...) |
| 1222 | case fantasybedrock.Name: |
| 1223 | bedrockOpts := []fantasybedrock.Option{ |
| 1224 | fantasybedrock.WithUserAgent(userAgent), |
| 1225 | } |