Skip to content

Commit

Permalink
Move to plugin-pt15
Browse files Browse the repository at this point in the history
  • Loading branch information
allevo committed Oct 11, 2024
1 parent 0bb6169 commit 775bc26
Show file tree
Hide file tree
Showing 26 changed files with 1,404 additions and 172 deletions.
42 changes: 42 additions & 0 deletions benchmarks/algorithms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import b from 'benny'
import { create, insertMultiple, search } from 'orama_latest'
import { pluginPT15 } from '@orama/plugin-pt15'
import dataset from './src/dataset.json' assert { type: 'json' }
import {stopwords} from '@orama/stopwords/english'

const dbBM25 = create({
schema: {
description: 'string'
},
components: {
tokenizer: {
stopWords: stopwords,
},
}
})
const dbWithPT15 = create({
schema: {
description: 'string'
},
plugins: [pluginPT15()],
components: {
tokenizer: {
stopWords: stopwords,
},
}
})
await insertMultiple(dbBM25, dataset)
await insertMultiple(dbWithPT15, dataset)

b.suite('search-algorithms',
b.add('search bm25', () => {
search(dbBM25, { term: 'L' })
}),
b.add('search pt15', () => {
search(dbWithPT15, { term: 'L' })
}),
b.cycle(),
b.complete(),
b.save({ file: 'insert', version: '1.0.0' }),
b.save({ file: 'search-algorithms', format: 'chart.html' }),
)
15 changes: 15 additions & 0 deletions benchmarks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ function benchmarkInsert() {
b.add('insert in Orama latest', () => {
insert.oramaLatest()
}),
b.add('insert in Orama latest with PT15', () => {
insert.oramaLatestPT15()
}),
b.cycle(),
b.complete(),
b.save({ file: 'insert', version: '1.0.0' }),
Expand All @@ -30,6 +33,9 @@ function benchmarkInsertMultiple() {
b.add('insert multiple in Orama latest', () => {
insertMultiple.oramaLatest()
}),
b.add('insert multiple in Orama latest with PT15', () => {
insertMultiple.oramaLatestPT15()
}),
b.cycle(),
b.complete(),
b.save({ file: 'insert multiple', version: '1.0.0' }),
Expand All @@ -48,6 +54,9 @@ function benchmarkSearch() {
b.add('plain search in Orama latest', () => {
searchPlain.oramaLatest()
}),
b.add('plain search in Orama latest with PT15', () => {
searchPlain.oramaLatestPT15()
}),
b.cycle(),
b.complete(),
b.save({ file: 'plain search', version: '1.0.0' }),
Expand All @@ -66,6 +75,9 @@ function benchmarkSearchWithFilters() {
b.add('search with filters in Orama latest', () => {
searchWithFilters.oramaLatest()
}),
b.add('search with filters in Orama latest with PT15', () => {
searchWithFilters.oramaLatestPT15()
}),
b.cycle(),
b.complete(),
b.save({ file: 'search with filters', version: '1.0.0' }),
Expand All @@ -84,6 +96,9 @@ function benchmarkSearchWithLongTextAndComplexFilters() {
b.add('search with long text and complex filters in Orama latest', () => {
searchWithLongTextAndComplexFilters.oramaLatest()
}),
b.add('search with long text and complex filters in Orama latest with PT15', () => {
searchWithLongTextAndComplexFilters.oramaLatestPT15()
}),
b.cycle(),
b.complete(),
b.save({ file: 'search with long text and complex filters', version: '1.0.0' }),
Expand Down
73 changes: 72 additions & 1 deletion benchmarks/package-lock.json

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

5 changes: 4 additions & 1 deletion benchmarks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
"dependencies": {
"orama_211": "npm:@orama/[email protected]",
"orama_300_rc_2": "npm:@orama/[email protected]",
"orama_latest": "file:../packages/orama"
"orama_latest": "file:../packages/orama",
"@orama/plugin-qps": "file:../packages/plugin-qps",
"@orama/stopwords": "file:../packages/stopwords",
"@orama/plugin-pt15": "file:../packages/plugin-pt15"
},
"devDependencies": {
"benny": "^3.7.1"
Expand Down
23 changes: 22 additions & 1 deletion benchmarks/src/get-orama.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as orama211 from 'orama_211'
import * as orama300rc2 from 'orama_300_rc_2'
import * as oramaLatest from 'orama_latest'
import { pluginPT15 } from '@orama/plugin-pt15'
import dataset from './dataset.json' assert { type: 'json' }

export const schema = {
Expand All @@ -13,12 +14,14 @@ export const schema = {
const create = {
orama211: () => orama211.create({ schema }),
orama300rc2: () => orama300rc2.create({ schema }),
oramaLatest: () => oramaLatest.create({ schema })
oramaLatest: () => oramaLatest.create({ schema }),
oramaLatestPT15: () => oramaLatest.create({ schema, plugins: [pluginPT15()] })
}

const db211 = await create.orama211()
const db300rc2 = create.orama300rc2()
const dbLatest = create.oramaLatest()
const dbLatestPT15 = create.oramaLatestPT15()

export const insert = {
orama211: async () => {
Expand All @@ -38,6 +41,12 @@ export const insert = {
for (const record of dataset) {
oramaLatest.insert(db, record)
}
},
oramaLatestPT15: () => {
const db = create.oramaLatestPT15()
for (const record of dataset) {
oramaLatest.insert(db, record)
}
}
}

Expand All @@ -50,6 +59,9 @@ export const insertMultiple = {
},
oramaLatest: () => {
oramaLatest.insertMultiple(dbLatest, dataset, 50)
},
oramaLatestPT15: () => {
oramaLatest.insertMultiple(dbLatestPT15, dataset, 50)
}
}

Expand All @@ -62,6 +74,9 @@ export const searchPlain = {
},
oramaLatest: () => {
oramaLatest.search(dbLatest, { term: 'Legend of Zelda' })
},
oramaLatestPT15: () => {
oramaLatest.search(dbLatestPT15, { term: 'Legend of Zelda' })
}
}

Expand All @@ -74,6 +89,9 @@ export const searchWithFilters = {
},
oramaLatest: () => {
oramaLatest.search(dbLatest, { term: 'Super Hero', where: { rating: { gte: 4 } } })
},
oramaLatestPT15: () => {
oramaLatest.search(dbLatestPT15, { term: 'Super Hero', where: { rating: { gte: 4 } } })
}
}

Expand All @@ -86,5 +104,8 @@ export const searchWithLongTextAndComplexFilters = {
},
oramaLatest: () => {
oramaLatest.search(dbLatest, { term: 'classic run gun, action game focused on boss battles', where: { rating: { gte: 4 }, genres: { containsAll: ['Shooter'] } } })
},
oramaLatestPT15: () => {
oramaLatest.search(dbLatestPT15, { term: 'classic run gun, action game focused on boss battles', where: { rating: { gte: 4 }, genres: { containsAll: ['Shooter'] } } })
}
}
3 changes: 2 additions & 1 deletion packages/orama/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ const errors = {
PLUGIN_CRASHED: `A plugin crashed during initialization. Please check the error message for more information:`,
PLUGIN_SECURE_PROXY_NOT_FOUND: `Could not find '@orama/secure-proxy-plugin' installed in your Orama instance.\nPlease install it before proceeding with creating an answer session.\nRead more at https://docs.orama.com/open-source/plugins/plugin-secure-proxy\n`,
PLUGIN_SECURE_PROXY_MISSING_CHAT_MODEL: `Could not find a chat model defined in the secure proxy plugin configuration.\nPlease provide a chat model before proceeding with creating an answer session.\nRead more at https://docs.orama.com/open-source/plugins/plugin-secure-proxy\n`,
ANSWER_SESSION_LAST_MESSAGE_IS_NOT_ASSISTANT: `The last message in the session is not an assistant message. Cannot regenerate non-assistant messages.`
ANSWER_SESSION_LAST_MESSAGE_IS_NOT_ASSISTANT: `The last message in the session is not an assistant message. Cannot regenerate non-assistant messages.`,
PLUGIN_COMPONENT_CONFLICT: `The component "%s" is already defined. The plugin "%s" is trying to redefine it.`,
}

export type ErrorCode = keyof typeof errors
Expand Down
18 changes: 18 additions & 0 deletions packages/orama/src/methods/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,24 @@ export function create<
components = {}
}

for (const plugin of plugins ?? []) {
if (!('getComponents' in plugin)) {
continue
}
if (typeof plugin.getComponents !== 'function') {
continue;
}

const pluginComponents = plugin.getComponents(schema);

const keys = Object.keys(pluginComponents)
for (const key of keys) {
if (components[key]) {
throw createError('PLUGIN_COMPONENT_CONFLICT', key, plugin.name)
}
}
}

if (!id) {
id = uniqueId()
}
Expand Down
1 change: 1 addition & 0 deletions packages/orama/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1315,6 +1315,7 @@ export type OramaPluginSync<T = unknown> = {
beforeUpdateMultiple?: <T extends AnyOrama>(orama: T, docs: AnyDocument[]) => SyncOrAsyncValue
afterUpdateMultiple?: <T extends AnyOrama>(orama: T, docs: AnyDocument[]) => SyncOrAsyncValue
afterCreate?: <T extends AnyOrama>(orama: T) => SyncOrAsyncValue
getComponents?: <T extends AnyOrama>(schema: T['schema']) => SyncOrAsyncValue<Components<T, any, any, any, any>>
}

export type OramaPluginAsync<T = unknown> = Promise<OramaPluginSync<T>>
Expand Down
13 changes: 13 additions & 0 deletions packages/plugin-pt15/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright 2024 OramaSearch Inc

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
29 changes: 29 additions & 0 deletions packages/plugin-pt15/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Orama Plugin PT15

Fast ranking algorithm based on token position.

## Installation

To get started with **Orama Plugin PT15**, just install it with npm:

```sh
npm i @orama/plugin-pt15
```

## Usage

```js
import { create } from '@orama/orama'
import { pluginPT15 } from '@orama/plugin-pt15'

const db = await create({
schema: {
description: 'string',
},
plugins: [ pluginPT15() ],
})
```

# License

[Apache 2.0](/LICENSE.md)
Loading

0 comments on commit 775bc26

Please sign in to comment.