Search code examples
pythonazureazure-web-app-servicechromadb

ConnectTimeout Error When Connecting to a ChromaDB Client that is hosted on Azure Web App


When trying to connect via the client to my ChromaDB instance that is hosted on Azure Web App, I keep getting a connection timeout error

import chromadb

client = chromadb.HttpClient(host="https://chroma-db.azurewebsites.net")

client.list_collections()


Part of the error log includes:

 Traceback (most recent call last):
...
TimeoutError: [Errno 60] Operation timed out
...
urllib3.exceptions.ConnectTimeoutError: (<urllib3.connection.HTTPConnection object at 0x...>, 'Connection to chroma-db.azurewebsites.net timed out. (connect timeout=None)')
...
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='chroma-db.azurewebsites.net', port=8000): Max retries exceeded with url: /api/v1/collections (Caused by ConnectTimeoutError(<urllib3.connection.HTTPConnection object at 0x...>, 'Connection to my-chroma-db.azurewebsites.net timed out. (connect timeout=None)'))
...
requests.exceptions.ConnectTimeout: HTTPConnectionPool(host='chroma-db.azurewebsites.net', port=8000): Max retries exceeded with url: /api/v1/collections (Caused by ConnectTimeoutError(<urllib3.connection.HTTPConnection object at 0x...>, 'Connection to chroma-db.azurewebsites.net timed out. (connect timeout=None)'))

I tried adding hostname, changing the URL to include https protocol, tries different port numbers.

What changes should I make in my Azure Web App Configuration and/or in my code to connect via the client?

I'm using ChromaDB v.3.9.10


Solution

  • I also got the same error with your code in my environment.

    The error might occur because of two reasons:

    1. If you don't log in to your Azure account, you will get the same error.

    enter image description here

    1. If the Azure web app URL is incorrect, you will also get this error.
      So, make sure your login credentials and web app URL are correct.

    enter image description here enter image description here

    I tried the below sample code to query the chromaDB with the Azure web app.

    Code :

    import chromadb
    from flask import Flask, render_template, jsonify
    
    app = Flask(__name__)
    
    chroma_db_url = "https://<web_app_name>.azurewebsites.net/"
    
    chroma_client = chromadb.Client()
    
    collection = chroma_client.create_collection(name="my_collection")
    
    collection.add(
        documents=["This is a document", "This is another document"],
        metadatas=[{"source": "my_source"}, {"source": "my_source"}],
        ids=["id1", "id2"]
    )
    
    query_text = "This is a query document"
    
    @app.route('/')
    def index():
        return render_template('query.html')
    
    @app.route('/query')
    def query_chromadb():
        try:
            results = collection.query(query_texts=[query_text], n_results=2)
    
            return jsonify(results)
    
        except Exception as e:
            return jsonify({"error": "An error occurred while processing the query: " + str(e)})
    
    if __name__ == '__main__':
        app.run()
    

    templates/query.html :

    <!DOCTYPE html>
    <html>
    <head>
        <title>ChromaDB Query</title>
    </head>
    <body>
        <h1>ChromaDB Query</h1>
        <p>Click the button below to execute the predefined query:</p>
        <form method="GET" action="/query">
            <input type="submit" value="Execute Query">
        </form>
    </body>
    </html>
    

    requirements.txt :

    chromadb==0.4.12
    Flask==2.0.1
    requests>=2.28
    

    Output :

    It runs successfully as shown below.

    enter image description here

    I got the below output in the browser with the above output URL, then I clicked on Execute Query to get the query.

    enter image description here

    I got the query output as below:

    enter image description here