Skip to content

Commit

Permalink
Working Examples
Browse files Browse the repository at this point in the history
  • Loading branch information
awhiteside1 committed Sep 16, 2024
1 parent 1a56925 commit 7b0de08
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 104 deletions.
14 changes: 9 additions & 5 deletions packages/semantic-search/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
".": {
"types": "./src/index.ts",
"default": "./dist/index.mjs"
},
"./lancedb": {
"types": "./src/components/lancedb/index.ts",
"default": "./dist/components/lancedb/index.mjs"
}
},
"scripts": {
Expand All @@ -20,16 +24,16 @@
"postinstall": "pnpm build:stub"
},
"devDependencies": {
"@lancedb/lancedb": "0.10.0",

"@payloadcms/db-mongodb": "^3.0.0-beta.107",
"@payloadcms/db-postgres": "^3.0.0-beta.107",
"@types/dockerode": "^3.3.31",
"apache-arrow": "^17.0.0",
"dockerode": "^4.0.2",
"mongodb-memory-server": "^10.0.0",
"ollama": "^0.5.9"
"mongodb-memory-server": "^10.0.0"
},
"dependencies": {
"apache-arrow": "^17.0.0",
"ollama": "^0.5.9",
"@lancedb/lancedb": "0.10.0",
"@workspace/llm-utils": "workspace:*",
"defu": "^6.1.4",
"pathe": "^1.1.2",
Expand Down
13 changes: 8 additions & 5 deletions packages/semantic-search/src/components/lancedb/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { type Table, connect } from '@lancedb/lancedb'
import { LanceSchema } from '@lancedb/lancedb/embedding'
import { Utf8 } from 'apache-arrow'
import type { Identifier, InsertFields, VectorDB } from '../../types'
import { OllamaEmbeddings } from '../ollama'
import {connect, type Table} from '@lancedb/lancedb'
import {getRegistry, LanceSchema} from '@lancedb/lancedb/embedding'
import {Utf8} from 'apache-arrow'
import type {Identifier, InsertFields, VectorDB} from '../../types'
import {OllamaEmbeddings, register} from '../ollama'

register()

export class LanceDB implements VectorDB {
public name = 'lancedb'
Expand All @@ -23,6 +25,7 @@ export class LanceDB implements VectorDB {
}

static async create(path = './lancedb') {

const func = new OllamaEmbeddings({
host: 'http://100.67.29.127:11434',
model: 'nomic-embed-text',
Expand Down
14 changes: 4 additions & 10 deletions packages/semantic-search/src/components/ollama/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
import { embedding } from '@lancedb/lancedb'
import {
EmbeddingFunction,
TextEmbeddingFunction,
getRegistry,
} from '@lancedb/lancedb/embedding'
import type { Float } from 'apache-arrow'
import { Ollama } from 'ollama'
import {getRegistry, TextEmbeddingFunction,} from '@lancedb/lancedb/embedding'
import type {Float} from 'apache-arrow'
import {Ollama} from 'ollama'

interface Options {
model: string
timeout: number
host: string
}
// @ts-ignore
@embedding.register('ollama')

export class OllamaEmbeddings extends TextEmbeddingFunction<Partial<Options>> {
private client: Ollama
constructor(private modelOptions: Options) {
Expand Down
6 changes: 3 additions & 3 deletions packages/semantic-search/src/hooks/afterChangeHook.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { FieldHook, FieldHookArgs } from 'payload'
import { isObject, isString } from 'radash'
import { getSemanticSearchCustom } from '../utils/customContext'
import type {FieldHook, FieldHookArgs} from 'payload'
import {isObject, isString} from 'radash'
import {getSemanticSearchCustom} from '../utils/customContext'

export const afterChangeHook: FieldHook = (args) => {
if (args.operation === 'create' || args.operation === 'update') {
Expand Down
11 changes: 5 additions & 6 deletions packages/semantic-search/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import defu from 'defu'
import type { Config, FieldBase } from 'payload'
import { afterChangeHook } from './hooks/afterChangeHook'
import type { SemanticSearchPluginConfig } from './types'
import { setupSemanticSearchCustom } from './utils/customContext'
import { getField, parseFields } from './utils/fields'
import type {Config, FieldBase} from 'payload'
import {afterChangeHook} from './hooks/afterChangeHook'
import type {SemanticSearchPluginConfig} from './types'
import {setupSemanticSearchCustom} from './utils/customContext'
import {getField, parseFields} from './utils/fields'

export const semanticSearchPlugin =
(incomingPluginConfig: SemanticSearchPluginConfig) =>
Expand All @@ -16,7 +16,6 @@ export const semanticSearchPlugin =

return setupSemanticSearchCustom(config, {
vectorDB: incomingPluginConfig.vectorDB,
embeddingFn: incomingPluginConfig.vectorDB,
})
}

Expand Down
7 changes: 3 additions & 4 deletions packages/semantic-search/src/utils/customContext.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import type { Config } from 'payload'
import { isObject } from 'radash'
import type { SemanticSearchPluginConfig, VectorDB } from '../types'
import type {Config} from 'payload'
import {isObject} from 'radash'
import type {VectorDB} from '../types'

export type SemanticSearchCustomObject = {
vectorDB: VectorDB
embeddingFn: SemanticSearchPluginConfig['embeddingFn']
}

export const CUSTOMKEY = 'semantic-search'
Expand Down
7 changes: 1 addition & 6 deletions packages/semantic-search/src/utils/fields.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import type {
CollectionConfig,
Config,
TextField,
TextareaField,
} from 'payload'
import type {CollectionConfig, Config, TextareaField, TextField,} from 'payload'

export interface FieldEntry {
collectionConfig: CollectionConfig
Expand Down
1 change: 1 addition & 0 deletions playground/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ node_modules

# Payload default media upload directory
public/media/
db/
7 changes: 6 additions & 1 deletion playground/next.config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { withPayload } from '@payloadcms/next/withPayload'
import {withPayload} from '@payloadcms/next/withPayload'

import redirects from './redirects.js'

const NEXT_PUBLIC_SERVER_URL = process.env.NEXT_PUBLIC_SERVER_URL || 'http://localhost:3000'

/** @type {import('next').NextConfig} */
const nextConfig = {
webpack(config) {
config.externals.push('@lancedb/lancedb')
return config;
},
serverExternalPackages:['@payload-llm-plugins/semantic-search','@lancedb/lancedb'],
images: {
remotePatterns: [
...[NEXT_PUBLIC_SERVER_URL /* 'https://example.com' */].map((item) => {
Expand Down
Binary file removed playground/playground.db
Binary file not shown.
3 changes: 2 additions & 1 deletion playground/src/payload.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {Header} from './Header/config'
import {revalidateRedirects} from './hooks/revalidateRedirects'
import {GenerateTitle, GenerateURL} from '@payloadcms/plugin-seo/types'
import {Page, Post} from './payload-types'
import {LanceDB} from "@payload-llm-plugins/semantic-search/lancedb";

const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)
Expand Down Expand Up @@ -188,7 +189,7 @@ export default buildConfig({
},
}),
payloadCloudPlugin(), // storage-adapter-placeholder
semanticSearchPlugin({enabled:true, vectorDB: LanceDB.create('')})
semanticSearchPlugin({enabled:true, vectorDB: await LanceDB.create('./db/lancedb/'), dimensions:768, indexableFields:['posts.content'] })
],
secret: process.env.PAYLOAD_SECRET!,
sharp,
Expand Down
Loading

0 comments on commit 7b0de08

Please sign in to comment.