mirror of
https://github.com/hpware/news-analyze.git
synced 2025-06-23 13:04:23 +00:00
Made a simple system for custom groq apis.
This commit is contained in:
parent
b356afe766
commit
a4a522974a
5 changed files with 64 additions and 33 deletions
|
@ -1,30 +0,0 @@
|
|||
import sql from "~/server/components/postgres";
|
||||
export default defineEventHandler(async (event) => {
|
||||
if (event.method !== "POST") {
|
||||
return {
|
||||
error: "ERR_METHOD_NOT_ALLOWED",
|
||||
};
|
||||
}
|
||||
const body = readBody(event);
|
||||
if (!body.apiKey) {
|
||||
return {
|
||||
error: "ERR_API_KEY_REQUIRED",
|
||||
};
|
||||
}
|
||||
const readUserToken = getCookie(event, "token");
|
||||
if (!readUserToken) {
|
||||
return {
|
||||
error: "ERR_NOT_USER_LOGIN",
|
||||
};
|
||||
}
|
||||
const verifyUserToken = await sql`
|
||||
SELECT * FROM usertokens
|
||||
where token=${readUserToken}
|
||||
`;
|
||||
if (verifyUserToken.length === 0) {
|
||||
return {
|
||||
error: "ERR_NOT_USER_LOGIN",
|
||||
requested_action: "LOGOUT_USER",
|
||||
};
|
||||
}
|
||||
});
|
|
@ -1,19 +1,32 @@
|
|||
import { Groq } from "groq-sdk";
|
||||
import sql from "~/server/components/postgres";
|
||||
import { checkIfUserHasCustomGroqKey } from "~/server/components/customgroqsystem";
|
||||
|
||||
const groq = new Groq();
|
||||
const groq = new Groq({
|
||||
apiKey: process.env.GROQ_API_KEY,
|
||||
});
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const host = getRequestHost(event);
|
||||
const protocol = getRequestProtocol(event);
|
||||
const slug = getRouterParam(event, "slug");
|
||||
const userToken = getCookie(event, "token") || "";
|
||||
const doesTheUserHasACustomGroqApiAndWhatIsIt =
|
||||
await checkIfUserHasCustomGroqKey(userToken);
|
||||
let groqClient = groq;
|
||||
if (doesTheUserHasACustomGroqApiAndWhatIsIt.status === true) {
|
||||
groqClient = new Groq({
|
||||
apiKey: doesTheUserHasACustomGroqApiAndWhatIsIt.customApi,
|
||||
});
|
||||
}
|
||||
|
||||
const query = getQuery(event);
|
||||
const locale = query.locale;
|
||||
const buildURL = protocol + "://" + host + "/api/news/get/lt/" + slug;
|
||||
const data = await fetch(buildURL);
|
||||
const fetchNewsArticle = await data.json();
|
||||
console.log(locale);
|
||||
const chatCompletion = await groq.chat.completions.create({
|
||||
const chatCompletion = await groqClient.chat.completions.create({
|
||||
messages: [
|
||||
{
|
||||
role: "user",
|
||||
|
|
1
server/api/user/sendUserInfo.ts
Normal file
1
server/api/user/sendUserInfo.ts
Normal file
|
@ -0,0 +1 @@
|
|||
export default defineEventHandler(async (event) => {});
|
33
server/components/customgroqsystem.ts
Normal file
33
server/components/customgroqsystem.ts
Normal file
|
@ -0,0 +1,33 @@
|
|||
import sql from "./postgres";
|
||||
|
||||
export async function checkIfUserHasCustomGroqKey(token?: string) {
|
||||
if (!token) {
|
||||
return {
|
||||
status: false,
|
||||
customApi: "",
|
||||
};
|
||||
}
|
||||
const checkRealToken = await sql`
|
||||
select * from usertokens
|
||||
where tokens=${token}
|
||||
`;
|
||||
if (checkRealToken.length === 0) {
|
||||
return {
|
||||
status: false,
|
||||
customApi: "",
|
||||
};
|
||||
}
|
||||
const fetchUserToken = await sql`
|
||||
select groq_api_key from user_other_data
|
||||
where user=${checkRealToken[0].username}`;
|
||||
if (fetchUserToken.length === 0) {
|
||||
return {
|
||||
status: false,
|
||||
customApi: "",
|
||||
};
|
||||
}
|
||||
return {
|
||||
status: true,
|
||||
customAPi: fetchUserToken[0].groq_api_key,
|
||||
};
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue