main
py 46 lines 1.34 KB
Raw
1 # SPDX-License-Identifier: Apache-2.0
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3 """Example of using the OpenAI entrypoint's rerank API which is compatible with
4 the Cohere SDK: https://github.com/cohere-ai/cohere-python
5 Note that `pip install cohere` is needed to run this example.
6
7 run: vllm serve BAAI/bge-reranker-base
8 """
9
10 import cohere
11 from cohere import Client, ClientV2
12
13 model = "BAAI/bge-reranker-base"
14
15 query = "What is the capital of France?"
16
17 documents = [
18 "The capital of France is Paris",
19 "Reranking is fun!",
20 "vLLM is an open-source framework for fast AI serving",
21 ]
22
23
24 def cohere_rerank(
25 client: Client | ClientV2, model: str, query: str, documents: list[str]
26 ) -> dict:
27 return client.rerank(model=model, query=query, documents=documents)
28
29
30 def main():
31 # cohere v1 client
32 cohere_v1 = cohere.Client(base_url="http://localhost:8000", api_key="sk-fake-key")
33 rerank_v1_result = cohere_rerank(cohere_v1, model, query, documents)
34 print("-" * 50)
35 print("rerank_v1_result:\n", rerank_v1_result)
36 print("-" * 50)
37
38 # or the v2
39 cohere_v2 = cohere.ClientV2("sk-fake-key", base_url="http://localhost:8000")
40 rerank_v2_result = cohere_rerank(cohere_v2, model, query, documents)
41 print("rerank_v2_result:\n", rerank_v2_result)
42 print("-" * 50)
43
44
45 if __name__ == "__main__":
46 main()