-
Notifications
You must be signed in to change notification settings - Fork 0
/
pinecone-utils.ts
66 lines (61 loc) · 2.05 KB
/
pinecone-utils.ts
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
import { Document } from '@langchain/core/documents';
import { PineconeStore } from '@langchain/pinecone';
import { Pinecone } from '@pinecone-database/pinecone';
import { RecursiveCharacterTextSplitter } from 'langchain/text_splitter';
import { Resource } from 'sst';
import { constants } from './config';
import { openAIEmbeddings } from './embeddings-utils';
import { getDocsFromFile } from './loader-utils';
const { pineconeIndexName } = constants;
export const pineconeClient = new Pinecone({ apiKey: Resource.PineconeApiSecret.value });
export async function pineconeIndexDocs(
docs: Document<Record<string, any>>[],
client: Pinecone = pineconeClient,
indexName = pineconeIndexName,
namespace: string
) {
try {
// 1. Retrieve Pinecone index w namespace
const pineconeIndex = client.Index(indexName);
// 2. Log the retrieved index name
console.log(`Pinecone index ${indexName}; namespace ${namespace}`);
// 3. Create RecursiveCharacterTextSplitter instance
const textSplitter = new RecursiveCharacterTextSplitter({
chunkSize: 500,
chunkOverlap: 20
});
// 4. get splitted docs
const splittedDocs = await textSplitter.splitDocuments(docs);
// 5. embed docs and store in pinecone
await PineconeStore.fromDocuments(splittedDocs, openAIEmbeddings(), {
pineconeIndex,
namespace
});
} catch (error) {
console.error(error);
throw new Error('Error indexing docs in pinecone');
}
}
export async function embedDocFile(localPath: string, namespace: string) {
try {
const documents = await getDocsFromFile(localPath);
await pineconeIndexDocs(documents, pineconeClient, pineconeIndexName, namespace);
return {
ok: true
};
} catch (error) {
return {
ok: false
};
}
}
export async function deleteByNamespace(namespace: string) {
const client = pineconeClient;
const index = client.Index(pineconeIndexName);
try {
await index.namespace(namespace).deleteAll();
} catch (error) {
console.error(error);
throw new Error('Error deleting namespace');
}
}