Spaces:
Runtime error
Runtime error
File size: 4,646 Bytes
58faf93 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
import { ChromaClient, DefaultEmbeddingFunction } from 'chromadb'
enum IncludeEnum {
Documents = 'documents',
Embeddings = 'embeddings',
Metadatas = 'metadatas',
Distances = 'distances',
}
type Auth = {
authType: string
token: string
username: string
password: string
}
function formatAuth(auth: Auth) {
if (auth.authType === 'token') {
return {
provider: 'token',
credentials: auth.token,
}
} else if (auth.authType === 'basic') {
return {
provider: 'basic',
credentials: {
username: auth.username,
password: auth.password,
},
}
}
}
export async function fetchCollections(connectionString: string, auth: Auth, tenant: string, database: string) {
const client = new ChromaClient({
path: connectionString,
auth: formatAuth(auth),
database: database,
tenant: tenant,
})
const collections = await client.listCollections()
return collections
}
const PAGE_SIZE = 20
export async function fetchRecords(
connectionString: string,
auth: Auth,
collectionName: string,
page: number,
tenant: string,
database: string
) {
const client = new ChromaClient({
path: connectionString,
auth: formatAuth(auth),
database: database,
tenant: tenant,
})
const embeddingFunction = new DefaultEmbeddingFunction()
const collection = await client.getCollection({ name: collectionName, embeddingFunction: embeddingFunction })
const response = await collection.get({
limit: PAGE_SIZE,
offset: (page - 1) * PAGE_SIZE,
include: [IncludeEnum.Documents, IncludeEnum.Embeddings, IncludeEnum.Metadatas],
})
return response.ids.map((id, index) => ({
id,
document: response.documents[index],
metadata: response.metadatas[index],
embedding: response.embeddings?.[index],
}))
}
const QUERY_K = 10
type queryErrorResponse = {
error: string
}
export async function queryRecords(
connectionString: string,
auth: Auth,
collectionName: string,
queryEmbeddings: number[],
tenant: string,
database: string
) {
const client = new ChromaClient({
path: connectionString,
auth: formatAuth(auth),
database: database,
tenant: tenant,
})
const embeddingFunction = new DefaultEmbeddingFunction()
const collection = await client.getCollection({ name: collectionName, embeddingFunction: embeddingFunction })
const response = await collection.query({
queryEmbeddings: queryEmbeddings,
nResults: QUERY_K,
include: [IncludeEnum.Documents, IncludeEnum.Embeddings, IncludeEnum.Metadatas, IncludeEnum.Distances],
})
if ((response as unknown as queryErrorResponse)['error'] != null) {
throw new Error((response as unknown as queryErrorResponse)['error'])
}
return response.ids[0].map((id, index) => ({
id,
document: response.documents[0][index],
metadata: response.metadatas[0][index],
embedding: response.embeddings?.[0][index],
distance: response.distances?.[0][index],
}))
}
// src/lib/server/db.ts
export async function queryRecordsText(
connectionString: string,
auth: Auth,
collectionName: string,
queryTexts: string,
tenant: string,
database: string
) {
const client = new ChromaClient({
path: connectionString,
auth: formatAuth(auth),
database: database,
tenant: tenant,
})
const embeddingFunction = new DefaultEmbeddingFunction()
const collection = await client.getCollection({ name: collectionName, embeddingFunction: embeddingFunction })
const response = await collection.get({
ids: [queryTexts],
include: [IncludeEnum.Documents, IncludeEnum.Embeddings, IncludeEnum.Metadatas],
})
if ((response as unknown as queryErrorResponse)['error'] != null) {
throw new Error((response as unknown as queryErrorResponse)['error'])
}
console.log(response)
// Check if the response is empty
if (response.ids.length === 0) {
throw new Error('RecordNotFound')
}
return [
{
id: response.ids[0],
document: response.documents[0],
metadata: response.metadatas[0],
embedding: response.embeddings?.[0],
distance: 0,
},
]
}
export async function countRecord(
connectionString: string,
auth: Auth,
collectionName: string,
tenant: string,
database: string
) {
const client = new ChromaClient({
path: connectionString,
auth: formatAuth(auth),
database: database,
tenant: tenant,
})
const embeddingFunction = new DefaultEmbeddingFunction()
const collection = await client.getCollection({ name: collectionName, embeddingFunction: embeddingFunction })
return await collection.count()
}
|