Skip to content

Commit

Permalink
Add cohere ranker integration information (#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
AnushreeBannadabhavi authored Apr 10, 2024
1 parent aaa7beb commit c633ded
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion integrations/cohere.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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).
Expand Down

0 comments on commit c633ded

Please sign in to comment.