From 9b63fb98cc6589272a0bd6edd576600c65ed3e44 Mon Sep 17 00:00:00 2001 From: brkagithub Date: Fri, 7 Feb 2025 13:27:20 +0100 Subject: [PATCH 1/3] Minor improvements --- .../plugin-dkg/src/actions/dkgAnalyzeSentiment.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/plugin-dkg/src/actions/dkgAnalyzeSentiment.ts b/packages/plugin-dkg/src/actions/dkgAnalyzeSentiment.ts index 7b596070b59..b7866659e42 100644 --- a/packages/plugin-dkg/src/actions/dkgAnalyzeSentiment.ts +++ b/packages/plugin-dkg/src/actions/dkgAnalyzeSentiment.ts @@ -317,7 +317,7 @@ export const dkgAnalyzeSentiment: Action = { await scraper.login(username, password, email, twitter2faSecret); if (!(await scraper.isLoggedIn())) { let attempts = 0; - const maxAttempts = 5; + const maxAttempts = 10; while (attempts < maxAttempts) { attempts++; @@ -343,6 +343,16 @@ export const dkgAnalyzeSentiment: Action = { } } + if (!topic || topic.toLowerCase() === "none") { + await postTweet( + `Didn't recognize a ticker of a financial asset in your post. Please post again while clearly stating which stock or cryptocurrency you want to analyze.`, + scraper, + postId, + ); + + return true; + } + const scrapedTweets = scraper.searchTweets( topic, 100, From 085d46390782ca18785af60d5c4c3b97935cb6fe Mon Sep 17 00:00:00 2001 From: brkagithub Date: Mon, 10 Feb 2025 12:55:20 +0100 Subject: [PATCH 2/3] Add log + update dkg.js version --- packages/plugin-dkg/package.json | 2 +- packages/plugin-dkg/src/actions/dkgInsert.ts | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/plugin-dkg/package.json b/packages/plugin-dkg/package.json index d5901b1a04f..10d50a262ed 100644 --- a/packages/plugin-dkg/package.json +++ b/packages/plugin-dkg/package.json @@ -20,7 +20,7 @@ ], "dependencies": { "@elizaos/core": "workspace:*", - "dkg.js": "^8.0.4", + "dkg.js": "^8.0.6", "tsup": "8.3.5", "agent-twitter-client": "0.0.18", "vader-sentiment": "^1.1.3", diff --git a/packages/plugin-dkg/src/actions/dkgInsert.ts b/packages/plugin-dkg/src/actions/dkgInsert.ts index 97fe2eca139..fa05c0d9e22 100644 --- a/packages/plugin-dkg/src/actions/dkgInsert.ts +++ b/packages/plugin-dkg/src/actions/dkgInsert.ts @@ -119,6 +119,10 @@ export const dkgInsert: Action = { try { elizaLogger.log("Publishing message to DKG"); + elizaLogger.log( + `KA: ${JSON.stringify(memoryKnowledgeGraph, null, 2)}`, + ); + createAssetResult = await DkgClient.asset.create( { public: memoryKnowledgeGraph, From 75a19584810c852443028db324dd60823d3c5358 Mon Sep 17 00:00:00 2001 From: brkagithub Date: Mon, 10 Feb 2025 13:51:36 +0100 Subject: [PATCH 3/3] Retry if malformed JSON error --- .../src/actions/dkgAnalyzeSentiment.ts | 113 ++++++++++++++---- packages/plugin-dkg/src/actions/dkgInsert.ts | 50 +++++++- 2 files changed, 136 insertions(+), 27 deletions(-) diff --git a/packages/plugin-dkg/src/actions/dkgAnalyzeSentiment.ts b/packages/plugin-dkg/src/actions/dkgAnalyzeSentiment.ts index b7866659e42..a640bc53377 100644 --- a/packages/plugin-dkg/src/actions/dkgAnalyzeSentiment.ts +++ b/packages/plugin-dkg/src/actions/dkgAnalyzeSentiment.ts @@ -392,37 +392,104 @@ export const dkgAnalyzeSentiment: Action = { ? "Positive 🟢" : "Negative 🔴"; - const createAssetResult = await DkgClient.asset.create( - { - public: ka, - }, - { epochsNum: 12 }, - ); + elizaLogger.log("Publishing sentiment analysis to DKG"); + elizaLogger.log(`KA: ${JSON.stringify(ka, null, 2)}`); - const sentimentData = await getSentimentChart(averageScore, topic); + try { + let createAssetResult; - const file = await fetchFileFromUrl(sentimentData.url); + try { + createAssetResult = await DkgClient.asset.create( + { public: ka }, + { epochsNum: 12 }, + ); + } catch (error) { + elizaLogger.error( + "Error occurred while creating asset:", + error.message, + ); - let tweetContent = `${topic} sentiment based on top ${tweets.length} latest posts`; - if (numOfTotalTweets - tweets.length > 0) { - tweetContent += ` and ${numOfTotalTweets - tweets.length} existing analysis Knowledge Assets`; - } - tweetContent += ` from the past 48 hours: ${sentiment}\n\n`; + if ( + error.message.includes("Unexpected") || + error.message.includes("JSON") + ) { + elizaLogger.warn( + "Detected JSON formatting issue. Attempting to fix...", + ); + try { + const fixedJSON = await generateText({ + runtime, + context: `Fix this malformed JSON-LD and return only the corrected JSON-LD:\n${JSON.stringify(ka, null, 2)} + + Make sure to only output the JSON-LD object. DO NOT OUTPUT ANYTHING ELSE, DONT ADD ANY COMMENTS, REMARKS AND DO NOT WRAP IT IN A CODE/JSON BLOCK, JUST THE JSON LD CONTENT WRAPPED IN { }.`, + modelClass: ModelClass.LARGE, + }); + + elizaLogger.log( + `Fixed JSON generated by LLM. Retrying...`, + ); + + // Retry asset creation with the fixed JSON + createAssetResult = await DkgClient.asset.create( + { public: JSON.parse(fixedJSON) }, + { epochsNum: 12 }, + ); + } catch (llmError) { + elizaLogger.error( + "Failed to fix JSON using LLM:", + llmError.message, + ); + } + } + } + + if (!createAssetResult?.UAL) { + elizaLogger.error("UAL not found after asset creation."); + await postTweet( + `Apologies, something went wrong with the sentiment analysis.`, + scraper, + postId, + ); + return true; + } + + elizaLogger.log("======================== ASSET CREATED"); + elizaLogger.log(JSON.stringify(createAssetResult)); + + // Proceed with posting sentiment analysis tweet + const sentimentData = await getSentimentChart(averageScore, topic); + const file = await fetchFileFromUrl(sentimentData.url); - tweetContent += `Top 5 most influential accounts analyzed for ${topic}:\n`; - tweetContent += - topAuthors - .slice(0, 5) - .map((a) => `@${a}`) - .join(", ") + "\n\n"; + let tweetContent = `${topic} sentiment based on top ${tweets.length} latest posts`; + if (numOfTotalTweets - tweets.length > 0) { + tweetContent += ` and ${numOfTotalTweets - tweets.length} existing analysis Knowledge Assets`; + } + tweetContent += ` from the past 48 hours: ${sentiment}\n\n`; - tweetContent += `Analysis memorized on @origin_trail Decentralized Knowledge Graph `; - tweetContent += `${DKG_EXPLORER_LINKS[runtime.getSetting("DKG_ENVIRONMENT")]}${createAssetResult.UAL} @${twitterUser}\n\n`; + tweetContent += `Top 5 most influential accounts analyzed for ${topic}:\n`; + tweetContent += + topAuthors + .slice(0, 5) + .map((a) => `@${a}`) + .join(", ") + "\n\n"; - tweetContent += `This is not financial advice.`; + tweetContent += `Analysis memorized on @origin_trail Decentralized Knowledge Graph `; + tweetContent += `${DKG_EXPLORER_LINKS[runtime.getSetting("DKG_ENVIRONMENT")]}${createAssetResult.UAL} @${twitterUser}\n\n`; - await postTweet(tweetContent.trim(), scraper, postId, file.data); + tweetContent += `This is not financial advice.`; + await postTweet(tweetContent.trim(), scraper, postId, file.data); + } catch (error) { + elizaLogger.error( + "Unexpected error in sentiment analysis process:", + error.message, + ); + await postTweet( + `Apologies, something went wrong with the sentiment analysis.`, + scraper, + postId, + ); + } return true; }, examples: [ diff --git a/packages/plugin-dkg/src/actions/dkgInsert.ts b/packages/plugin-dkg/src/actions/dkgInsert.ts index fa05c0d9e22..89501079264 100644 --- a/packages/plugin-dkg/src/actions/dkgInsert.ts +++ b/packages/plugin-dkg/src/actions/dkgInsert.ts @@ -147,12 +147,54 @@ export const dkgInsert: Action = { JSON.stringify(error.response.data, null, 2), ); } + + if ( + error.message.includes("Unexpected") || + error.message.includes("JSON") + ) { + elizaLogger.warn( + "Detected JSON formatting issue. Attempting to fix...", + ); + try { + const fixedJSON = await generateText({ + runtime, + context: `Fix this malformed JSON-LD and return only the corrected JSON-LD:\n${JSON.stringify(memoryKnowledgeGraph, null, 2)} + + Make sure to only output the JSON-LD object. DO NOT OUTPUT ANYTHING ELSE, DONT ADD ANY COMMENTS, REMARKS AND DO NOT WRAP IT IN A CODE/JSON BLOCK, JUST THE JSON LD CONTENT WRAPPED IN { }.`, + modelClass: ModelClass.LARGE, + }); + + elizaLogger.log( + `Fixed JSON generated by LLM: ${fixedJSON}. Retrying...`, + ); + + createAssetResult = await DkgClient.asset.create( + { public: JSON.parse(fixedJSON) }, + { epochsNum: 12 }, + ); + + elizaLogger.log( + "======================== ASSET CREATED AFTER FIX", + ); + elizaLogger.log(JSON.stringify(createAssetResult)); + } catch (llmError) { + elizaLogger.error( + "Failed to fix JSON using LLM:", + llmError.message, + ); + } + } } - // Reply - callback({ - text: `Created a new memory!\n\nRead my mind on @origin_trail Decentralized Knowledge Graph ${DKG_EXPLORER_LINKS[runtime.getSetting("DKG_ENVIRONMENT")]}${createAssetResult.UAL} @${twitterUser}`, - }); + if (createAssetResult.UAL) { + callback({ + text: `Created a new memory!\n\nRead my mind on @origin_trail Decentralized Knowledge Graph ${DKG_EXPLORER_LINKS[runtime.getSetting("DKG_ENVIRONMENT")]}${createAssetResult.UAL} @${twitterUser}`, + }); + } else { + callback({ + text: `Apologies, something went wrong with creating the memory.`, + }); + } return true; },