diff --git a/arthas-api/api/post/prompt/index.js b/arthas-api/api/post/prompt/index.js index 234e59a..673952c 100644 --- a/arthas-api/api/post/prompt/index.js +++ b/arthas-api/api/post/prompt/index.js @@ -28,11 +28,15 @@ const { const { prefixInput } = require('arthasgpt/src/utils/prefix'); +const __statefulChatFunction = {}; + /** * Prompt */ module.exports = asyncCache => async (req, res) => { + const config = await asyncCache.getItem('config'); + const timeout = await asyncCache.getItem('timeout'); let answer = await asyncCache.getItem('answer'); @@ -44,8 +48,6 @@ module.exports = asyncCache => async (req, res) => { body.push(chunk); }) .on('end', async () => { - const config = await asyncCache.getItem('config'); - body = Buffer.concat(body).toString(); const { key, input } = JSON.parse(body || '{}'); @@ -112,16 +114,29 @@ module.exports = asyncCache => async (req, res) => { await delay(timeout); } - if (isVerbose) { - log(CREATING_AGENT); - } + const currentChat = __statefulChatFunction?.[key]; - answer = await ArthasGPT({ - ...currentConfig, + if (currentChat) { + await currentChat(messageResponse); + } else { + if (isVerbose) { + log(CREATING_AGENT); + } - query: messageResponse, - cache: false - }); + answer = await ArthasGPT({ + ...currentConfig, + + query: messageResponse, + cache: true + }); + + await asyncCache.setItem('answer', answer); + + // Cache the stateful chat method in the API + // (antipattern) + + __statefulChatFunction[key] = answer?.chat; + } res.end(JSON.stringify({ success: true, diff --git a/arthas-api/index.js b/arthas-api/index.js index 1d1e174..3c4e0fc 100644 --- a/arthas-api/index.js +++ b/arthas-api/index.js @@ -31,7 +31,8 @@ module.exports = (routes = { GET: {}, POST: {}}, init) => { * Config */ - const numCPUs = availableParallelism(); + // const numCPUs = availableParallelism(); + const numCPUs = 1; const onCluster = require('./events/cluster'); const onWorker = require('./events/worker');