Skip to content

Commit

Permalink
Merge pull request #1305 from elizaOS/tcm-compose-random-user
Browse files Browse the repository at this point in the history
fix: {{user}} tags in templates/examples empty when passed to LLM
  • Loading branch information
monilpat authored Dec 24, 2024
2 parents 4d04b8b + 5b3b0f3 commit 6f61755
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 23 deletions.
4 changes: 2 additions & 2 deletions packages/client-discord/src/messages.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { composeContext } from "@elizaos/core";
import { composeContext, composeRandomUser } from "@elizaos/core";
import { generateMessageResponse, generateShouldRespond } from "@elizaos/core";
import {
Content,
Expand Down Expand Up @@ -1228,7 +1228,7 @@ export class MessageManager {
this.runtime.character.templates
?.discordShouldRespondTemplate ||
this.runtime.character.templates?.shouldRespondTemplate ||
discordShouldRespondTemplate,
composeRandomUser(discordShouldRespondTemplate, 2),
});

const response = await generateShouldRespond({
Expand Down
34 changes: 17 additions & 17 deletions packages/client-discord/src/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,48 @@ About {{agentName}}:
# INSTRUCTIONS: Determine if {{agentName}} should respond to the message and participate in the conversation. Do not comment. Just respond with "RESPOND" or "IGNORE" or "STOP".
# RESPONSE EXAMPLES
<user 1>: I just saw a really great movie
<user 2>: Oh? Which movie?
{{user1}}: I just saw a really great movie
{{user2}}: Oh? Which movie?
Result: [IGNORE]
{{agentName}}: Oh, this is my favorite scene
<user 1>: sick
<user 2>: wait, why is it your favorite scene
{{user1}}: sick
{{user2}}: wait, why is it your favorite scene
Result: [RESPOND]
<user>: stfu bot
{{user1}}: stfu bot
Result: [STOP]
<user>: Hey {{agent}}, can you help me with something
{{user1}}: Hey {{agent}}, can you help me with something
Result: [RESPOND]
<user>: {{agentName}} stfu plz
{{user1}}: {{agentName}} stfu plz
Result: [STOP]
<user>: i need help
{{user1}}: i need help
{{agentName}}: how can I help you?
<user>: no. i need help from someone else
{{user1}}: no. i need help from someone else
Result: [IGNORE]
<user>: Hey {{agent}}, can I ask you a question
{{user1}}: Hey {{agent}}, can I ask you a question
{{agentName}}: Sure, what is it
<user>: can you ask claude to create a basic react module that demonstrates a counter
{{user1}}: can you ask claude to create a basic react module that demonstrates a counter
Result: [RESPOND]
<user>: {{agentName}} can you tell me a story
<user>: {about a girl named elara
{{user1}}: {{agentName}} can you tell me a story
{{user1}}: about a girl named elara
{{agentName}}: Sure.
{{agentName}}: Once upon a time, in a quaint little village, there was a curious girl named Elara.
{{agentName}}: Elara was known for her adventurous spirit and her knack for finding beauty in the mundane.
<user>: I'm loving it, keep going
{{user1}}: I'm loving it, keep going
Result: [RESPOND]
<user>: {{agentName}} stop responding plz
{{user1}}: {{agentName}} stop responding plz
Result: [STOP]
<user>: okay, i want to test something. can you say marco?
{{user1}}: okay, i want to test something. can you say marco?
{{agentName}}: marco
<user>: great. okay, now do it again
{{user1}}: great. okay, now do it again
Result: [RESPOND]
Response options are [RESPOND], [IGNORE] and [STOP].
Expand Down
3 changes: 2 additions & 1 deletion packages/client-discord/src/voice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
State,
UUID,
composeContext,
composeRandomUser,
elizaLogger,
getEmbeddingZeroVector,
generateMessageResponse,
Expand Down Expand Up @@ -840,7 +841,7 @@ export class VoiceManager extends EventEmitter {
this.runtime.character.templates
?.discordShouldRespondTemplate ||
this.runtime.character.templates?.shouldRespondTemplate ||
discordShouldRespondTemplate,
composeRandomUser(discordShouldRespondTemplate, 2),
});

const response = await generateShouldRespond({
Expand Down
5 changes: 2 additions & 3 deletions packages/client-telegram/src/messageManager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Message } from "@telegraf/types";
import { Context, Telegraf } from "telegraf";

import { composeContext, elizaLogger, ServiceType } from "@elizaos/core";
import { composeContext, elizaLogger, ServiceType, composeRandomUser } from "@elizaos/core";
import { getEmbeddingZeroVector } from "@elizaos/core";
import {
Content,
Expand Down Expand Up @@ -661,7 +660,7 @@ export class MessageManager {
this.runtime.character.templates
?.telegramShouldRespondTemplate ||
this.runtime.character?.templates?.shouldRespondTemplate ||
telegramShouldRespondTemplate,
composeRandomUser(telegramShouldRespondTemplate, 2),
});

const response = await generateShouldRespond({
Expand Down
33 changes: 33 additions & 0 deletions packages/core/src/context.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import handlebars from "handlebars";
import { type State } from "./types.ts";
import { names, uniqueNamesGenerator } from "unique-names-generator";

/**
* Composes a context string by replacing placeholders in a template with corresponding values from the state.
Expand Down Expand Up @@ -69,3 +70,35 @@ export const composeContext = ({
export const addHeader = (header: string, body: string) => {
return body.length > 0 ? `${header ? header + "\n" : header}${body}\n` : "";
};

/**
* Generates a string with random user names populated in a template.
*
* This function generates a specified number of random user names and populates placeholders
* in the provided template with these names. Placeholders in the template should follow the format `{{userX}}`
* where `X` is the position of the user (e.g., `{{user1}}`, `{{user2}}`).
*
* @param {string} params.template - The template string containing placeholders for random user names.
* @param {number} params.length - The number of random user names to generate.
* @returns {string} The template string with placeholders replaced by random user names.
*
* @example
* // Given a template and a length
* const template = "Hello, {{user1}}! Meet {{user2}} and {{user3}}.";
* const length = 3;
*
* // Composing the random user string will result in:
* // "Hello, John! Meet Alice and Bob."
* const result = composeRandomUser({ template, length });
*/
export const composeRandomUser = (template: string, length: number) => {
const exampleNames = Array.from({ length }, () =>
uniqueNamesGenerator({ dictionaries: [names] })
);
let result = template;
for (let i = 0; i < exampleNames.length; i++) {
result = result.replaceAll(`{{user${i + 1}}}`, exampleNames[i]);
}

return result;
};

0 comments on commit 6f61755

Please sign in to comment.