1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15- from typing import Any , Callable , Literal
15+ from __future__ import annotations
16+
17+ from typing import Any , Callable , Literal , Union
1618
1719from pydantic import BaseModel , Field
18- from typing_extensions import Union
1920
2021from veadk .knowledgebase .backends .base_backend import BaseKnowledgebaseBackend
2122from veadk .knowledgebase .entry import KnowledgebaseEntry
@@ -54,11 +55,11 @@ def _get_backend_cls(backend: str) -> type[BaseKnowledgebaseBackend]:
5455 raise ValueError (f"Unsupported knowledgebase backend: { backend } " )
5556
5657
57- def build_knowledgebase_index ( app_name : str ):
58- return f"veadk_kb_ { app_name } "
58+ class KnowledgeBase ( BaseModel ):
59+ name : str = "user_knowledgebase "
5960
61+ description : str = "This knowledgebase stores some user-related information."
6062
61- class KnowledgeBase (BaseModel ):
6263 backend : Union [
6364 Literal ["local" , "opensearch" , "viking" , "redis" ], BaseKnowledgebaseBackend
6465 ] = "local"
@@ -73,9 +74,7 @@ class KnowledgeBase(BaseModel):
7374 """Configuration for the backend"""
7475
7576 top_k : int = 10
76- """Number of top similar documents to retrieve during search.
77-
78- Default is 10."""
77+ """Number of top similar documents to retrieve during search"""
7978
8079 app_name : str = ""
8180
@@ -85,38 +84,27 @@ class KnowledgeBase(BaseModel):
8584 def model_post_init (self , __context : Any ) -> None :
8685 if isinstance (self .backend , BaseKnowledgebaseBackend ):
8786 self ._backend = self .backend
87+ self .index = self ._backend .index
8888 logger .info (
8989 f"Initialized knowledgebase with provided backend instance { self ._backend .__class__ .__name__ } "
9090 )
9191 return
9292
93- # must provide at least one of them
94- if not self .app_name and not self .index :
95- raise ValueError (
96- "Either `app_name` or `index` must be provided one of them."
97- )
98-
99- # priority use index
100- if self .app_name and self .index :
101- logger .warning (
102- "`app_name` and `index` are both provided, using `index` as the knowledgebase index name."
103- )
93+ # Once user define backend config, use it directly
94+ if self .backend_config :
95+ self ._backend = _get_backend_cls (self .backend )(** self .backend_config )
96+ return
10497
105- # generate index name if `index` not provided but `app_name` is provided
106- if self .app_name and not self .index :
107- self .index = build_knowledgebase_index (self .app_name )
108- logger .info (
109- f"Knowledgebase index is set to { self .index } (generated by the app_name: { self .app_name } )."
110- )
98+ self .index = self .index or self .app_name
99+ if not self .index :
100+ raise ValueError ("Either `index` or `app_name` must be provided." )
111101
112102 logger .info (
113- f"Initializing knowledgebase: backend={ self .backend } top_k={ self .top_k } "
114- )
115- self ._backend = _get_backend_cls (self .backend )(
116- index = self .index , ** self .backend_config if self .backend_config else {}
103+ f"Initializing knowledgebase: backend={ self .backend } index={ self .index } top_k={ self .top_k } "
117104 )
105+ self ._backend = _get_backend_cls (self .backend )(index = self .index )
118106 logger .info (
119- f"Initialized knowledgebase with backend { self ._backend .__class__ .__name__ } "
107+ f"Initialized knowledgebase with backend { self .backend .__class__ .__name__ } "
120108 )
121109
122110 def add_from_directory (self , directory : str , ** kwargs ) -> bool :
@@ -133,8 +121,7 @@ def add_from_text(self, text: str | list[str], **kwargs) -> bool:
133121
134122 def search (self , query : str , top_k : int = 0 , ** kwargs ) -> list [KnowledgebaseEntry ]:
135123 """Search knowledge from knowledgebase"""
136- if top_k == 0 :
137- top_k = self .top_k
124+ top_k = top_k if top_k != 0 else self .top_k
138125
139126 _entries = self ._backend .search (query = query , top_k = top_k , ** kwargs )
140127
0 commit comments