Skip to content

Commit 27c7cf0

Browse files
committed
cleanup and documentation.
1 parent bffc7b0 commit 27c7cf0

5 files changed

Lines changed: 76 additions & 4 deletions

File tree

docs/cli.md

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* [Vectorising Your Code](#vectorising-your-code)
1919
* [File Specs](#file-specs)
2020
* [Making a Query](#making-a-query)
21+
* [Query Rewriting](#query-rewriting)
2122
* [Listing All Collections](#listing-all-collections)
2223
* [Removing a Collection](#removing-a-collection)
2324
* [Checking Project Setup](#checking-project-setup)
@@ -356,7 +357,13 @@ The JSON configuration file may hold the following values:
356357
command line flag. You can also set this to `_auto`, which uses
357358
[charset-normalizer](https://charset-normalizer.readthedocs.io/en/latest/index.html)
358359
to automatically detect the encoding, but this is not very accurate,
359-
especially on small files.
360+
especially on small files;
361+
- `rewriter`: string, the type of rewriter to use. Currently the only supported
362+
value is `OpenAIRewriter`, which uses a openai-compatible LLM provider as the
363+
rewriter;
364+
- `rewriter_params`: dictionary, the options to be used for the construction of
365+
the rewriter. The options are documented in [the source
366+
code](../src/vectorcode/rewriter/).
360367

361368
See
362369
[the wiki](https://github.com/Davidyz/VectorCode/wiki/Default-Configuration#default-cli-configuration)
@@ -461,6 +468,47 @@ the number of retrieved chunks when you use `--include chunk`. For the sake of
461468
completeness, the first and last lines of a chunk will be completed to include
462469
the whole lines if the chunker broke the text from mid-line.
463470
471+
#### Query Rewriting
472+
473+
When your query messages are noisy (for example, containing a lot of symbols
474+
that are not relevant to the RAG tasks), the retrieval results may be
475+
compromised. To address this, you can try to use
476+
[query rewriting](https://docs.llamaindex.ai/en/stable/examples/query_transformations/query_transform_cookbook/#query-rewriting).
477+
The VectorCode implementation of query rewriting uses an LLM to rewrite your
478+
search query so that it contains a curated list of keywords and (hopefully) will
479+
improve your search results. To do this, you'd need to [configure your rewriter](#configuring-vectorcode)
480+
and pass the `--rewrite` flag to your query command. For example:
481+
```json5
482+
// .vectorcode/config.json
483+
// `OpenAIRewriter` works for any openai-compatible LLM API service that works
484+
// provides structured_output.
485+
{
486+
"rewriter": "OpenAIRewriter",
487+
"rewriter_params": {
488+
"client_kwargs": {
489+
// see openai.Client
490+
// https://github.com/openai/openai-python/blob/67997a4ec1ebcdf8e740afb0d0b2e37897657bde/src/openai/_client.py#L80
491+
"base_url": "https://api.siliconflow.cn/v1",
492+
"api_key": "$SILICONFLOW_API_KEY"
493+
},
494+
"completion_kwargs": {
495+
// see openai.Client.beta.chat.completions.parse
496+
// https://github.com/openai/openai-python/blob/main/helpers.md#structured-outputs-parsing-helpers
497+
"model": "Qwen/Qwen2.5-7B-Instruct",
498+
"temperature": 0
499+
}
500+
}
501+
}
502+
```
503+
504+
And when making a query, if you pass the `--rewrite` flag, VectorCode will send
505+
your query message to the LLM and get a list of strings, which it will use as
506+
the query for the search:
507+
508+
```bash
509+
vectorcode query "reranker implementation" "class" "struct" "transformers" --rewrite
510+
```
511+
464512
### Listing All Collections
465513

466514
You can use `vectorcode ls` command to list all collections in your ChromaDB.

src/vectorcode/rewriter/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ def get_rewriter(configs: Config) -> Optional[RewriterBase]:
2525
rewriter_cls = getattr(sys.modules[__name__], configs.rewriter)
2626
if issubclass(rewriter_cls, RewriterBase):
2727
logger.info(f"Loaded {configs.rewriter}")
28-
return rewriter_cls(configs)
28+
return rewriter_cls.create(configs)
2929
raise RewriterError(f"Failed to find {configs.rewriter}!")

src/vectorcode/rewriter/base.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,20 @@ def __init__(self, config: Config) -> None:
88
super().__init__()
99
self.config = config
1010

11+
@classmethod
12+
def create(cls, configs: Config):
13+
try:
14+
return cls(configs)
15+
except Exception as e:
16+
e.add_note(
17+
"\n"
18+
+ (
19+
cls.__doc__
20+
or f"There was an issue initialising {cls}. Please doublecheck your configuration."
21+
)
22+
)
23+
raise
24+
1125
@abstractmethod
1226
async def rewrite(self, original_query: list[str]) -> list[str]:
1327
raise NotImplementedError

src/vectorcode/rewriter/openai.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ class _NewQuery(BaseModel):
1717

1818

1919
class OpenAIRewriter(RewriterBase):
20+
"""
21+
OpenAIRewriter class is an adapter for openai-compatible API services that provides
22+
structured output support. The `rewriter_params` dictionary accepts 3 keys:
23+
- `client_kwargs`: dictionary, containing arguments that are passed to `openai.Client`.
24+
See https://github.com/openai/openai-python/blob/67997a4ec1ebcdf8e740afb0d0b2e37897657bde/src/openai/_client.py#L80;
25+
- `completion_kwargs`: dictionary, containing arguments that are passed to `openai.Client.beta.chat.completions.parse`.
26+
See https://github.com/openai/openai-python/blob/main/helpers.md#structured-outputs-parsing-helpers.
27+
- `system_prompt`: string, the system prompt that contains the guidelines for rewriting the query.
28+
"""
29+
2030
def __init__(self, config: Config) -> None:
2131
super().__init__(config)
2232
self.client = openai.Client(
@@ -65,7 +75,7 @@ async def rewrite(self, original_query: list[str]):
6575
)
6676
if comp is None or len(comp.choices) == 0:
6777
logger.info(
68-
"Recieved no rewritten query. Fallingback to original_query."
78+
"Received no rewritten query. Fallingback to original_query."
6979
)
7080
return original_query
7181
choice = comp.choices[0].message

tests/rewriter/test_rewriter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def test_get_openai_rewriter():
2121
patch("vectorcode.rewriter.issubclass") as mock_issubclass,
2222
):
2323
mock_rewriter = MagicMock()
24-
mock_openai_cls.return_value = mock_rewriter
24+
mock_openai_cls.create.return_value = mock_rewriter
2525
mock_issubclass.return_value = True
2626
assert get_rewriter(Config(rewriter="OpenAIRewriter")) == mock_rewriter
2727

0 commit comments

Comments
 (0)