CohereGenerator
Generates text using Cohere's models through the Cohere generate endpoint.
generate APICohere discontinued the generate API, so this generator is a wrapper around CohereChatGenerator provided for backward compatibility.
Key Features
- Generates text using Cohere's
commandmodel family. - Backward-compatible wrapper around
CohereChatGenerator. - Supports streaming responses with a configurable callback.
- Configurable via additional keyword arguments passed to the model.
- Works with
PromptBuilderfor dynamic prompt construction. - Connects to
DeepsetAnswerBuilderto build answers with references.
Configuration
To use this component, connect Haystack Platform with Cohere first. For detailed instructions, see Use Cohere Models.
- Drag the
CohereGeneratorcomponent onto the canvas from the Component Library. - Click the component to open the configuration panel.
- On the General tab:
- Enter the model name, for example
command-r.
- Enter the model name, for example
- Go to the Advanced tab to configure the API key, API base URL, streaming callback, and additional model parameters.
Connections
CohereGenerator accepts a prompt string as input. It outputs a list of reply strings (replies) and request metadata (meta).
Connect PromptBuilder to the prompt input to build dynamic prompts. Connect the replies output to DeepsetAnswerBuilder to build answers with document references.
Usage Example
Basic Configuration
This is a simple example showing how to configure CohereGenerator:
components:
CohereGenerator:
type: haystack_integrations.components.generators.cohere.generator.CohereGenerator
init_parameters:
api_key:
type: env_var
env_vars:
- COHERE_API_KEY
- CO_API_KEY
strict: false
model: command-r
streaming_callback:
api_base_url:
Using the Component in a Pipeline
This pipeline uses CohereGenerator to generate replies to a question. It uses DeepsetAnswerBuilder to build the answers with references. It's also optimized for uploading files when testing it in Playground.
components:
retriever: # Selects the most similar documents from the document store
type: haystack_integrations.components.retrievers.opensearch.open_search_hybrid_retriever.OpenSearchHybridRetriever
init_parameters:
document_store:
type: haystack_integrations.document_stores.opensearch.document_store.OpenSearchDocumentStore
init_parameters:
hosts:
index: ''
max_chunk_bytes: 104857600
embedding_dim: 768
return_embedding: false
method:
mappings:
settings:
create_index: true
http_auth:
use_ssl:
verify_certs:
timeout:
top_k: 20 # The number of results to return
fuzziness: 0
embedder:
type: deepset_cloud_custom_nodes.embedders.nvidia.text_embedder.DeepsetNvidiaTextEmbedder
init_parameters:
normalize_embeddings: true
model: intfloat/e5-base-v2
ranker:
type: deepset_cloud_custom_nodes.rankers.nvidia.ranker.DeepsetNvidiaRanker
init_parameters:
model: intfloat/simlm-msmarco-reranker
top_k: 8
meta_field_grouping_ranker:
type: haystack.components.rankers.meta_field_grouping_ranker.MetaFieldGroupingRanker
init_parameters:
group_by: file_id
subgroup_by:
sort_docs_by: split_id
prompt_builder:
type: haystack.components.builders.prompt_builder.PromptBuilder
init_parameters:
required_variables: "*"
template: |-
You are a technical expert.
You answer questions truthfully based on provided documents.
If the answer exists in several documents, summarize them.
Ignore documents that don't contain the answer to the question.
Only answer based on the documents provided. Don't make things up.
If no information related to the question can be found in the document, say so.
Always use references in the form [NUMBER OF DOCUMENT] when using information from a document, e.g. [3] for Document [3] .
Never name the documents, only enter a number in square brackets as a reference.
The reference must only refer to the number that comes in square brackets after the document.
Otherwise, do not use brackets in your answer and reference ONLY the number of the document without mentioning the word document.
These are the documents:
{%- if documents|length > 0 %}
{%- for document in documents %}
Document [{{ loop.index }}] :
Name of Source File: {{ document.meta.file_name }}
{{ document.content }}
{% endfor -%}
{%- else %}
No relevant documents found.
Respond with "Sorry, no matching documents were found, please adjust the filters or try a different question."
{% endif %}
Question: {{ question }}
Answer:
answer_builder:
type: deepset_cloud_custom_nodes.augmenters.deepset_answer_builder.DeepsetAnswerBuilder
init_parameters:
reference_pattern: acm
attachments_joiner:
type: haystack.components.joiners.document_joiner.DocumentJoiner
init_parameters:
join_mode: concatenate
weights:
top_k:
sort_by_score: true
CohereGenerator:
type: haystack_integrations.components.generators.cohere.generator.CohereGenerator
init_parameters:
api_key:
type: env_var
env_vars:
- COHERE_API_KEY
- CO_API_KEY
strict: false
model: command-r
streaming_callback:
api_base_url:
S3Downloader:
type: haystack_integrations.components.downloaders.s3.s3_downloader.S3Downloader
init_parameters:
aws_access_key_id:
type: env_var
env_vars:
- AWS_ACCESS_KEY_ID
strict: false
aws_secret_access_key:
type: env_var
env_vars:
- AWS_SECRET_ACCESS_KEY
strict: false
aws_session_token:
type: env_var
env_vars:
- AWS_SESSION_TOKEN
strict: false
aws_region_name:
type: env_var
env_vars:
- AWS_DEFAULT_REGION
strict: false
aws_profile_name:
type: env_var
env_vars:
- AWS_PROFILE
strict: false
boto3_config:
file_root_path:
file_extensions:
file_name_meta_key: file_name
max_workers: 32
max_cache_size: 100
s3_key_generation_function: deepset_cloud_custom_nodes.utils.storage.get_s3_key
connections: # Defines how the components are connected
- sender: retriever.documents
receiver: ranker.documents
- sender: ranker.documents
receiver: meta_field_grouping_ranker.documents
- sender: prompt_builder.prompt
receiver: answer_builder.prompt
- sender: meta_field_grouping_ranker.documents
receiver: attachments_joiner.documents
- sender: attachments_joiner.documents
receiver: answer_builder.documents
- sender: attachments_joiner.documents
receiver: prompt_builder.documents
- sender: prompt_builder.prompt
receiver: CohereGenerator.prompt
- sender: CohereGenerator.replies
receiver: answer_builder.replies
- sender: retriever.documents
receiver: S3Downloader.documents
- sender: S3Downloader.documents
receiver: attachments_joiner.documents
inputs: # Define the inputs for your pipeline
query: # These components will receive the query as input
- "retriever.query"
- "ranker.query"
- "prompt_builder.question"
- "answer_builder.query"
filters: # These components will receive a potential query filter as input
- "retriever.filters_bm25"
- "retriever.filters_embedding"
outputs: # Defines the output of your pipeline
documents: "attachments_joiner.documents" # The output of the pipeline is the retrieved documents
answers: "answer_builder.answers" # The output of the pipeline is the generated answers
max_runs_per_component: 100
metadata: {}
Parameters
Inputs
| Parameter | Type | Default | Description |
|---|---|---|---|
| prompt | str | The prompt with instructions for the model. |
Outputs
| Parameter | Type | Default | Description |
|---|---|---|---|
| replies | List[str] | A list of replies generated by the model. | |
| meta | List[Dict[str, Any]] | Information about the request. |
Init Parameters
These are the parameters you can configure in Pipeline Builder:
| Parameter | Type | Default | Description |
|---|---|---|---|
| api_key | Secret | Secret.from_env_var(['COHERE_API_KEY', 'CO_API_KEY']) | Cohere API key. |
| model | str | command-r | Cohere model to use for generation. |
| streaming_callback | Optional[Callable] | None | Callback function that is called when a new token is received from the stream. For more information, see Enable Streaming. |
| api_base_url | Optional[str] | None | Cohere base URL. |
| **kwargs | Any | Additional arguments passed to the model. These arguments are specific to the model. You can check them in model's documentation. |
Run Method Parameters
These are the parameters you can configure for the component's run() method. This means you can pass these parameters at query time through the API, in Playground, or when running a job. For details, see Modify Pipeline Parameters at Query Time.
| Parameter | Type | Default | Description |
|---|---|---|---|
| prompt | str | The prompt with instructions for the model. |
Was this page helpful?