| 544 | |
| 545 | @singleton |
| 546 | class LangChainEmbeddingHelper: |
| 547 | def __init__(self): |
| 548 | # Map{dbId, db} |
| 549 | self.dbMap = {} |
| 550 | |
| 551 | def from_documents(self, documents: list[Document], dbId: str, embedding=OpenAIEmbeddings(), |
| 552 | persist_directory: str = None): |
| 553 | if persist_directory is None: |
| 554 | persist_directory = "./embedding" + dbId |
| 555 | db = Chroma.from_documents(documents, embedding=embedding, persist_directory=persist_directory) |
| 556 | self.dbMap[dbId] = db |
| 557 | |
| 558 | def from_texts(self, texts: list[str], dbId: str, embedding=OpenAIEmbeddings(), persist_directory: str = None): |
| 559 | if persist_directory is None: |
| 560 | persist_directory = "./embedding" + dbId |
| 561 | db = Chroma.from_texts(texts, embedding=embedding, persist_directory=persist_directory) |
| 562 | self.dbMap[dbId] = db |
| 563 | |
| 564 | def similarity_search(self, query: str, fileID: str, topK: int = 4) -> list[str]: |
| 565 | db = self.dbMap[fileID] |
| 566 | docs = db.similarity_search(query, k=topK) |
| 567 | texts = [] |
| 568 | for doc in docs: |
| 569 | texts.append(doc.page_content) |
| 570 | return texts |
| 571 | |
| 572 | |
| 573 | @singleton |
nothing calls this directly
no outgoing calls
no test coverage detected