1212import yaml
1313from llama_index .core import Settings , load_index_from_storage
1414from llama_index .core .llms .utils import resolve_llm
15+ from llama_index .core .schema import NodeWithScore , TextNode
1516from llama_index .core .storage .storage_context import StorageContext
1617from llama_index .embeddings .huggingface import HuggingFaceEmbedding
1718from llama_index .vector_stores .faiss import FaissVectorStore
@@ -35,20 +36,34 @@ def _llama_index_query(args: argparse.Namespace) -> None:
3536
3637 if args .node is not None :
3738 node = storage_context .docstore .get_node (args .node )
38- result = {
39- "query" : args .query ,
40- "type" : "single_node" ,
41- "node_id" : args .node ,
42- "node" : {
43- "id" : node .node_id ,
44- "text" : node .text ,
45- "metadata" : node .metadata if hasattr (node , "metadata" ) else {},
46- },
47- }
48- if args .json :
49- print (json .dumps (result , indent = 2 ))
39+ if isinstance (node , TextNode ):
40+ result = {
41+ "query" : args .query ,
42+ "type" : "single_node" ,
43+ "node_id" : args .node ,
44+ "node" : {
45+ "id" : node .node_id ,
46+ "text" : node .text ,
47+ "metadata" : node .metadata if hasattr (node , "metadata" ) else {},
48+ },
49+ }
50+ if args .json :
51+ print (json .dumps (result , indent = 2 ))
52+ else :
53+ print (node )
5054 else :
51- print (node )
55+ logging .warning (
56+ f"Node { args .node } is not a TextNode, type: { type (node ).__name__ } "
57+ )
58+ if args .json :
59+ result = {
60+ "query" : args .query ,
61+ "type" : "single_node" ,
62+ "node_id" : args .node ,
63+ "error" : f"Node is not a TextNode (type: { type (node ).__name__ } )" ,
64+ }
65+ print (json .dumps (result , indent = 2 ))
66+ exit (1 )
5267 else :
5368 retriever = vector_index .as_retriever (similarity_top_k = args .top_k )
5469 nodes = retriever .retrieve (args .query )
@@ -88,13 +103,18 @@ def _llama_index_query(args: argparse.Namespace) -> None:
88103 "nodes" : [],
89104 }
90105 for node in nodes : # type: ignore
91- node_data = {
92- "id" : node .node_id ,
93- "score" : node .score ,
94- "text" : node .text ,
95- "metadata" : node .metadata if hasattr (node , "metadata" ) else {},
96- }
97- result ["nodes" ].append (node_data )
106+ if isinstance (node , NodeWithScore ):
107+ node_data = {
108+ "id" : node .node_id ,
109+ "score" : node .score ,
110+ "text" : node .text ,
111+ "metadata" : node .metadata if hasattr (node , "metadata" ) else {},
112+ }
113+ result ["nodes" ].append (node_data )
114+ else :
115+ logging .debug (
116+ f"Skipping node of type { type (node ).__name__ } , expected NodeWithScore"
117+ )
98118
99119 if args .json :
100120 print (json .dumps (result , indent = 2 ))
@@ -134,7 +154,7 @@ def _llama_stack_query(args: argparse.Namespace) -> None:
134154 yaml .safe_dump (cfg , open (cfg_file , "w" , encoding = "utf-8" ))
135155
136156 stack_lib = importlib .import_module ("llama_stack" )
137- client = stack_lib .distribution .library_client .LlamaStackAsLibraryClient (cfg_file )
157+ client = stack_lib .core .library_client .LlamaStackAsLibraryClient (cfg_file )
138158 client .initialize ()
139159
140160 # No need to register the DB as it's defined in llama-stack.yaml
0 commit comments