Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion vector-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"wrangler": "^3.50.0"
},
"dependencies": {
"@hono/zod-validator": "^0.2.1",
"@types/body-parser": "^1.19.5",
"@upstash/vector": "^1.0.7",
"body-parser": "^1.20.2",
Expand All @@ -29,6 +30,7 @@
"hono": "^4.2.4",
"langchain": "^0.1.34",
"openai": "^4.35.0",
"ts-node": "^10.9.2"
"ts-node": "^10.9.2",
"zod": "^3.23.7"
}
}
46 changes: 33 additions & 13 deletions vector-api/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 7 additions & 10 deletions vector-api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { Hono } from 'hono'
import { env } from 'hono/adapter'
import { cors } from 'hono/cors'
import { RecursiveCharacterTextSplitter } from 'langchain/text_splitter'
import { zValidator } from '@hono/zod-validator';
import { z } from 'zod'

const semanticSplitter = new RecursiveCharacterTextSplitter({
chunkSize: 25,
Expand All @@ -24,11 +26,11 @@ const app = new Hono()

app.use(cors())

app.post('/', async (c) => {
if (c.req.header('Content-Type') !== 'application/json') {
return c.json({ error: 'JSON body expected.' }, { status: 406 })
}
const zodSchema = z.object({
message: z.string().min(1, { message: "Message argument is required." })
})

app.post('/', zValidator('json', zodSchema), async (c) => {
try {
const { VECTOR_TOKEN, VECTOR_URL } = env<Environment>(c)

Expand All @@ -38,12 +40,7 @@ app.post('/', async (c) => {
cache: false, // disable needed for cf worker deployment
})

const body = await c.req.json()
let { message } = body as { message: string }

if (!message) {
return c.json({ error: 'Message argument is required.' }, { status: 400 })
}
let { message } = c.req.valid('json');

// this is because of the cloudflare worker sub-request limit
if (message.split(/\s/).length > 35 || message.length > 1000) {
Expand Down