Skip to content

Commit

Permalink
Fetch all AgentDSConfigs at once for GET /agent_configurations (#2821)
Browse files Browse the repository at this point in the history
* Fetch all AgentDSConfigs at once for GET /agent_configurations

* Fix var name
  • Loading branch information
lasryaric authored Dec 11, 2023
1 parent e357d2d commit f607383
Showing 1 changed file with 54 additions and 19 deletions.
73 changes: 54 additions & 19 deletions front/lib/api/assistant/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ import { generateModelSId } from "@app/lib/utils";
export async function getAgentConfiguration(
auth: Authenticator,
agentId: string,
agentConfiguration?: AgentConfiguration
preFetchedAgentConfiguration?: AgentConfiguration,
preFetchedDataSourceConfigurations?: AgentDataSourceConfiguration[]
): Promise<AgentConfigurationType | null> {
const owner = auth.workspace();
if (!owner || !auth.isUser()) {
Expand All @@ -68,7 +69,7 @@ export async function getAgentConfiguration(
}
const user = auth.user();
const agent =
agentConfiguration ??
preFetchedAgentConfiguration ??
(await AgentConfiguration.findOne({
where: {
sId: agentId,
Expand Down Expand Up @@ -112,23 +113,26 @@ export async function getAgentConfiguration(
| null = null;

if (agent.retrievalConfigurationId) {
const dataSourcesConfig = await AgentDataSourceConfiguration.findAll({
where: {
retrievalConfigurationId: agent.retrievalConfiguration?.id,
},
include: [
{
model: DataSource,
as: "dataSource",
include: [
{
model: Workspace,
as: "workspace",
const dataSourcesConfig =
preFetchedDataSourceConfigurations !== undefined
? preFetchedDataSourceConfigurations
: await AgentDataSourceConfiguration.findAll({
where: {
retrievalConfigurationId: agent.retrievalConfiguration?.id,
},
],
},
],
});
include: [
{
model: DataSource,
as: "dataSource",
include: [
{
model: Workspace,
as: "workspace",
},
],
},
],
});
const retrievalConfig = agent.retrievalConfiguration;

if (!retrievalConfig) {
Expand Down Expand Up @@ -293,9 +297,40 @@ export async function getAgentConfigurations(
agentsSequelizeQuery: FindOptions
) => {
const agents = await AgentConfiguration.findAll(agentsSequelizeQuery);
const retrievalConfigurationIds = agents
.map((a) => a.retrievalConfigurationId)
.flatMap((id) => (id ? [id] : []));
const dataSourcesConfig = await AgentDataSourceConfiguration.findAll({
where: {
retrievalConfigurationId: { [Op.in]: retrievalConfigurationIds },
},
include: [
{
model: DataSource,
as: "dataSource",
include: [
{
model: Workspace,
as: "workspace",
},
],
},
],
});

return (
await Promise.all(
agents.map((a) => getAgentConfiguration(auth, a.sId, a))
agents.map((a) =>
getAgentConfiguration(
auth,
a.sId,
a,
dataSourcesConfig.filter(
(dsc) =>
dsc.retrievalConfigurationId === a.retrievalConfigurationId
)
)
)
)
).filter((a) => a !== null) as AgentConfigurationType[];
};
Expand Down

0 comments on commit f607383

Please sign in to comment.