Tonic commited on
Commit
ebe18c1
1 Parent(s): 8c90037

try chroma direct loading and wait and run

Browse files
Files changed (1) hide show
  1. app.py +12 -15
app.py CHANGED
@@ -125,33 +125,30 @@ def load_documents(file_path: str, mode: str = "elements"):
125
  docs = loader.load()
126
  return [doc.page_content for doc in docs]
127
 
128
- def wait_for_chroma_server(host, port, retries=5, delay=5):
129
- url = f"http://{host}:{port}/"
130
  for _ in range(retries):
131
  try:
132
- response = httpx.get(url)
133
- if response.status_code == 200:
134
- print("Chroma server is up and running!")
135
- return True
136
- except httpx.HTTPStatusError as e:
137
- print(f"Attempt to connect to Chroma server failed with status code: {e.response.status_code}")
138
- except httpx.RequestError as e:
139
  print(f"Attempt to connect to Chroma server failed: {e}")
140
  time.sleep(delay)
141
  print("Failed to connect to Chroma server after multiple attempts.")
142
  return False
143
-
144
-
145
  def initialize_chroma(collection_name: str, embedding_function: MyEmbeddingFunction):
146
  host = 'localhost'
147
  port = 8000
148
- if not wait_for_chroma_server(host, port):
149
- raise ConnectionError("Could not connect to Chroma server. Ensure it is running.")
150
 
151
  client = HttpClient(host=host, port=port, settings=Settings(allow_reset=True, anonymized_telemetry=False))
152
- client.reset()
 
 
 
 
153
  collection = client.create_collection(collection_name)
154
- return client, collection
155
 
156
  def add_documents_to_chroma(client, collection, documents: list, embedding_function: MyEmbeddingFunction):
157
  for doc in documents:
 
125
  docs = loader.load()
126
  return [doc.page_content for doc in docs]
127
 
128
+ def wait_for_chroma_server(client, retries=10, delay=0.5):
 
129
  for _ in range(retries):
130
  try:
131
+ client.heartbeat()
132
+ print("Chroma server is up and running!")
133
+ return True
134
+ except Exception as e:
 
 
 
135
  print(f"Attempt to connect to Chroma server failed: {e}")
136
  time.sleep(delay)
137
  print("Failed to connect to Chroma server after multiple attempts.")
138
  return False
139
+
 
140
  def initialize_chroma(collection_name: str, embedding_function: MyEmbeddingFunction):
141
  host = 'localhost'
142
  port = 8000
 
 
143
 
144
  client = HttpClient(host=host, port=port, settings=Settings(allow_reset=True, anonymized_telemetry=False))
145
+
146
+ if not wait_for_chroma_server(client):
147
+ raise ConnectionError("Could not connect to Chroma server. Ensure it is running.")
148
+
149
+ client.reset() # Empties and completely resets the database
150
  collection = client.create_collection(collection_name)
151
+ return client, collection
152
 
153
  def add_documents_to_chroma(client, collection, documents: list, embedding_function: MyEmbeddingFunction):
154
  for doc in documents: