Skip to content

Commit

Permalink
Assistant: Lib function createAgentConfiguration
Browse files Browse the repository at this point in the history
  • Loading branch information
PopDaph committed Sep 7, 2023
1 parent f4160e0 commit b83599a
Showing 1 changed file with 140 additions and 9 deletions.
149 changes: 140 additions & 9 deletions front/lib/api/assistant/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@ import {
} from "@app/lib/actions/registry";
import { runAction } from "@app/lib/actions/server";
import { Authenticator } from "@app/lib/auth";
import { front_sequelize } from "@app/lib/databases";
import { AgentRetrievalConfiguration } from "@app/lib/models/assistant/actions/retrieval";
import {
AgentConfiguration,
AgentGenerationConfiguration,
} from "@app/lib/models/assistant/agent";
import { Err, Ok, Result } from "@app/lib/result";
import { generateModelSId } from "@app/lib/utils";
import { TimeFrame } from "@app/types/assistant/actions/retrieval";
import {
AgentActionConfigurationType,
AgentActionSpecification,
Expand All @@ -29,6 +36,74 @@ import { renderConversationForModel } from "./conversation";
* Agent configuration.
*/

function _buildAgentActionConfigurationType(
action: AgentRetrievalConfiguration
): AgentActionConfigurationType {
let timeframe: "auto" | "none" | TimeFrame = "auto";
if (
action.relativeTimeFrame === "custom" &&
action.relativeTimeFrameDuration &&
action.relativeTimeFrameUnit
) {
timeframe = {
duration: action.relativeTimeFrameDuration,
unit: action.relativeTimeFrameUnit,
};
} else if (action.relativeTimeFrame === "none") {
timeframe = action.relativeTimeFrame;
}

let query: "auto" | "none" | { template: string } = "auto";
if (action.query === "templated" && action.queryTemplate) {
query = {
template: action.queryTemplate,
};
} else if (action.query === "none") {
query = "none";
}

return {
query: query,
relativeTimeFrame: timeframe,
topK: action.topK,
type: "retrieval_configuration",
dataSources: "all",
};
}

function _buildAgentGenerationConfigurationType(
generation: AgentGenerationConfiguration
): AgentGenerationConfigurationType {
return {
prompt: generation.prompt,
model: {
providerId: generation.modelProvider,
modelId: generation.modelId,
},
};
}

function _getAgentConfigurationType({
agent,
action,
generation,
}: {
agent: AgentConfiguration;
action: AgentRetrievalConfiguration | null;
generation: AgentGenerationConfiguration | null;
}): AgentConfigurationType {
return {
sId: agent.sId,
name: agent.name,
pictureUrl: agent.pictureUrl,
status: agent.status,
action: action ? _buildAgentActionConfigurationType(action) : null,
generation: generation
? _buildAgentGenerationConfigurationType(generation)
: null,
};
}

export async function createAgentConfiguration(
auth: Authenticator,
{
Expand All @@ -42,15 +117,71 @@ export async function createAgentConfiguration(
action?: AgentActionConfigurationType;
generation?: AgentGenerationConfigurationType;
}
): Promise<AgentConfigurationType> {
return {
sId: generateModelSId(),
name,
pictureUrl: pictureUrl ?? null,
status: "active",
action: action ?? null,
generation: generation ?? null,
};
): Promise<AgentConfigurationType | void> {
const owner = auth.workspace();

if (!owner) {
return;
}

return await front_sequelize.transaction(async (t) => {
const agentConfigRow = await AgentConfiguration.create(
{
sId: generateModelSId(),
status: "active",
name: name,
pictureUrl: pictureUrl ?? null,
scope: "workspace",
workspaceId: owner.id,
},
{ transaction: t }
);

let agentGenerationConfigRow: AgentGenerationConfiguration | null = null;
let agentActionConfigRow: AgentRetrievalConfiguration | null = null;

if (generation) {
agentGenerationConfigRow = await AgentGenerationConfiguration.create(
{
prompt: generation.prompt,
modelProvider: generation.model.providerId,
modelId: generation.model.modelId,
agentId: agentConfigRow.id,
},
{ transaction: t }
);
}
if (action) {
agentActionConfigRow = await AgentRetrievalConfiguration.create(
{
query: typeof action.query === "object" ? "templated" : action.query,
queryTemplate:
typeof action.query === "object" ? action.query.template : null,
relativeTimeFrame:
typeof action.relativeTimeFrame === "object"
? "custom"
: action.relativeTimeFrame,
relativeTimeFrameDuration:
typeof action.relativeTimeFrame === "object"
? action.relativeTimeFrame.duration
: null,
relativeTimeFrameUnit:
typeof action.relativeTimeFrame === "object"
? action.relativeTimeFrame.unit
: null,
topK: action.topK,
agentId: agentConfigRow.id,
},
{ transaction: t }
);
}

return _getAgentConfigurationType({
agent: agentConfigRow,
action: agentActionConfigRow,
generation: agentGenerationConfigRow,
});
});
}

export async function updateAgentConfiguration(
Expand Down

0 comments on commit b83599a

Please sign in to comment.