1111
1212import yaml
1313from llama_index .core import Settings , load_index_from_storage
14+ from llama_index .core .schema import NodeWithScore , TextNode
1415from llama_index .core .llms .utils import resolve_llm
1516from llama_index .core .storage .storage_context import StorageContext
1617from llama_index .embeddings .huggingface import HuggingFaceEmbedding
@@ -35,20 +36,32 @@ 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 (f"Node { args .node } is not a TextNode, type: { type (node ).__name__ } " )
56+ if args .json :
57+ result = {
58+ "query" : args .query ,
59+ "type" : "single_node" ,
60+ "node_id" : args .node ,
61+ "error" : f"Node is not a TextNode (type: { type (node ).__name__ } )" ,
62+ }
63+ print (json .dumps (result , indent = 2 ))
64+ exit (1 )
5265 else :
5366 retriever = vector_index .as_retriever (similarity_top_k = args .top_k )
5467 nodes = retriever .retrieve (args .query )
@@ -88,13 +101,16 @@ def _llama_index_query(args: argparse.Namespace) -> None:
88101 "nodes" : [],
89102 }
90103 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 )
104+ if isinstance (node , NodeWithScore ):
105+ node_data = {
106+ "id" : node .node_id ,
107+ "score" : node .score ,
108+ "text" : node .text ,
109+ "metadata" : node .metadata if hasattr (node , "metadata" ) else {},
110+ }
111+ result ["nodes" ].append (node_data )
112+ else :
113+ logging .debug (f"Skipping node of type { type (node ).__name__ } , expected NodeWithScore" )
98114
99115 if args .json :
100116 print (json .dumps (result , indent = 2 ))
@@ -134,7 +150,7 @@ def _llama_stack_query(args: argparse.Namespace) -> None:
134150 yaml .safe_dump (cfg , open (cfg_file , "w" , encoding = "utf-8" ))
135151
136152 stack_lib = importlib .import_module ("llama_stack" )
137- client = stack_lib .distribution .library_client .LlamaStackAsLibraryClient (cfg_file )
153+ client = stack_lib .core .library_client .LlamaStackAsLibraryClient (cfg_file )
138154 client .initialize ()
139155
140156 # No need to register the DB as it's defined in llama-stack.yaml
0 commit comments