| 32 | |
| 33 | export default class LangChain { |
| 34 | static async predict({ |
| 35 | query, |
| 36 | typesenseApiKey, |
| 37 | communityName, |
| 38 | summarize, |
| 39 | accountId, |
| 40 | threadId, |
| 41 | }: { |
| 42 | query: string; |
| 43 | typesenseApiKey: string; |
| 44 | communityName: string; |
| 45 | accountId: string; |
| 46 | summarize: boolean; |
| 47 | threadId: string; |
| 48 | }) { |
| 49 | const body = await Typesense.queryThreads({ |
| 50 | query, |
| 51 | apiKey: typesenseApiKey, |
| 52 | }); |
| 53 | |
| 54 | if (summarize) { |
| 55 | for await (let item of body) { |
| 56 | item.body = await this.summarize(item.body); |
| 57 | } |
| 58 | } |
| 59 | const typesenseResults = body |
| 60 | .filter((t) => t.id !== threadId) |
| 61 | .map( |
| 62 | (b) => |
| 63 | new Document({ |
| 64 | pageContent: b.body, |
| 65 | metadata: { source: b.id }, |
| 66 | }) |
| 67 | ); |
| 68 | |
| 69 | const directory = `.db/${communityName}`; |
| 70 | const fileStore = await FaissStore.load(directory, embeddingsModel); |
| 71 | const fileResults = await fileStore.similaritySearch(query, env.CHUNKS); |
| 72 | |
| 73 | const prismaVectorStore = PrismaVectorStore.withModel<database.embeddings>( |
| 74 | database.prisma |
| 75 | ).create(embeddingsModel, { |
| 76 | prisma: database.Prisma, |
| 77 | tableName: 'embeddings', |
| 78 | vectorColumnName: 'embedding', |
| 79 | columns: { |
| 80 | threadId: PrismaVectorStore.IdColumn, |
| 81 | value: PrismaVectorStore.ContentColumn, |
| 82 | }, |
| 83 | filter: { |
| 84 | accountId: { equals: accountId }, |
| 85 | confidence: { equals: 100 }, |
| 86 | }, |
| 87 | }); |
| 88 | const vectorResults = await prismaVectorStore.similaritySearch( |
| 89 | query, |
| 90 | env.CHUNKS |
| 91 | ); |