Skip to content

Commit b2a9f46

Browse files
authored
又双叒叕修改了llm验证逻辑 (#1777)
* added RL functions * removed redis * modified llm verify logic * modified verification logic
1 parent 73499da commit b2a9f46

4 files changed

Lines changed: 181 additions & 284 deletions

File tree

src/hasura/llm.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,71 @@ export const update_llm_usage_by_uuid = async (
8383
return query.update_llm_usage_by_pk;
8484
};
8585

86+
export const upsert_llm_usage_by_uuids = async (
87+
uuids: string[],
88+
token_limit: number = 0,
89+
) => {
90+
const uniqueUuids = Array.from(new Set(uuids)).filter(Boolean);
91+
if (uniqueUuids.length === 0) {
92+
return 0;
93+
}
94+
95+
const query: any = await client.request(
96+
gql`
97+
mutation UpsertLlmUsage($objects: [llm_usage_insert_input!]!) {
98+
insert_llm_usage(
99+
objects: $objects
100+
on_conflict: { constraint: llm_usage_pkey, update_columns: [] }
101+
) {
102+
affected_rows
103+
}
104+
}
105+
`,
106+
{
107+
objects: uniqueUuids.map((uuid) => ({
108+
uuid,
109+
token_limit,
110+
total_tokens_used: 0,
111+
})),
112+
},
113+
);
114+
115+
return query.insert_llm_usage?.affected_rows ?? 0;
116+
};
117+
118+
export const upsert_llm_usage_by_uuid = async (
119+
uuid: string,
120+
token_limit: number = 0,
121+
) => {
122+
return upsert_llm_usage_by_uuids([uuid], token_limit);
123+
};
124+
125+
export const sync_rl_registered_users_to_llm_usage = async (
126+
token_limit: number = 0,
127+
) => {
128+
const query: any = await client.request(
129+
gql`
130+
query GetRLRegisteredUserUuids {
131+
contest_team_member(
132+
where: { contest_team: { contest: { name: { _ilike: "RL%" } } } }
133+
) {
134+
user_uuid
135+
}
136+
}
137+
`,
138+
);
139+
140+
const uuids = (query.contest_team_member || []).map(
141+
(member: any) => member.user_uuid,
142+
);
143+
const affectedRows = await upsert_llm_usage_by_uuids(uuids, token_limit);
144+
145+
return {
146+
totalRegisteredUsers: Array.from(new Set(uuids)).length,
147+
insertedUsers: affectedRows,
148+
};
149+
};
150+
86151
export const get_user_llm_usage = async (student_no: string) => {
87152
const query: any = await client.request(
88153
gql`

src/routes/competition.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as utils from "../helpers/utils";
77
import * as COS from "../helpers/cos";
88
import * as ContConf from "../configs/contest";
99
import * as ContHasFunc from "../hasura/contest";
10+
import * as LlmHasFunc from "../hasura/llm";
1011

1112
const router = express.Router();
1213

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

1865+
router.post(
1866+
"/sync_RL_llm_usage",
1867+
authenticate(["root", "counselor"]),
1868+
async (req, res) => {
1869+
try {
1870+
const token_limit =
1871+
typeof req.body.token_limit === "number" ? req.body.token_limit : 0;
1872+
const result =
1873+
await LlmHasFunc.sync_rl_registered_users_to_llm_usage(token_limit);
1874+
1875+
return res.status(200).json({
1876+
message: "200 OK: RL registered users synced to llm_usage",
1877+
...result,
1878+
});
1879+
} catch (err: any) {
1880+
console.error(err);
1881+
return res.status(500).json({
1882+
error: "500 Internal Server Error",
1883+
message: err.message,
1884+
});
1885+
}
1886+
},
1887+
);
1888+
18641889
export default router;

0 commit comments

Comments
 (0)