Skip to content

Commit

Permalink
Merge branch 'improve_twitter_responses' of https://github.com/cygaar…
Browse files Browse the repository at this point in the history
…/eliza into HEAD
  • Loading branch information
lalalune committed Dec 1, 2024
2 parents 585ee23 + 58d38c9 commit 2ad51b9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 64 deletions.
28 changes: 3 additions & 25 deletions packages/client-twitter/src/interactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@ import { ClientBase } from "./base";
import { buildConversationThread, sendTweet, wait } from "./utils.ts";

export const twitterMessageHandlerTemplate =
`{{timeline}}
# Knowledge
`
# Areas of Expertise
{{knowledge}}
# Task: Generate a post for the character {{agentName}}.
About {{agentName}} (@{{twitterUserName}}):
# About {{agentName}} (@{{twitterUserName}}):
{{bio}}
{{lore}}
{{topics}}
Expand Down Expand Up @@ -226,17 +224,6 @@ export class TwitterInteractionClient {
};
const currentPost = formatTweet(tweet);

let homeTimeline: Tweet[] = [];
// read the file if it exists

const cachedTimeline = await this.client.getCachedTimeline();
if (cachedTimeline) {
homeTimeline = cachedTimeline;
} else {
homeTimeline = await this.client.fetchHomeTimeline(50);
await this.client.cacheTimeline(homeTimeline);
}

elizaLogger.debug("Thread: ", thread);
const formattedConversation = thread
.map(
Expand All @@ -254,20 +241,11 @@ export class TwitterInteractionClient {

elizaLogger.debug("formattedConversation: ", formattedConversation);

const formattedHomeTimeline =
`# ${this.runtime.character.name}'s Home Timeline\n\n` +
homeTimeline
.map((tweet) => {
return `ID: ${tweet.id}\nFrom: ${tweet.name} (@${tweet.username})${tweet.inReplyToStatusId ? ` In reply to: ${tweet.inReplyToStatusId}` : ""}\nText: ${tweet.text}\n---\n`;
})
.join("\n");

let state = await this.runtime.composeState(message, {
twitterClient: this.client.twitterClient,
twitterUserName: this.runtime.getSetting("TWITTER_USERNAME"),
currentPost,
formattedConversation,
timeline: formattedHomeTimeline,
});

// check if the tweet exists, save if it doesn't
Expand Down
43 changes: 9 additions & 34 deletions packages/client-twitter/src/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,24 @@ import {
import { elizaLogger } from "@ai16z/eliza";
import { ClientBase } from "./base.ts";

const twitterPostTemplate = `{{timeline}}
# Knowledge
const twitterPostTemplate = `
# Areas of Expertise
{{knowledge}}
About {{agentName}} (@{{twitterUserName}}):
# About {{agentName}} (@{{twitterUserName}}):
{{bio}}
{{lore}}
{{postDirections}}
{{topics}}
{{providers}}
{{recentPosts}}
{{characterPostExamples}}
# Task: Generate a post in the voice and style of {{agentName}}, aka @{{twitterUserName}}
Write a single sentence post that is {{adjective}} about {{topic}} (without mentioning {{topic}} directly), from the perspective of {{agentName}}. Try to write something totally different than previous posts. Do not add commentary or acknowledge this request, just write the post.
Your response should not contain any questions. Brief, concise statements only. No emojis. Use \\n\\n (double spaces) between statements.`;
{{postDirections}}
# Task: Generate a post in the voice and style and perspective of {{agentName}} @{{twitterUserName}}.
Write a 1-3 sentence post that is {{adjective}} about {{topic}} (without mentioning {{topic}} directly), from the perspective of {{agentName}}. Do not add commentary or acknowledge this request, just write the post.
Your response should not contain any questions. Brief, concise statements only. The total character count MUST be less than 280. No emojis. Use \\n\\n (double spaces) between statements.`;

const MAX_TWEET_LENGTH = 280;

Expand Down Expand Up @@ -132,28 +131,7 @@ export class TwitterPostClient {
"twitter"
);

let homeTimeline: Tweet[] = [];

const cachedTimeline = await this.client.getCachedTimeline();

// console.log({ cachedTimeline });

if (cachedTimeline) {
homeTimeline = cachedTimeline;
} else {
homeTimeline = await this.client.fetchHomeTimeline(10);
await this.client.cacheTimeline(homeTimeline);
}
const formattedHomeTimeline =
`# ${this.runtime.character.name}'s Home Timeline\n\n` +
homeTimeline
.map((tweet) => {
return `#${tweet.id}\n${tweet.name} (@${tweet.username})${tweet.inReplyToStatusId ? `\nIn reply to: ${tweet.inReplyToStatusId}` : ""}\n${new Date(tweet.timestamp).toDateString()}\n\n${tweet.text}\n---\n`;
})
.join("\n");

const topics = this.runtime.character.topics.join(", ");

const state = await this.runtime.composeState(
{
userId: this.runtime.agentId,
Expand All @@ -166,7 +144,6 @@ export class TwitterPostClient {
},
{
twitterUserName: this.client.profile.username,
timeline: formattedHomeTimeline,
}
);

Expand Down Expand Up @@ -243,8 +220,6 @@ export class TwitterPostClient {

await this.client.cacheTweet(tweet);

homeTimeline.push(tweet);
await this.client.cacheTimeline(homeTimeline);
elizaLogger.log(`Tweet posted:\n ${tweet.permanentUrl}`);

const roomId = stringToUuid(
Expand Down
12 changes: 7 additions & 5 deletions packages/core/src/providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ export async function getProviders(
message: Memory,
state?: State
) {
const providerResults = await Promise.all(
runtime.providers.map(async (provider) => {
return await provider.get(runtime, message, state);
})
);
const providerResults = (
await Promise.all(
runtime.providers.map(async (provider) => {
return await provider.get(runtime, message, state);
})
)
).filter((result) => result != null && result !== "");

return providerResults.join("\n");
}

0 comments on commit 2ad51b9

Please sign in to comment.