Elffuss BraidHub

kikocis/llama-stack public

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