Skip to content

Commit

Permalink
Move types
Browse files Browse the repository at this point in the history
  • Loading branch information
PopDaph committed Sep 8, 2023
1 parent 31202d9 commit b49e22d
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 84 deletions.
6 changes: 3 additions & 3 deletions front/lib/api/assistant/actions/retrieval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ import {
RetrievalDocumentType,
TimeFrame,
} from "@app/types/assistant/actions/retrieval";
import { AgentActionSpecification } from "@app/types/assistant/agent";
import {
AgentConfigurationType,
AgentDataSourceConfigurationType,
isRetrievalConfiguration,
RetrievalConfigurationType,
} from "@app/types/assistant/configuration";
} from "@app/types/assistant/actions/retrieval";
import { AgentActionSpecification } from "@app/types/assistant/agent";
import { AgentConfigurationType } from "@app/types/assistant/configuration";
import {
AgentMessageType,
ConversationType,
Expand Down
6 changes: 2 additions & 4 deletions front/lib/api/assistant/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ import {
import { Authenticator } from "@app/lib/auth";
import { Err, Ok, Result } from "@app/lib/result";
import { generateModelSId } from "@app/lib/utils";
import { isRetrievalConfiguration } from "@app/types/assistant/actions/retrieval";
import { AgentActionSpecification } from "@app/types/assistant/agent";
import {
AgentConfigurationType,
isRetrievalConfiguration,
} from "@app/types/assistant/configuration";
import { AgentConfigurationType } from "@app/types/assistant/configuration";
import {
AgentActionType,
AgentMessageType,
Expand Down
36 changes: 18 additions & 18 deletions front/lib/api/assistant/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from "@app/lib/models";
import { generateModelSId } from "@app/lib/utils";
import {
AgentDataSourceConfigurationType,
isTemplatedQuery,
isTimeFrame,
RetrievalQuery,
Expand All @@ -21,7 +22,6 @@ import {
AgentActionConfigurationType,
AgentConfigurationStatus,
AgentConfigurationType,
AgentDataSourceConfigurationType,
} from "@app/types/assistant/configuration";

/**
Expand All @@ -45,25 +45,25 @@ export async function getAgentConfiguration(
throw new Error("Cannot find Agent: no workspace");
}

const generation = agent.generationId
const generation = agent.generationConfigId
? await AgentGenerationConfiguration.findOne({
where: {
id: agent.generationId,
id: agent.generationConfigId,
},
})
: null;

const action = agent.retrievalId
const action = agent.retrievalConfigId
? await AgentRetrievalConfiguration.findOne({
where: {
id: agent.retrievalId,
id: agent.retrievalConfigId,
},
})
: null;
const datasources = action?.id
? await AgentDataSourceConfiguration.findAll({
where: {
retrievalId: action.id,
retrievalConfigId: action.id,
},
})
: [];
Expand Down Expand Up @@ -170,8 +170,8 @@ export async function createAgentConfiguration(
pictureUrl: pictureUrl,
scope: "workspace",
workspaceId: owner.id,
generationId: genConfig?.id ?? null,
retrievalId: retrievalConfig?.id ?? null,
generationConfigId: genConfig?.id ?? null,
retrievalConfigId: retrievalConfig?.id ?? null,
},
{ transaction: t }
);
Expand Down Expand Up @@ -238,26 +238,26 @@ export async function updateAgentConfiguration(
"Cannot create AgentGenerationConfiguration: Agent not found"
);
}
const existingGeneration = agentConfig.generationId
const existingGeneration = agentConfig.generationConfigId
? await AgentGenerationConfiguration.findOne({
where: {
id: agentConfig.generationId,
id: agentConfig.generationConfigId,
},
})
: null;

const existingRetrivalConfig = agentConfig.retrievalId
const existingRetrivalConfig = agentConfig.retrievalConfigId
? await AgentRetrievalConfiguration.findOne({
where: {
id: agentConfig.retrievalId,
id: agentConfig.retrievalConfigId,
},
})
: null;

const existingDataSourcesConfig = existingRetrivalConfig?.id
? await AgentDataSourceConfiguration.findAll({
where: {
retrievalId: existingRetrivalConfig.id,
retrievalConfigId: existingRetrivalConfig.id,
},
})
: [];
Expand Down Expand Up @@ -361,19 +361,19 @@ export async function updateAgentRetrievalConfiguration(
"Cannot create AgentGenerationConfiguration: Agent not found"
);
}
const generationConfig = agentConfig.generationId
const generationConfig = agentConfig.generationConfigId
? await AgentGenerationConfiguration.findOne({
where: {
id: agentConfig.generationId,
id: agentConfig.generationConfigId,
},
})
: null;

return await front_sequelize.transaction(async (t) => {
if (agentConfig.retrievalId) {
if (agentConfig.retrievalConfigId) {
const existingRetrivalConfig = await AgentRetrievalConfiguration.findOne({
where: {
id: agentConfig.retrievalId,
id: agentConfig.retrievalConfigId,
},
});
if (existingRetrivalConfig) {
Expand Down Expand Up @@ -611,7 +611,7 @@ export async function _createAgentDataSourcesConfigData(
tagsNotIn: dsConfig.filter.tags?.not,
parentsIn: dsConfig.filter.parents?.in,
parentsNotIn: dsConfig.filter.parents?.not,
retrievalId: retrievalConfigId,
retrievalConfigId: retrievalConfigId,
},
{ transaction: t }
);
Expand Down
2 changes: 1 addition & 1 deletion front/lib/api/assistant/generation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { CoreAPI } from "@app/lib/core_api";
import { Err, Ok, Result } from "@app/lib/result";
import logger from "@app/logger/logger";
import { isRetrievalActionType } from "@app/types/assistant/actions/retrieval";
import { AgentConfigurationType } from "@app/types/assistant/agent";
import { AgentConfigurationType } from "@app/types/assistant/configuration";
import {
AgentMessageType,
ConversationType,
Expand Down
16 changes: 10 additions & 6 deletions front/lib/models/assistant/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@ export class AgentConfiguration extends Model<
declare scope: AgentConfigurationScope;

declare workspaceId: ForeignKey<Workspace["id"]> | null; // null = it's a global agent
declare generationId: ForeignKey<AgentGenerationConfiguration["id"]> | null;
declare retrievalId: ForeignKey<AgentRetrievalConfiguration["id"]> | null;
declare generationConfigId: ForeignKey<
AgentGenerationConfiguration["id"]
> | null;
declare retrievalConfigId: ForeignKey<
AgentRetrievalConfiguration["id"]
> | null;
}
AgentConfiguration.init(
{
Expand Down Expand Up @@ -262,7 +266,7 @@ export class AgentDataSourceConfiguration extends Model<
declare parentsNotIn: string[] | null;

declare dataSourceId: ForeignKey<DataSource["id"]>;
declare retrievalId: ForeignKey<AgentRetrievalConfiguration["id"]>;
declare retrievalConfigId: ForeignKey<AgentRetrievalConfiguration["id"]>;
}
AgentDataSourceConfiguration.init(
{
Expand Down Expand Up @@ -328,18 +332,18 @@ Workspace.hasMany(AgentConfiguration, {

// Agent config <> Generation config
AgentGenerationConfiguration.hasOne(AgentConfiguration, {
foreignKey: { name: "generationId", allowNull: true }, // null = no generation set for this Agent
foreignKey: { name: "generationConfigId", allowNull: true }, // null = no generation set for this Agent
onDelete: "CASCADE",
});
// Agent config <> Retrieval config
AgentRetrievalConfiguration.hasOne(AgentConfiguration, {
foreignKey: { name: "retrievalId", allowNull: true }, // null = no retrieval action set for this Agent
foreignKey: { name: "retrievalConfigId", allowNull: true }, // null = no retrieval action set for this Agent
onDelete: "CASCADE",
});

// Retrieval config <> Data source config
AgentRetrievalConfiguration.hasOne(AgentDataSourceConfiguration, {
foreignKey: { name: "retrievalId", allowNull: false },
foreignKey: { name: "retrievalConfigId", allowNull: false },
onDelete: "CASCADE",
});

Expand Down
32 changes: 31 additions & 1 deletion front/types/assistant/actions/retrieval.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ModelId } from "@app/lib/databases";
import { AgentDataSourceConfigurationType } from "@app/types/assistant/configuration";
import { AgentActionConfigurationType } from "@app/types/assistant/configuration";
import { AgentActionType } from "@app/types/assistant/conversation";

export type TimeframeUnit = "hour" | "day" | "week" | "month" | "year";
Expand All @@ -21,6 +21,36 @@ export function isTimeFrame(arg: RetrievalTimeframe): arg is TimeFrame {
);
}

/**
* Retrieval Action config
*/
export type RetrievalConfigurationType = {
id: ModelId;

type: "retrieval_configuration";
dataSources: AgentDataSourceConfigurationType[];
query: RetrievalQuery;
relativeTimeFrame: RetrievalTimeframe;
topK: number;
};
export function isRetrievalConfiguration(
arg: AgentActionConfigurationType | null
): arg is RetrievalConfigurationType {
return arg !== null && arg.type && arg.type === "retrieval_configuration";
}

/**
* Datasources config for Retrieval Action
*/
export type AgentDataSourceConfigurationType = {
workspaceId: string; // need sId to talk with Core (external id)
dataSourceId: string; // need Datasource.name to talk with Core (external id)
filter: {
tags: { in: string[]; not: string[] } | null;
parents: { in: string[]; not: string[] } | null;
};
};

// Retrieval specifies a list of data sources (with possible parent / tags filtering, possible "all"
// data sources), a query ("auto" generated by the model "none", no query, `TemplatedQuery`, fixed
// query), a relative time frame ("auto" generated by the model, "none" no time filtering
Expand Down
21 changes: 0 additions & 21 deletions front/types/assistant/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,6 @@
* Agent Action
*/

// Each AgentActionConfigurationType is capable of generating this type at runtime to specify which
// inputs should be generated by the model. As an example, to run the retrieval action for which the
// `relativeTimeFrame` has been specified in the configuration but for which the `query` is "auto",
// it would generate:
//
// ```
// { inputs: [{ name: "query", description: "...", type: "string" }]
// ```
//
// The params generator model for this action would be tasked to generate that query. If the
// retrieval configuration sets `relativeTimeFrame` to "auto" as well we would get:
//
// ```
// {
// inputs: [
// { name: "query", description: "...", type: "string" },
// { name: "relativeTimeFrame", description: "...", type: "string" },
// ]
// }
// ```

export type AgentActionSpecification = {
name: string;
description: string;
Expand Down
51 changes: 21 additions & 30 deletions front/types/assistant/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ModelId } from "@app/lib/databases";
import {
RetrievalConfigurationType,
RetrievalQuery,
RetrievalTimeframe,
} from "@app/types/assistant/actions/retrieval";
Expand Down Expand Up @@ -33,34 +34,24 @@ export type AgentGenerationConfigurationType = {
/**
* Action > Retrieval
*/
// Each AgentActionConfigurationType is capable of generating this type at runtime to specify which
// inputs should be generated by the model. As an example, to run the retrieval action for which the
// `relativeTimeFrame` has been specified in the configuration but for which the `query` is "auto",
// it would generate:
//
// ```
// { inputs: [{ name: "query", description: "...", type: "string" }]
// ```
//
// The params generator model for this action would be tasked to generate that query. If the
// retrieval configuration sets `relativeTimeFrame` to "auto" as well we would get:
//
// ```
// {
// inputs: [
// { name: "query", description: "...", type: "string" },
// { name: "relativeTimeFrame", description: "...", type: "string" },
// ]
// }
// ```
export type AgentActionConfigurationType = RetrievalConfigurationType;

/**
* Retrieval Action config
*/
export type RetrievalConfigurationType = {
id: ModelId;

type: "retrieval_configuration";
dataSources: AgentDataSourceConfigurationType[];
query: RetrievalQuery;
relativeTimeFrame: RetrievalTimeframe;
topK: number;
};
export function isRetrievalConfiguration(
arg: AgentActionConfigurationType | null
): arg is RetrievalConfigurationType {
return arg !== null && arg.type && arg.type === "retrieval_configuration";
}

/**
* Datasources config for Retrieval Action
*/
export type AgentDataSourceConfigurationType = {
workspaceId: string; // need sId to talk with Core (external id)
dataSourceId: string; // need Datasource.name to talk with Core (external id)
filter: {
tags: { in: string[]; not: string[] } | null;
parents: { in: string[]; not: string[] } | null;
};
};

0 comments on commit b49e22d

Please sign in to comment.