@@ -16,7 +16,7 @@ import {
1616} from '@kepler.gl/components' ;
1717import { State } from '../index' ;
1818import ApiKey from '../icons/api-key' ;
19- import { PROVIDER_MODELS } from '../config/models' ;
19+ import { PROVIDER_MODELS , PROVIDER_DEFAULT_BASE_URLS } from '../config/models' ;
2020import { useLocalStorage } from 'usehooks-ts' ;
2121import { GetAssistantModelByProvider } from '@openassistant/core' ;
2222import { updateAiAssistantConfig } from '../actions' ;
@@ -152,7 +152,7 @@ export function AiAssistantConfig() {
152152 const [ topP , setTopP ] = useLocalStorage ( 'ai-assistant-top-p' , aiAssistantConfig . topP || 1.0 ) ;
153153 const [ baseUrl , setBaseUrl ] = useLocalStorage (
154154 'ai-assistant-base-url' ,
155- aiAssistantConfig . baseUrl || 'http://localhost:11434/api'
155+ aiAssistantConfig . baseUrl || PROVIDER_DEFAULT_BASE_URLS [ provider ]
156156 ) ;
157157 const [ mapboxToken , setMapboxToken ] = useLocalStorage (
158158 'ai-assistant-mapbox-token' ,
@@ -168,6 +168,7 @@ export function AiAssistantConfig() {
168168 if ( typeof value === 'string' ) {
169169 setProvider ( value ) ;
170170 setModel ( PROVIDER_MODELS [ value ] [ 0 ] ) ;
171+ setBaseUrl ( PROVIDER_DEFAULT_BASE_URLS [ value ] ) ;
171172 setConnectionError ( false ) ;
172173 setErrorMessage ( '' ) ;
173174 }
@@ -224,19 +225,32 @@ export function AiAssistantConfig() {
224225 setTimeout ( ( ) => reject ( new Error ( 'Connection timeout after 15 seconds' ) ) , 15000 ) ;
225226 } ) ;
226227
227- const AssistantModel = GetAssistantModelByProvider ( {
228+ const AssistantModel = await GetAssistantModelByProvider ( {
228229 provider : provider
229230 } ) ;
230231
232+ if ( ! AssistantModel || ! AssistantModel . configure || ! AssistantModel . testConnection ) {
233+ throw new Error ( 'Failed to initialize AI model' ) ;
234+ }
235+
236+ // configure model
237+ AssistantModel . configure ( {
238+ model : model ,
239+ baseURL : baseUrl || PROVIDER_DEFAULT_BASE_URLS [ provider ] ,
240+ apiKey : apiKey ,
241+ temperature : Number ( temperature ) ,
242+ topP : Number ( topP )
243+ } ) ;
244+
231245 const success = ( await Promise . race ( [
232- AssistantModel ? .testConnection ( apiKey , model ) ,
246+ AssistantModel . testConnection ( apiKey , model ) ,
233247 timeoutPromise
234248 ] ) ) as boolean ;
235249
236250 const errorMessage = ! success
237251 ? provider === 'ollama'
238- ? 'Connection failed: maybe invalid Ollama Base URL'
239- : 'Connection failed: maybe invalid API Key'
252+ ? 'Connection failed: maybe invalid Base URL'
253+ : 'Connection failed: maybe invalid API Key or Base URL '
240254 : '' ;
241255 setConnectionError ( ! success ) ;
242256 setErrorMessage ( errorMessage ) ;
@@ -245,7 +259,7 @@ export function AiAssistantConfig() {
245259 provider : provider ,
246260 model : model ,
247261 apiKey : apiKey ,
248- baseUrl : baseUrl ,
262+ baseUrl : baseUrl || PROVIDER_DEFAULT_BASE_URLS [ provider ] ,
249263 isReady : success ,
250264 temperature : temperature ,
251265 topP : topP ,
@@ -322,11 +336,11 @@ export function AiAssistantConfig() {
322336 />
323337 </ OllamaModelInputWrapper >
324338 ) }
325- { provider !== 'ollama' ? (
339+ { provider !== 'ollama' && (
326340 < >
327341 < PanelLabelWrapper >
328342 < SectionTitle >
329- < FormattedMessage id = "aiAssistantManager.apiKey.placeholder " />
343+ < FormattedMessage id = "aiAssistantManager.apiKey.title " />
330344 </ SectionTitle >
331345 </ PanelLabelWrapper >
332346 < div className = "api-key-input" >
@@ -341,26 +355,23 @@ export function AiAssistantConfig() {
341355 />
342356 </ div >
343357 </ >
344- ) : (
345- < >
346- < PanelLabelWrapper >
347- < SectionTitle >
348- < FormattedMessage id = "aiAssistantManager.baseUrl.placeholder" />
349- </ SectionTitle >
350- </ PanelLabelWrapper >
351- < div className = "api-key-input" >
352- < div className = "api-key-input__icon" >
353- < ApiKey height = "20px" />
354- </ div >
355- < Input
356- type = "text"
357- onChange = { onBaseUrlChange }
358- placeholder = { intl . formatMessage ( { id : 'aiAssistantManager.baseUrl.placeholder' } ) }
359- value = { baseUrl }
360- />
361- </ div >
362- </ >
363358 ) }
359+ < PanelLabelWrapper >
360+ < SectionTitle >
361+ < FormattedMessage id = "aiAssistantManager.baseUrl.title" />
362+ </ SectionTitle >
363+ </ PanelLabelWrapper >
364+ < div className = "api-key-input" >
365+ < div className = "api-key-input__icon" >
366+ < ApiKey height = "20px" />
367+ </ div >
368+ < Input
369+ type = "text"
370+ onChange = { onBaseUrlChange }
371+ placeholder = { PROVIDER_DEFAULT_BASE_URLS [ provider ] }
372+ value = { baseUrl }
373+ />
374+ </ div >
364375 { connectionError && (
365376 < StyleErrorMessage className = "error-message" > { errorMessage } </ StyleErrorMessage >
366377 ) }
0 commit comments