| |
| import React, { useEffect, useState } from 'react'; |
| |
| |
| |
| import InputWithLabel from './InputWithLabel'; |
| import type { TConfigProps } from '~/common'; |
|
|
| function isJson(str: string) { |
| try { |
| JSON.parse(str); |
| } catch (e) { |
| return false; |
| } |
| return true; |
| } |
|
|
| const OpenAIConfig = ({ userKey, setUserKey, endpoint }: TConfigProps) => { |
| const [showPanel, setShowPanel] = useState(endpoint === 'azureOpenAI'); |
|
|
| useEffect(() => { |
| if (isJson(userKey)) { |
| setShowPanel(true); |
| } |
| setUserKey(''); |
| }, []); |
|
|
| useEffect(() => { |
| if (!showPanel && isJson(userKey)) { |
| setUserKey(''); |
| } |
| }, [showPanel]); |
|
|
| function getAzure(name: string) { |
| if (isJson(userKey)) { |
| const newKey = JSON.parse(userKey); |
| return newKey[name]; |
| } else { |
| return ''; |
| } |
| } |
|
|
| function setAzure(name: string, value: number | string | boolean) { |
| let newKey = {}; |
| if (isJson(userKey)) { |
| newKey = JSON.parse(userKey); |
| } |
| newKey[name] = value; |
|
|
| setUserKey(JSON.stringify(newKey)); |
| } |
| return ( |
| <> |
| {!showPanel ? ( |
| <> |
| <InputWithLabel |
| id={endpoint} |
| value={userKey ?? ''} |
| onChange={(e: { target: { value: string } }) => setUserKey(e.target.value ?? '')} |
| label={'OpenAI API Key'} |
| /> |
| </> |
| ) : ( |
| <> |
| <InputWithLabel |
| id={'instanceNameLabel'} |
| value={getAzure('azureOpenAIApiInstanceName') ?? ''} |
| onChange={(e: { target: { value: string } }) => |
| setAzure('azureOpenAIApiInstanceName', e.target.value ?? '') |
| } |
| label={'Azure OpenAI Instance Name'} |
| /> |
| |
| <InputWithLabel |
| id={'deploymentNameLabel'} |
| value={getAzure('azureOpenAIApiDeploymentName') ?? ''} |
| onChange={(e: { target: { value: string } }) => |
| setAzure('azureOpenAIApiDeploymentName', e.target.value ?? '') |
| } |
| label={'Azure OpenAI Deployment Name'} |
| /> |
| |
| <InputWithLabel |
| id={'versionLabel'} |
| value={getAzure('azureOpenAIApiVersion') ?? ''} |
| onChange={(e: { target: { value: string } }) => |
| setAzure('azureOpenAIApiVersion', e.target.value ?? '') |
| } |
| label={'Azure OpenAI API Version'} |
| /> |
| |
| <InputWithLabel |
| id={'apiKeyLabel'} |
| value={getAzure('azureOpenAIApiKey') ?? ''} |
| onChange={(e: { target: { value: string } }) => |
| setAzure('azureOpenAIApiKey', e.target.value ?? '') |
| } |
| label={'Azure OpenAI API Key'} |
| /> |
| </> |
| )} |
| { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| } |
| </> |
| ); |
| }; |
|
|
| export default OpenAIConfig; |
|
|