Skip to content

Commit

Permalink
Merge pull request #4432 from alkem-io/develop
Browse files Browse the repository at this point in the history
Release: Limit History Size of VCs
  • Loading branch information
valentinyanakiev authored Aug 20, 2024
2 parents b5cbf8d + 24307be commit 975a355
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 23 deletions.
4 changes: 4 additions & 0 deletions alkemio.yml
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,10 @@ platform:
vector_db:
host: ${VECTOR_DB_HOST}:localhost
port: ${VECTOR_DB_PORT}:8765

virtual_contributors:
history_length: ${VC_HISTORY_LENGTH}:10

ssi:
# Jolocom SDK is used for providing SSI capabilities on the platform.
# Note: NOT FOR PRODUCTION USAGE, experimental functionality,
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "alkemio-server",
"version": "0.88.1",
"version": "0.88.2",
"description": "Alkemio server, responsible for managing the shared Alkemio platform",
"author": "Alkemio Foundation",
"private": false,
Expand Down
2 changes: 2 additions & 0 deletions src/domain/communication/room/room.resolver.mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,13 +315,15 @@ export class RoomResolverMutations {
reply,
agentInfo
);

// TODO extact in a helper function
if (accessVirtualContributors) {
// Check before processing so as not to reply to same message where interaction started
const vcInteraction = await this.roomService.getVcInteractionByThread(
room.id,
threadID
);

await this.roomServiceMentions.processVirtualContributorMentions(
mentions,
messageData.message,
Expand Down
1 change: 1 addition & 0 deletions src/domain/community/ai-persona/ai.persona.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ export class AiPersonaService {

const input: AiServerAdapterAskQuestionInput = {
question: question,
displayName: '',
aiPersonaServiceID: aiPersona.aiPersonaServiceID,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ export class VirtualContributorService {
authorization: true,
aiPersona: true,
agent: true,
profile: true,
},
}
);
Expand All @@ -369,6 +370,8 @@ export class VirtualContributorService {
userID: vcQuestionInput.userID,
threadID: vcQuestionInput.threadID,
vcInteractionID: vcQuestionInput.vcInteractionID,
description: virtualContributor.profile.description,
displayName: virtualContributor.profile.displayName,
};

return await this.aiServerAdapter.askQuestion(aiServerAdapterQuestionInput);
Expand Down
10 changes: 4 additions & 6 deletions src/services/adapters/ai-server-adapter/ai.server.adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,16 @@ export class AiServerAdapter {
async getPersonaServiceBodyOfKnowledgeType(
personaServiceId: string
): Promise<AiPersonaBodyOfKnowledgeType> {
const aiPersonaService = await this.aiServer.getAiPersonaServiceOrFail(
personaServiceId
);
const aiPersonaService =
await this.aiServer.getAiPersonaServiceOrFail(personaServiceId);
return aiPersonaService.bodyOfKnowledgeType;
}

async getPersonaServiceBodyOfKnowledgeID(
personaServiceId: string
): Promise<string> {
const aiPersonaService = await this.aiServer.getAiPersonaServiceOrFail(
personaServiceId
);
const aiPersonaService =
await this.aiServer.getAiPersonaServiceOrFail(personaServiceId);
return aiPersonaService.bodyOfKnowledgeID;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ export class AiServerAdapterAskQuestionInput {
userID?: string;
threadID?: string;
vcInteractionID?: string;
description?: string;
displayName!: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ export interface AiPersonaEngineAdapterQueryInput
bodyOfKnowledgeID: string;
interactionID?: string;
history?: InteractionMessage[];
description?: string;
displayName: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ export class AiPersonaServiceService {
contextID: personaQuestionInput.contextID,
history,
interactionID: personaQuestionInput.interactionID,
displayName: personaQuestionInput.displayName,
description: personaQuestionInput.description,
};

return this.aiPersonaEngineAdapter.sendQuery(input);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,39 @@ export class AiPersonaServiceQuestionInput {
@Field(() => String, {
nullable: true,
description:
'The ID of the context, the Virtual Persona is asked a question',
'The ID of the context, the Virtual Persona is asked a question.',
})
contextID?: string | undefined = undefined;
contextID?: string = undefined;

@Field(() => String, {
nullable: true,
description: 'User identifier used internaly by the engine',
description: 'User identifier used internaly by the engine.',
})
userID?: string | undefined = undefined;
userID?: string = undefined;

@Field(() => String, {
nullable: true,
description:
'The ID of the message thread where the Virtual Contributor is asked a question if applicable',
'The ID of the message thread where the Virtual Contributor is asked a question if applicable.',
})
threadID?: string | undefined = undefined;
threadID?: string = undefined;

@Field(() => String, {
nullable: true,
description:
'The Virtual Contributor interaciton part of which is this question',
'The Virtual Contributor interaciton part of which is this question.',
})
interactionID: string | undefined = undefined;
interactionID?: string = undefined;

@Field(() => String, {
nullable: true,
description: 'The Virtual Contributor description.',
})
description?: string = undefined;

@Field(() => String, {
nullable: false,
description: 'The Virtual Contributor displayName.',
})
displayName!: string;
}
24 changes: 18 additions & 6 deletions src/services/ai-server/ai-server/ai.server.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,25 @@ export class AiServerService {
new IngestSpace(questionInput.contextID, SpaceIngestionPurpose.CONTEXT)
);
}
const history = await this.getInteractionMessages(
questionInput.interactionID
const historyLimit = parseInt(
this.config.get<number>('platform.virtual_contributors.history_length', {
infer: true,
})
);

const history = await this.getLastNInteractionMessages(
questionInput.interactionID,
historyLimit
);

return await this.aiPersonaServiceService.askQuestion(
questionInput,
history
);
}
async getInteractionMessages(
interactionID: string | undefined
async getLastNInteractionMessages(
interactionID: string | undefined,
limit: number = 10
): Promise<InteractionMessage[]> {
if (!interactionID) {
return [];
Expand All @@ -109,7 +117,8 @@ export class AiServerService {
);

const messages: InteractionMessage[] = [];
for (let i = 0; i < room.messages.length; i++) {

for (let i = room.messages.length - 1; i >= 0; i--) {
const message = room.messages[i];
// try to skip this check and use Matrix to filter by Room and Thread
if (
Expand All @@ -123,10 +132,13 @@ export class AiServerService {
role = MessageSenderRole.ASSISTANT;
}

messages.push({
messages.unshift({
content: message.message,
role,
});
if (messages.length === limit) {
break;
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/types/alkemio.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ export type AlkemioConfig = {
host: string;
port: number;
};
virtual_contributors: {
history_length: number;
};
};
ssi: {
enabled: boolean;
Expand Down

0 comments on commit 975a355

Please sign in to comment.