Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions src/hasura/llm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,71 @@ export const update_llm_usage_by_uuid = async (
return query.update_llm_usage_by_pk;
};

export const upsert_llm_usage_by_uuids = async (
uuids: string[],
token_limit: number = 0,
) => {
const uniqueUuids = Array.from(new Set(uuids)).filter(Boolean);
if (uniqueUuids.length === 0) {
return 0;
}

const query: any = await client.request(
gql`
mutation UpsertLlmUsage($objects: [llm_usage_insert_input!]!) {
insert_llm_usage(
objects: $objects
on_conflict: { constraint: llm_usage_pkey, update_columns: [] }
) {
affected_rows
}
}
`,
{
objects: uniqueUuids.map((uuid) => ({
uuid,
token_limit,
total_tokens_used: 0,
})),
},
);

return query.insert_llm_usage?.affected_rows ?? 0;
};

export const upsert_llm_usage_by_uuid = async (
uuid: string,
token_limit: number = 0,
) => {
return upsert_llm_usage_by_uuids([uuid], token_limit);
};

export const sync_rl_registered_users_to_llm_usage = async (
token_limit: number = 0,
) => {
const query: any = await client.request(
gql`
query GetRLRegisteredUserUuids {
contest_team_member(
where: { contest_team: { contest: { name: { _ilike: "RL%" } } } }
) {
user_uuid
}
}
`,
);

const uuids = (query.contest_team_member || []).map(
(member: any) => member.user_uuid,
);
const affectedRows = await upsert_llm_usage_by_uuids(uuids, token_limit);

return {
totalRegisteredUsers: Array.from(new Set(uuids)).length,
insertedUsers: affectedRows,
};
};

export const get_user_llm_usage = async (student_no: string) => {
const query: any = await client.request(
gql`
Expand Down
25 changes: 25 additions & 0 deletions src/routes/competition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as utils from "../helpers/utils";
import * as COS from "../helpers/cos";
import * as ContConf from "../configs/contest";
import * as ContHasFunc from "../hasura/contest";
import * as LlmHasFunc from "../hasura/llm";

const router = express.Router();

Expand Down Expand Up @@ -1861,4 +1862,28 @@ router.post("/rl-score/update", authenticate(), async (req, res) => {
}
});

router.post(
"/sync_RL_llm_usage",
authenticate(["root", "counselor"]),
async (req, res) => {
try {
const token_limit =
typeof req.body.token_limit === "number" ? req.body.token_limit : 0;
const result =
await LlmHasFunc.sync_rl_registered_users_to_llm_usage(token_limit);

return res.status(200).json({
message: "200 OK: RL registered users synced to llm_usage",
...result,
});
} catch (err: any) {
console.error(err);
return res.status(500).json({
error: "500 Internal Server Error",
message: err.message,
});
}
},
);

export default router;
Loading
Loading