From c633ded3d8a0c1a1085bbfbbca6536b5005e73d9 Mon Sep 17 00:00:00 2001 From: Anushree Bannadabhavi Date: Wed, 10 Apr 2024 05:55:26 -0400 Subject: [PATCH] Add cohere ranker integration information (#217) --- integrations/cohere.md | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/integrations/cohere.md b/integrations/cohere.md index b1659448..cb962dfa 100644 --- a/integrations/cohere.md +++ b/integrations/cohere.md @@ -24,12 +24,13 @@ toc: true - [Usage](#usage) - [Embedding Models](#embedding-models) - [Generative Models (LLMs)](#generative-models-llms) + - [Ranker Models](#ranker-models) - [Haystack 1.x](#haystack-1x) - [Installation (1.x)](#installation-1x) - [Usage (1.x)](#usage-1x) - [Embedding Models](#embedding-models-1) - [Generative Models (LLMs)](#generative-models-llms-1) - - [Ranker Models](#ranker-models) + - [Ranker Models](#ranker-models-1) ## Haystack 2.0 @@ -134,6 +135,39 @@ res = pipe.run(data={"prompt_builder": {"template_variables": {"country": "Germa print(res) ``` +#### Ranker Models + +To use `/ranker` models from Cohere, initialize a [CohereRanker](https://docs.haystack.deepset.ai/docs/cohereranker) with the model name. By default, the Cohere API key with be automatically read from either the `COHERE_API_KEY` environment variable or the `CO_API_KEY` environment variable. You can then use this `CohereRanker` to rank documents based on semantic relevance to a specified query. + +Below is the example indexing pipeline with `InMemoryDocumentStore`, `InMemoryBM25Retriever` and `CohereRanker`: + +```python +from haystack import Document, Pipeline +from haystack.components.retrievers.in_memory import InMemoryBM25Retriever +from haystack.document_stores.in_memory import InMemoryDocumentStore +from haystack_integrations.components.rankers.cohere import CohereRanker + +docs = [ + Document(content="Paris is in France"), + Document(content="Berlin is in Germany"), + Document(content="Lyon is in France"), +] +document_store = InMemoryDocumentStore() +document_store.write_documents(docs) + +retriever = InMemoryBM25Retriever(document_store=document_store) +ranker = CohereRanker() + +document_ranker_pipeline = Pipeline() +document_ranker_pipeline.add_component(instance=retriever, name="retriever") +document_ranker_pipeline.add_component(instance=ranker, name="ranker") + +document_ranker_pipeline.connect("retriever.documents", "ranker.documents") + +query = "Cities in France" +res = document_ranker_pipeline.run(data = {"retriever": {"query": query, "top_k": 3}, "ranker": {"query": query, "top_k": 2}}) +``` + ## Haystack 1.x You can use [Cohere Models](https://cohere.com/) in your Haystack pipelines with the [EmbeddingRetriever](https://docs.haystack.deepset.ai/v1.25/docs/retriever#embedding-retrieval-recommended), [PromptNode](https://docs.haystack.deepset.ai/v1.25/docs/prompt_node), and [CohereRanker](https://docs.haystack.deepset.ai/v1.25/docs/ranker#cohereranker).