Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Mansive committed Jan 8, 2024
1 parent ae6084d commit 14128ae
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 111 deletions.
47 changes: 11 additions & 36 deletions src/app/api/search/route.tsx
Original file line number Diff line number Diff line change
@@ -1,58 +1,33 @@
import { getXataClient } from "@/lib/xata";
import { vectorSearch } from "@/lib/search";
import { NextRequest, NextResponse } from "next/server";

export const runtime = "edge";

const xata = getXataClient();

export async function GET(request: NextRequest) {
const searchParams: string = decodeURIComponent(
request.nextUrl.searchParams.toString()
);
if (searchParams.length > 128) {
const searchParams: string | null = request.nextUrl.searchParams.get("query");
let query: string;
if (searchParams === null) {
query = "";
} else {
query = searchParams;
}
if (query.length > 128) {
return NextResponse.json(
{ message: "Search entry is too long" },
{ status: 403 }
);
}

try {
// const results = await xata.db.Books.search(searchParams, {
// target: ["title"],
// prefix: "phrase",
// fuzziness: 0,
// page: { size: 35, offset: 0 },
// });

// Generate embeddings
const embeddings = await fetch("https://api.embaas.io/v1/embeddings/", {
method: "POST",
body: JSON.stringify({
texts: ["query: " + searchParams],
model: "multilingual-e5-large",
}),
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + process.env.EMBAAS_API_KEY,
},
}).then((response) => response.json());

const results = await xata.db.Books.vectorSearch(
"embeddings",
embeddings["data"][0]["embedding"],
{
similarityFunction: "cosineSimilarity",
size: 35,
}
);
const results = await vectorSearch(query);

// Reduce network payload size for users
const records = JSON.parse(JSON.stringify(results))["records"];
records.forEach((book: Record<string, any>) => {
delete book.id;
delete book.embeddings;
//delete book.xata;
console.log(book);
//console.log(book);
});

return NextResponse.json({ records });
Expand Down
4 changes: 2 additions & 2 deletions src/components/Card/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './Card';
export { default } from './Card';
export * from "./Card";
export { default } from "./Card";
8 changes: 4 additions & 4 deletions src/components/NetworkShader/NetworkMaterial.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NoBlending, ShaderMaterial, Vector2 } from "three";
import { ShaderMaterialProps } from "@react-three/fiber";
import { NoBlending, ShaderMaterial, Vector2 } from "three";

declare global {
namespace JSX {
Expand All @@ -25,7 +25,7 @@ const vertexShader = `
const fragmentShader = `
uniform float uTime;
uniform vec2 uResolution;
varying vec2 vUv;
// sine randomness function from The Book Of Shaders
Expand All @@ -47,9 +47,9 @@ const fragmentShader = `
float pointSegmentDistance(vec2 p, vec2 a, vec2 b) {
vec2 ab = b - a;
vec2 ap = p - a;
float d = clamp(dot(ap, ab) / dot(ab, ab), 0.0, 1.0);
return length(ap - ab * d);
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/NetworkShader/NetworkShader.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use client";
import { Suspense, useEffect, useRef } from "react";
import { OrthographicCamera } from "@react-three/drei";
import { Canvas, extend, useFrame } from "@react-three/fiber";
import { Suspense, useEffect, useRef } from "react";
import * as THREE from "three";

import NetworkMaterial from "./NetworkMaterial";
Expand Down
2 changes: 1 addition & 1 deletion src/components/SearchBar/SearchBar.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use client";
import { useState } from "react";
import { motion } from "framer-motion";
import { useState } from "react";

import styles from "./SearchBar.module.css";

Expand Down
1 change: 0 additions & 1 deletion src/components/Spinner/Spinner.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import React from "react";
import { Hexagon } from "react-feather";

import styles from "./Spinner.module.css";
Expand Down
66 changes: 0 additions & 66 deletions src/data/book-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,69 +5,3 @@ export interface Book {
size: number;
extension: string;
}

export const BOOKS = [
{
id: "rec_clo13k3ebat5c83jnr90",
title: "Kusuriya to Honshin no Kagami",
sources: ["Nyaa"],
size: 36.1,
extension: "rar",
},
{
id: "rec_clo14jbebat5c83jnr9g",
title: "[白石定規]魔女の旅々3",
sources: ["TMW Collections"],
size: 1.52,
extension: "epub",
},
{
id: "rec_clo0quijp1p3khg20umg",
title: "魔女の旅々",
sources: ["Anna's Archive", "Nyaa", "PeepoHappyBooks", "TMW Collections"],
size: 1.8,
extension: "azw3",
},
{
id: "rec_clo18m3ebat5c83jolqg",
title: "俺、ツインテールになります。4.5",
sources: ["PeepoHappyBooks", "Nyaa"],
size: 12.3,
extension: "epub",
},
{
id: "rec_clo0t7bebat5c83jnqog",
title: "灼眼のシャナXII",
sources: ["Anna's Archive", "Nyaa", "TMW Collections"],
size: 24.2,
extension: "epub",
},
{
id: "rec_clo151hk6ckak4tfv7g0",
title: "魔女の旅々6",
sources: ["PeepoHappyBooks"],
size: 12.56,
extension: "rar",
},
{
id: "rec_clo158ijp1p3khg20ut0",
title: "魔女の旅々10",
sources: ["Anna's Archive", "Nyaa", "PeepoHappyBooks", "TMW Collections"],
size: 2.62,
extension: "azw3",
},
{
id: "rec_clo15rijp1p3khg20utg",
title: "魔女の旅々8",
sources: ["Anna's Archive","Nyaa","PeepoHappyBooks","TMW Collections"],
size: 623.12,
extension: "rar",
},
{
id: "rec_clo1602jp1p3khg20uu0",
title: "魔女の旅々9",
sources: ["PeepoHappyBooks"],
size: 12.5,
extension: "kfx",
},
];
38 changes: 38 additions & 0 deletions src/lib/search.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { getXataClient } from "@/lib/xata";

const xata = getXataClient();

export async function search(query: string) {
return await xata.db.Books.search(query, {
target: ["title"],
prefix: "phrase",
fuzziness: 0,
page: { size: 35, offset: 0 },
});
}

export async function vectorSearch(query: string) {
// Is this how JavaScript experts write functions?
return await fetch("https://api.embaas.io/v1/embeddings/", {
method: "POST",
body: JSON.stringify({
texts: ["query: " + query],
model: "multilingual-e5-large",
}),
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + process.env.EMBAAS_API_KEY,
},
})
.then((response) => response.json())
.then((embeddings) =>
xata.db.Books.vectorSearch(
"embeddings",
embeddings["data"][0]["embedding"],
{
similarityFunction: "cosineSimilarity",
size: 35,
}
)
);
}

0 comments on commit 14128ae

Please sign in to comment.