Skip to content

Commit

Permalink
feat(memory): switch from LLM to ChatLLM in SummarizeMemory
Browse files Browse the repository at this point in the history
Signed-off-by: Tomas Dvorak <toomas2d@gmail.com>
  • Loading branch information
Tomas2D committed Oct 8, 2024
1 parent 69702b6 commit 022c05a
Showing 1 changed file with 22 additions and 31 deletions.
53 changes: 22 additions & 31 deletions src/memory/summarizeMemory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,45 +14,26 @@
* limitations under the License.
*/

import { BaseMessage } from "@/llms/primitives/message.js";
import { BaseMessage, Role } from "@/llms/primitives/message.js";
import { BaseMemory } from "@/memory/base.js";
import { PromptTemplate } from "@/template.js";
import { LLM } from "@/llms/llm.js";
import { BaseLLMOutput } from "@/llms/base.js";
import { shallowCopy } from "@/serializer/utils.js";
import { z } from "zod";
import { ChatLLM, ChatLLMOutput } from "@/llms/chat.js";

export interface SummarizeMemoryInput {
llm: LLM<BaseLLMOutput>;
template: typeof SummarizeMemoryTemplate;
llm: ChatLLM<ChatLLMOutput>;
template?: typeof SummarizeMemoryTemplate;
}

export const SummarizeMemoryTemplate = new PromptTemplate({
schema: z.object({
summary: z.string(),
new_lines: z.string(),
}),
template: `Progressively summarize the lines of conversation provided, adding onto the previous summary returning a new summary.
EXAMPLE
Current summary:
The human asks what the AI thinks of artificial intelligence. The AI thinks artificial intelligence is a force for good.
New lines of conversation:
Human: Why do you think artificial intelligence is a force for good?
AI: Because artificial intelligence will help humans reach their full potential.
New summary:
The human asks what the AI thinks of artificial intelligence. The AI thinks artificial intelligence is a force for good because it will help humans reach their full potential.
END OF EXAMPLE
Current summary:
{{summary}}
New lines of conversation:
{{new_lines}}
New summary:`,
{{summary}}`,
});

export class SummarizeMemory extends BaseMemory {
Expand All @@ -62,7 +43,7 @@ export class SummarizeMemory extends BaseMemory {

constructor(config: SummarizeMemoryInput) {
super();
this.template = config.template;
this.template = config.template ?? SummarizeMemoryTemplate;
this.llm = config.llm;
}

Expand All @@ -78,19 +59,29 @@ export class SummarizeMemory extends BaseMemory {

return [
BaseMessage.of({
role: "system",
role: Role.ASSISTANT,
text: currentSummary,
}),
];
}

async add(message: BaseMessage) {
const prompt = this.template.render({
summary: this.summary,
new_lines: `${message.role}: ${message.text}`,
});
const response = await this.llm.generate([
BaseMessage.of({
role: Role.SYSTEM,
text: this.template.render({
summary: this.summary,
}),
}),
BaseMessage.of({
role: Role.ASSISTANT,
text: `New lines of conversation:
${message.role}: ${message.text}
const response = await this.llm.generate(prompt);
New summary:
`,
}),
]);
this.summary = response.getTextContent();
}

Expand Down

0 comments on commit 022c05a

Please sign in to comment.