Skip to content

Commit

Permalink
feat: updates secure proxy plugin to use @oramacloud/client package (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
micheleriva authored Jan 24, 2024
1 parent 59efc74 commit c766791
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 56 deletions.
2 changes: 1 addition & 1 deletion packages/docs/cloud/orama-ai/orama-secure-proxy.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ const proxy = new OramaProxy({
})

const chatParams = {
model '<YOUR API KEY>',
model 'openai/gpt-4',
messages: [{ role: 'user', content: 'Who is Michael Scott?' }]
}

Expand Down
12 changes: 11 additions & 1 deletion packages/docs/open-source/plugins/plugin-secure-proxy.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import { pluginSecureProxy } from '@orama/plugin-secure-proxy'

const secureProxy = secureProxyPlugin({
apiKey: 'YOUR API KEY',
defaultProperty: 'embeddings'
defaultProperty: 'embeddings',
model: 'openai/text-embedding-ada-002'
})

const db = await create({
Expand All @@ -41,6 +42,15 @@ const db = await create({
})
```

## Available models

Right now, the Orama Secure Proxy Plugin supports two different models for generating embeddings:

| Model name | Provider | Dimensions |
| ------------------------------ | -------- | ---------- |
| `orama/gte-small | Orama | 384 |
| `openai/text-embedding-ada-002 | Openai | 1536 |

## Running queries

By telling on which property to perform search by default (in the example above, `'embeddings'`), the plugin will automatically translate your search term into a vector by calling the OpenAI API for you and setting the result into the `vector.value` property.
Expand Down
14 changes: 11 additions & 3 deletions packages/plugin-secure-proxy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Tests](https://github.com/oramasearch/orama/actions/workflows/turbo.yml/badge.svg)](https://github.com/oramasearch/orama/actions/workflows/turbo.yml)

Orama plugin for generating embeddings securely on the front-end.
Orama plugin for generating embeddings and performing vector/hybrid search securely on the front-end.

# Usage

Expand All @@ -18,17 +18,25 @@ const db = await create({
schema: {
title: 'string',
description: 'string',
embeddings: 'vector[1536]'
embeddings: 'vector[384]'
},
plugins: [
pluginSecureProxy({
apiKey: '<API-KEY>',
defaultProperty: 'embeddings'
defaultProperty: 'embeddings',
model: 'orama/gte-small'
})
]
})
```

Available models:

- `orama/gte-small`. 384 dimensions.
- `openai/text-embedding-ada-002`. 1536 dimensions.

Mode models coming soon!

For the full configuration guide of this plugin, please follow the [official plugin documentation](https://docs.oramasearch.com/open-source/plugins/plugin-secure-proxy).

# License
Expand Down
5 changes: 4 additions & 1 deletion packages/plugin-secure-proxy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
"access": "public"
},
"devDependencies": {
"@types/node": "^20.9.0",
"@orama/orama": "workspace:*",
"@types/node": "^20.9.0",
"c8": "^7.12.0",
"sinon": "^17.0.1",
"tap": "^18.6.1",
Expand All @@ -54,5 +54,8 @@
},
"tap": {
"extends": "src/tests/config/tap.yml"
},
"dependencies": {
"@oramacloud/client": "^1.0.4"
}
}
59 changes: 10 additions & 49 deletions packages/plugin-secure-proxy/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,20 @@
import type { AnyOrama, SearchParams, TypedDocument, OramaPluginAsync } from '@orama/orama'

type InitResponse = {
clientID: string
ttl: number
csrfToken: string
}

type EmbeddingsResponse = number[]
import { OramaProxy, EmbeddingModel } from '@oramacloud/client'

export type SecureProxyPluginOptions = {
apiKey: string
defaultProperty: string
model: EmbeddingModel
}

const SECURE_PROXY_ENDPOINT = 'https://secure-proxy.orama.run'
const INIT_URL = `${SECURE_PROXY_ENDPOINT}/init`
const SEARCH_URL = `${SECURE_PROXY_ENDPOINT}/query`

function isServer() {
return typeof window === 'undefined'
}

function getReferer() {
return isServer() ? 'http://localhost' : window.location.href
}

async function getCSRFToken(apiKey: string): Promise<InitResponse> {
const response = await fetch(`${INIT_URL}?apiKey=${apiKey}`, {
headers: {
Referer: getReferer()
}
})

return response.json()
}

async function getEmbeddings(apiKey: string, query: string, csrfToken: string): Promise<EmbeddingsResponse> {
const body = new URLSearchParams({ query, csrf: csrfToken }).toString()

const response = await fetch(`${SEARCH_URL}?apiKey=${apiKey}`, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
Referer: getReferer()
},
body
})

return response.json()
}

export async function pluginSecureProxy(pluginParams: SecureProxyPluginOptions): OramaPluginAsync {
export async function pluginSecureProxy(pluginParams: SecureProxyPluginOptions): Promise<OramaPluginAsync> {
if (!pluginParams.apiKey) throw new Error('Missing "apiKey" parameter for plugin-telemetry')
if (!pluginParams.defaultProperty) throw new Error('Missing "defaultProperty" parameter for plugin-telemetry')
if (!pluginParams.model) throw new Error('Missing "model" parameter for plugin-telemetry')

const { csrfToken } = await getCSRFToken(pluginParams.apiKey)
const proxy = new OramaProxy({
api_key: pluginParams.apiKey,
})

return {
name: 'secure-proxy',
Expand All @@ -73,11 +33,12 @@ export async function pluginSecureProxy(pluginParams: SecureProxyPluginOptions):
}

const term = params.term
const embeddings = await getEmbeddings(pluginParams.apiKey, term, csrfToken)
const embeddings = await proxy.generateEmbeddings(term, pluginParams.model)

if (!params.vector) {
params.vector = {
// @ts-expect-error - vector param is not present in full-text search
// eslint-disable-next-line
// @ts-ignore
property: params?.vector?.property ?? pluginParams.defaultProperty,
value: embeddings
}
Expand Down
15 changes: 15 additions & 0 deletions pnpm-lock.yaml

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

6 changes: 5 additions & 1 deletion turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
"^build"
],
"outputs": ["dist/**"]
}
},
"@orama/plugin-secure-proxy#build": {
"dependsOn": ["@orama/orama#build"],
"outputs": ["dist/**"]
}
}
}

0 comments on commit c766791

Please sign in to comment.