Repository: oobabooga/text-generation-webui

Issue:
Superbooga: Cannot return the results in a contigious 2D array. Probably ef or M is too small
### Describe the bug

Superbooga: When using any data input, "Cannot return the results in a contigious 2D array. Probably ef or M is too small."

MetaIX_GPT4-X-Alpasta30b-4bit
Instruct mode
Alpaca prompt

Input chunks loaded:  10
Chunk length:  500
Chunk count:  10
Max tokens:  1-2000

Found relevant report for langchain to provide more descriptive error:  https://github.com/hwchase17/langchain/pull/1149

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Reproduction

Set chunk length, chunk count, or max tokens outside of unspecified range from any data input source in Superbooga.

### Screenshot

_No response_

### Logs

```shell
Traceback (most recent call last):
  File "/home/quietday/miniconda3/envs/textgen/lib/python3.10/site-packages/gradio/routes.py", line 395, in run_predict
    output = await app.get_blocks().process_api(
  File "/home/quietday/miniconda3/envs/textgen/lib/python3.10/site-packages/gradio/blocks.py", line 1193, in process_api
    result = await self.call_function(
  File "/home/quietday/miniconda3/envs/textgen/lib/python3.10/site-packages/gradio/blocks.py", line 930, in call_function
    prediction = await anyio.to_thread.run_sync(
  File "/home/quietday/miniconda3/envs/textgen/lib/python3.10/site-packages/anyio/to_thread.py", line 31, in run_sync
    return await get_asynclib().run_sync_in_worker_thread(
  File "/home/quietday/miniconda3/envs/textgen/lib/python3.10/site-packages/anyio/_backends/_asyncio.py", line 937, in run_sync_in_worker_thread
    return await future
  File "/home/quietday/miniconda3/envs/textgen/lib/python3.10/site-packages/anyio/_backends/_asyncio.py", line 867, in run
    result = context.run(func, *args)
  File "/home/quietday/miniconda3/envs/textgen/lib/python3.10/site-packages/gradio/utils.py", line 491, in async_iteration
    return next(iterator)
  File "/mnt/tank/AGI/text-generation-webui/modules/text_generation.py", line 164, in generate_reply
    question = apply_extensions('input', question)
  File "/mnt/tank/AGI/text-generation-webui/modules/extensions.py", line 132, in apply_extensions
    return EXTENSION_MAP[typ](*args, **kwargs)
  File "/mnt/tank/AGI/text-generation-webui/modules/extensions.py", line 58, in _apply_string_extensions
    text = getattr(extension, function_name)(text)
  File "/mnt/tank/AGI/text-generation-webui/extensions/superbooga/script.py", line 150, in input_modifier
    results = collector.get(user_input, n_results=chunk_count)
  File "/mnt/tank/AGI/text-generation-webui/extensions/superbooga/script.py", line 55, in get
    result = self.collection.query(query_texts=search_strings, n_results=n_results, include=['documents'])['documents'][0]
  File "/home/quietday/miniconda3/envs/textgen/lib/python3.10/site-packages/chromadb/api/models/Collection.py", line 203, in query
    return self._client._query(
  File "/home/quietday/miniconda3/envs/textgen/lib/python3.10/site-packages/chromadb/api/local.py", line 247, in _query
    uuids, distances = self._db.get_nearest_neighbors(
  File "/home/quietday/miniconda3/envs/textgen/lib/python3.10/site-packages/chromadb/db/clickhouse.py", line 521, in get_nearest_neighbors
    uuids, distances = index.get_nearest_neighbors(embeddings, n_results, ids)
  File "/home/quietday/miniconda3/envs/textgen/lib/python3.10/site-packages/chromadb/db/index/hnswlib.py", line 248, in get_nearest_neighbors
    database_labels, distances = self._index.knn_query(query, k=k, filter=filter_function)
RuntimeError: Cannot return the results in a contigious 2D array. Probably ef or M is too small
```


### System Info

```shell
Ubuntu 22.04, Linux 6.2.6-76060206-generic
Latest 7 May 2023 pulls

text-generation-ui(main)
9754d6a8119a4c48226d40f69fe83d9f03079016

GPTQ-for-LLaMa(triton)
dfbf9a94b7426ea36051fd4146a7602ca461d235
```



Discussion:

Resolving patch:
diff --git a/extensions/superbooga/script.py b/extensions/superbooga/script.py
--- a/extensions/superbooga/script.py
+++ b/extensions/superbooga/script.py
@@ -52,10 +52,12 @@ def add(self, texts: list[str]):
         self.collection.add(documents=texts, ids=self.ids)
 
     def get(self, search_strings: list[str], n_results: int) -> list[str]:
+        n_results = min(len(self.ids), n_results)
         result = self.collection.query(query_texts=search_strings, n_results=n_results, include=['documents'])['documents'][0]
         return result
 
     def get_ids(self, search_strings: list[str], n_results: int) -> list[str]:
+        n_results = min(len(self.ids), n_results)
         result = self.collection.query(query_texts=search_strings, n_results=n_results, include=['documents'])['ids'][0]
         return list(map(lambda x : int(x[2:]), result))
 


Test patch: