Skip to content

Commit 6f61755

Browse files
authored
Merge pull request #1305 from elizaOS/tcm-compose-random-user
fix: {{user}} tags in templates/examples empty when passed to LLM
2 parents 4d04b8b + 5b3b0f3 commit 6f61755

File tree

5 files changed

+56
-23
lines changed

5 files changed

+56
-23
lines changed

packages/client-discord/src/messages.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { composeContext } from "@elizaos/core";
1+
import { composeContext, composeRandomUser } from "@elizaos/core";
22
import { generateMessageResponse, generateShouldRespond } from "@elizaos/core";
33
import {
44
Content,
@@ -1228,7 +1228,7 @@ export class MessageManager {
12281228
this.runtime.character.templates
12291229
?.discordShouldRespondTemplate ||
12301230
this.runtime.character.templates?.shouldRespondTemplate ||
1231-
discordShouldRespondTemplate,
1231+
composeRandomUser(discordShouldRespondTemplate, 2),
12321232
});
12331233

12341234
const response = await generateShouldRespond({

packages/client-discord/src/templates.ts

+17-17
Original file line numberDiff line numberDiff line change
@@ -8,48 +8,48 @@ About {{agentName}}:
88
# 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".
99
1010
# RESPONSE EXAMPLES
11-
<user 1>: I just saw a really great movie
12-
<user 2>: Oh? Which movie?
11+
{{user1}}: I just saw a really great movie
12+
{{user2}}: Oh? Which movie?
1313
Result: [IGNORE]
1414
1515
{{agentName}}: Oh, this is my favorite scene
16-
<user 1>: sick
17-
<user 2>: wait, why is it your favorite scene
16+
{{user1}}: sick
17+
{{user2}}: wait, why is it your favorite scene
1818
Result: [RESPOND]
1919
20-
<user>: stfu bot
20+
{{user1}}: stfu bot
2121
Result: [STOP]
2222
23-
<user>: Hey {{agent}}, can you help me with something
23+
{{user1}}: Hey {{agent}}, can you help me with something
2424
Result: [RESPOND]
2525
26-
<user>: {{agentName}} stfu plz
26+
{{user1}}: {{agentName}} stfu plz
2727
Result: [STOP]
2828
29-
<user>: i need help
29+
{{user1}}: i need help
3030
{{agentName}}: how can I help you?
31-
<user>: no. i need help from someone else
31+
{{user1}}: no. i need help from someone else
3232
Result: [IGNORE]
3333
34-
<user>: Hey {{agent}}, can I ask you a question
34+
{{user1}}: Hey {{agent}}, can I ask you a question
3535
{{agentName}}: Sure, what is it
36-
<user>: can you ask claude to create a basic react module that demonstrates a counter
36+
{{user1}}: can you ask claude to create a basic react module that demonstrates a counter
3737
Result: [RESPOND]
3838
39-
<user>: {{agentName}} can you tell me a story
40-
<user>: {about a girl named elara
39+
{{user1}}: {{agentName}} can you tell me a story
40+
{{user1}}: about a girl named elara
4141
{{agentName}}: Sure.
4242
{{agentName}}: Once upon a time, in a quaint little village, there was a curious girl named Elara.
4343
{{agentName}}: Elara was known for her adventurous spirit and her knack for finding beauty in the mundane.
44-
<user>: I'm loving it, keep going
44+
{{user1}}: I'm loving it, keep going
4545
Result: [RESPOND]
4646
47-
<user>: {{agentName}} stop responding plz
47+
{{user1}}: {{agentName}} stop responding plz
4848
Result: [STOP]
4949
50-
<user>: okay, i want to test something. can you say marco?
50+
{{user1}}: okay, i want to test something. can you say marco?
5151
{{agentName}}: marco
52-
<user>: great. okay, now do it again
52+
{{user1}}: great. okay, now do it again
5353
Result: [RESPOND]
5454
5555
Response options are [RESPOND], [IGNORE] and [STOP].

packages/client-discord/src/voice.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
State,
99
UUID,
1010
composeContext,
11+
composeRandomUser,
1112
elizaLogger,
1213
getEmbeddingZeroVector,
1314
generateMessageResponse,
@@ -840,7 +841,7 @@ export class VoiceManager extends EventEmitter {
840841
this.runtime.character.templates
841842
?.discordShouldRespondTemplate ||
842843
this.runtime.character.templates?.shouldRespondTemplate ||
843-
discordShouldRespondTemplate,
844+
composeRandomUser(discordShouldRespondTemplate, 2),
844845
});
845846

846847
const response = await generateShouldRespond({

packages/client-telegram/src/messageManager.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Message } from "@telegraf/types";
22
import { Context, Telegraf } from "telegraf";
3-
4-
import { composeContext, elizaLogger, ServiceType } from "@elizaos/core";
3+
import { composeContext, elizaLogger, ServiceType, composeRandomUser } from "@elizaos/core";
54
import { getEmbeddingZeroVector } from "@elizaos/core";
65
import {
76
Content,
@@ -661,7 +660,7 @@ export class MessageManager {
661660
this.runtime.character.templates
662661
?.telegramShouldRespondTemplate ||
663662
this.runtime.character?.templates?.shouldRespondTemplate ||
664-
telegramShouldRespondTemplate,
663+
composeRandomUser(telegramShouldRespondTemplate, 2),
665664
});
666665

667666
const response = await generateShouldRespond({

packages/core/src/context.ts

+33
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import handlebars from "handlebars";
22
import { type State } from "./types.ts";
3+
import { names, uniqueNamesGenerator } from "unique-names-generator";
34

45
/**
56
* Composes a context string by replacing placeholders in a template with corresponding values from the state.
@@ -69,3 +70,35 @@ export const composeContext = ({
6970
export const addHeader = (header: string, body: string) => {
7071
return body.length > 0 ? `${header ? header + "\n" : header}${body}\n` : "";
7172
};
73+
74+
/**
75+
* Generates a string with random user names populated in a template.
76+
*
77+
* This function generates a specified number of random user names and populates placeholders
78+
* in the provided template with these names. Placeholders in the template should follow the format `{{userX}}`
79+
* where `X` is the position of the user (e.g., `{{user1}}`, `{{user2}}`).
80+
*
81+
* @param {string} params.template - The template string containing placeholders for random user names.
82+
* @param {number} params.length - The number of random user names to generate.
83+
* @returns {string} The template string with placeholders replaced by random user names.
84+
*
85+
* @example
86+
* // Given a template and a length
87+
* const template = "Hello, {{user1}}! Meet {{user2}} and {{user3}}.";
88+
* const length = 3;
89+
*
90+
* // Composing the random user string will result in:
91+
* // "Hello, John! Meet Alice and Bob."
92+
* const result = composeRandomUser({ template, length });
93+
*/
94+
export const composeRandomUser = (template: string, length: number) => {
95+
const exampleNames = Array.from({ length }, () =>
96+
uniqueNamesGenerator({ dictionaries: [names] })
97+
);
98+
let result = template;
99+
for (let i = 0; i < exampleNames.length; i++) {
100+
result = result.replaceAll(`{{user${i + 1}}}`, exampleNames[i]);
101+
}
102+
103+
return result;
104+
};

0 commit comments

Comments
 (0)