Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve twitter post content quality #763

Merged
merged 3 commits into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is incorrectly named "timeline" - it was really just a list of the bot's previous posts

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meh, I’ve heard it called timeline. Profile timeline might be more apt and precise

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, we can revisit this in a future pr. didn't actually change the underlying function

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, if we need to have a list of previous posts for some analysis, we can quickly re-introduce it. For the PR its good

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.`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a bit cleaner would be something like this. Maybe we can fine-tune or A/B test the prompt.

Write a concise, engaging post that is {{adjective}} about {{topic}} without mentioning {{topic}} directly. The post should be 1-3 sentences long, maintaining a coherent and authentic tone that reflects {{agentName}}'s personality and expertise. Avoid adding any commentary or acknowledgment of this request. Ensure the total character count is less than 280 characters. Use \\n\\n (double spaces) between statements. No emojis or questions allowed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can try this out


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");
}
Loading