@sanity/embeddings-index-ui 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +148 -0
- package/dist/index.d.ts +67 -0
- package/dist/index.esm.js +2792 -0
- package/dist/index.esm.js.map +1 -0
- package/dist/index.js +2807 -0
- package/dist/index.js.map +1 -0
- package/package.json +93 -0
- package/sanity.json +8 -0
- package/src/api/embeddingsApi.ts +68 -0
- package/src/api/embeddingsApiHooks.ts +12 -0
- package/src/embeddingsIndexDashboard/EmbeddingsIndexTool.tsx +150 -0
- package/src/embeddingsIndexDashboard/IndexEditor.tsx +181 -0
- package/src/embeddingsIndexDashboard/IndexFormInput.tsx +75 -0
- package/src/embeddingsIndexDashboard/IndexInfo.tsx +116 -0
- package/src/embeddingsIndexDashboard/IndexList.tsx +83 -0
- package/src/embeddingsIndexDashboard/QueryIndex.tsx +107 -0
- package/src/embeddingsIndexDashboard/dashboardPlugin.ts +15 -0
- package/src/embeddingsIndexDashboard/hooks.ts +36 -0
- package/src/index.ts +9 -0
- package/src/preview/DocumentPreview.tsx +135 -0
- package/src/referenceInput/SemanticSearchReferenceInput.tsx +227 -0
- package/src/referenceInput/referencePlugin.tsx +21 -0
- package/src/referenceInput/types.ts +0 -0
- package/src/schemas/typeDefExtensions.ts +19 -0
- package/src/utils/id.ts +3 -0
- package/src/utils/types.ts +11 -0
- package/v2-incompatible.js +11 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Sanity
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
# @sanity/embeddings-index-ui
|
|
2
|
+
> This package contains plugins for **Sanity Studio v3**.
|
|
3
|
+
|
|
4
|
+
`@sanity/embeddings-index-ui` Sanity Studio plugins that interacts with the `/embeddings-index` HTTP API.
|
|
5
|
+
|
|
6
|
+
The embeddings index API allows the creation of named embeddings vector indexes.
|
|
7
|
+
An embeddings index contains embeddings for all Sanity documents matching a configured GROQ filter in a dataset.
|
|
8
|
+
A GROQ projection is applied to matching documents before vectorization.
|
|
9
|
+
|
|
10
|
+
Indexes can be queried using semantic text search, and returns a list of matching document ids,
|
|
11
|
+
sorted by relevance.
|
|
12
|
+
|
|
13
|
+
When an index is first created, all documents matching the configured filter will be synced into the index.
|
|
14
|
+
This can take some time depending on the number of documents that need to be synced.
|
|
15
|
+
|
|
16
|
+
For a CLI alternative, check out the [Embeddings index CLI](https://github.com/sanity-io/embeddings-index-cli) package.
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
```sh
|
|
21
|
+
npm install @sanity/embeddings-index-ui
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
`@sanity/embeddings-index-ui` contains the following Sanity Studio plugins:
|
|
25
|
+
|
|
26
|
+
* [embeddingsIndexReferenceInput](#embeddings-index-reference-input): semantic search mode for reference inputs
|
|
27
|
+
* [embeddingsIndexDashboard](#embeddings-index-dashboard): manage indexes in a Sanity studio tool
|
|
28
|
+
|
|
29
|
+
Consult each section for usage details.
|
|
30
|
+
|
|
31
|
+
## Embeddings index reference input
|
|
32
|
+
|
|
33
|
+
<img width="619" alt="image" src="https://github.com/sanity-io/sanity/assets/835514/55d372fe-c5fe-40dd-882b-10c6e8794442">
|
|
34
|
+
|
|
35
|
+
The `embeddingsIndexReferenceInput` plugin allows references fields to opt-in to embeddings index search.
|
|
36
|
+
This users to search for references using natural language to find documents based on semantic meaning,
|
|
37
|
+
bypassing the need for exact word matches.
|
|
38
|
+
|
|
39
|
+
### Usage
|
|
40
|
+
|
|
41
|
+
Add `embeddingsIndexReferenceInput` as a plugin in `sanity.config.ts` (or .js):
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
import {defineConfig} from 'sanity'
|
|
45
|
+
import {embeddingsIndexReferenceInput} from '@sanity/embeddings-index-ui'
|
|
46
|
+
|
|
47
|
+
export default defineConfig({
|
|
48
|
+
//...
|
|
49
|
+
plugins: [embeddingsIndexReferenceInput()],
|
|
50
|
+
})
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Next, enabled semantic search using `options.embeddingsIndex` on reference fields:
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
defineField({
|
|
57
|
+
name: 'myField',
|
|
58
|
+
type: 'reference',
|
|
59
|
+
to: [{type: 'myType'}],
|
|
60
|
+
options: {
|
|
61
|
+
embeddingsIndex: {
|
|
62
|
+
indexName: 'my-index',
|
|
63
|
+
maxResults: 10, // default,
|
|
64
|
+
searchMode: 'embeddings' // defaults value is 'default'
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
})
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Setting `options.embeddings.indexName` on a reference field will allow enabled searching into the named index.
|
|
71
|
+
|
|
72
|
+
This will add a "search mode" toggle button to the field.
|
|
73
|
+
|
|
74
|
+
*Note*: The search will use `to` types as a filter into the index, so it important
|
|
75
|
+
that the types the reference field expects actually exist in the index
|
|
76
|
+
(ie, the embeddings index `filter` contains one or more documents relevant to the reference field).
|
|
77
|
+
|
|
78
|
+
*Caveats*:
|
|
79
|
+
`options.filter` is not respected by the semantic search.
|
|
80
|
+
|
|
81
|
+
## Embeddings index dashboard
|
|
82
|
+
An UI alternative to the [Embeddings index CLI](https://github.com/sanity-io/embeddings-index-cli)
|
|
83
|
+
Manage embeddings indexes in a Studio dashboard.
|
|
84
|
+
|
|
85
|
+
<img width="1227" alt="image" src="https://github.com/sanity-io/sanity/assets/835514/279b03b8-d2c0-4cc1-bbe6-9d335937f25a">
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
## Usage
|
|
89
|
+
|
|
90
|
+
Add `embeddingsIndexDashboard` as a plugin in `sanity.config.ts` (or .js):
|
|
91
|
+
|
|
92
|
+
```ts
|
|
93
|
+
import {defineConfig} from 'sanity'
|
|
94
|
+
import {embeddingsIndexDashboard} from '@sanity/embeddings-index-ui'
|
|
95
|
+
|
|
96
|
+
export default defineConfig({
|
|
97
|
+
//...
|
|
98
|
+
plugins: [
|
|
99
|
+
process.env.NODE_ENV === 'development'
|
|
100
|
+
? embeddingsIndexDashboard()
|
|
101
|
+
: {name: 'embeddings-index-dashboard-disabled'}
|
|
102
|
+
],
|
|
103
|
+
})
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
This will add the Embeddings tool to the studio nav-bar, only when studio is running in developer mode (localhost).
|
|
107
|
+
|
|
108
|
+
You might instead want to enable this tool based on roles instead:
|
|
109
|
+
|
|
110
|
+
```ts
|
|
111
|
+
import {defineConfig} from 'sanity'
|
|
112
|
+
import {embeddingsIndexDashboard} from '@sanity/embeddings-index-ui'
|
|
113
|
+
|
|
114
|
+
export default defineConfig({
|
|
115
|
+
//...
|
|
116
|
+
plugins: [embeddingsIndexDashboard()],
|
|
117
|
+
|
|
118
|
+
tools: (prev, context) => {
|
|
119
|
+
const enabledForRoles = ['developer']
|
|
120
|
+
const canManageEmbeddingsIndex = context.currentUser?.roles
|
|
121
|
+
.map((role) => role.name)
|
|
122
|
+
.some((roleName) => enabledForRoles.includes(roleName))
|
|
123
|
+
return canManageEmbeddingsIndex ? prev : prev.filter((tool) => tool.name !== 'embeddings-index')
|
|
124
|
+
},
|
|
125
|
+
})
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
## License
|
|
131
|
+
|
|
132
|
+
[MIT](LICENSE) © Sanity
|
|
133
|
+
|
|
134
|
+
## Develop & test
|
|
135
|
+
|
|
136
|
+
This plugin uses [@sanity/plugin-kit](https://github.com/sanity-io/plugin-kit)
|
|
137
|
+
with default configuration for build & watch scripts.
|
|
138
|
+
|
|
139
|
+
See [Testing a plugin in Sanity Studio](https://github.com/sanity-io/plugin-kit#testing-a-plugin-in-sanity-studio)
|
|
140
|
+
on how to run this plugin with hotreload in the studio.
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
### Release new version
|
|
144
|
+
|
|
145
|
+
Run ["CI & Release" workflow](https://github.com/sanity-io/embeddings-plugin/actions/workflows/main.yml).
|
|
146
|
+
Make sure to select the main branch and check "Release new version".
|
|
147
|
+
|
|
148
|
+
Semantic release will only release on configured branches, so it is safe to run release on any branch.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import {Plugin as Plugin_2} from 'sanity'
|
|
2
|
+
import {SanityClient} from 'sanity'
|
|
3
|
+
|
|
4
|
+
export declare function deleteIndex(indexName: string, client: SanityClient): Promise<IndexState>
|
|
5
|
+
|
|
6
|
+
export declare const embeddingsIndexDashboard: Plugin_2<void>
|
|
7
|
+
|
|
8
|
+
export declare const embeddingsIndexReferenceInput: Plugin_2<void>
|
|
9
|
+
|
|
10
|
+
export declare function getIndexes(client: SanityClient): Promise<IndexState[]>
|
|
11
|
+
|
|
12
|
+
export declare interface IndexState extends NamedIndex {
|
|
13
|
+
status: string
|
|
14
|
+
startDocumentCount: number
|
|
15
|
+
remainingDocumentCount: number
|
|
16
|
+
failedDocumentCount: number
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export declare interface NamedIndex {
|
|
20
|
+
indexName: string
|
|
21
|
+
dataset: string
|
|
22
|
+
project: string
|
|
23
|
+
projection: string
|
|
24
|
+
filter: string
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export declare interface QueryConfig {
|
|
28
|
+
query: string
|
|
29
|
+
indexName: string
|
|
30
|
+
maxResults?: number
|
|
31
|
+
filter?: {
|
|
32
|
+
type?: string | string[]
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export declare function queryIndex(
|
|
37
|
+
queryConfig: QueryConfig,
|
|
38
|
+
client: SanityClient,
|
|
39
|
+
): Promise<QueryResult[]>
|
|
40
|
+
|
|
41
|
+
export declare interface QueryResult {
|
|
42
|
+
score: number
|
|
43
|
+
value: {
|
|
44
|
+
documentId: string
|
|
45
|
+
type?: string
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export {}
|
|
50
|
+
|
|
51
|
+
declare module 'sanity' {
|
|
52
|
+
interface ReferenceBaseOptions {
|
|
53
|
+
embeddingsIndex?: {
|
|
54
|
+
/**
|
|
55
|
+
* Name of the index
|
|
56
|
+
*/
|
|
57
|
+
indexName: string
|
|
58
|
+
maxResults?: number
|
|
59
|
+
/**
|
|
60
|
+
* Determines if which search mode is enabled by default for the reference field.
|
|
61
|
+
* Default is the studio default search, while 'embeddings' enables
|
|
62
|
+
* Defaults to 'default' behaviour
|
|
63
|
+
*/
|
|
64
|
+
searchMode?: 'embeddings' | 'default'
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|