| 1 | # Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | # All rights reserved. |
| 3 | # |
| 4 | # This source code is licensed under the terms described in the LICENSE file in |
| 5 | # the root directory of this source tree. |
| 6 | # Copyright (c) Meta Platforms, Inc. and affiliates. |
| 7 | # All rights reserved. |
| 8 | # |
| 9 | # This source code is licensed under the terms described in the LICENSE file in |
| 10 | # the root directory of this source tree. |
| 11 | from typing import List, Optional, Protocol |
| 12 | |
| 13 | from llama_models.schema_utils import json_schema_type, webmethod |
| 14 | |
| 15 | from pydantic import BaseModel, Field |
| 16 | from typing_extensions import Annotated |
| 17 | |
| 18 | from llama_models.llama3.api.datatypes import * # noqa: F403 |
| 19 | |
| 20 | |
| 21 | @json_schema_type |
| 22 | class MemoryBankDocument(BaseModel): |
| 23 | document_id: str |
| 24 | content: InterleavedTextMedia | URL |
| 25 | mime_type: str | None = None |
| 26 | metadata: Dict[str, Any] = Field(default_factory=dict) |
| 27 | |
| 28 | |
| 29 | @json_schema_type |
| 30 | class MemoryBankType(Enum): |
| 31 | vector = "vector" |
| 32 | keyvalue = "keyvalue" |
| 33 | keyword = "keyword" |
| 34 | graph = "graph" |
| 35 | |
| 36 | |
| 37 | class VectorMemoryBankConfig(BaseModel): |
| 38 | type: Literal[MemoryBankType.vector.value] = MemoryBankType.vector.value |
| 39 | embedding_model: str |
| 40 | chunk_size_in_tokens: int |
| 41 | overlap_size_in_tokens: Optional[int] = None |
| 42 | |
| 43 | |
| 44 | class KeyValueMemoryBankConfig(BaseModel): |
| 45 | type: Literal[MemoryBankType.keyvalue.value] = MemoryBankType.keyvalue.value |
| 46 | |
| 47 | |
| 48 | class KeywordMemoryBankConfig(BaseModel): |
| 49 | type: Literal[MemoryBankType.keyword.value] = MemoryBankType.keyword.value |
| 50 | |
| 51 | |
| 52 | class GraphMemoryBankConfig(BaseModel): |
| 53 | type: Literal[MemoryBankType.graph.value] = MemoryBankType.graph.value |
| 54 | |
| 55 | |
| 56 | MemoryBankConfig = Annotated[ |
| 57 | Union[ |
| 58 | VectorMemoryBankConfig, |
| 59 | KeyValueMemoryBankConfig, |
| 60 | KeywordMemoryBankConfig, |
| 61 | GraphMemoryBankConfig, |
| 62 | ], |
| 63 | Field(discriminator="type"), |
| 64 | ] |
| 65 | |
| 66 | |
| 67 | class Chunk(BaseModel): |
| 68 | content: InterleavedTextMedia |
| 69 | token_count: int |
| 70 | document_id: str |
| 71 | |
| 72 | |
| 73 | @json_schema_type |
| 74 | class QueryDocumentsResponse(BaseModel): |
| 75 | chunks: List[Chunk] |
| 76 | scores: List[float] |
| 77 | |
| 78 | |
| 79 | @json_schema_type |
| 80 | class QueryAPI(Protocol): |
| 81 | @webmethod(route="/query_documents") |
| 82 | def query_documents( |
| 83 | self, |
| 84 | query: InterleavedTextMedia, |
| 85 | params: Optional[Dict[str, Any]] = None, |
| 86 | ) -> QueryDocumentsResponse: ... |
| 87 | |
| 88 | |
| 89 | @json_schema_type |
| 90 | class MemoryBank(BaseModel): |
| 91 | bank_id: str |
| 92 | name: str |
| 93 | config: MemoryBankConfig |
| 94 | # if there's a pre-existing (reachable-from-distribution) store which supports QueryAPI |
| 95 | url: Optional[URL] = None |
| 96 | |
| 97 | |
| 98 | class Memory(Protocol): |
| 99 | @webmethod(route="/memory/create") |
| 100 | async def create_memory_bank( |
| 101 | self, |
| 102 | name: str, |
| 103 | config: MemoryBankConfig, |
| 104 | url: Optional[URL] = None, |
| 105 | ) -> MemoryBank: ... |
| 106 | |
| 107 | @webmethod(route="/memory/list", method="GET") |
| 108 | async def list_memory_banks(self) -> List[MemoryBank]: ... |
| 109 | |
| 110 | @webmethod(route="/memory/get", method="GET") |
| 111 | async def get_memory_bank(self, bank_id: str) -> Optional[MemoryBank]: ... |
| 112 | |
| 113 | @webmethod(route="/memory/drop", method="DELETE") |
| 114 | async def drop_memory_bank( |
| 115 | self, |
| 116 | bank_id: str, |
| 117 | ) -> str: ... |
| 118 | |
| 119 | # this will just block now until documents are inserted, but it should |
| 120 | # probably return a Job instance which can be polled for completion |
| 121 | @webmethod(route="/memory/insert") |
| 122 | async def insert_documents( |
| 123 | self, |
| 124 | bank_id: str, |
| 125 | documents: List[MemoryBankDocument], |
| 126 | ttl_seconds: Optional[int] = None, |
| 127 | ) -> None: ... |
| 128 | |
| 129 | @webmethod(route="/memory/update") |
| 130 | async def update_documents( |
| 131 | self, |
| 132 | bank_id: str, |
| 133 | documents: List[MemoryBankDocument], |
| 134 | ) -> None: ... |
| 135 | |
| 136 | @webmethod(route="/memory/query") |
| 137 | async def query_documents( |
| 138 | self, |
| 139 | bank_id: str, |
| 140 | query: InterleavedTextMedia, |
| 141 | params: Optional[Dict[str, Any]] = None, |
| 142 | ) -> QueryDocumentsResponse: ... |
| 143 | |
| 144 | @webmethod(route="/memory/documents/get", method="GET") |
| 145 | async def get_documents( |
| 146 | self, |
| 147 | bank_id: str, |
| 148 | document_ids: List[str], |
| 149 | ) -> List[MemoryBankDocument]: ... |
| 150 | |
| 151 | @webmethod(route="/memory/documents/delete", method="DELETE") |
| 152 | async def delete_documents( |
| 153 | self, |
| 154 | bank_id: str, |
| 155 | document_ids: List[str], |
| 156 | ) -> None: ... |