11'use client' ;
2- import CustomSelect from '@/components/CustomSelect/CustomSelect' ;
32import React , { useEffect , useState } from 'react' ;
43import { Button } from '@nextui-org/react' ;
54import { createOrUpdateLLMAPIKey , getLLMAPIKeys } from '@/api/DashboardService' ;
65import toast from 'react-hot-toast' ;
7-
8- interface ModelsList {
9- model_name : string ;
10- api_key : string ;
11- }
6+ import CustomInput from '@/components/CustomInput/CustomInput' ;
7+ import imagePath from '@/app/imagePath' ;
8+ import { ModelsList } from '../../../../types/settingTypes' ;
129
1310export default function Models ( ) {
14- const [ selectedValue , setSelectedValue ] = useState < string > ( '' ) ;
15- const [ apiKey , setApiKey ] = useState < string | null > ( '' ) ;
1611 const [ modelsList , setModelsList ] = useState < ModelsList [ ] | null > ( null ) ;
12+ const modelDetails = {
13+ 'gpt-4o' : { text : 'Open AI API Key (gpt-4o)' , icon : imagePath . openAIIcon } ,
14+ 'claude-3' : {
15+ text : 'Anthropic API Key (claude-3.4-sonnet)' ,
16+ icon : imagePath . claudeIcon ,
17+ } ,
18+ } ;
1719
1820 const handleButtonClick = ( ) => {
1921 toCreateOrUpdateLLMAPIKey ( ) . then ( ) . catch ( ) ;
2022 } ;
2123
24+ const handleApiKeyChange = ( model_name : string , value : string ) => {
25+ setModelsList (
26+ ( prev ) =>
27+ prev ?. map ( ( model ) =>
28+ model . model_name === model_name
29+ ? { ...model , api_key : value }
30+ : model ,
31+ ) || null ,
32+ ) ;
33+ } ;
34+
2235 useEffect ( ( ) => {
2336 toGetLLMAPIKeys ( ) . then ( ) . catch ( ) ;
2437 } , [ ] ) ;
2538
26- useEffect ( ( ) => {
27- if ( modelsList && selectedValue . length > 0 ) {
28- const selectedModel = modelsList . find (
29- ( model ) => model . model_name === selectedValue ,
30- ) ;
31- if ( selectedModel ) {
32- setApiKey ( selectedModel . api_key ) ;
33- }
34- }
35- } , [ selectedValue ] ) ;
36-
37- useEffect ( ( ) => {
38- if ( modelsList ) setSelectedValue ( modelsList [ 0 ] . model_name ) ;
39- } , [ modelsList ] ) ;
40-
4139 async function toGetLLMAPIKeys ( ) {
4240 try {
4341 const organisation_id = localStorage . getItem ( 'organisationId' ) ;
4442 const response = await getLLMAPIKeys ( organisation_id ) ;
4543 if ( response ) {
46- const data = response . data ;
47- console . log ( data ) ;
44+ const data = response . data . map ( ( model : ModelsList ) => ( {
45+ ...model ,
46+ ...modelDetails [ model . model_name ] ,
47+ } ) ) ;
4848 setModelsList ( data ) ;
4949 }
5050 } catch ( error ) {
@@ -54,19 +54,17 @@ export default function Models() {
5454
5555 async function toCreateOrUpdateLLMAPIKey ( ) {
5656 try {
57- let id = null ;
58- if ( typeof window !== 'undefined' ) {
59- id = localStorage . getItem ( 'organisationId' ) ;
60- }
57+ const apiKeys = modelsList . map ( ( model ) => ( {
58+ llm_model : model . model_name ,
59+ llm_api_key : model . api_key ,
60+ } ) ) ;
61+
6162 const payload = {
62- organisation_id : Number ( id ) ,
63- llm_model : selectedValue ,
64- llm_api_key : apiKey ,
63+ api_keys : apiKeys ,
6564 } ;
65+
6666 const response = await createOrUpdateLLMAPIKey ( payload ) ;
6767 if ( response ) {
68- const data = response . data ;
69- console . log ( data ) ;
7068 toast . success ( 'Model is setup successfully' ) ;
7169 toGetLLMAPIKeys ( ) . then ( ) . catch ( ) ;
7270 }
@@ -81,40 +79,28 @@ export default function Models() {
8179 return (
8280 < div id = { 'models_section' } className = { 'proxima_nova flex flex-col gap-6' } >
8381 < span id = { 'title' } className = { 'text-xl font-semibold text-white' } >
84- { ' ' }
85- Setup Model{ ' ' }
82+ Setup Models
8683 </ span >
87- < div id = { 'model_selection_section' } className = { 'flex flex-col gap-2' } >
88- < span className = { 'secondary_color text-[13px] font-medium' } > Model</ span >
89- < CustomSelect
90- selectedValues = { selectedValue }
91- setSelectedValues = { setSelectedValue }
92- >
93- { modelsList &&
94- modelsList . length > 0 &&
95- modelsList . map ( ( model , index ) => (
96- < CustomSelect . Item key = { index } value = { model . model_name } >
97- { model . model_name }
98- </ CustomSelect . Item >
99- ) ) }
100- </ CustomSelect >
101- </ div >
102- < div id = { 'api_key_section' } className = { 'flex flex-col gap-2' } >
103- < span className = { 'secondary_color text-[13px] font-medium' } >
104- { ' ' }
105- Open AI API Key{ ' ' }
106- </ span >
107- < input
108- value = { apiKey }
109- type = { 'password' }
110- className = { 'input_medium' }
111- placeholder = { 'Enter API Key here' }
112- onChange = { ( event ) => setApiKey ( event . target . value ) }
113- />
84+ < div id = { 'api_key_section' } className = { 'flex flex-col gap-6' } >
85+ { modelsList ?. map ( ( model ) => (
86+ < div key = { model . model_name } className = { 'flex flex-col gap-2' } >
87+ < span className = { 'secondary_color text-[13px] font-medium' } >
88+ { model . text }
89+ </ span >
90+ < CustomInput
91+ format = { 'password' }
92+ value = { model . api_key || '' }
93+ setter = { ( value ) => handleApiKeyChange ( model . model_name , value ) }
94+ placeholder = { 'Enter API Key here' }
95+ icon = { model . icon }
96+ iconCSS = { 'size-4' }
97+ alt = { `${ model . model_name } _icon` }
98+ />
99+ </ div >
100+ ) ) }
114101 </ div >
115102 < Button className = { 'primary_medium w-fit' } onClick = { handleButtonClick } >
116- { ' ' }
117- Update Changes{ ' ' }
103+ Update Changes
118104 </ Button >
119105 </ div >
120106 ) ;
0 commit comments