Skip to content

Commit

Permalink
refactor: Update synergy.ts to include latest changes for conversatio…
Browse files Browse the repository at this point in the history
…n data retrieval
  • Loading branch information
fayeed committed Sep 6, 2024
1 parent 5dca097 commit 8260511
Showing 1 changed file with 101 additions and 22 deletions.
123 changes: 101 additions & 22 deletions packages/utils/src/synergy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LinkQuery, PerspectiveProxy } from "@coasys/ad4m";
import { Ad4mClient, Ad4mSignalCB, AITask, LinkQuery, PerspectiveProxy } from "@coasys/ad4m";
import { getAd4mClient } from "@coasys/ad4m-connect/utils";
import {
Conversation,
Expand Down Expand Up @@ -79,16 +79,18 @@ async function getConversationData(perspective, conversationRepo) {
source: latestConversation.id,
});
latestSubgroups = (await subgroupRepo.getAllData()) as any;
const latestSubgroup = latestSubgroups[latestSubgroups.length - 1] as any;
const subgroupItems = await getSubgroupItems(perspective, latestSubgroup.id);
// calculate time since last item was created
const lastItemTimestamp = subgroupItems[subgroupItems.length - 1].timestamp;
const minsSinceLastItemCreated =
(new Date().getTime() - new Date(lastItemTimestamp).getTime()) / (1000 * 60);
if (minsSinceLastItemCreated < 30) {
// if less than 30 mins, consider the new item part of the latest conversation
conversation = latestConversation;
latestSubgroupItems = subgroupItems;
if (latestSubgroups.length) {
const latestSubgroup = latestSubgroups[latestSubgroups.length - 1] as any;
const subgroupItems = await getSubgroupItems(perspective, latestSubgroup.id);
// calculate time since last item was created
const lastItemTimestamp = subgroupItems[subgroupItems.length - 1].timestamp;
const minsSinceLastItemCreated =
(new Date().getTime() - new Date(lastItemTimestamp).getTime()) / (1000 * 60);
if (minsSinceLastItemCreated < 30) {
// if less than 30 mins, consider the new item part of the latest conversation
conversation = latestConversation;
latestSubgroupItems = subgroupItems;
}
}
}
if (!conversation) {
Expand Down Expand Up @@ -136,7 +138,7 @@ async function LLMProcessing(newItem, latestSubgroups, latestSubgroupItems, allT
const prompt = `
I'm passing you a JSON object with the following properties: 'lastGroupings' (string block broken up into sections by line breaks <br/>), 'lastMessages' (string array), 'newMessage' (string), and 'existingTopics' (string array).
{ lastGroupings: [${latestSubgroups.map((s) => s.summary).join(" <br/> ")}], lastMessages: [${latestSubgroupItems.map((si) => si.text).join(", ")}], newMessage: '${newItem.text}', existingTopics: [${allTopics.map((t) => t.name).join(", ")}] }
{ lastGroupings: [], lastMessages: [], newMessage: 'Some text', existingTopics: [] }
Firstly, analyze the 'newMessage' string and identify between 1 and 5 topics (each a single word string in lowercase) that are relevant to the content of the 'newMessage' string. If any of the topics you choose are similar to topics listed in the 'existingTopics' array, use the existing topic instead of creating a new one (e.g., if one of the new topics you picked was 'foods' and you find an existing topic 'food', use 'food' instead of creating a new topic that is just a plural version of the existing topic). For each topic, provide a relevance score between 0 and 100 (0 being irrelevant and 100 being highly relevant) that indicates how relevant the topic is to the content of the 'newMessage' string.
Expand Down Expand Up @@ -167,16 +169,93 @@ async function LLMProcessing(newItem, latestSubgroups, latestSubgroupItems, allT
Make sure the response is in a format that can be parsed using JSON.parse(). Don't wrap it in code syntax.
`;

const openai = new OpenAI({
apiKey: localStorage?.getItem("openAIKey") || "",
dangerouslyAllowBrowser: true,
});
const result = await openai.chat.completions.create({
messages: [{ role: "user", content: prompt }],
model: "gpt-4o",
});
const data = JSON.parse(result.choices[0].message.content || "");
console.log("Open AI response: ", data);
const examples = [{
input: `{ lastGroupings: [], lastMessages: [], newMessage: 'hello world', existingTopics: [greeting] }`,
output: `{"topics":[{"name":"greeting","relevance":100}],"changedSubject":true,"newSubgroupName":"Hello World","newSubgroupSummary":"The conversation starts with a simple greeting: 'hello world'.","newConversationName":"Hello World","newConversationSummary":"The conversation starts with a simple greeting: 'hello world'."}`,
}, {
input: `{ lastGroupings: [The conversation starts with a simple greeting: 'hello world'.], lastMessages: [<p>hello world</p><p></p>], newMessage: 'another hello 2', existingTopics: [greeting] }`,
output: `{"topics":[{"name":"hello","relevance":80},{"name":"greeting","relevance":70}],"changedSubject":false,"newSubgroupName":"More Greetings","newSubgroupSummary":"The conversation continues with another greeting, showing the ongoing exchange of pleasantries.","newConversationName":"Simple Greetings","newConversationSummary":"The conversation starts with a simple greeting: 'hello world'. Following this, another greeting is exchanged, indicating the continuation of pleasantries."}`,
}, {
input: `{ lastGroupings: [The conversation continues with another greeting, showing the ongoing exchange of pleasantries.], lastMessages: [<p>hello world</p><p></p>, <p>another hello 2</p><p></p>], newMessage: 'game talk here', existingTopics: [greeting, hello] }`,
output: `{"topics":[{"name":"game","relevance":100},{"name":"talk","relevance":80}],"changedSubject":true,"newSubgroupName":"Game Talk","newSubgroupSummary":"The conversation introduces a new topic with a focus on discussing games.","newConversationName":"Exchange of Pleasantries and Game Talk","newConversationSummary":"The conversation continues with another greeting, showing the ongoing exchange of pleasantries. The conversation then introduces a new topic with a focus on discussing games."}`
}, {
input: `{ lastGroupings: [The conversation continues with another greeting, showing the ongoing exchange of pleasantries. <br/> The conversation introduces a new topic with a focus on discussing games.], lastMessages: [<p>game talk here</p><p></p>], newMessage: 'dota 2 is the biggest esport game there is', existingTopics: [greeting, hello, game, talk] }`,
output: `{"topics":[{"name":"game","relevance":90},{"name":"esport","relevance":85},{"name":"dota","relevance":100}],"changedSubject":false,"newSubgroupName":"Dota 2 Discussion","newSubgroupSummary":"The conversation continues with a focus on Dota 2, highlighting its prominence in the esports scene.","newConversationName":"Games and Esports","newConversationSummary":"The conversation continues with another greeting, showing the ongoing exchange of pleasantries. The conversation introduces a new topic with a focus on discussing games. The latest discussion centers on Dota 2, highlighting its significance in the world of esports."}`
}, {
input: `{ lastGroupings: [The conversation introduces a new topic with an emphasis on discussing models and their effectiveness.], lastMessages: [<p>lets see if this model works better or not</p><p></p>], newMessage: 'haha', existingTopics: [, game, dota2 is the biggest MOBA in the world, esport, dota, esports, hello, dota 2, model, work] }`,
output: `{
"topics": [
{ "name": "text", "relevance": 100 }
],
"changedSubject": true,
"newSubgroupName": "Some Text",
"newSubgroupSummary": "The message contains some text.",
"newConversationName": "Topic Summary",
"newConversationSummary": "The entire discussion so far is about some text."
}`
}, {
input: `{ lastGroupings: [The conversation introduces a new topic with an emphasis on discussing models and their effectiveness. <br/> The conversation introduces a new topic with an emphasis on discussing models and their effectiveness. <br/> The conversation introduces a new topic with an emphasis on discussing rap music. <br/> The message contains some text.], lastMessages: [<p>doesnt make sense</p><p></p>], newMessage: 'lets try a
gain', existingTopics: [, game, dota2 is the biggest MOBA in the world, esport, dota, esports, hello, dota 2, model, work, rap, text] }`,
output: `
{
"topics": [
{
"name": "text",
"relevance": 100
}
],
"changedSubject": true,
"newSubgroupName": "New Text Discussed",
"newSubgroupSummary": "A new text has been introduced and is open for discussion.",
"newConversationName": "Final Discussion",
"newConversationSummary": "The latest conversation wraps up the final topics discussed and introduces a new text for consideration."
}
`
}]

const client: Ad4mClient = await getAd4mClient();

const tasks = await client.ai.tasks();

let task = tasks.find((t) => t.systemPrompt.includes("I'm passing you a JSON object with the following properties: 'lastGroupings'"));

console.log("Task: ", task);

if (task) {
// task.promptExamples = examples;
// task.systemPrompt = prompt;
// await client.ai.updateTask(task.taskId, task);
} else {
task = await client.ai.addTask("llama", prompt, examples)
}

console.log("Task: calling prompt");

const response = await client.ai.prompt(task.taskId, `{ lastGroupings: [${latestSubgroups.map((s) => s.summary).join(" <br/> ")}], lastMessages: [${latestSubgroupItems.map((si) => si.text).join(", ")}], newMessage: '${newItem.text}', existingTopics: [${allTopics.map((t) => t.name).join(", ")}] }`);

console.log("AI Response: ", response);

// const openai = new OpenAI({
// apiKey: localStorage?.getItem("openAIKey") || "",
// dangerouslyAllowBrowser: true,
// });
// const result = await openai.chat.completions.create({
// messages: [{ role: "user", content: prompt }],
// model: "gpt-4o",
// });
// console.log("Open AI request: ", `{ lastGroupings: [${latestSubgroups.map((s) => s.summary).join(" <br/> ")}], lastMessages: [${latestSubgroupItems.map((si) => si.text).join(", ")}], newMessage: '${newItem.text}', existingTopics: [${allTopics.map((t) => t.name).join(", ")}] }`);
// console.log("Open AI response: ", JSON.parse(response), JSON.parse(result.choices[0].message.content));
// const data = JSON.parse(result.choices[0].message.content || "");

const data = JSON.parse(response);
if (!data.newConversationName) data.newConversationName = ""
if (!data.newConversationSummary) data.newConversationSummary = ""
if (!data.newSubgroupName) data.newSubgroupName = ""
if (!data.newSubgroupSummary) data.newSubgroupSummary = ""
if (!data.topics) data.topics = []
if (!data.changedSubject) data.changedSubject = false
console.log("LLM Processing Data: ", data);
return data;
}

Expand Down

0 comments on commit 8260511

Please sign in to comment.