@deep-foundation/chatgpt-azure
Advanced tools
Comparing version 3.0.5 to 3.0.6
150
deep.json
{ | ||
"package": { | ||
"name": "@deep-foundation/chatgpt-azure", | ||
"version": "3.0.5" | ||
"version": "3.0.6" | ||
}, | ||
@@ -10,3 +10,3 @@ "data": [ | ||
"dependencyId": 0, | ||
"containValue": "Reply" | ||
"containValue": "Type" | ||
}, | ||
@@ -17,4 +17,4 @@ "id": 1 | ||
"package": { | ||
"dependencyId": 0, | ||
"containValue": "Message" | ||
"dependencyId": 1, | ||
"containValue": "Reply" | ||
}, | ||
@@ -26,3 +26,3 @@ "id": 2 | ||
"dependencyId": 1, | ||
"containValue": "Any" | ||
"containValue": "Message" | ||
}, | ||
@@ -33,4 +33,4 @@ "id": 3 | ||
"package": { | ||
"dependencyId": 2, | ||
"containValue": "TSX" | ||
"dependencyId": 0, | ||
"containValue": "Any" | ||
}, | ||
@@ -41,4 +41,4 @@ "id": 4 | ||
"package": { | ||
"dependencyId": 1, | ||
"containValue": "Handler" | ||
"dependencyId": 0, | ||
"containValue": "TreeIncludeUp" | ||
}, | ||
@@ -50,3 +50,3 @@ "id": 5 | ||
"dependencyId": 1, | ||
"containValue": "clientSupportsJs" | ||
"containValue": "messagingTree" | ||
}, | ||
@@ -57,4 +57,4 @@ "id": 6 | ||
"package": { | ||
"dependencyId": 3, | ||
"containValue": "Dependency" | ||
"dependencyId": 2, | ||
"containValue": "TSX" | ||
}, | ||
@@ -65,4 +65,4 @@ "id": 7 | ||
"package": { | ||
"dependencyId": 4, | ||
"containValue": "DependencyRoot" | ||
"dependencyId": 0, | ||
"containValue": "Handler" | ||
}, | ||
@@ -73,4 +73,4 @@ "id": 8 | ||
"package": { | ||
"dependencyId": 1, | ||
"containValue": "SyncTextFile" | ||
"dependencyId": 0, | ||
"containValue": "clientSupportsJs" | ||
}, | ||
@@ -81,4 +81,4 @@ "id": 9 | ||
"package": { | ||
"dependencyId": 1, | ||
"containValue": "dockerSupportsJs" | ||
"dependencyId": 3, | ||
"containValue": "Dependency" | ||
}, | ||
@@ -89,4 +89,4 @@ "id": 10 | ||
"package": { | ||
"dependencyId": 1, | ||
"containValue": "HandleInsert" | ||
"dependencyId": 4, | ||
"containValue": "DependencyRoot" | ||
}, | ||
@@ -97,4 +97,4 @@ "id": 11 | ||
"package": { | ||
"dependencyId": 1, | ||
"containValue": "Type" | ||
"dependencyId": 0, | ||
"containValue": "SyncTextFile" | ||
}, | ||
@@ -105,4 +105,4 @@ "id": 12 | ||
"package": { | ||
"dependencyId": 5, | ||
"containValue": "Model" | ||
"dependencyId": 0, | ||
"containValue": "dockerSupportsJs" | ||
}, | ||
@@ -113,4 +113,4 @@ "id": 13 | ||
"package": { | ||
"dependencyId": 1, | ||
"containValue": "Value" | ||
"dependencyId": 0, | ||
"containValue": "HandleInsert" | ||
}, | ||
@@ -121,4 +121,4 @@ "id": 14 | ||
"package": { | ||
"dependencyId": 1, | ||
"containValue": "String" | ||
"dependencyId": 5, | ||
"containValue": "Model" | ||
}, | ||
@@ -129,4 +129,4 @@ "id": 15 | ||
"package": { | ||
"dependencyId": 1, | ||
"containValue": "HandleClient" | ||
"dependencyId": 0, | ||
"containValue": "Value" | ||
}, | ||
@@ -137,4 +137,4 @@ "id": 16 | ||
"package": { | ||
"dependencyId": 1, | ||
"containValue": "TreeIncludeUp" | ||
"dependencyId": 0, | ||
"containValue": "String" | ||
}, | ||
@@ -146,3 +146,3 @@ "id": 17 | ||
"dependencyId": 0, | ||
"containValue": "messagingTree" | ||
"containValue": "HandleClient" | ||
}, | ||
@@ -152,10 +152,26 @@ "id": 18 | ||
{ | ||
"id": "Conversation", | ||
"type": 1 | ||
}, | ||
{ | ||
"id": "Reply", | ||
"type": 2, | ||
"from": 3, | ||
"to": 4 | ||
}, | ||
{ | ||
"id": "messagingTreeReply", | ||
"type": 5, | ||
"from": 6, | ||
"to": "Reply" | ||
}, | ||
{ | ||
"id": "System", | ||
"type": 1, | ||
"from": 2, | ||
"to": 3 | ||
"from": 3, | ||
"to": 4 | ||
}, | ||
{ | ||
"id": "managerClientHandlerCode", | ||
"type": 4, | ||
"type": 7, | ||
"value": { | ||
@@ -167,4 +183,4 @@ "value": "async ({ deep, require }) => {\n const PACKAGE_NAME = \"@deep-foundation/chatgpt-azure-discord-bot\";\n const logsPrefix = \"@chatgpt-azure\" + \" \" + \"manager\" + \"ClientHandler\";\n const React = await deep.import(\"react\");\n const { useState, useMemo, useRef } = React;\n const Chakra = await deep.import(\"@chakra-ui/react\");\n console.log(logsPrefix, { Chakra });\n const {\n Input,\n InputGroup,\n InputRightElement,\n IconButton,\n Popover,\n PopoverTrigger,\n PopoverContent,\n PopoverArrow,\n PopoverBody,\n FormControl,\n FormLabel,\n Box,\n PopoverHeader,\n PopoverCloseButton,\n HStack,\n Stack,\n Text,\n Link,\n Select,\n Tooltip,\n Button,\n NumberInput,\n NumberInputField,\n FormHelperText,\n FormErrorMessage,\n Divider,\n } = Chakra;\n const ChakraIcons = await deep.import(\"@chakra-ui/icons\");\n const { InfoOutlineIcon } = ChakraIcons;\n console.log(logsPrefix, ChakraIcons);\n const DeepCase = await deep.import(\"@deep-foundation/deepcase\");\n const { FinderPopover } = await deep.import(\"./cyto/hooks\");\n const { ClientHandler } = DeepCase;\n console.log(logsPrefix, { DeepCase });\n console.log(logsPrefix, { FinderPopover });\n const ReactHookForm = await deep.import(\"react-hook-form\");\n console.log(logsPrefix, { ReactHookForm });\n const { useForm, Controller } = ReactHookForm;\n const containTypeLinkId = await deep.id(\"@deep-foundation/core\", \"Contain\");\n\n const chatGPTAzureClientHandlerLinkId = await deep.id(\n \"@deep-foundation/chatgpt-azure\",\n \"managerClientHandler\"\n );\n\n return ({ fillSize, style, link }) => {\n const {\n handleSubmit,\n register,\n formState: { errors, isSubmitting },\n control,\n setValue,\n setError,\n } = useForm();\n\n async function onSubmit(values: {\n apiKey: string;\n chatGPTModelLinkId: string;\n userLinkId: string;\n containerLinkId: string;\n }) {\n const { apiKey, chatGPTModelLinkId, userLinkId, containerLinkId } =\n values;\n\n const containTypeLinkId = await deep.id(\n \"@deep-foundation/core\",\n \"Contain\"\n );\n const reservedLinkIds = await deep.reserve(6);\n const apiKeyLinkId = reservedLinkIds.pop();\n const containForApiKeyLinkId = reservedLinkIds.pop();\n const usesApiKeyLinkId = reservedLinkIds.pop();\n const containForUsesApiKeyLinkId = reservedLinkIds.pop();\n const usesModelLinkId = reservedLinkIds.pop();\n const containForUsesModelLinkId = reservedLinkIds.pop();\n\n await deep\n .serial({\n operations: [\n {\n type: \"insert\",\n table: \"links\",\n objects: {\n id: apiKeyLinkId,\n type_id: await deep.id(\"@deep-foundation/openai\", \"ApiKey\"),\n },\n },\n {\n type: \"insert\",\n table: \"strings\",\n objects: {\n link_id: apiKeyLinkId,\n value: apiKey,\n },\n },\n {\n type: \"insert\",\n table: \"links\",\n objects: {\n id: containForApiKeyLinkId,\n type_id: containTypeLinkId,\n from_id: containerLinkId,\n to_id: apiKeyLinkId,\n },\n },\n {\n type: \"insert\",\n table: \"links\",\n objects: {\n id: usesApiKeyLinkId,\n type_id: await deep.id(\"@deep-foundation/openai\", \"UsesApiKey\"),\n from_id: userLinkId,\n to_id: apiKeyLinkId,\n },\n },\n {\n type: \"insert\",\n table: \"links\",\n objects: {\n id: containForUsesApiKeyLinkId,\n type_id: containTypeLinkId,\n from_id: containerLinkId,\n to_id: usesApiKeyLinkId,\n },\n },\n {\n type: \"insert\",\n table: \"links\",\n objects: {\n id: usesModelLinkId,\n type_id: await deep.id(\"@deep-foundation/openai\", \"UsesModel\"),\n from_id: userLinkId,\n to_id: chatGPTModelLinkId,\n },\n },\n {\n type: \"insert\",\n table: \"links\",\n objects: {\n id: containForUsesModelLinkId,\n type_id: containTypeLinkId,\n from_id: containerLinkId,\n to_id: usesModelLinkId,\n },\n },\n ],\n })\n .catch(setError);\n }\n\n return (\n <Box\n style={{ minWidth: 450, ...style }}\n bg={\"bgColor\"}\n color={\"text\"}\n borderColor={\"borderColor\"}\n borderWidth=\"1px\"\n borderRadius=\"lg\"\n padding={4}\n >\n <form onSubmit={handleSubmit(onSubmit)}>\n <FormControl isInvalid={errors.root}>\n <Stack\n spacing={6}\n divider={<Divider />}\n display=\"flex\"\n justifyContent=\"center\"\n >\n <ApiKeyTextField\n register={register}\n error={errors.apiKey && errors.apiKey.message}\n />\n <ModelLinkIdSelect\n register={register}\n error={\n errors.chatGPTModelLinkId && errors.chatGPTModelLinkId.message\n }\n />\n <UserLinkIdTextField\n control={control}\n error={errors.userLinkId && errors.userLinkId.message}\n setValue={setValue}\n />\n <ContainerLinkIdTextField\n control={control}\n setValue={setValue}\n error={errors.containerLinkId && errors.containerLinkId.message}\n />\n {errors.root && <FormErrorMessage>\n {JSON.stringify(errors.root, null, 2)}\n </FormErrorMessage>}\n <Button isLoading={isSubmitting} type=\"submit\">\n Submit\n </Button>\n </Stack>\n </FormControl>\n </form>\n </Box>\n );\n };\n\n function ModelLinkIdSelect(options: { register: Function; error: string }) {\n const { register, error } = options;\n const { data: modelLinks } = deep.useDeepSubscription({\n type_id: {\n _id: [\"@deep-foundation/openai\", \"Model\"],\n },\n });\n\n const modelOptions = useMemo(() => {\n if (!modelLinks || modelLinks.length === 0) return null;\n return modelLinks.map((link) => {\n const name = link.value.value;\n return <option value={link.id}>{name}</option>;\n });\n }, [modelLinks]);\n console.log({ modelOptions });\n\n return (\n <Tooltip\n label={`No model links of type [\"@deep-foundation/openai\", \"Model\"] available to choose from. You can install \"@deep-foundation/chatgpt-azure-deep\" for already configured chatgpt models`}\n isDisabled={modelLinks.length > 0}\n >\n <FormControl isInvalid={error}>\n <FormLabel htmlFor=\"chatGPTModelLinkId\">\n ChatGPT Model Link ID\n </FormLabel>\n <Select\n placeholder=\"ChatGPT Model link ID\"\n {...register(\"chatGPTModelLinkId\", {\n required: \"ChatGPT Model link ID is required\",\n })}\n isDisabled={modelLinks.length === 0}\n >\n {modelOptions}\n </Select>\n <FormErrorMessage>{error}</FormErrorMessage>\n <FormHelperText>\n ChatGPT model is required to know which model should be used for\n requests\n </FormHelperText>\n </FormControl>\n </Tooltip>\n );\n }\n\n // function ApiKeyTextField(options: {\n // apiKey: string;\n // handleApiKeyChange: (apiKey: string) => void;\n // }) {\n // const { apiKey: apiKey, handleApiKeyChange: handleApiKeyChange } = options;\n\n // const helpContent = (\n // <Stack>\n // <Text>\n // Api Key is required to make requests to discord on your behalf\n // </Text>\n // <Link\n // isExternal\n // href=\"https://discord.com/developers/docs/getting-started#configuring-your-bot\"\n // >\n // Obtain Api Key\n // </Link>\n // </Stack>\n // );\n\n // return (\n // <Stack>\n // <HStack>\n // <Input\n // placeholder=\"Enter Api Key\"\n // variant=\"outlined\"\n // value={apiKey}\n // onChange={(event) => {\n // handleApiKeyChange(event.target.value);\n // }}\n // />\n // <Popover>\n // <PopoverTrigger>\n // <IconButton aria-label=\"help\" icon={<InfoOutlineIcon />} />\n // </PopoverTrigger>\n // <PopoverContent>\n // <PopoverArrow />\n // <PopoverCloseButton />\n // <PopoverBody>{helpContent}</PopoverBody>\n // </PopoverContent>\n // </Popover>\n // </HStack>\n // </Stack>\n // );\n // }\n\n // function UserLinkIdTextField(options: {\n // userLinkId: number;\n // handleUserLinkIdChange: (value: number) => void;\n // }) {\n // const { userLinkId, handleUserLinkIdChange } = options;\n // const [isPopoverOpen, setIsPopoverOpen] = useState(false);\n\n // const handleHelpClick = () => {\n // setIsPopoverOpen(!isPopoverOpen);\n // };\n\n // const helpContent = `User link is a link that is considered as a user of ApiKey. User link will be \"from\" for UsesApiKey`;\n\n // return (\n // <HStack>\n // <FormControl>\n // <InputGroup>\n // <Input\n // placeholder=\"User Link ID\"\n // type=\"text\"\n // variant=\"outline\"\n // value={userLinkId}\n // onChange={(event) => {\n // handleUserLinkIdChange(event.target.value);\n // }}\n // required\n // />\n // </InputGroup>\n // </FormControl>\n // <Popover>\n // <PopoverTrigger>\n // <IconButton aria-label=\"help\" icon={<InfoOutlineIcon />} />\n // </PopoverTrigger>\n // <PopoverContent>\n // <PopoverArrow />\n // <PopoverCloseButton />\n // <PopoverBody>{helpContent}</PopoverBody>\n // </PopoverContent>\n // </Popover>\n // <FinderPopover\n // link={deep.linkId}\n // onSubmit={async (link) => {\n // handleUserLinkIdChange(link.id);\n // }}\n // >\n // <IconButton aria-label=\"finder\" icon={<>🪬</>} />\n // </FinderPopover>\n // </HStack>\n // );\n // }\n\n function ApiKeyTextField(options: { error: string; register: Function }) {\n const { error, register } = options;\n\n return (\n <HStack>\n <FormControl isInvalid={error}>\n <FormLabel htmlFor=\"apiKey\">ApiKey Link ID</FormLabel>\n <Input\n {...register(\"apiKey\", {\n required: \"Api Key is required\",\n })}\n />\n <FormErrorMessage>{error}</FormErrorMessage>\n <FormHelperText>\n <Stack>\n <Text>\n Api Key is required to make requests to openai on your behalf\n </Text>\n <Link\n isExternal\n href=\"https://platform.openai.com/api-keys\"\n >\n Obtain Api Key\n </Link>\n </Stack>\n </FormHelperText>\n </FormControl>\n </HStack>\n );\n }\n\n function UserLinkIdTextField(options: {\n setValue: any;\n error: string;\n control: any;\n }) {\n const { setValue, error, control } = options;\n\n return (\n <FormControl isInvalid={error}>\n <FormLabel htmlFor=\"userLinkId\">User Link ID</FormLabel>\n <Controller\n name=\"userLinkId\"\n control={control}\n defaultValue={undefined}\n rules={{\n required: {\n value: true,\n message: \"User link ID is required\",\n },\n validate: async (value) => {\n const valueNumber = parseInt(value);\n if (isNaN(valueNumber)) {\n return \"User link ID must be a number\";\n }\n const {\n data: [link],\n } = await deep.select({ id: valueNumber });\n if (!link) {\n return `Link ${valueNumber} does not exist`;\n } else {\n return true;\n }\n },\n }}\n render={({ field }) => (\n <HStack>\n <NumberInput {...field} min={0}>\n <NumberInputField ref={field.ref} name={field.name} />\n </NumberInput>\n <FinderPopover\n link={deep.linkId}\n onSubmit={async (link) => {\n setValue(\"containerLinkId\", link.id);\n }}\n >\n <IconButton aria-label=\"finder\" icon={<>🪬</>} />\n </FinderPopover>\n </HStack>\n )}\n />\n <FormErrorMessage>{error}</FormErrorMessage>\n <FormHelperText>\n User link is a link that is considered as a user of ApiKey. User link\n will be \"from\" for UsesApiKey\n </FormHelperText>\n </FormControl>\n );\n }\n\n function ContainerLinkIdTextField(options: {\n setValue: any;\n error: string;\n control: any;\n }) {\n const { setValue, error, control } = options;\n\n const helpContent = `Container link is a link that will contain newly created ApiKey, UsesApiKey`;\n\n return (\n <FormControl isInvalid={error}>\n <FormLabel htmlFor=\"containerLinkId\">Container Link ID</FormLabel>\n <HStack>\n <Controller\n name=\"containerLinkId\"\n control={control}\n defaultValue={undefined}\n rules={{\n required: {\n value: true,\n message: \"Container link id is required\",\n },\n validate: async (value) => {\n const valueNumber = parseInt(value);\n if (isNaN(valueNumber)) {\n return \"Container link id must be a number\";\n }\n const {\n data: [link],\n } = await deep.select({ id: valueNumber });\n if (!link) {\n return `Link ${valueNumber} does not exist`;\n } else {\n return true;\n }\n },\n }}\n render={({ field }) => (\n <NumberInput {...field} min={0}>\n <NumberInputField ref={field.ref} name={field.name} />\n </NumberInput>\n )}\n />\n <FinderPopover\n link={deep.linkId}\n onSubmit={async (link) => {\n setValue(\"containerLinkId\", link.id);\n }}\n >\n <IconButton aria-label=\"finder\" icon={<>🪬</>} />\n </FinderPopover>\n </HStack>\n <FormErrorMessage>{error}</FormErrorMessage>\n <FormHelperText>\n Container link is a link that will contain newly created ApiKey,\n UsesApiKey\n </FormHelperText>\n </FormControl>\n );\n }\n};\n" | ||
"id": "managerClientHandler", | ||
"type": 5, | ||
"from": 6, | ||
"type": 8, | ||
"from": 9, | ||
"to": "managerClientHandlerCode" | ||
@@ -174,9 +190,9 @@ }, | ||
"id": "gpt4EncoderDependency", | ||
"type": 7, | ||
"from": 8, | ||
"to": 8 | ||
"type": 10, | ||
"from": 11, | ||
"to": 11 | ||
}, | ||
{ | ||
"id": "replyInsertHandlerCode", | ||
"type": 9, | ||
"type": 12, | ||
"value": { | ||
@@ -188,4 +204,4 @@ "value": "async ({ data: { newLink: reply, triggeredByLinkId }, deep }) => {\n const ml = deep.minilinks;\n\n const startTime = Date.now();\n const axios = await deep.import('axios');\n\n const ChatGPT = await deep.id(deep.linkId, 'ChatGPT');\n const Conversation = await deep.id(deep.linkId, 'Conversation');\n const ApiKey = await deep.id('@deep-foundation/openai', 'ApiKey');\n const UsesApiKey = await deep.id('@deep-foundation/openai', 'UsesApiKey');\n const Model = await deep.id('@deep-foundation/openai', 'Model');\n const UsesModel = await deep.id('@deep-foundation/openai', 'UsesModel');\n const Message = await deep.id('@deep-foundation/messaging', 'Message');\n const Reply = await deep.id('@deep-foundation/chatgpt-azure', 'Reply');\n const Author = await deep.id('@deep-foundation/messaging', 'Author');\n const messagingTree = await deep.id('@deep-foundation/messaging', 'messagingTree');\n const System = await deep.id('@deep-foundation/chatgpt-azure', 'System');\n const Tokens = await deep.id(\"@deep-foundation/tokens\", \"Tokens\")\n const ProvidedBy = await deep.id(\"@deep-foundation/chatgpt-azure\", \"ProvidedBy\")\n\n await deep.await(reply.from_id); const { data: [message] } = await deep.select({\n id: reply.from_id,\n _not: { out: { to_id: ChatGPT, type_id: Author } },\n });\n if (!message) return 'No need to react to message of this reply.';\n if (!message.value?.value) throw new Error(`Message (##${message?.id}) must have a value`);\n\n ml.apply(await deep.select({\n type_id: ApiKey,\n in: { from_id: triggeredByLinkId, type_id: UsesApiKey },\n return: {\n uses: { relation: 'in', type_id: UsesApiKey },\n },\n }), 'apikey');\n\n const apikeys = ml.select({ type_id: ApiKey });\n const useses = ml.select({ tyoe_id: UsesApiKey, to: { type_id: ApiKey } });\n \n if (!apikeys.length) throw new Error(`ApiKey not founded`);\n if (useses.length > 1) throw new Error(`More than 1 links of type UsesApiKey are found`);\n if (!apikeys[0]?.value?.value) throw new Error(`ApiKey ##${apikeys[0]?.id} has no value`);\n\n const { data: [conversation] } = await deep.select({\n type_id: Conversation,\n down: { tree_id: messagingTree, link_id: reply.id }\n });\n if (!conversation) return 'Conversation upper not founded.';\n\n const messagesByReply = await deep.select({\n type_id: Message,\n down: { tree_id: messagingTree, link_id: reply.from_id },\n order_by: { id: 'asc' },\n return: {\n authors: { relation: 'out', type_id: Author },\n tokens: { relation: 'out', type_id: Tokens },\n }\n });\n ml.apply(messagesByReply, 'messagesByReply');\n\n const messagesById = await deep.select({\n id: { _nin: messagesByReply.data.map(l => l.id) },\n type_id: Message,\n up: { tree_id: messagingTree, parent_id: conversation.id },\n order_by: { id: 'asc' },\n return: {\n authors: { relation: 'out', type_id: Author },\n tokens: { relation: 'out', type_id: Tokens },\n }\n });\n ml.apply(messagesById, 'messagesById');\n\n const messagesToSend = [...messagesById.data, ...messagesByReply.data];\n const models = await deep.select({\n type_id: Model,\n in: { type_id: UsesModel, from_id: { _in: [conversation.id, triggeredByLinkId] } },\n return: {\n usesModel: { relation: 'in', type_id: UsesModel },\n tokens: { relation: 'out', type_id: Tokens },\n providedBy: {\n relation: 'out', type_id: ProvidedBy,\n return: {\n endpoint: { relation: 'to' }\n },\n },\n },\n });\n ml.apply(models, 'models');\n\n const [userLinkedModel] = ml.select({ type_id: Model, in: { type_id: UsesModel, from_id: triggeredByLinkId } });\n if (!userLinkedModel) throw new Error(`UsersModel from triggeredByLinkId ${triggeredByLinkId} to Model not founded.`);\n const [userLinkedModelTokens] = ml.select({ type_id: Tokens, from_id: userLinkedModel?.id });\n const [linkedModel] = ml.select({ type_id: Model, in: { type_id: UsesModel, from_id: conversation.id } });\n \n const MAX_TOKENS = userLinkedModelTokens?.value?.value;\n if (!MAX_TOKENS) throw new Error(`MAX_TOKENS Tokens (##${userLinkedModelTokens?.id}) .value from model not founded.`);\n \n const endpoint = ml.select({ in: { type_id: ProvidedBy } })[0];\n const API_ENDPOINT = endpoint?.value?.value;\n if (!API_ENDPOINT) throw new Error(`API_ENDPOINT Endpoint (##${endpoint?.id}) instance.value nout founded.`);\n \n const model = linkedModel?.value?.value || userLinkedModel?.value?.value;\n if(!model) throw new Error(`UsersModel from conversation.id (##${conversation.id}) to Model not founded.`);\n\n let preparedMessages = messagesToSend.map((link) => {\n const tokens = ml.select({ type_id: Tokens, from_id: link.id })[0]?.value?.value;\n return {\n role: ml.select({ type_id: Author, from_id: link.id })[0]?.to_id === ChatGPT ? 'assistant' : 'user',\n content: link.value.value,\n tokens: ml.select({ type_id: Tokens, from_id: link.id })[0]?.value?.value,\n }\n });\n\n const systems = await deep.select({\n type_id: System, to_id: { _in: [triggeredByLinkId, conversation.id] },\n return: {\n message: { relation: 'from' },\n conversation: { relation: 'to' },\n tokens: { relation: 'in', type_id: Tokens },\n },\n });\n ml.apply(systems, 'systems');\n\n const conversationSystem = ml.select({ type_id: System, to_id: conversation.id })?.[0];\n if (!!conversationSystem && !conversationSystem?.value?.value) throw new Error(`System .value to conversation not defined`);\n const userSystems = ml.select({ type_id: System, to_id: triggeredByLinkId });\n if (userSystems.length > 1) throw new Error(\"Multiple system messages linked to the user are found\");\n const userSystem = userSystems[0];\n\n const systemMessage = conversationSystem?.from || userSystem?.from;\n if (systemMessage) {\n if (!systemMessage?.value?.value) throw new Error(`System (##${systemMessage?.id}) .from Message.value not founded`);\n preparedMessages.unshift({ role: 'system', content: systemMessage?.value?.value });\n }\n\n const tokenLimit = MAX_TOKENS * 7 / 8;\n let totalTokens = 0;\n let messages = [];\n\n for (let i = preparedMessages.length - 1; i >= 0; i--) {\n const message = preparedMessages[i];\n if (message.role === 'system' || totalTokens + message.tokens <= tokenLimit) {\n messages.unshift({ role: message.role, content: message.content });\n totalTokens += message.tokens;\n } else {\n break;\n }\n }\n\n let response;\n try {\n response = await axios.post(API_ENDPOINT, { messages }, {\n headers: { 'Content-Type': 'application/json', 'api-key': apikeys[0]?.value?.value },\n });\n } catch (error) {\n console.error('Error making API call:', error.response ? error.response.data : error.message);\n }\n\n const content = response.data.choices[0]?.message?.content;\n await deep.insert({\n type_id: Message,\n string: { data: { value: content } },\n out: { data: [\n { type_id: Author, to_id: ChatGPT },\n { type_id: Reply, to_id: reply.from_id },\n ] },\n });\n\n const endTime = Date.now();\n const duration = (endTime - startTime) / 1000;\n\n ml.remove(Object.keys(ml.byId));\n\n return {\n request: { model, messages },\n response: response.data,\n duration, totalTokens,\n };\n};" | ||
"id": "replyInsertHandler", | ||
"type": 5, | ||
"from": 10, | ||
"type": 8, | ||
"from": 13, | ||
"to": "replyInsertHandlerCode" | ||
@@ -195,3 +211,3 @@ }, | ||
"id": "replyHandleInsert", | ||
"type": 11, | ||
"type": 14, | ||
"from": "Reply", | ||
@@ -202,8 +218,8 @@ "to": "replyInsertHandler" | ||
"id": "Endpoint", | ||
"type": 12 | ||
"type": 1 | ||
}, | ||
{ | ||
"id": "ProvidedBy", | ||
"type": 12, | ||
"from": 13, | ||
"type": 1, | ||
"from": 15, | ||
"to": "Endpoint" | ||
@@ -213,13 +229,13 @@ }, | ||
"id": "endpointValue", | ||
"type": 14, | ||
"type": 16, | ||
"from": "Endpoint", | ||
"to": 15 | ||
"to": 17 | ||
}, | ||
{ | ||
"id": "manager", | ||
"type": 12 | ||
"type": 1 | ||
}, | ||
{ | ||
"id": "handlleManagerClient", | ||
"type": 16, | ||
"type": 18, | ||
"from": "manager", | ||
@@ -229,26 +245,10 @@ "to": "managerClientHandler" | ||
{ | ||
"id": "System", | ||
"type": 12, | ||
"from": 2, | ||
"to": 3 | ||
}, | ||
{ | ||
"id": "ChatGPT", | ||
"type": 12 | ||
"type": 1 | ||
}, | ||
{ | ||
"id": "Conversation", | ||
"type": 12 | ||
}, | ||
{ | ||
"id": "conversationValue", | ||
"type": 14, | ||
"type": 16, | ||
"from": "Conversation", | ||
"to": 15 | ||
}, | ||
{ | ||
"id": "messagingTreeReply", | ||
"type": 17, | ||
"from": 18, | ||
"to": "Reply" | ||
"to": 17 | ||
} | ||
@@ -259,2 +259,6 @@ ], | ||
{ | ||
"name": "@deep-foundation/core", | ||
"version": "0.0.2" | ||
}, | ||
{ | ||
"name": "@deep-foundation/messaging", | ||
@@ -264,6 +268,2 @@ "version": "2.0.0" | ||
{ | ||
"name": "@deep-foundation/core", | ||
"version": "0.0.2" | ||
}, | ||
{ | ||
"name": "@deep-foundation/tsx", | ||
@@ -270,0 +270,0 @@ "version": "0.0.5" |
@@ -12,3 +12,3 @@ { | ||
}, | ||
"version": "3.0.5", | ||
"version": "3.0.6", | ||
"keywords": [ | ||
@@ -15,0 +15,0 @@ "deep-package" |
28590