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

Save chat optional #7

Merged
merged 5 commits into from
Jul 26, 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
7 changes: 7 additions & 0 deletions src/agents/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ export interface AgentOptions {

// Optional: The geographic region where the agent should be deployed or run
region?: string;

// Optional: Determines whether to save the chat, defaults to true
saveChat?: boolean;
}

/**
Expand All @@ -63,6 +66,9 @@ export abstract class Agent {
/** A description of the agent's capabilities and expertise. */
description: string;

/** Whether to save the chat or not. */
saveChat: boolean;

/**
* Constructs a new Agent instance.
* @param options - Configuration options for the agent.
Expand All @@ -71,6 +77,7 @@ export abstract class Agent {
this.name = options.name;
this.id = this.generateKeyFromName(options.name);
this.description = options.description;
this.saveChat = options.saveChat ?? true; // Default to true if not provided
}

/**
Expand Down
45 changes: 28 additions & 17 deletions src/orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { BedrockLLMAgent } from "./agents/bedrockLLMAgent";
import { ChatStorage } from "./storage/chatStorage";
import { InMemoryChatStorage } from "./storage/memoryChatStorage";
import { AccumulatorTransform } from "./utils/helpers";
import { saveChat } from "./utils/chatUtils";
import { saveConversationExchange } from "./utils/chatUtils";
import { Logger } from "./utils/logger";
import { BedrockClassifier } from "./classifiers/bedrockClassifier";
import { Classifier } from "./classifiers/classifier";
Expand Down Expand Up @@ -283,7 +283,7 @@ export class MultiAgentOrchestrator {
return obj != null && typeof obj[Symbol.asyncIterator] === "function";
}

private async dispatchToAgent(
async dispatchToAgent(
params: DispatchToAgentsParams
): Promise<string | AsyncIterable<any>> {
const {
Expand Down Expand Up @@ -355,6 +355,7 @@ export class MultiAgentOrchestrator {
"Classifying user intent",
() => this.classifier.classify(userInput, chatHistory)
);

this.logger.printIntent(userInput, classifierResult);
} catch (error) {
this.logger.error("Error during intent classification:", error);
Expand All @@ -364,7 +365,7 @@ export class MultiAgentOrchestrator {
streaming: false,
};
}

// Handle case where no agent was selected
if (!classifierResult.selectedAgent) {
if (this.config.USE_DEFAULT_AGENT_IF_NONE_IDENTIFIED) {
Expand Down Expand Up @@ -398,7 +399,7 @@ export class MultiAgentOrchestrator {
userInput,
userId,
sessionId,
metadata
classifierResult.selectedAgent
);
return {
metadata,
Expand All @@ -407,16 +408,20 @@ export class MultiAgentOrchestrator {
};
}

await saveChat(
userInput,
agentResponse,
this.storage,
userId,
sessionId,
classifierResult.selectedAgent.id,
this.config.MAX_MESSAGE_PAIRS_PER_AGENT
);

// Check if we should save the conversation
if (classifierResult?.selectedAgent.saveChat) {
await saveConversationExchange(
userInput,
agentResponse,
this.storage,
userId,
sessionId,
classifierResult?.selectedAgent.id,
this.config.MAX_MESSAGE_PAIRS_PER_AGENT
);
}


return {
metadata,
output: agentResponse,
Expand All @@ -440,7 +445,7 @@ export class MultiAgentOrchestrator {
userInput: string,
userId: string,
sessionId: string,
metadata: any
agent: Agent
): Promise<void> {
const streamStartTime = Date.now();
let chunkCount = 0;
Expand All @@ -462,14 +467,20 @@ export class MultiAgentOrchestrator {

const fullResponse = accumulatorTransform.getAccumulatedData();
if (fullResponse) {
await saveChat(



if (agent.saveChat) {
await saveConversationExchange(
userInput,
fullResponse,
this.storage,
userId,
sessionId,
metadata.agentId
agent.id
);
}

} else {
this.logger.warn("No data accumulated, messages not saved");
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/chatUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ChatStorage } from '../storage/chatStorage';
import { ConversationMessage, ParticipantRole } from '../types';


export async function saveChat(
export async function saveConversationExchange(
userInput: string,
agentResponse: string,
storage: ChatStorage,
Expand Down
Loading