Find answers from the community

Updated 11 months ago

hi there! how can i convert llama_index.

hi there! how can i convert llama_index.schema.TextNode
to str or list?
L
a
31 comments
do you want just the node text? Or the entire structure?
the entire structure
i have been trying with a lot of ways but nothing @Logan M
will convert to string
then TextNode.parse_raw(json_str) will recreate it
it appears that i cant in that way
right, its a list

nodes_str = [x.json() for x in nodes]
but in that case it will be a list and i need in string
if now i do str(nodes_str) i will have a lot of \
json.dumps(nodes_str)
what i was talking about
i need the same way but in string format
what i want to do it is save it in a azure container, for that i need to convert it to string
This works fine for me, ez pz

Plain Text
>>> from llama_index.schema import TextNode
>>> nodes = [TextNode(text="test1"), TextNode(text="test2")]
>>> import json
>>> nodes_str = [x.dict() for x in nodes]
>>> data = json.dumps(nodes_str)
>>> data
'[{"id_": "d44c4b9c-5803-4d32-95a7-414b5a520138", "embedding": null, "metadata": {}, "excluded_embed_metadata_keys": [], "excluded_llm_metadata_keys": [], "relationships": {}, "text": "test1", "start_char_idx": null, "end_char_idx": null, "text_template": "{metadata_str}\\n\\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\\n", "class_name": "TextNode"}, {"id_": "001fea50-26cf-4a13-9648-b56a206d9a99", "embedding": null, "metadata": {}, "excluded_embed_metadata_keys": [], "excluded_llm_metadata_keys": [], "relationships": {}, "text": "test2", "start_char_idx": null, "end_char_idx": null, "text_template": "{metadata_str}\\n\\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\\n", "class_name": "TextNode"}]'
>>> nodes_str = json.loads(data)
>>> nodes = [TextNode.parse_obj(x) for x in nodes_str]
>>> nodes[0].text
'test1'
>>> 
yeah this works! here is x.dict instead of x.json
i just want to save the nodes using cloud and then recover it as it was
you need to iterate
nodes_str = json.loads(data)
nodes = [TextNode.parse_obj(x) for x in nodes_str]
If you really wanted, we have various storage layers that automate all this

Docstores (mongodb, redis, to disk, postgres, firestore, dynamodb)

Vector Stores (basically any vector store you can think of)
thanks but not👍 now it works
maybe i will mention you in my Bachelor's Thesis😅
Add a reply
Sign up and join the conversation on Discord