Skip to content

Commit

Permalink
Assistant V2: Models for configuration of Agent (#1283)
Browse files Browse the repository at this point in the history
* Assistant V2: Models for configuration of Agent

* Rename remove prefix Assistant from models

* Apply feedback

* update timeframe after rebase

* Fix missed in rename

* Remove topKdefault value

* Another fix after rebase
  • Loading branch information
PopDaph authored and philipperolet committed Sep 6, 2023
1 parent 5d2ec26 commit a43e986
Show file tree
Hide file tree
Showing 8 changed files with 395 additions and 41 deletions.
13 changes: 13 additions & 0 deletions front/admin/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ import {
XP1Run,
XP1User,
} from "@app/lib/models";
import {
AgentDataSourceConfiguration,
AgentRetrievalConfiguration,
} from "@app/lib/models/assistant/actions/retrieval";
import {
AgentConfiguration,
AgentGenerationConfiguration,
} from "@app/lib/models/assistant/agent";

async function main() {
await User.sync({ alter: true });
Expand Down Expand Up @@ -54,6 +62,11 @@ async function main() {
await AgentMessage.sync({ alter: true });
await Message.sync({ alter: true });

await AgentConfiguration.sync({ alter: true });
await AgentGenerationConfiguration.sync({ alter: true });
await AgentRetrievalConfiguration.sync({ alter: true });
await AgentDataSourceConfiguration.sync({ alter: true });

await XP1User.sync({ alter: true });
await XP1Run.sync({ alter: true });

Expand Down
28 changes: 14 additions & 14 deletions front/lib/api/assistant/actions/retrieval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,50 +45,50 @@ export function parseTimeFrame(raw: string): TimeFrame | null {
return null;
}

const count = parseInt(m[1], 10);
if (isNaN(count)) {
const duration = parseInt(m[1], 10);
if (isNaN(duration)) {
return null;
}

let duration: TimeFrame["duration"];
let unit: TimeFrame["unit"];
switch (m[2]) {
case "d":
duration = "day";
unit = "day";
break;
case "w":
duration = "week";
unit = "week";
break;
case "m":
duration = "month";
unit = "month";
break;
case "y":
duration = "year";
unit = "year";
break;
default:
return null;
}

return {
count,
duration,
unit,
};
}

// Turns a TimeFrame into a number of milliseconds from now.
export function timeFrameFromNow(timeFrame: TimeFrame): number {
const now = Date.now();

switch (timeFrame.duration) {
switch (timeFrame.unit) {
case "hour":
return now - timeFrame.count * 60 * 60 * 1000;
return now - timeFrame.duration * 60 * 60 * 1000;
case "day":
return now - timeFrame.count * 24 * 60 * 60 * 1000;
return now - timeFrame.duration * 24 * 60 * 60 * 1000;
case "week":
return now - timeFrame.count * 7 * 24 * 60 * 60 * 1000;
return now - timeFrame.duration * 7 * 24 * 60 * 60 * 1000;
case "month":
return now - timeFrame.count * 30 * 24 * 60 * 60 * 1000;
return now - timeFrame.duration * 30 * 24 * 60 * 60 * 1000;
case "year":
return now - timeFrame.count * 365 * 24 * 60 * 60 * 1000;
return now - timeFrame.duration * 365 * 24 * 60 * 60 * 1000;
}
}

Expand Down
28 changes: 14 additions & 14 deletions front/lib/api/assistant/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
AgentActionSpecification,
AgentConfigurationStatus,
AgentConfigurationType,
AgentMessageConfigurationType,
AgentGenerationConfigurationType,
} from "@app/types/assistant/agent";
import {
AgentActionType,
Expand All @@ -35,12 +35,12 @@ export async function createAgentConfiguration(
name,
pictureUrl,
action,
message,
generation,
}: {
name: string;
pictureUrl?: string;
action?: AgentActionConfigurationType;
message?: AgentMessageConfigurationType;
generation?: AgentGenerationConfigurationType;
}
): Promise<AgentConfigurationType> {
return {
Expand All @@ -49,7 +49,7 @@ export async function createAgentConfiguration(
pictureUrl: pictureUrl ?? null,
status: "active",
action: action ?? null,
message: message ?? null,
generation: generation ?? null,
};
}

Expand All @@ -61,13 +61,13 @@ export async function updateAgentConfiguration(
pictureUrl,
status,
action,
message,
generation,
}: {
name: string;
pictureUrl?: string;
status: AgentConfigurationStatus;
action?: AgentActionConfigurationType;
message?: AgentMessageConfigurationType;
generation?: AgentGenerationConfigurationType;
}
): Promise<AgentConfigurationType> {
return {
Expand All @@ -76,7 +76,7 @@ export async function updateAgentConfiguration(
pictureUrl: pictureUrl ?? null,
status,
action: action ?? null,
message: message ?? null,
generation: generation ?? null,
};
}

Expand Down Expand Up @@ -188,20 +188,20 @@ export type AgentActionSuccessEvent = {
};

// Event sent when tokens are streamed as the the agent is generating a message.
export type AgentMessageTokensEvent = {
type: "agent_message_tokens";
export type AgentGenerationTokensEvent = {
type: "agent_generation_tokens";
created: number;
configurationId: string;
messageId: string;
text: string;
};

// Event sent once the message is completed and successful.
export type AgentMessageSuccessEvent = {
type: "agent_message_success";
export type AgentGenerationSuccessEvent = {
type: "agent_generation_success";
created: number;
configurationId: string;
messageId: string;
generationId: string;
message: AgentMessageType;
};

Expand All @@ -217,8 +217,8 @@ export async function* runAgent(
| AgentErrorEvent
| AgentActionEvent
| AgentActionSuccessEvent
| AgentMessageTokensEvent
| AgentMessageSuccessEvent
| AgentGenerationTokensEvent
| AgentGenerationSuccessEvent
> {
yield {
type: "agent_error",
Expand Down
18 changes: 9 additions & 9 deletions front/lib/api/assistant/conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import {
AgentActionEvent,
AgentActionSuccessEvent,
AgentErrorEvent,
AgentGenerationSuccessEvent,
AgentGenerationTokensEvent,
AgentMessageNewEvent,
AgentMessageSuccessEvent,
AgentMessageTokensEvent,
} from "@app/lib/api/assistant/agent";
import { Authenticator } from "@app/lib/auth";
import { CoreAPI } from "@app/lib/core_api";
Expand Down Expand Up @@ -176,8 +176,8 @@ export async function* postUserMessage(
| AgentErrorEvent
| AgentActionEvent
| AgentActionSuccessEvent
| AgentMessageTokensEvent
| AgentMessageSuccessEvent
| AgentGenerationTokensEvent
| AgentGenerationSuccessEvent
> {
const user = auth.user();

Expand Down Expand Up @@ -266,7 +266,7 @@ export async function* postUserMessage(
name: "foo", // TODO
pictureUrl: null, // TODO
action: null, // TODO
message: null, // TODO
generation: null, // TODO
},
});
}
Expand Down Expand Up @@ -320,8 +320,8 @@ export async function* retryAgentMessage(
| AgentErrorEvent
| AgentActionEvent
| AgentActionSuccessEvent
| AgentMessageTokensEvent
| AgentMessageSuccessEvent
| AgentGenerationTokensEvent
| AgentGenerationSuccessEvent
> {
yield {
type: "agent_error",
Expand Down Expand Up @@ -354,8 +354,8 @@ export async function* editUserMessage(
| AgentErrorEvent
| AgentActionEvent
| AgentActionSuccessEvent
| AgentMessageTokensEvent
| AgentMessageSuccessEvent
| AgentGenerationTokensEvent
| AgentGenerationSuccessEvent
> {
yield {
type: "agent_error",
Expand Down
Loading

0 comments on commit a43e986

Please sign in to comment.