feat: add some ai stuff

This commit is contained in:
2023-11-06 02:24:50 +01:00
parent dfd5e79246
commit acf086da13
23 changed files with 2022 additions and 54 deletions

View File

@ -0,0 +1,22 @@
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;
}

View File

@ -0,0 +1,83 @@
import { writable as internal, type Writable } from 'svelte/store'
declare type Updater<T> = (value: T) => T;
declare type StoreDict<T> = { [key: string]: Writable<T> }
/* eslint-disable @typescript-eslint/no-explicit-any */
interface Stores {
local: StoreDict<any>,
session: StoreDict<any>,
}
const stores: Stores = {
local: {},
session: {}
}
export interface Serializer<T> {
parse(text: string): T
stringify(object: T): string
}
export type StorageType = 'local' | 'session'
export interface Options<T> {
serializer?: Serializer<T>
storage?: StorageType
}
function getStorage(type: StorageType) {
return type === 'local' ? localStorage : sessionStorage
}
export function persisted<T>(key: string, initialValue: T, options?: Options<T>): Writable<T> {
const serializer = options?.serializer ?? JSON
const storageType = options?.storage ?? 'local'
const browser = typeof (window) !== 'undefined' && typeof (document) !== 'undefined'
const storage = browser ? getStorage(storageType) : null
function updateStorage(key: string, value: T) {
storage?.setItem(key, serializer.stringify(value))
}
if (!stores[storageType][key]) {
const store = internal(initialValue, (set) => {
const json = storage?.getItem(key)
if (json) {
set(<T>serializer.parse(json))
}
if (browser && storageType == 'local') {
const handleStorage = (event: StorageEvent) => {
if (event.key === key)
set(event.newValue ? serializer.parse(event.newValue) : null)
}
window.addEventListener("storage", handleStorage)
return () => window.removeEventListener("storage", handleStorage)
}
})
const { subscribe, set } = store
stores[storageType][key] = {
set(value: T) {
updateStorage(key, value)
set(value)
},
update(callback: Updater<T>) {
return store.update((last) => {
const value = callback(last)
updateStorage(key, value)
return value
})
},
subscribe
}
}
return stores[storageType][key]
}

20
src/lib/helpers/s3.ts Normal file
View File

@ -0,0 +1,20 @@
import Minio from 'minio'
import { S3_ENDPOINT_URL, S3_SECRET_ACCESS_KEY, S3_BUCKET_NAME, S3_ACCESS_KEY } from "$env/static/private"
const minioClient = new Minio.Client({
endPoint: S3_ENDPOINT_URL,
port: 80,
useSSL: false,
accessKey: S3_ACCESS_KEY,
secretKey: S3_SECRET_ACCESS_KEY,
})
export function putObject(fileName: string, content: Buffer, metadata: Minio.ItemBucketMetadata = {}) {
return minioClient.putObject(S3_BUCKET_NAME, fileName, content, metadata);
}
export function listBuckets() {
return minioClient.listBuckets();
}

View File

@ -0,0 +1,48 @@
import { DREAM_API_KEY } from "$env/static/private";
const path =
"https://api.stability.ai/v1/generation/stable-diffusion-xl-1024-v1-0/text-to-image";
const headers = {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: DREAM_API_KEY,
};
export async function generateImage(prompt: string, negativePrompt: string) {
const body = {
steps: 10,
width: 832,
height: 1216,
seed: Math.floor(Math.random() * 100000),
cfg_scale: 5,
samples: 1,
style_preset: "fantasy-art",
text_prompts: [
{
"text": prompt,
"weight": 1
},
{
"text": negativePrompt,
"weight": -1
}
],
};
const response = await fetch(
path,
{
headers,
method: "POST",
body: JSON.stringify(body),
}
);
if (!response.ok) {
throw new Error(`Non-200 response: ${await response.text()}`)
}
const responseJSON = await response.json();
return responseJSON.artifacts[0];
}