Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Update cohere docs with changes to secret management #169

Merged
merged 1 commit into from
Feb 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 28 additions & 24 deletions integrations/cohere.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ layout: integration
name: Cohere
description: Use Cohere models with Haystack
authors:
- name: deepset
socials:
github: deepset-ai
twitter: deepset_ai
linkedin: deepset-ai
- name: deepset
socials:
github: deepset-ai
twitter: deepset_ai
linkedin: deepset-ai
pypi: https://pypi.org/project/cohere-haystack/
repo: https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/cohere
type: Model Provider
Expand All @@ -22,9 +22,14 @@ toc: true
- [Haystack 2.0](#haystack-20)
- [Installation](#installation)
- [Usage](#usage)
- [Embedding Models](#embedding-models)
- [Generative Models (LLMs)](#generative-models-llms)
- [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)

## Haystack 2.0

Expand All @@ -44,9 +49,9 @@ You can use Cohere models in various ways:

You can leverage `/embed` models from Cohere through two components: [CohereTextEmbedder](https://docs.haystack.deepset.ai/v2.0/docs/coheretextembedder) and [CohereDocumentEmbedder](https://docs.haystack.deepset.ai/v2.0/docs/coheredocumentembedder). These components support both **Embed v2** and **Embed v3** models.

To create semantic embeddings for documents, use `CohereDocumentEmbedder` in your indexing pipeline. For generating embeddings for queries, use `CohereTextEmbedder`. Once you've selected the suitable component for your specific use case, initialize the component with the model name and Cohere API key.
To create semantic embeddings for documents, use `CohereDocumentEmbedder` in your indexing pipeline. For generating embeddings for queries, use `CohereTextEmbedder`. Once you've selected the suitable component for your specific use case, initialize the component 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.

Below is the example indexing pipeline with `InMemoryDocumentStore`, `CohereDocumentEmbedder` and `DocumentWriter`:
Below is the example indexing pipeline with `InMemoryDocumentStore`, `CohereDocumentEmbedder` and `DocumentWriter`:

```python
from haystack import Document, Pipeline
Expand All @@ -62,18 +67,18 @@ documents = [Document(content="My name is Wolfgang and I live in Berlin"),
Document(content="Germany has many big cities")]

indexing_pipeline = Pipeline()
indexing_pipeline.add_component("embedder", CohereDocumentEmbedder(api_key="COHERE_API_KEY", model="embed-multilingual-v3.0", input_type="search_document"))
indexing_pipeline.add_component("embedder", CohereDocumentEmbedder(model="embed-multilingual-v3.0", input_type="search_document"))
indexing_pipeline.add_component("writer", DocumentWriter(document_store=document_store))
indexing_pipeline.connect("embedder", "writer")

indexing_pipeline.run({"embedder": {"documents": documents}})
```

#### Generative Models (LLMs)
#### Generative Models (LLMs)

To use `/generate` models from Cohere, initialize a [CohereGenerator](https://docs.haystack.deepset.ai/v2.0/docs/coheregenerator) with the model name and Cohere API key. You can then use this `CohereGenerator` in a question answering pipeline after the `PromptBuilder`.
To use `/generate` models from Cohere, initialize a [CohereGenerator](https://docs.haystack.deepset.ai/v2.0/docs/coheregenerator) 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 `CohereGenerator` in a question answering pipeline after the `PromptBuilder`.

Below is the example of generative questions answering pipeline using RAG with `PromptBuilder` and `CohereGenerator`:
Below is the example of generative questions answering pipeline using RAG with `PromptBuilder` and `CohereGenerator`:

```python
from haystack import Pipeline
Expand All @@ -85,26 +90,26 @@ from haystack_integrations.components.generators.cohere import CohereGenerator
template = """
Given the following information, answer the question.

Context:
Context:
{% for document in documents %}
{{ document.text }}
{% endfor %}

Question: What's the official language of {{ country }}?
"""
pipe = Pipeline()
pipe.add_component("embedder", CohereTextEmbedder(api_key=api_key, model="embed-multilingual-v3.0"))
pipe.add_component("embedder", CohereTextEmbedder(model="embed-multilingual-v3.0"))
pipe.add_component("retriever", InMemoryEmbeddingRetriever(document_store=document_store))
pipe.add_component("prompt_builder", PromptBuilder(template=template))
pipe.add_component("llm", CohereGenerator(api_key=api_key, model="command-light"))
pipe.add_component("llm", CohereGenerator(model="command-light"))
pipe.connect("embedder.embedding", "retriever.query_embedding")
pipe.connect("retriever", "prompt_builder.documents")
pipe.connect("prompt_builder", "llm")

pipe.run({
"embedder": {"text": "France"},
"prompt_builder": {"country": "France"}
})
})
```

Similar to the above example, you can also use [`CohereChatGenerator`](https://docs.haystack.deepset.ai/v2.0/docs/coherechatgenerator) to use Cohere `/chat` models and features (streaming, connectors) in your pipeline.
Expand All @@ -118,7 +123,7 @@ from haystack_integrations.components.generators.cohere.chat import CohereChatGe

pipe = Pipeline()
pipe.add_component("prompt_builder", DynamicChatPromptBuilder())
pipe.add_component("llm", CohereChatGenerator(api_key="<COHERE_API_KEY>"))
pipe.add_component("llm", CohereChatGenerator())
pipe.connect("prompt_builder", "llm")

country = "Germany"
Expand All @@ -129,8 +134,7 @@ res = pipe.run(data={"prompt_builder": {"template_variables": {"country": "Germa
print(res)
```


## Haystack 1.x
## Haystack 1.x

You can use [Cohere Models](https://cohere.com/) in your Haystack pipelines with the [EmbeddingRetriever](https://docs.haystack.deepset.ai/docs/retriever#embedding-retrieval-recommended), [PromptNode](https://docs.haystack.deepset.ai/docs/prompt_node), and [CohereRanker](https://docs.haystack.deepset.ai/docs/ranker#cohereranker).

Expand All @@ -146,9 +150,9 @@ You can use Cohere models in various ways:

#### Embedding Models

To use `/embed` models from Cohere, initialize an `EmbeddingRetriever` with the model name and Cohere API key. You can then use this `EmbeddingRetriever` in an indexing pipeline to create Cohere embeddings for documents and index them to a document store.
To use `/embed` models from Cohere, initialize an `EmbeddingRetriever` with the model name and Cohere API key. You can then use this `EmbeddingRetriever` in an indexing pipeline to create Cohere embeddings for documents and index them to a document store.

Below is the example indexing pipeline with `PreProcessor`, `InMemoryDocumentStore` and `EmbeddingRetriever`:
Below is the example indexing pipeline with `PreProcessor`, `InMemoryDocumentStore` and `EmbeddingRetriever`:

```python
from haystack.nodes import EmbeddingRetriever
Expand All @@ -169,11 +173,11 @@ indexing_pipeline.add_node(component=document_store, name="document_store", inpu
indexing_pipeline.run(documents=[Document("This is my document")])
```

#### Generative Models (LLMs)
#### Generative Models (LLMs)

To use `/generate` models from Cohere, initialize a `PromptNode` with the model name, Cohere API key and the prompt template. You can then use this `PromptNode` in a question answering pipeline to generate answers based on the given context.
To use `/generate` models from Cohere, initialize a `PromptNode` with the model name, Cohere API key and the prompt template. You can then use this `PromptNode` in a question answering pipeline to generate answers based on the given context.

Below is the example of generative questions answering pipeline using RAG with `EmbeddingRetriever` and `PromptNode`:
Below is the example of generative questions answering pipeline using RAG with `EmbeddingRetriever` and `PromptNode`:

```python
from haystack.nodes import PromptNode, EmbeddingRetriever
Expand All @@ -194,7 +198,7 @@ query_pipeline.run("YOUR_QUERY")

To use `/rerank` models from Cohere, initialize a `CohereRanker` with the model name, and Cohere API key. You can then use this `CohereRanker` to sort documents based on their relevancy to the query.

Below is the example of document retrieval pipeline with `BM25Retriever` and `CohereRanker`:
Below is the example of document retrieval pipeline with `BM25Retriever` and `CohereRanker`:

```python
from haystack.nodes import CohereRanker, BM25Retriever
Expand Down