|
| 1 | +import { Injectable } from '@nestjs/common'; |
| 2 | +import { Pinecone as PineconeClient } from '@pinecone-database/pinecone'; |
| 3 | +import { PineconeStore } from '@langchain/pinecone'; |
| 4 | +import { OpenAIEmbeddings } from '@langchain/openai'; |
| 5 | +import { DocumentInterface } from '@langchain/core/documents'; |
| 6 | +import { HumanMessage } from '@langchain/core/messages'; |
| 7 | +import { ChatProvider } from '../../chat-core/provider'; |
| 8 | +import { ChatModel } from '../../enums/chat-model'; |
| 9 | +import { ConfigService } from '@nestjs/config'; |
| 10 | + |
| 11 | +@Injectable() |
| 12 | +export class PineconeDbService { |
| 13 | + private readonly CHAT_GPT_EMBEDDING_MODEL: string; |
| 14 | + private readonly PINECONE_INDEX: string; |
| 15 | + |
| 16 | + constructor( |
| 17 | + private configService: ConfigService, |
| 18 | + private chatProvider: ChatProvider, |
| 19 | + ) { |
| 20 | + this.CHAT_GPT_EMBEDDING_MODEL = |
| 21 | + this.configService.get<string>('CHAT_GPT_EMBEDDING_MODEL') || |
| 22 | + 'default_model'; |
| 23 | + |
| 24 | + this.PINECONE_INDEX = |
| 25 | + this.configService.get<string>('PINECONE_INDEX') || 'default_index'; |
| 26 | + } |
| 27 | + |
| 28 | + async askToPineconeDb( |
| 29 | + question: string, |
| 30 | + customIndex?: string, |
| 31 | + model?: ChatModel, |
| 32 | + ) { |
| 33 | + const embeddings = new OpenAIEmbeddings({ |
| 34 | + model: this.CHAT_GPT_EMBEDDING_MODEL, |
| 35 | + }); |
| 36 | + |
| 37 | + const pinecone = new PineconeClient(); |
| 38 | + const index = pinecone.Index(customIndex || this.PINECONE_INDEX); |
| 39 | + |
| 40 | + const vectorStore = await PineconeStore.fromExistingIndex(embeddings, { |
| 41 | + pineconeIndex: index, |
| 42 | + maxConcurrency: 5, |
| 43 | + }); |
| 44 | + |
| 45 | + //query |
| 46 | + const documents = await vectorStore.similaritySearch(question, 2); |
| 47 | + // retorna solamente las 100 primeras palabras de cada documento |
| 48 | + console.log( |
| 49 | + 'docuemnts', |
| 50 | + documents.map((doc) => doc.pageContent.slice(0, 100)), |
| 51 | + ); |
| 52 | + return this.getExactlyAnswer(documents, question, model); |
| 53 | + } |
| 54 | + |
| 55 | + async pineconeIndexes() { |
| 56 | + const pinecone = new PineconeClient(); |
| 57 | + const indexes = await pinecone.listIndexes(); |
| 58 | + return indexes.indexes; |
| 59 | + } |
| 60 | + |
| 61 | + getExactlyAnswer( |
| 62 | + documents: DocumentInterface[], |
| 63 | + question: string, |
| 64 | + model?: ChatModel, |
| 65 | + ) { |
| 66 | + const humanMessages: HumanMessage[] = []; |
| 67 | + for (const document of documents) { |
| 68 | + const message = new HumanMessage({ |
| 69 | + content: [ |
| 70 | + { |
| 71 | + type: 'text', |
| 72 | + text: document.pageContent, |
| 73 | + }, |
| 74 | + ], |
| 75 | + }); |
| 76 | + |
| 77 | + humanMessages.push(message); |
| 78 | + } |
| 79 | + |
| 80 | + if (model) { |
| 81 | + return this.chatProvider.askQuestionByModel( |
| 82 | + question, |
| 83 | + model, |
| 84 | + humanMessages, |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + return this.chatProvider.askQuestion(question, humanMessages); |
| 89 | + } |
| 90 | +} |
0 commit comments