logan-markewich/llama_index_starter_pack

OSError: [WinError 10049] The requested address is not valid in its context

NLBGoncalves opened this issue · 21 comments

when I try to initialize a python folder flask_demo.py:
import os
from multiprocessing.managers import BaseManager
from flask import Flask, request, jsonify, make_response
from flask_cors import CORS
from werkzeug.utils import secure_filename

app = Flask(name)
CORS(app)

initialize manager connection

NOTE: you might want to handle the password in a less hardcoded way

manager = BaseManager(('', 5602), b'password')
manager.register('query_index')
manager.register('insert_into_index')
manager.register('get_documents_list')
manager.connect()

@app.route("/query", methods=["GET"])
def query_index():
global manager
query_text = request.args.get("text", None)
if query_text is None:
return "No text found, please include a ?text=blah parameter in the URL", 400

response = manager.query_index(query_text)._getvalue()
response_json = {
    "text": str(response),
    "sources": [{"text": str(x.source_text), 
                 "similarity": round(x.similarity, 2),
                 "doc_id": str(x.doc_id),
                 "start": x.node_info['start'],
                 "end": x.node_info['end']
                } for x in response.source_nodes]
}
return make_response(jsonify(response_json)), 200

@app.route("/uploadFile", methods=["POST"])
def upload_file():
global manager
if 'file' not in request.files:
return "Please send a POST request with a file", 400

filepath = None
try:
    uploaded_file = request.files["file"]
    filename = secure_filename(uploaded_file.filename)
    filepath = os.path.join('documents', os.path.basename(filename))
    uploaded_file.save(filepath)

    if request.form.get("filename_as_doc_id", None) is not None:
        manager.insert_into_index(filepath, doc_id=filename)
    else:
        manager.insert_into_index(filepath)
except Exception as e:
    # cleanup temp file
    if filepath is not None and os.path.exists(filepath):
        os.remove(filepath)
    return "Error: {}".format(str(e)), 500

# cleanup temp file
if filepath is not None and os.path.exists(filepath):
    os.remove(filepath)

return "File inserted!", 200

@app.route("/getDocuments", methods=["GET"])
def get_documents():
document_list = manager.get_documents_list()._getvalue()

return make_response(jsonify(document_list)), 200

@app.route("/")
def home():
return "Hello, World! Welcome to the llama_index docker image!"

if name == "main":
app.run(host="0.0.0.0", port=5601)

I get this error in the terminal:

python flask_demo.py
Traceback (most recent call last):
File "C:\Users\Tuindula\Desktop\flask_react\flask_demo.py", line 16, in
manager.connect()
File "C:\Users\Tuindula\AppData\Local\Programs\Python\Python311\Lib\multiprocessing\managers.py", line 532, in connect
conn = Client(self._address, authkey=self._authkey)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Tuindula\AppData\Local\Programs\Python\Python311\Lib\multiprocessing\connection.py", line 501, in Client
c = SocketClient(address)
^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Tuindula\AppData\Local\Programs\Python\Python311\Lib\multiprocessing\connection.py", line 629, in SocketClient
s.connect(address)
OSError: [WinError 10049] The requested address is not valid in its context

can someone help me!

Did you launch the index_server.py before running the flask server?

No, I ran flask_demo.py first

Try launching index_server.py first (run it in another terminal or in the background)

Then launch the flask demo. Should work!

even initializing the index_server.py folder first, it stays for more than an hour and does not output this information in the terminal: python index_server.py
initializing index...
server started...

Right, that means it's running 💪

So then if you open another terminal and run python ./flask_demo.py then the flask demo will work

The flask server connects to the index server.

So the index server has to be running in order to launch the flask server

I get this error when initializing index, I think it's version compatibility:
python index_server.py
initializing index...
Traceback (most recent call last):
File "C:\Users\Tuindula\Desktop\flask_react\index_server.py", line 74, in initialize_index()
File "C:\Users\Tuindula\Desktop\flask_react\index_server.py", line 26, in initialize_index
index = load_index_from_storage(StorageContext.from_defaults(persist_dir=index_name), service_context=service_context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Tuindula\AppData\Local\Programs\Python\Python311\Lib\site-packages\llama_index\indices\loading.py", line 33, in load_index_from_storage
indices = load_indices_from_storage(storage_context, index_ids=index_ids, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Tuindula\AppData\Local\Programs\Python\Python311\Lib\site-packages\llama_index\indices\loading.py", line 64, in load_indices_from_storage
index_structs = storage_context.index_store.index_structs()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Tuindula\AppData\Local\Programs\Python\Python311\Lib\site-packages\llama_index\storage\index_store\keyval_index_store.py", line 75, in index_structs
return [json_to_index_struct(json) for json in jsons.values()]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Tuindula\AppData\Local\Programs\Python\Python311\Lib\site-packages\llama_index\storage\index_store\keyval_index_store.py", line 75, in
return [json_to_index_struct(json) for json in jsons.values()]
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Tuindula\AppData\Local\Programs\Python\Python311\Lib\site-packages\llama_index\storage\index_store\utils.py", line 18, in json_to_index_struct
return cls.from_dict(data_dict)
^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Tuindula\AppData\Local\Programs\Python\Python311\Lib\site-packages\dataclasses_json\api.py", line 72, in from_dict
return _decode_dataclass(cls, kvs, infer_missing)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Tuindula\AppData\Local\Programs\Python\Python311\Lib\site-packages\dataclasses_json\core.py", line 137, in _decode_dataclass
kvs = {decode_names.get(k, k): v for k, v in kvs.items()}
^^^^^^^^^
AttributeError: 'str' object has no attribute 'items'

Try deleting the "./savex_index" folder, maybe something got corrupted from before

ok i will do that

OK! One last issue, and I think it's because of windows

In flask_demo.py near the bottom, change "0.0.0.0" to "localhost"

image
I'm using windows 10, and it seems to me that the problem could be this, because even so the initialization code didn't work

I will try on another computer

Hmm sounds good! Another option for windows is trying "127.0.0.1", I forget which ip address works with windows lol

keeps giving the same error

what operating systems are used?

I personally use WSL Ubuntu on windows for everything.

Even "127.0.0.1" does not work?

yes even using "127.0.0.1", it's not working

Ah I overlooked one thing,

So, change flask_demo.py to have app.run(host="127.0.0.1", port=5601)

Then, in addition, change the top of flask_demo.py to have manager = BaseManager(('127.0.0.1', 5602), b'password')

Then, in index_server.py, modify the manager to be the same: manager = BaseManager(('127.0.0.1', 5602), b'password')

I just tested in powershell, seems to work

image
I did it and it worked very well, I am very happy for your help, leave your social networks to me, I live in Angola (Africa)

Perfect! Glad it works now 👍🏻

Feel free to follow on LinkedIn, or follow LlamaIndex on Twitter :)

https://www.linkedin.com/in/logan-markewich/
https://twitter.com/llama_index