diff --git a/api/app/citeText.js b/api/app/citeText.js new file mode 100644 index 0000000000..fc85c3285f --- /dev/null +++ b/api/app/citeText.js @@ -0,0 +1,27 @@ +/* +// example +const ex = "Fetch API[^1^], Axios[^3^], or XMLHttpRequest[^2^]. Each of these..."; +const links = [ + 'https://www.freecodecamp.org/news/here-is-the-most-popular-ways-to-make-an-http-request-in-javascript-954ce8c95aaa/', + 'https://stackoverflow.com/questions/247483/http-get-request-in-javascript', + 'https://livecodestream.dev/post/5-ways-to-make-http-requests-in-javascript/' +]; + +const regex = /\[\^\d+?\^]/g; + +const citations = Array.from(new Set(ex.match(regex))); +const linkMap = {}; +citations.forEach(citation => { + const digit = citation.match(/\d+?/g)[0]; + linkMap[citation] = links[digit - 1]; +}); +*/ +const citationRegex = /\[\^\d+?\^]/g; + +const citeText = (res) => { + let sources = res.details.sourceAttributions; + if (!sources) return res.response; + sources = sources.map((source) => source.seeMoreUrl); +}; + +module.exports = citeText; \ No newline at end of file diff --git a/api/app/getCitations.js b/api/app/getCitations.js new file mode 100644 index 0000000000..46d4e72c07 --- /dev/null +++ b/api/app/getCitations.js @@ -0,0 +1,13 @@ +// const regex = / \[\d+\..*?\]\(.*?\)/g; +const regex = / \[.*?]\(.*?\)/g; + +const getCitations = (res) => { + const textBlocks = res.details.adaptiveCards[0].body; + if (!textBlocks) return ''; + let links = textBlocks[textBlocks.length - 1]?.text.match(regex); + if (links?.length === 0) return ''; + links = links.map((link) => '- ' + link.trim()); + return 'Learn more:\n' + links.join('\n'); +}; + +module.exports = getCitations; \ No newline at end of file diff --git a/api/app/index.js b/api/app/index.js index a1d0088d9b..0a829609fd 100644 --- a/api/app/index.js +++ b/api/app/index.js @@ -4,6 +4,7 @@ const customClient = require('./chatgpt-custom'); const { askBing } = require('./bingai'); const { askSydney } = require('./sydney'); const titleConvo = require('./titleConvo'); +const getCitations = require('./getCitations'); const detectCode = require('./detectCode'); module.exports = { @@ -13,5 +14,6 @@ module.exports = { askBing, askSydney, titleConvo, + getCitations, detectCode }; \ No newline at end of file diff --git a/api/package-lock.json b/api/package-lock.json index ebad132167..911fda953e 100644 --- a/api/package-lock.json +++ b/api/package-lock.json @@ -11,7 +11,7 @@ "dependencies": { "@keyv/mongo": "^2.1.8", "@vscode/vscode-languagedetection": "^1.0.22", - "@waylaidwanderer/chatgpt-api": "^1.28.0", + "@waylaidwanderer/chatgpt-api": "^1.28.2", "cors": "^2.8.5", "dotenv": "^16.0.3", "express": "^4.18.2", @@ -1492,9 +1492,9 @@ } }, "node_modules/@waylaidwanderer/chatgpt-api": { - "version": "1.28.0", - "resolved": "https://registry.npmjs.org/@waylaidwanderer/chatgpt-api/-/chatgpt-api-1.28.0.tgz", - "integrity": "sha512-753dc/Eaf+1XmSXrLu+99LR4N+ACL7fQyA7GlsUXjRVXcH+m/iDqqXrU+o1JKGy84VlNKokAvq3oy9LVunKCIw==", + "version": "1.28.2", + "resolved": "https://registry.npmjs.org/@waylaidwanderer/chatgpt-api/-/chatgpt-api-1.28.2.tgz", + "integrity": "sha512-efNvZr8uosiYD69zFq50OPM36s+tyRMixlHpwDzn2q9UuZrdHC++kmm23OAnDxv3/+vA4UwCsZXn+92c35NHBQ==", "dependencies": { "@dqbd/tiktoken": "^0.4.0", "@fastify/cors": "^8.2.0", @@ -5781,9 +5781,9 @@ "integrity": "sha512-rQ/BgMyLuIXSmbA0MSkIPHtcOw14QkeDbAq19sjvaS9LTRr905yij0S8lsyqN5JgOsbtIx7pAcyOxFMzPmqhZQ==" }, "@waylaidwanderer/chatgpt-api": { - "version": "1.28.0", - "resolved": "https://registry.npmjs.org/@waylaidwanderer/chatgpt-api/-/chatgpt-api-1.28.0.tgz", - "integrity": "sha512-753dc/Eaf+1XmSXrLu+99LR4N+ACL7fQyA7GlsUXjRVXcH+m/iDqqXrU+o1JKGy84VlNKokAvq3oy9LVunKCIw==", + "version": "1.28.2", + "resolved": "https://registry.npmjs.org/@waylaidwanderer/chatgpt-api/-/chatgpt-api-1.28.2.tgz", + "integrity": "sha512-efNvZr8uosiYD69zFq50OPM36s+tyRMixlHpwDzn2q9UuZrdHC++kmm23OAnDxv3/+vA4UwCsZXn+92c35NHBQ==", "requires": { "@dqbd/tiktoken": "^0.4.0", "@fastify/cors": "^8.2.0", diff --git a/api/package.json b/api/package.json index 74c420c8e5..28bd6769d3 100644 --- a/api/package.json +++ b/api/package.json @@ -21,7 +21,7 @@ "dependencies": { "@keyv/mongo": "^2.1.8", "@vscode/vscode-languagedetection": "^1.0.22", - "@waylaidwanderer/chatgpt-api": "^1.28.0", + "@waylaidwanderer/chatgpt-api": "^1.28.2", "cors": "^2.8.5", "dotenv": "^16.0.3", "express": "^4.18.2", diff --git a/api/server/routes/askSydney.js b/api/server/routes/askSydney.js index a928bc75fc..1755a40d16 100644 --- a/api/server/routes/askSydney.js +++ b/api/server/routes/askSydney.js @@ -1,7 +1,7 @@ const express = require('express'); const crypto = require('crypto'); const router = express.Router(); -const { titleConvo, askSydney } = require('../../app/'); +const { titleConvo, getCitations, askSydney } = require('../../app/'); const { saveMessage, deleteMessages, saveConvo, getConvoTitle } = require('../../models'); const { handleError, sendMessage } = require('./handlers'); @@ -39,17 +39,8 @@ router.post('/', async (req, res) => { }); console.log('SYDNEY RESPONSE'); - // the usual values expected by the client are generated since only - // jailbreakConversationId and initial messageId is needed by sydney - // to continue the conversation - // if (!convo.jailbreakConversationId) { - // response.title = await titleConvo({ - // model, - // message: text, - // response: JSON.stringify(response.response) - // }); - // } - + // console.dir(response, { depth: null }); + // Save sydney response response.id = response.messageId; // response.parentMessageId = convo.parentMessageId ? convo.parentMessageId : response.messageId; @@ -75,6 +66,9 @@ router.post('/', async (req, res) => { response.sender = model; response.final = true; + const links = getCitations(response); + console.log('sydney links', links); + // Save user message userMessage.conversationId = response.conversationId; userMessage.parentMessageId = response.parentMessageId; diff --git a/client/src/components/Messages/Message.jsx b/client/src/components/Messages/Message.jsx index 0accb1afeb..9413f21856 100644 --- a/client/src/components/Messages/Message.jsx +++ b/client/src/components/Messages/Message.jsx @@ -49,7 +49,7 @@ export default function Message({ chatgpt: 'rgb(16, 163, 127)', chatgptBrowser: 'rgb(25, 207, 207)', bingai: '', - sydney: 'rgb(214, 255, 255)', + sydney: '', }; const isBing = sender === 'bingai' || sender === 'sydney'; @@ -65,7 +65,7 @@ export default function Message({ if ((notUser && backgroundColor) || isBing) { icon = (
{isBing ? : }