@deep-foundation/chatgpt-azure
Advanced tools
Comparing version 1.0.3 to 2.0.0
156
deep.json
{ | ||
"package": { | ||
"name": "@deep-foundation/chatgpt-azure", | ||
"version": "1.0.3" | ||
"version": "2.0.0" | ||
}, | ||
@@ -10,3 +10,3 @@ "data": [ | ||
"dependencyId": 0, | ||
"containValue": "Type" | ||
"containValue": "SyncTextFile" | ||
}, | ||
@@ -17,4 +17,4 @@ "id": 1 | ||
"package": { | ||
"dependencyId": 1, | ||
"containValue": "Dependency" | ||
"dependencyId": 0, | ||
"containValue": "Type" | ||
}, | ||
@@ -25,4 +25,4 @@ "id": 2 | ||
"package": { | ||
"dependencyId": 2, | ||
"containValue": "TokensDependency" | ||
"dependencyId": 1, | ||
"containValue": "Message" | ||
}, | ||
@@ -33,4 +33,4 @@ "id": 3 | ||
"package": { | ||
"dependencyId": 3, | ||
"containValue": "Reply" | ||
"dependencyId": 0, | ||
"containValue": "Any" | ||
}, | ||
@@ -41,4 +41,4 @@ "id": 4 | ||
"package": { | ||
"dependencyId": 3, | ||
"containValue": "Message" | ||
"dependencyId": 0, | ||
"containValue": "Value" | ||
}, | ||
@@ -50,3 +50,3 @@ "id": 5 | ||
"dependencyId": 0, | ||
"containValue": "Any" | ||
"containValue": "String" | ||
}, | ||
@@ -57,4 +57,4 @@ "id": 6 | ||
"package": { | ||
"dependencyId": 0, | ||
"containValue": "TreeIncludeUp" | ||
"dependencyId": 2, | ||
"containValue": "Model" | ||
}, | ||
@@ -66,3 +66,3 @@ "id": 7 | ||
"dependencyId": 3, | ||
"containValue": "messagingTree" | ||
"containValue": "Dependency" | ||
}, | ||
@@ -73,4 +73,4 @@ "id": 8 | ||
"package": { | ||
"dependencyId": 0, | ||
"containValue": "SyncTextFile" | ||
"dependencyId": 4, | ||
"containValue": "TokensDependency" | ||
}, | ||
@@ -81,4 +81,4 @@ "id": 9 | ||
"package": { | ||
"dependencyId": 0, | ||
"containValue": "Handler" | ||
"dependencyId": 1, | ||
"containValue": "Reply" | ||
}, | ||
@@ -90,3 +90,3 @@ "id": 10 | ||
"dependencyId": 0, | ||
"containValue": "dockerSupportsJs" | ||
"containValue": "TreeIncludeUp" | ||
}, | ||
@@ -97,4 +97,4 @@ "id": 11 | ||
"package": { | ||
"dependencyId": 0, | ||
"containValue": "HandleInsert" | ||
"dependencyId": 1, | ||
"containValue": "messagingTree" | ||
}, | ||
@@ -106,3 +106,3 @@ "id": 12 | ||
"dependencyId": 0, | ||
"containValue": "Value" | ||
"containValue": "Handler" | ||
}, | ||
@@ -114,3 +114,3 @@ "id": 13 | ||
"dependencyId": 0, | ||
"containValue": "String" | ||
"containValue": "dockerSupportsJs" | ||
}, | ||
@@ -121,4 +121,4 @@ "id": 14 | ||
"package": { | ||
"dependencyId": 4, | ||
"containValue": "Model" | ||
"dependencyId": 0, | ||
"containValue": "HandleInsert" | ||
}, | ||
@@ -128,73 +128,73 @@ "id": 15 | ||
{ | ||
"id": "replyInsertHandlerCode", | ||
"type": 1, | ||
"value": { | ||
"value": "async ({ data: { newLink: replyLink, triggeredByLinkId }, deep, require }) => {\n const startTime = Date.now();\n const PACKAGE_NAME = `@deep-foundation/chatgpt-azure`;\n const axios = require('axios');\n\n const { Configuration, OpenAIApi } = require('openai');\n const chatGPTTypeLinkId = await deep.id(PACKAGE_NAME, 'ChatGPT');\n const conversationTypeLinkId = await deep.id(PACKAGE_NAME, 'Conversation');\n const apiKeyTypeLinkId = await deep.id('@deep-foundation/openai', 'ApiKey');\n const usesApiKeyTypeLinkId = await deep.id('@deep-foundation/openai', 'UsesApiKey');\n const modelTypeLinkId = await deep.id('@deep-foundation/openai', 'Model');\n const usesModelTypeLinkId = await deep.id('@deep-foundation/openai', 'UsesModel');\n const messageTypeLinkId = await deep.id('@deep-foundation/messaging', 'Message');\n const replyTypeLinkId = await deep.id('@deep-foundation/chatgpt-azure', 'Reply');\n const authorTypeLinkId = await deep.id('@deep-foundation/messaging', 'Author');\n const containTypeLinkId = await deep.id('@deep-foundation/core', 'Contain');\n const messagingTreeId = await deep.id('@deep-foundation/messaging', 'messagingTree');\n const systemTypeLinkId = await deep.id('@deep-foundation/chatgpt-azure', 'System');\n const tokensTypeLinkId = await deep.id(\"@deep-foundation/tokens\", \"Tokens\")\n const providedByTypeLinkId= await deep.id(\"@deep-foundation/chatgpt-azure\", \"ProvidedBy\") \n const reservedIds = await deep.reserve(1);\n const chatGPTMessageLinkId = reservedIds.pop();\n let systemMessageId;\n let model;\n let systemMessage;\nawait deep.await(replyLink.from_id)\n const { data: [messageLink] } = await deep.select({\n id: replyLink.from_id,\n _not: {\n out: {\n to_id: chatGPTTypeLinkId,\n type_id: authorTypeLinkId,\n },\n },\n });\n if (!messageLink) {\n return 'No need to react to message of this reply.';\n }\n if (!messageLink.value?.value) {\n throw new Error(`Message ##${messageLink.id} must have a value`);\n }\n const message = messageLink.value.value;\n\n const apiKeyLink = await getTokenLink();\n const apiKey = apiKeyLink.value.value;\n const configuration = new Configuration({\n apiKey: apiKey,\n });\n const openai = new OpenAIApi(configuration);\n\n const { data: conversationLink } = await deep.select({\n tree_id: { _eq: messagingTreeId },\n parent: { type_id: { _in: [conversationTypeLinkId, messageTypeLinkId] } },\n link: { id: { _eq: replyLink.from_id } },\n }, {\n table: 'tree',\n variables: { order_by: { depth: \"asc\" } },\n returning: `\n id\n depth\n root_id\n parent_id\n link_id\n parent {\n id\n from_id\n type_id\n to_id\n value\n author: out (where: { type_id: { _eq: ${authorTypeLinkId}} }) { \n id\n from_id\n type_id\n to_id\n }\n tokens: out (where: { type_id: { _eq: ${tokensTypeLinkId}} }) { \n id\n from_id\n type_id\n to_id\n value\n }\n }`\n })\n if (!conversationLink) {\n throw new Error('A conversationLink link is not found');\n }\n const currentConversation = conversationLink.find(\n (link) => link.parent.type_id === conversationTypeLinkId\n );\n\n conversationLink.forEach((link) => {\n if (link.parent.author && link.parent.author.length > 0) {\n link.parent.author = link.parent.author[0];\n }\n });\n\n const {\n data: [linkedModel],\n } = await deep.select({\n type_id: modelTypeLinkId,\n in: {\n type_id: usesModelTypeLinkId,\n from_id: currentConversation.parent.id,\n },\n });\n\n const {\n data: [userLinkedModel],\n } = await deep.select({\n type_id: modelTypeLinkId,\n in: {\n type_id: usesModelTypeLinkId,\n from_id: triggeredByLinkId,\n }, \n }, {\n returning: `\n id\n value\n in(where: { type_id: { _eq: ${usesModelTypeLinkId} } }) {\n id\n type_id\n from_id\n to_id\n value\n }\n tokens: out(where: { type_id: { _eq: ${tokensTypeLinkId} } }) {\n id\n from_id\n to_id\n value\n }\n endpointLink: out(where: { type_id: { _eq: ${providedByTypeLinkId} } }) {\n id\n from_id\n to_id\n endpoint: to {\n id\n value\n }\n }\n `\n });\nlet MAX_TOKENS\nif (!userLinkedModel.tokens?.[0]?.value?.value) {\n throw new Error(\"Token limit for the model is not specified.\");\n}else if(userLinkedModel.tokens?.[0]?.value?.value){\n MAX_TOKENS = userLinkedModel.tokens[0].value.value;\n}\n\nif (!userLinkedModel.endpointLink?.[0]?.endpoint?.value?.value) {\n throw new Error(\"Endpoint for the model is not specified.\");\n}\nconst API_ENDPOINT = userLinkedModel.endpointLink[0].endpoint.value?.value;\n\n if (!linkedModel && !userLinkedModel) {\n model = 'gpt-35-turbo';\n } else if (\n (linkedModel &&\n linkedModel.value?.value &&\n userLinkedModel &&\n userLinkedModel.value?.value) ||\n (linkedModel && linkedModel.value?.value)\n ) {\n model = linkedModel.value.value;\n } else {\n if (!userLinkedModel) {\n throw new Error(`A link with type ##${userLinkedModel} is not found`);\n }\n if (!userLinkedModel.value?.value) {\n throw new Error(`Linked model with user ##${userLinkedModel.id} must have a value`);\n } else {\n model = userLinkedModel.value.value;\n }\n }\n const messageLinks = conversationLink\n .map(item => item.parent)\n .filter(link => link && link.type_id === messageTypeLinkId);\n let allMessages = await getMessages({ messageLinks });\n const messagesToSend = [...allMessages];\n\n const { data: userLinkedSystemMessageLinks } = await deep.select({\n type_id: systemTypeLinkId,\n to_id: triggeredByLinkId,\n }, { returning: `id message: from{ id value} conversation:to{id}` });\n \n const { data: conversationLinkedSystemMessageLink } = await deep.select({\n type_id: systemTypeLinkId,\n to_id: currentConversation.parent.id,\n }, { returning: `id message: from{ id value} conversation:to{id}` });\n\nif (conversationLinkedSystemMessageLink && conversationLinkedSystemMessageLink.length > 0) {\n const systemMessageLink = conversationLinkedSystemMessageLink[0];\n if (!systemMessageLink.message?.value?.value) {\n throw new Error(`System message with link to conversation ##${systemMessageLink.id} must have a value`);\n } else {\n systemMessage = systemMessageLink.message.value.value;\n systemMessageId = systemMessageLink.message;\n await deep.await(systemMessageId.id)\n\n }\n} else if (userLinkedSystemMessageLinks && userLinkedSystemMessageLinks.length > 0) {\n if (userLinkedSystemMessageLinks.length > 1) {\n throw new Error(\"Multiple system messages linked to the user are found\");\n }\n\n const userLinkedSystemMessageLink = userLinkedSystemMessageLinks[0];\n\n if (!userLinkedSystemMessageLink.message?.value?.value) {\n throw new Error(`System message with link to user ##${userLinkedSystemMessageLink.id} must have a value`);\n } else {\n systemMessage = userLinkedSystemMessageLink.message.value.value;\n systemMessageId = userLinkedSystemMessageLink.message;\n await deep.await(systemMessageId.id)\n }\n}\n\nif (systemMessage) {\n const { data: tokensLinkedToSystemMessage } = await deep.select({\n type_id: tokensTypeLinkId,\n from_id: systemMessageId.id,\n to_id: systemMessageId.id,\n });\n if(!tokensLinkedToSystemMessage[0]?.value?.value){\n throw new Error(`System message does not contain tokens`);\n } else if(tokensLinkedToSystemMessage[0]?.value?.value){\n let tokenCount = tokensLinkedToSystemMessage[0].value?.value;\n messagesToSend.unshift({\n role: \"system\",\n content: systemMessage,\n tokens: tokenCount,\n });\n }\n}\n\n const tokenLimit = MAX_TOKENS * 7 / 8;\n let totalTokens = 0;\n let messagesToSendToOpenAI = [];\n\n for (let i = messagesToSend.length - 1; i >= 0; i--) {\n const message = messagesToSend[i];\n\n if (message.role === 'system' || totalTokens + message.tokens <= tokenLimit) {\n messagesToSendToOpenAI.unshift({ role: message.role, content: message.content });\n totalTokens += message.tokens;\n } else {\n break;\n }\n }\nlet response;\n\ntry {\n response = await axios.post(API_ENDPOINT, {\n messages: [...messagesToSendToOpenAI]\n }, {\n headers: {\n 'Content-Type': 'application/json',\n 'api-key': apiKey\n }\n });\n} catch (error) {\n console.error('Error making API call:', error.response ? error.response.data : error.message);\n}\n\nconst content = response.data.choices[0]?.message?.content;\n\n await deep.serial({\n operations: [\n {\n table: 'links',\n type: 'insert',\n objects: {\n id: chatGPTMessageLinkId,\n type_id: messageTypeLinkId,\n out: {\n data: [\n {\n type_id: authorTypeLinkId,\n to_id: chatGPTTypeLinkId,\n },\n ],\n },\n },\n },\n {\n table: 'strings',\n type: 'insert',\n objects: {\n link_id: chatGPTMessageLinkId,\n value: content\n }\n },\n {\n table: 'links',\n type: 'insert',\n objects: {\n type_id: replyTypeLinkId,\n from_id: chatGPTMessageLinkId,\n to_id: replyLink.from_id,\n },\n },\n ],\n });\n\n async function getMessages({ messageLinks }) {\n return Promise.all(\n messageLinks.map(async (link) => {\n const tokens = link.tokens?.length > 0 ? link.tokens[0].value.value : undefined;\n return {\n role: await getMessageRole({ messageLink: link }),\n content: link.value.value,\n tokens: tokens,\n }\n })\n );\n }\n\n async function getMessageRole({ messageLink }) {\n const authorLink = messageLink.author;\n if (!authorLink) {\n throw new Error(`Author link not found for message ##${messageLink.id}`);\n }\n return authorLink.to_id === chatGPTTypeLinkId ? 'assistant' : 'user';\n }\n\n async function getTokenLink() {\n let resultTokenLink;\n const { data } = await deep.select({\n _or: [\n {\n type_id: apiKeyTypeLinkId,\n in: {\n type_id: containTypeLinkId,\n from_id: triggeredByLinkId,\n },\n },\n {\n from_id: triggeredByLinkId,\n type_id: usesApiKeyTypeLinkId,\n },\n ],\n });\n if (data.length === 0) {\n throw new Error(`ApiKey ##${apiKeyTypeLinkId} is not found`);\n }\n const usesLinks = data.filter(\n (link) => link.type_id === usesApiKeyTypeLinkId\n );\n if (usesLinks.length > 1) {\n throw new Error(\n `More than 1 links of type ##${usesApiKeyTypeLinkId} are found`\n );\n }\n const usesLink = data.find(\n (link) => link.type_id === usesApiKeyTypeLinkId\n );\n if (usesLink) {\n const tokenLink = data.find((link) => link.id === usesLink.to_id);\n if (!tokenLink) {\n throw new Error(`ApiKey ##${apiKeyTypeLinkId} is not found`);\n } else {\n resultTokenLink = tokenLink;\n }\n } else {\n const tokenLink = data.filter(\n (link) => link.type_id === apiKeyTypeLinkId\n );\n if (tokenLink.length > 1) {\n throw new Error(\n `For 2 or more ApiKey ##${apiKeyTypeLinkId} links you must activate it with usesOpenAiApiKey link`\n );\n } else {\n const tokenLink = data.find(\n (link) => link.type_id === apiKeyTypeLinkId\n );\n if (!tokenLink) {\n throw new Error(`ApiKey ##${apiKeyTypeLinkId} is not found`);\n }\n resultTokenLink = tokenLink;\n }\n }\n if (!resultTokenLink.value?.value) {\n throw new Error(`ApiKey ##${apiKeyTypeLinkId} has no value`);\n }\n return resultTokenLink;\n }\n\n const endTime = Date.now();\n const duration = (endTime - startTime) / 1000;\n return {\n request: {\n model: model,\n messages: [\n ...messagesToSendToOpenAI\n ],\n },\n response: response.data,\n duration: duration,\n totalTokens: totalTokens\n };\n};" | ||
} | ||
}, | ||
{ | ||
"id": "ChatGPT", | ||
"type": 1 | ||
"type": 2 | ||
}, | ||
{ | ||
"id": "Dependency", | ||
"id": "System", | ||
"type": 2, | ||
"from": 3, | ||
"to": 3 | ||
"to": 4 | ||
}, | ||
{ | ||
"id": "Reply", | ||
"type": 4, | ||
"from": 5, | ||
"to": 6 | ||
"id": "Conversation", | ||
"type": 2 | ||
}, | ||
{ | ||
"id": "messagingTreeReply", | ||
"type": 7, | ||
"from": 8, | ||
"to": "Reply" | ||
}, | ||
{ | ||
"id": "System", | ||
"type": 1, | ||
"from": 5, | ||
"id": "conversationValue", | ||
"type": 5, | ||
"from": "Conversation", | ||
"to": 6 | ||
}, | ||
{ | ||
"id": "ReplyInsertHandlerCode", | ||
"type": 9, | ||
"value": { | ||
"value": "async ({ data: { newLink: replyLink, triggeredByLinkId }, deep, require }) => {\n const startTime = Date.now();\n const PACKAGE_NAME = `@deep-foundation/chatgpt-azure`;\n const axios = require('axios');\n\n const { Configuration, OpenAIApi } = require('openai');\n const chatGPTTypeLinkId = await deep.id(PACKAGE_NAME, 'ChatGPT');\n const conversationTypeLinkId = await deep.id(PACKAGE_NAME, 'Conversation');\n const apiKeyTypeLinkId = await deep.id('@deep-foundation/openai', 'ApiKey');\n const usesApiKeyTypeLinkId = await deep.id('@deep-foundation/openai', 'UsesApiKey');\n const modelTypeLinkId = await deep.id('@deep-foundation/openai', 'Model');\n const usesModelTypeLinkId = await deep.id('@deep-foundation/openai', 'UsesModel');\n const messageTypeLinkId = await deep.id('@deep-foundation/messaging', 'Message');\n const replyTypeLinkId = await deep.id('@deep-foundation/chatgpt-azure', 'Reply');\n const authorTypeLinkId = await deep.id('@deep-foundation/messaging', 'Author');\n const containTypeLinkId = await deep.id('@deep-foundation/core', 'Contain');\n const messagingTreeId = await deep.id('@deep-foundation/messaging', 'messagingTree');\n const systemTypeLinkId = await deep.id('@deep-foundation/chatgpt-azure', 'System');\n const tokensTypeLinkId = await deep.id(\"@deep-foundation/tokens\", \"Tokens\")\n const providedByTypeLinkId= await deep.id(\"@deep-foundation/chatgpt-azure\", \"ProvidedBy\") \n const reservedIds = await deep.reserve(1);\n const chatGPTMessageLinkId = reservedIds.pop();\n let systemMessageId;\n let model;\n let systemMessage;\n\n const { data: [messageLink] } = await deep.select({\n id: replyLink.from_id,\n _not: {\n out: {\n to_id: chatGPTTypeLinkId,\n type_id: authorTypeLinkId,\n },\n },\n });\n if (!messageLink) {\n return 'No need to react to message of this reply.';\n }\n if (!messageLink.value?.value) {\n throw new Error(`Message ##${messageLink.id} must have a value`);\n }\n const message = messageLink.value.value;\n\n const apiKeyLink = await getTokenLink();\n const apiKey = apiKeyLink.value.value;\n const configuration = new Configuration({\n apiKey: apiKey,\n });\n const openai = new OpenAIApi(configuration);\n\n const { data: conversationLink } = await deep.select({\n tree_id: { _eq: messagingTreeId },\n parent: { type_id: { _in: [conversationTypeLinkId, messageTypeLinkId] } },\n link: { id: { _eq: replyLink.from_id } },\n }, {\n table: 'tree',\n variables: { order_by: { depth: \"asc\" } },\n returning: `\n id\n depth\n root_id\n parent_id\n link_id\n parent {\n id\n from_id\n type_id\n to_id\n value\n author: out (where: { type_id: { _eq: ${authorTypeLinkId}} }) { \n id\n from_id\n type_id\n to_id\n }\n tokens: out (where: { type_id: { _eq: ${tokensTypeLinkId}} }) { \n id\n from_id\n type_id\n to_id\n value\n }\n }`\n })\n if (!conversationLink) {\n throw new Error('A conversationLink link is not found');\n }\n const currentConversation = conversationLink.find(\n (link) => link.parent.type_id === conversationTypeLinkId\n );\n\n conversationLink.forEach((link) => {\n if (link.parent.author && link.parent.author.length > 0) {\n link.parent.author = link.parent.author[0];\n }\n });\n\n const {\n data: [linkedModel],\n } = await deep.select({\n type_id: modelTypeLinkId,\n in: {\n type_id: usesModelTypeLinkId,\n from_id: currentConversation.parent.id,\n },\n });\n\n const {\n data: [userLinkedModel],\n } = await deep.select({\n type_id: modelTypeLinkId,\n in: {\n type_id: usesModelTypeLinkId,\n from_id: triggeredByLinkId,\n }, \n }, {\n returning: `\n id\n value\n in(where: { type_id: { _eq: ${usesModelTypeLinkId} } }) {\n id\n type_id\n from_id\n to_id\n value\n }\n tokens: out(where: { type_id: { _eq: ${tokensTypeLinkId} } }) {\n id\n from_id\n to_id\n value\n }\n endpointLink: out(where: { type_id: { _eq: ${providedByTypeLinkId} } }) {\n id\n from_id\n to_id\n endpoint: to {\n id\n value\n }\n }\n `\n });\n\nif (!userLinkedModel.tokens?.[0]?.value?.value) {\n throw new Error(\"Token limit for the model is not specified.\");\n}\nconst MAX_TOKENS = userLinkedModel.tokens[0].value.value;\n\nif (!userLinkedModel.endpointLink?.[0]?.endpoint?.value?.value) {\n throw new Error(\"Endpoint for the model is not specified.\");\n}\nconst API_ENDPOINT = userLinkedModel.endpointLink[0].endpoint.value?.value;\n\n if (!linkedModel && !userLinkedModel) {\n model = 'gpt-35-turbo';\n } else if (\n (linkedModel &&\n linkedModel.value?.value &&\n userLinkedModel &&\n userLinkedModel.value?.value) ||\n (linkedModel && linkedModel.value?.value)\n ) {\n model = linkedModel.value.value;\n } else {\n if (!userLinkedModel) {\n throw new Error(`A link with type ##${userLinkedModel} is not found`);\n }\n if (!userLinkedModel.value?.value) {\n throw new Error(`Linked model with user ##${userLinkedModel.id} must have a value`);\n } else {\n model = userLinkedModel.value.value;\n }\n }\n const messageLinks = conversationLink\n .map(item => item.parent)\n .filter(link => link && link.type_id === messageTypeLinkId);\n let allMessages = await getMessages({ messageLinks });\n const messagesToSend = [...allMessages];\n\n const { data: userLinkedSystemMessageLinks } = await deep.select({\n type_id: systemTypeLinkId,\n to_id: triggeredByLinkId,\n }, { returning: `id message: from{ id value} conversation:to{id}` });\n \n const { data: conversationLinkedSystemMessageLink } = await deep.select({\n type_id: systemTypeLinkId,\n to_id: currentConversation.parent.id,\n }, { returning: `id message: from{ id value} conversation:to{id}` });\n\n if (conversationLinkedSystemMessageLink && conversationLinkedSystemMessageLink.length > 0) {\n const systemMessageLink = conversationLinkedSystemMessageLink[0];\n if (!systemMessageLink.message?.value?.value) {\n throw new Error(`System message with link to conversation ##${systemMessageLink.id} must have a value`);\n } else {\n systemMessage = systemMessageLink.message.value.value;\n systemMessageId = systemMessageLink.message;\n }\n } else if (userLinkedSystemMessageLinks && userLinkedSystemMessageLinks.length > 0) {\n if (userLinkedSystemMessageLinks.length > 1) {\n throw new Error(\"Multiple system messages linked to the user are found\");\n }\n\n const userLinkedSystemMessageLink = userLinkedSystemMessageLinks[0];\n\n if (!userLinkedSystemMessageLink.message?.value?.value) {\n throw new Error(`System message with link to user ##${userLinkedSystemMessageLink.id} must have a value`);\n } else {\n systemMessage = userLinkedSystemMessageLink.message.value.value;\n systemMessageId = userLinkedSystemMessageLink.message;\n }\n }\n\n if (systemMessage) {\n const { data: tokensLinkedToSystemMessage } = await deep.select({\n type_id: tokensTypeLinkId,\n from_id: systemMessageId.id,\n to_id: systemMessageId.id,\n });\n let tokenCount = tokensLinkedToSystemMessage[0].value?.value;\n messagesToSend.unshift({\n role: \"system\",\n content: systemMessage,\n tokens: tokenCount,\n });\n\n }\n\n const tokenLimit = MAX_TOKENS * 7 / 8;\n let totalTokens = 0;\n let messagesToSendToOpenAI = [];\n\n for (let i = messagesToSend.length - 1; i >= 0; i--) {\n const message = messagesToSend[i];\n\n if (message.role === 'system' || totalTokens + message.tokens <= tokenLimit) {\n messagesToSendToOpenAI.unshift({ role: message.role, content: message.content });\n totalTokens += message.tokens;\n } else {\n break;\n }\n }\nlet response;\n\ntry {\n response = await axios.post(API_ENDPOINT, {\n messages: [...messagesToSendToOpenAI]\n }, {\n headers: {\n 'Content-Type': 'application/json',\n 'api-key': apiKey\n }\n });\n} catch (error) {\n console.error('Error making API call:', error.response ? error.response.data : error.message);\n}\n\nconst content = response.data.choices[0]?.message?.content;\n\n await deep.serial({\n operations: [\n {\n table: 'links',\n type: 'insert',\n objects: {\n id: chatGPTMessageLinkId,\n type_id: messageTypeLinkId,\n out: {\n data: [\n {\n type_id: authorTypeLinkId,\n to_id: chatGPTTypeLinkId,\n },\n ],\n },\n },\n },\n {\n table: 'strings',\n type: 'insert',\n objects: {\n link_id: chatGPTMessageLinkId,\n value: content\n }\n },\n {\n table: 'links',\n type: 'insert',\n objects: {\n type_id: replyTypeLinkId,\n from_id: chatGPTMessageLinkId,\n to_id: replyLink.from_id,\n },\n },\n ],\n });\n\n async function getMessages({ messageLinks }) {\n return Promise.all(\n messageLinks.map(async (link) => {\n const tokens = link.tokens?.length > 0 ? link.tokens[0].value.value : undefined;\n return {\n role: await getMessageRole({ messageLink: link }),\n content: link.value.value,\n tokens: tokens,\n }\n })\n );\n }\n\n async function getMessageRole({ messageLink }) {\n const authorLink = messageLink.author;\n if (!authorLink) {\n throw new Error(`Author link not found for message ##${messageLink.id}`);\n }\n return authorLink.to_id === chatGPTTypeLinkId ? 'assistant' : 'user';\n }\n\n async function getTokenLink() {\n let resultTokenLink;\n const { data } = await deep.select({\n _or: [\n {\n type_id: apiKeyTypeLinkId,\n in: {\n type_id: containTypeLinkId,\n from_id: triggeredByLinkId,\n },\n },\n {\n from_id: triggeredByLinkId,\n type_id: usesApiKeyTypeLinkId,\n },\n ],\n });\n if (data.length === 0) {\n throw new Error(`ApiKey ##${apiKeyTypeLinkId} is not found`);\n }\n const usesLinks = data.filter(\n (link) => link.type_id === usesApiKeyTypeLinkId\n );\n if (usesLinks.length > 1) {\n throw new Error(\n `More than 1 links of type ##${usesApiKeyTypeLinkId} are found`\n );\n }\n const usesLink = data.find(\n (link) => link.type_id === usesApiKeyTypeLinkId\n );\n if (usesLink) {\n const tokenLink = data.find((link) => link.id === usesLink.to_id);\n if (!tokenLink) {\n throw new Error(`ApiKey ##${apiKeyTypeLinkId} is not found`);\n } else {\n resultTokenLink = tokenLink;\n }\n } else {\n const tokenLink = data.filter(\n (link) => link.type_id === apiKeyTypeLinkId\n );\n if (tokenLink.length > 1) {\n throw new Error(\n `For 2 or more ApiKey ##${apiKeyTypeLinkId} links you must activate it with usesOpenAiApiKey link`\n );\n } else {\n const tokenLink = data.find(\n (link) => link.type_id === apiKeyTypeLinkId\n );\n if (!tokenLink) {\n throw new Error(`ApiKey ##${apiKeyTypeLinkId} is not found`);\n }\n resultTokenLink = tokenLink;\n }\n }\n if (!resultTokenLink.value?.value) {\n throw new Error(`ApiKey ##${apiKeyTypeLinkId} has no value`);\n }\n return resultTokenLink;\n }\n\n const endTime = Date.now();\n const duration = (endTime - startTime) / 1000;\n return {\n request: {\n model: model,\n messages: [\n ...messagesToSendToOpenAI\n ],\n },\n response: response.data,\n duration: duration,\n totalTokens: totalTokens\n };\n};" | ||
} | ||
"id": "Endpoint", | ||
"type": 2 | ||
}, | ||
{ | ||
"id": "ReplyInsertHandler", | ||
"type": 10, | ||
"from": 11, | ||
"to": "ReplyInsertHandlerCode" | ||
"id": "ProvidedBy", | ||
"type": 2, | ||
"from": 7, | ||
"to": "Endpoint" | ||
}, | ||
{ | ||
"id": "HandleInsert", | ||
"type": 12, | ||
"from": "Reply", | ||
"to": "ReplyInsertHandler" | ||
"id": "endpointValue", | ||
"type": 5, | ||
"from": "Endpoint", | ||
"to": 6 | ||
}, | ||
{ | ||
"id": "Conversation", | ||
"type": 1 | ||
"id": "dependency", | ||
"type": 8, | ||
"from": 9, | ||
"to": 9 | ||
}, | ||
{ | ||
"id": "ConversationValue", | ||
"type": 13, | ||
"from": "Conversation", | ||
"to": 14 | ||
"id": "Reply", | ||
"type": 10, | ||
"from": 3, | ||
"to": 4 | ||
}, | ||
{ | ||
"id": "Endpoint", | ||
"type": 1 | ||
"id": "messagingTreeReply", | ||
"type": 11, | ||
"from": 12, | ||
"to": "Reply" | ||
}, | ||
{ | ||
"id": "ProvidedBy", | ||
"type": 1, | ||
"from": 15, | ||
"to": "Endpoint" | ||
"id": "replyInsertHandler", | ||
"type": 13, | ||
"from": 14, | ||
"to": "replyInsertHandlerCode" | ||
}, | ||
{ | ||
"id": "EndpointValue", | ||
"type": 13, | ||
"from": "Endpoint", | ||
"to": 14 | ||
"id": "handleInsert", | ||
"type": 15, | ||
"from": "Reply", | ||
"to": "replyInsertHandler" | ||
} | ||
@@ -209,2 +209,10 @@ ], | ||
{ | ||
"name": "@deep-foundation/messaging", | ||
"version": "2.0.0" | ||
}, | ||
{ | ||
"name": "@deep-foundation/openai", | ||
"version": "1.0.2" | ||
}, | ||
{ | ||
"name": "@freephoenix888/dependency", | ||
@@ -216,12 +224,4 @@ "version": "0.0.1" | ||
"version": "1.0.1" | ||
}, | ||
{ | ||
"name": "@deep-foundation/messaging", | ||
"version": "2.0.0" | ||
}, | ||
{ | ||
"name": "@deep-foundation/openai", | ||
"version": "1.0.2" | ||
} | ||
] | ||
} |
@@ -11,3 +11,3 @@ { | ||
}, | ||
"version": "1.0.3", | ||
"version": "2.0.0", | ||
"keywords": [ | ||
@@ -14,0 +14,0 @@ "deep-package" |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
16624
246
1