From 95638f2bd15d58f0f748f0845d01543e2755c758 Mon Sep 17 00:00:00 2001 From: noreplydev Date: Wed, 13 Nov 2024 19:28:18 +0100 Subject: [PATCH] Remove functionCall chatbot from packages/app/chatbots --- .../app/chatbots/functionsCallsChatbot.ts | 111 ------------------ packages/app/chatbots/index.ts | 2 - 2 files changed, 113 deletions(-) delete mode 100644 packages/app/chatbots/functionsCallsChatbot.ts diff --git a/packages/app/chatbots/functionsCallsChatbot.ts b/packages/app/chatbots/functionsCallsChatbot.ts deleted file mode 100644 index cce817346..000000000 --- a/packages/app/chatbots/functionsCallsChatbot.ts +++ /dev/null @@ -1,111 +0,0 @@ -import { getAuth } from 'protonode' -import { APIContext } from "protolib/bundles/apiContext" -import { API, Protofy, getLogger } from "protobase"; -import { Application } from 'express'; -import fs from 'fs' -import path from "path"; -import { createChatbot } from "protolib/bundles/chatbots/createChatbot"; - -const root = path.join(process.cwd(), '..', '..') -const logger = getLogger() - -Protofy("type", "chatGPT") - -const get_name = (args) => { - console.log("args: ", args); - const dateTime = new Date().toLocaleString(); - return "Your name is " + args.name + " - " + dateTime; -} - -const tool_handlers = { - "get_name": get_name -} - -const tools = [ - { - name: "get_name", - description: "Get the name of a user", - parameters: { - type: "object", - properties: { - name: { - type: "string", - description: "the user name" - } - }, - required: ["name"], - additionalProperties: false - } - } -]; - -function transformChats(prevChats, prompt: string) { - const additionalSystemMessage = { - role: "system", - content: prompt - }; - return [additionalSystemMessage, ...prevChats]; -} - -function reduceArgsChunks(chunks) { - const result = chunks.reduce((acc, curr) => { - console.log("current: ", JSON.stringify(curr.choices[0], null, 2)) - return acc + curr.choices[0].delta.function_call.arguments - }, "") - - console.log("functionName: ", chunks[0].choices[0]) - return { - function_name: chunks[0].choices[0]?.delta?.function_call?.name ?? "unknown", - result: result - } -} - -export default Protofy("code", async (app: Application, context: typeof APIContext) => { - createChatbot(app, 'functionCall', async (req, res, chatbot) => { - const { metadata, ...body } = req.body - const { session, token } = getAuth(req) - - let chunks = [] - context.chatGPT.chatGPTPrompt({ - ...body, - functions: tools, - messages: transformChats(body.messages, metadata.context), - done: (response, message) => { - // tool_call response - if (chunks[chunks.length - 1].choices[0].finish_reason === "function_call") { - chunks.pop() // remove termination chunk - const chunksResult = reduceArgsChunks(chunks) - chunks = [] - - if (chunksResult && tool_handlers[chunksResult.function_name]) { - const argsObject = JSON.parse(chunksResult.result) - console.log("argsObject", argsObject) - const response = tool_handlers[chunksResult.function_name](argsObject) - console.log(response) - chatbot.send(response) - chatbot.end() - return - } - - chatbot.send("Cannot generate a response try again") - chatbot.end() - return - } - - // chat gpt response - if (response) { - console.log("response", response) - chatbot.send("automatic response") - chatbot.end() - return - } - }, - chunk: (chunk) => { - chunks.push(chunk) - }, - error: (error) => { - chatbot.sendError(error) - } - }) - }) -}) \ No newline at end of file diff --git a/packages/app/chatbots/index.ts b/packages/app/chatbots/index.ts index 1987dbea9..83303feb7 100644 --- a/packages/app/chatbots/index.ts +++ b/packages/app/chatbots/index.ts @@ -1,10 +1,8 @@ import { Protofy } from 'protobase' import assistantChatbot from "./assistant"; -import functionsCallsChatbot from "./functionsCallsChatbot"; const chatbots = Protofy("chatbots", { assistant: assistantChatbot, - functionsCallsChatbot: functionsCallsChatbot }) export default (app, context) => {