feat: add some stuff
This commit is contained in:
@ -1,22 +0,0 @@
|
||||
import OpenAI from 'openai';
|
||||
import { OPENAI_API_KEY } from '$env/static/private';
|
||||
|
||||
const openai = new OpenAI({
|
||||
apiKey: OPENAI_API_KEY,
|
||||
});
|
||||
|
||||
export async function chat(prompt: string) {
|
||||
|
||||
const chatCompletion = await openai.chat.completions.create({
|
||||
model: "gpt-4",
|
||||
messages: [
|
||||
{
|
||||
"role": "system",
|
||||
"content": prompt,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const res = chatCompletion.choices[0].message.content;
|
||||
return res;
|
||||
}
|
40
src/lib/helpers/openai.ts
Normal file
40
src/lib/helpers/openai.ts
Normal file
@ -0,0 +1,40 @@
|
||||
import OpenAI from 'openai';
|
||||
import { OPENAI_API_KEY } from '$env/static/private';
|
||||
|
||||
const openai = new OpenAI({
|
||||
apiKey: OPENAI_API_KEY,
|
||||
});
|
||||
|
||||
function processChatGptResult(resultString: string) {
|
||||
// Split the string by newline
|
||||
const lines: string[] = resultString.split('\n');
|
||||
|
||||
// Remove enumeration at the beginning of each line
|
||||
const processedLines: string[] = lines.map(line => line.replace(/^\s*[\d.-]+\s*/, ''));
|
||||
|
||||
return processedLines.filter(line => line.length > 0);
|
||||
}
|
||||
|
||||
export async function chat(prompt: string, { isList, temperature }: { isList?: boolean, temperature?: number } = {}) {
|
||||
|
||||
const chatCompletion = await openai.chat.completions.create({
|
||||
model: "gpt-4",
|
||||
temperature: temperature ?? 0.9,
|
||||
messages: [
|
||||
{
|
||||
"role": "system",
|
||||
"content": prompt,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const res = chatCompletion.choices[0].message.content;
|
||||
|
||||
if (res && isList) return processChatGptResult(res);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
export function image(prompt: string) {
|
||||
return openai.images.generate({ model: "dall-e-3", prompt });
|
||||
}
|
17
src/lib/helpers/pb.ts
Normal file
17
src/lib/helpers/pb.ts
Normal file
@ -0,0 +1,17 @@
|
||||
import Pocketbase from "pocketbase"
|
||||
import { POCKETBASE_URL } from "$env/dynamic/private"
|
||||
|
||||
const pb = new Pocketbase(POCKETBASE_URL || "http://localhost:8090");
|
||||
|
||||
export function createPerson({ name, confidence, portrait, portrait_public, noble_name, hair_color, hair_type, hair_length }: { name: string, portrait: string, portrait_public: boolean, hair_type: string, hair_length: string, hair_color: string, confidence: number, noble_name: string }) {
|
||||
return pb.collection("invites").create({
|
||||
name,
|
||||
confidence,
|
||||
portrait,
|
||||
portrait_public,
|
||||
noble_name,
|
||||
hair_type,
|
||||
hair_length,
|
||||
hair_color
|
||||
})
|
||||
}
|
114
src/lib/helpers/sheets.ts
Normal file
114
src/lib/helpers/sheets.ts
Normal file
@ -0,0 +1,114 @@
|
||||
import { env } from "$env/dynamic/private";
|
||||
import { google } from 'googleapis';
|
||||
const { GOOGLE_SHEET_ID, GOOGLE_APPLICATION_CREDENTIALS } = env;
|
||||
|
||||
const auth = GOOGLE_APPLICATION_CREDENTIALS && new google.auth.GoogleAuth({
|
||||
keyFile: GOOGLE_APPLICATION_CREDENTIALS, //the key file
|
||||
//url to spreadsheets API
|
||||
scopes: 'https://www.googleapis.com/auth/spreadsheets'
|
||||
});
|
||||
|
||||
|
||||
const googleApi = (async () => {
|
||||
|
||||
if (!auth) return;
|
||||
|
||||
const authClientObject = await auth.getClient();
|
||||
|
||||
const googleSheetsInstance = google.sheets({ version: 'v4', auth: authClientObject });
|
||||
|
||||
return googleSheetsInstance;
|
||||
})();
|
||||
|
||||
async function getSheet() {
|
||||
return await (
|
||||
await googleApi
|
||||
).spreadsheets.values.get({
|
||||
auth, //auth object
|
||||
spreadsheetId: GOOGLE_SHEET_ID, // spreadsheet id
|
||||
range: 'Gäste' //range of cells to read from.
|
||||
});
|
||||
}
|
||||
|
||||
function parseBoolean(o, key: string) {
|
||||
if (key in o) {
|
||||
o[key] = o[key].length ? !!parseInt(o[key]) : o[key];
|
||||
}
|
||||
}
|
||||
|
||||
function parseNumber(o, key: string) {
|
||||
if (key in o) {
|
||||
o[key] = o[key].length ? parseInt(o[key]) : null;
|
||||
}
|
||||
}
|
||||
|
||||
async function _getData(): Promise<{ Name: string; Alter: number }[]> {
|
||||
const raw = await getSheet();
|
||||
|
||||
const _rows = raw.data.values;
|
||||
|
||||
if (!_rows) return [];
|
||||
|
||||
const [headers, ...rows] = _rows;
|
||||
|
||||
function parseRow(row: string[]) {
|
||||
const o: {
|
||||
schlafen?: boolean;
|
||||
frauen?: boolean;
|
||||
single?: boolean;
|
||||
männer?: boolean;
|
||||
veggies?: boolean;
|
||||
Name: string;
|
||||
Alter: number;
|
||||
} = {
|
||||
Name: '',
|
||||
Alter: 0
|
||||
};
|
||||
|
||||
row.forEach((v, i) => {
|
||||
o[headers[i]] = v;
|
||||
});
|
||||
|
||||
parseBoolean(o, 'Schlafen');
|
||||
parseBoolean(o, 'Frauen');
|
||||
parseBoolean(o, 'Single');
|
||||
parseBoolean(o, 'Männer');
|
||||
parseBoolean(o, 'Veggies');
|
||||
|
||||
parseNumber(o, 'Alter');
|
||||
|
||||
return o;
|
||||
}
|
||||
|
||||
return rows.map((r: string[]) => parseRow(r));
|
||||
}
|
||||
|
||||
let lastGetUpdate: number;
|
||||
let cacheData: { Name: string; Alter: number }[];
|
||||
export async function getData() {
|
||||
if (!lastGetUpdate || Date.now() - 10000 > lastGetUpdate) {
|
||||
cacheData = await _getData();
|
||||
lastGetUpdate = Date.now();
|
||||
}
|
||||
|
||||
return cacheData;
|
||||
}
|
||||
|
||||
export async function addPerson({ name, confidence, noble_name }: { name: string, confidence: number, noble_name: string }): Promise<unknown> {
|
||||
|
||||
const api = await googleApi;
|
||||
|
||||
if (!api) return;
|
||||
|
||||
console.log({ name, confidence, noble_name });
|
||||
|
||||
return api.spreadsheets.values.append({
|
||||
auth, //auth object
|
||||
spreadsheetId: GOOGLE_SHEET_ID, // spreadsheet id
|
||||
range: 'Gäste', //range of cells to read from.
|
||||
valueInputOption: 'RAW',
|
||||
resource: {
|
||||
values: [[name, `${Number(confidence).toFixed(2).replace(".", ",")}%`, '', '', '', '', '', '', '', noble_name]]
|
||||
}
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user