From 81045bd15a569d5d8c4231a9482a89d44e410946 Mon Sep 17 00:00:00 2001 From: Na'aman Hirschfeld Date: Tue, 26 Dec 2023 15:50:25 +0100 Subject: [PATCH 01/15] chore: refactored cohere client --- .idea/runConfigurations/Cohere_Connector.xml | 15 + .../config-create-wizard/base-form.tsx | 74 +-- .../cohere-model-parameters-form.tsx | 296 ++++++++++++ .../openai-model-parameters-form.tsx | 354 ++++++++++++++ .../parameters-and-prompt-form.spec.tsx | 4 +- .../parameters-and-prompt-form.tsx | 439 ++---------------- .../config-create-wizard/parameter-slider.tsx | 52 +++ gen/go/cohere/v1/cohere.pb.go | 286 +++++------- gen/ts/cohere/v1/cohere.d.ts | 83 ++-- gen/ts/cohere/v1/cohere.js | 23 +- gen/ts/google/protobuf/any.d.ts | 214 --------- gen/ts/google/protobuf/any.js | 185 -------- proto/cohere/v1/cohere.proto | 45 +- services/cohere-connector/src/client.ts | 63 ++- .../cohere-connector/src/handlers.spec.ts | 94 ++-- services/cohere-connector/src/handlers.ts | 61 ++- services/cohere-connector/src/utils.spec.ts | 98 +--- services/cohere-connector/src/utils.ts | 82 ++-- .../openai-connector/src/handlers.spec.ts | 2 +- services/openai-connector/src/handlers.ts | 11 +- services/openai-connector/src/utils.ts | 10 - shared/ts/src/grpc.ts | 11 + 22 files changed, 1172 insertions(+), 1330 deletions(-) create mode 100644 .idea/runConfigurations/Cohere_Connector.xml create mode 100644 frontend/src/components/projects/[projectId]/applications/[applicationId]/config-create-wizard/cohere-model-parameters-form.tsx create mode 100644 frontend/src/components/projects/[projectId]/applications/[applicationId]/config-create-wizard/openai-model-parameters-form.tsx create mode 100644 frontend/src/components/prompt-display-components/config-create-wizard/parameter-slider.tsx delete mode 100644 gen/ts/google/protobuf/any.d.ts delete mode 100644 gen/ts/google/protobuf/any.js diff --git a/.idea/runConfigurations/Cohere_Connector.xml b/.idea/runConfigurations/Cohere_Connector.xml new file mode 100644 index 00000000..09ea8f44 --- /dev/null +++ b/.idea/runConfigurations/Cohere_Connector.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + diff --git a/frontend/src/components/projects/[projectId]/applications/[applicationId]/config-create-wizard/base-form.tsx b/frontend/src/components/projects/[projectId]/applications/[applicationId]/config-create-wizard/base-form.tsx index b45fa56d..01de8931 100644 --- a/frontend/src/components/projects/[projectId]/applications/[applicationId]/config-create-wizard/base-form.tsx +++ b/frontend/src/components/projects/[projectId]/applications/[applicationId]/config-create-wizard/base-form.tsx @@ -37,6 +37,44 @@ export function PromptConfigBaseForm({ return Object.values(modelVendorTypeMap[modelVendor]) as string[]; }, [modelVendor]); + const activeModelVendor = useMemo( + () => + Object.entries(ModelVendor) + .filter(([, value]) => + [ModelVendor.OpenAI, ModelVendor.Cohere].includes(value), + ) + .map(([key, value]) => { + return ( + + ); + }), + [], + ); + + const invalidModelVendors = useMemo( + () => + Object.entries(UnavailableModelVendor).map(([key, value]) => ( + + )), + [], + ); + + const choices = useMemo( + () => + modelChoices.map((modelChoice) => { + return ( + + ); + }), + [modelChoices], + ); + return (
{showConfigNameInput && ( @@ -73,27 +111,10 @@ export function PromptConfigBaseForm({ className="card-select w-full" value={modelVendor} onChange={handleChange(setVendor)} - disabled={true} data-testid="create-prompt-base-form-vendor-select" > - {Object.entries(ModelVendor) - .filter( - ([, value]) => value === ModelVendor.OpenAI, - ) - .map(([key, value]) => { - return ( - - ); - })} - {Object.entries(UnavailableModelVendor).map( - ([key, value]) => ( - - ), - )} + {activeModelVendor} + {invalidModelVendors}
@@ -108,20 +129,7 @@ export function PromptConfigBaseForm({ onChange={handleChange(setModelType)} data-testid="create-prompt-base-form-model-select" > - {modelChoices.map((modelChoice) => { - return ( - - ); - })} + {choices}
diff --git a/frontend/src/components/projects/[projectId]/applications/[applicationId]/config-create-wizard/cohere-model-parameters-form.tsx b/frontend/src/components/projects/[projectId]/applications/[applicationId]/config-create-wizard/cohere-model-parameters-form.tsx new file mode 100644 index 00000000..fa949e78 --- /dev/null +++ b/frontend/src/components/projects/[projectId]/applications/[applicationId]/config-create-wizard/cohere-model-parameters-form.tsx @@ -0,0 +1,296 @@ +import { useTranslations } from 'next-intl'; +import { Dispatch, SetStateAction, useEffect, useState } from 'react'; + +import { ParameterSlider } from '@/components/prompt-display-components/config-create-wizard/parameter-slider'; +import { openAIRoleColorMap } from '@/constants/models'; +import { + ModelParameters, + ModelVendor, + OpenAIContentMessage, + OpenAIMessageRole, + OpenAIModelType, + OpenAIPromptMessageRole, +} from '@/types'; +import { handleChange } from '@/utils/events'; +import { extractTemplateVariables } from '@/utils/models'; + +export function CohereParametersForm({ + modelType, + setParameters, + existingParameters, +}: { + existingParameters?: ModelParameters; + modelType: OpenAIModelType; + setParameters: (parameters: ModelParameters) => void; +}) { + const t = useTranslations('createConfigWizard'); + + const [temperature, setTemperature] = useState( + existingParameters?.temperature ?? 1, + ); + + const inputs: { + key: string; + labelText: string; + max: number; + min: number; + setter: Dispatch>; + step: number; + toolTipText: string; + value: number; + }[] = [ + { + key: 'temperature', + labelText: t('openaiParametersTemperatureLabel'), + max: 2, + min: 0, + setter: setTemperature, + step: 0.1, + toolTipText: t('openaiParametersTemperatureTooltip'), + value: temperature, + }, + ]; + + useEffect(() => { + setParameters({ + temperature, + }); + }, [temperature]); + + return ( +
+

{t('modelParameters')}

+
+ {inputs.map(ParameterSlider)} +
+
+ ); +} + +export const OPEN_AI_DRAFT_MESSAGE = { + content: '', + name: '', + role: OpenAIPromptMessageRole.System, +} satisfies OpenAIContentMessage; + +export function OpenAIMessageItem({ + name, + role, + dataTestId, + className, + handleClick, +}: { + className: string; + dataTestId: string; + handleClick: () => void; + name?: string; + role?: OpenAIMessageRole; +}) { + const t = useTranslations('createConfigWizard'); + + return ( + + ); +} + +export function OpenAIPromptTemplate({ + messages, + setMessages, +}: { + messages: OpenAIContentMessage[]; + setMessages: (messages: OpenAIContentMessage[]) => void; +}) { + const t = useTranslations('createConfigWizard'); + + const [activeMessageIndex, setActiveMessageIndex] = useState( + messages.length, + ); + const [draftMessage, setDraftMessage] = useState( + structuredClone(OPEN_AI_DRAFT_MESSAGE), + ); + + const handleRoleChange = (role: OpenAIPromptMessageRole) => { + setDraftMessage({ ...draftMessage, role }); + }; + + const handleNameChange = (value: string) => { + setDraftMessage({ ...draftMessage, name: value }); + }; + + const handleMessageChange = (value: string) => { + setDraftMessage({ ...draftMessage, content: value }); + }; + + const handleDeleteMessage = () => { + const copiedMessages = [...messages]; + + copiedMessages.splice(activeMessageIndex, 1); + + setMessages(copiedMessages); + setActiveMessageIndex( + activeMessageIndex - 1 >= 0 ? activeMessageIndex - 1 : 0, + ); + }; + + const handleSaveMessage = () => { + const copiedMessages = [...messages]; + draftMessage.templateVariables = extractTemplateVariables( + draftMessage.content, + ); + copiedMessages[activeMessageIndex] = draftMessage; + + setMessages(copiedMessages); + setDraftMessage(structuredClone(OPEN_AI_DRAFT_MESSAGE)); + setActiveMessageIndex(copiedMessages.length); + }; + + return ( +
+

{t('promptMessages')}

+
+
+ {messages.map((message, index) => ( + { + setActiveMessageIndex(index); + setDraftMessage(message); + }} + /> + ))} + { + setActiveMessageIndex(messages.length); + setDraftMessage({ + content: '', + name: '', + role: OpenAIPromptMessageRole.System, + }); + }} + className={ + activeMessageIndex === messages.length + ? 'btn-secondary' + : 'btn-secondary text-neutral-content btn-outline hover:text-secondary-content' + } + /> +
+
+
+
+ + +
+
+ + +
+
+
+
+ +