Simple, type-safe access to the ChatNoir search API.
Project description
🔍 chatnoir-api
Simple, type-safe access to the ChatNoir search API.
Working with PyTerrier? Check out the chatnoir-pyterrier
package.
Installation
Install the package from PyPI:
pip install chatnoir-api
Usage
The ChatNoir API offers two main features: search with BM25F and retrieving document contents.
Search
You can use our Python client to search for documents.
The results
object is an iterable wrapper of the search results which handles pagination for you.
List-style indexing is supported to access individual results or sub-lists of results:
from chatnoir_api.v1 import search
results = search("python library")
top10_results = results[:10]
print(top10_results)
result_1234 = results[1234]
print(result_1234)
Search a specific index
To limit your search requests to a single index (e.g., ClueWeb22 category B), set the index
parameter like this:
from chatnoir_api import Index
from chatnoir_api.v1 import search
api_key: str = "<API_KEY>"
results = search("python library", index="clueweb22/b")
Phrase search
To search for phrases, use the search_phrases
method in the same way as normal search
:
from chatnoir_api.v1 import search_phrases
results = search_phrases("python library")
API key
The public, shared, default API key comes with a limited request budget. To use the ChatNoir API more extensively, please request a dedicated API key.
Then, use the api_key
parameter to add it to your requests like this:
results = search("python library", api_key="<YOUR_API_KEY>")
Chat
To generate text with the ChatNoir Chat API you need to request an API key from the admins. With your API key, you can chat with the cat, like this:
from chatnoir_api.chat import ChatNoirChatClient
chat_client = ChatNoirChatClient(api_key="<API_KEY>")
response = chat_client.chat("how are you?")
Retrieve document contents
Often the title and ID of a document is not enough to effectively re-rank a list of search results.
To retrieve the full content or plain text for a given document you can use the html_contents
helper function.
The html_contents
function expects a ChatNoir-internal UUID, shorthand UUID, or a TREC ID and the index from which to retrieve the document.
Retrieve by TREC ID
You can retrieve a document by its TREC ID like this:
from chatnoir_api import cache_contents, Index
contents = cache_contents(
"clueweb09-en0051-90-00849",
index="clueweb09",
)
print(contents)
plain_contents = cache_contents(
"clueweb09-en0051-90-00849",
index="clueweb09",
plain=True,
)
print(plain_contents)
Retrieve by ChatNoir-internal short UUID
For newer ChatNoir versions, you can also retrieve a document by its ChatNoir-internal short UUID like this:
from chatnoir_api import cache_contents, Index, ShortUUID
contents = cache_contents(
ShortUUID("MzOlTIayX9ub7c13GLPr_g"),
index="clueweb22/b",
)
print(contents)
plain_contents = cache_contents(
ShortUUID("MzOlTIayX9ub7c13GLPr_g"),
index="clueweb22/b",
plain=True,
)
print(plain_contents)
Development
To build this package and contribute to its development you need to install the build
, and setuptools
and wheel
packages:
pip install build setuptools wheel
(On most systems, these packages are already pre-installed.)
Developer installation
Install package and test dependencies:
pip install -e .[tests]
Testing
Configure the API keys for testing:
export CHATNOIR_API_KEY="<API_KEY>"
export CHATNOIR_API_KEY_CHAT="<API_KEY>"
Verify your changes against the test suite to verify.
ruff check . # Code format and LINT
mypy . # Static typing
bandit -c pyproject.toml -r . # Security
pytest . # Unit tests
Please also add tests for your newly developed code.
Build wheels
Wheels for this package can be built with:
python -m build
Support
If you hit any problems using this package, please file an issue. We're happy to help!
License
This repository is released under the MIT license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file chatnoir_api-3.1.3.tar.gz
.
File metadata
- Download URL: chatnoir_api-3.1.3.tar.gz
- Upload date:
- Size: 24.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d393de00c3d53440b1039a0006bd92ad999764623f52732f7257e68bf4caed04 |
|
MD5 | 095f10eae5197dd204e075a45f51c97a |
|
BLAKE2b-256 | c6932b27243f57415cdff82ac9ff411a795c440c33d58ab812520532e9688f21 |
Provenance
The following attestation bundles were made for chatnoir_api-3.1.3.tar.gz
:
Publisher:
ci.yml
on chatnoir-eu/chatnoir-api
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
chatnoir_api-3.1.3.tar.gz
- Subject digest:
d393de00c3d53440b1039a0006bd92ad999764623f52732f7257e68bf4caed04
- Sigstore transparency entry: 148673996
- Sigstore integration time:
- Predicate type:
File details
Details for the file chatnoir_api-3.1.3-py3-none-any.whl
.
File metadata
- Download URL: chatnoir_api-3.1.3-py3-none-any.whl
- Upload date:
- Size: 25.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 735c8bafc78bc6a28f0dc372bc8e07ea16cafce1c31e927b29d01cef42f0b613 |
|
MD5 | 7bdb26aa68b3a3765ab2d30b676f2f94 |
|
BLAKE2b-256 | 0ce0298962e1ca7a880c53f941436dedb8831db8ce0d058178c22157eb48012d |
Provenance
The following attestation bundles were made for chatnoir_api-3.1.3-py3-none-any.whl
:
Publisher:
ci.yml
on chatnoir-eu/chatnoir-api
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
chatnoir_api-3.1.3-py3-none-any.whl
- Subject digest:
735c8bafc78bc6a28f0dc372bc8e07ea16cafce1c31e927b29d01cef42f0b613
- Sigstore transparency entry: 148673997
- Sigstore integration time:
- Predicate type: