executing-plans
executing-plans
Use when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is available
cohere-apiIntegrates Cohere API (Generate, Embed, Rerank, Classify, Chat, Tool Use) using the cohere Python SDK for NLP, search, and RAG applications.
Independent GitHub snapshot · Automated classification · Original documentation governs
name: cohere-api description: Integrates Cohere API (Generate, Embed, Rerank, Classify, Chat, Tool Use) using the cohere Python SDK for NLP, search, and RAG applications. license: MIT compatibility: opencode metadata: version: "1.0.0" domain: coding triggers: cohere, cohere api, cohere embed, cohere rerank, cohere generate, cohere chat, how do i use cohere, cohere classify archetypes:
Integrates Cohere API using the cohere Python SDK for embedding, reranking, classification, generation, chat with tool use, and multilingual NLP. When loaded, this skill makes the model implement Cohere API calls with proper authentication, error handling, and batch processing.
Use this skill when:
embed-multilingual-v3.0 or embed-english-v3.0 for multilingual semantic searchcoding-openai-apicoding-mistral-apicoding-langchainInitialize the Client — Create a cohere.Client() with the API key from the CO_API_KEY environment variable. Never hardcode keys. The v5 SDK provides typed request/response models. Checkpoint: Verify connectivity with client.check_api_key() which returns a boolean.
Generate Embeddings — Use client.embed() with texts (list of strings), model (e.g., "embed-english-v3.0"), and input_type ("search_document", "search_query", "classification", "clustering"). V3 embedding models require input_type for optimal performance. Checkpoint: Verify embedding dimensions match expectations (1024 for v3 english, 384 for multilingual).
Rerank Search Results — Use client.rerank() with a query, list of documents, and model. Reranking is critical for RAG quality — it re-orders retrieved documents by relevance to the query. Optionally set top_n to limit results. Checkpoint: Verify the reranked results have scores in descending order.
Use Chat with Tool Use — Use client.chat() with message, tools (tool definitions), and optional tool_results to feed back tool outputs. Cohere models support multi-turn tool use for agentic workflows. Checkpoint: Check response.tool_calls to see if the model requested tool execution.
Classify Text — Use client.classify() with inputs and examples (few-shot training data). Provide at least 2 examples per class for reliable results. Checkpoint: Verify predictions include confidence scores via prediction.confidence.
from __future__ import annotations
import cohere
from cohere import Client
# ❌ BAD — no input_type for v3 models, no batch handling
co = cohere.Client("api-key")
embeddings = co.embed(texts=["Hello world"], model="embed-english-v3.0").embeddings
# ✅ GOOD — proper input_type, batch processing, error handling
client = cohere.Client() # reads CO_API_KEY from environment
def embed_documents(texts: list[str], batch_size: int = 96) -> list[list[float]]:
"""Generate embeddings for documents using Cohere v3 models.
Args:
texts: List of document texts to embed.
batch_size: Max texts per API call (Cohere limit: 96).
Returns:
List of embedding vectors.
Raises:
RuntimeError: On API failures.
"""
all_embeddings: list[list[float]] = []
for i in range(0, len(texts), batch_size):
batch = texts[i : i + batch_size]
response = client.embed(
texts=batch,
model="embed-english-v3.0",
input_type="search_document",
)
if response.embeddings is not None:
all_embeddings.extend(response.embeddings)
return all_embeddings
def rerank_results(query: str, documents: list[str], top_n: int = 5) -> list[dict]:
"""Rerank documents by relevance to a query.
Args:
query: The search query.
documents: Candidate documents to rerank.
top_n: Number of top results to return.
Returns:
Reranked results with relevance scores.
"""
response = client.rerank(
query=query,
documents=documents,
model="rerank-english-v3.0",
top_n=top_n,
)
return [
{
"index": r.index,
"document": documents[r.index],
"relevance_score": r.relevance_score,
}
for r in response.results
]
from __future__ import annotations
from typing import Any
import cohere
client = cohere.Client()
TOOLS: list[dict[str, Any]] = [
{
"name": "get_weather",
"description": "Get the current weather for a city.",
"parameter_definitions": {
"location": {
"description": "City name, e.g., San Francisco, CA",
"type": "str",
"required": True,
}
},
}
]
def chat_with_tools(prompt: str) -> str:
"""Send a chat message with tool use capabilities.
Args:
prompt: The user's message.
Returns:
The assistant's response, potentially after tool execution.
"""
response = client.chat(
message=prompt,
model="command-r-plus",
tools=TOOLS,
)
# Handle tool calls if the model requests them
if response.tool_calls:
tool_results: list[dict[str, Any]] = []
for tc in response.tool_calls:
if tc.name == "get_weather":
location = tc.parameters.get("location", "")
tool_results.append({
"call": tc,
"outputs": [{"weather": "sunny", "temperature": 72}],
})
# Send tool results back for the final response
final = client.chat(
message=prompt,
model="command-r-plus",
tools=TOOLS,
tool_results=tool_results,
)
return final.text
return response.text
from __future__ import annotations
import cohere
client = cohere.Client()
def classify_text(
inputs: list[str],
examples: list[dict[str, str]],
) -> list[dict[str, float]]:
"""Classify text using Cohere's few-shot classification.
Args:
inputs: Texts to classify.
examples: List of {"text": str, "label": str} training examples.
Provide at least 2 per class.
Returns:
List of predictions with label and confidence.
"""
response = client.classify(
inputs=inputs,
examples=[cohere.ClassifyExample(**ex) for ex in examples],
model="embed-english-v3.0",
)
return [
{"label": pred.prediction, "confidence": pred.confidence}
for pred in response.classifications
]
CO_API_KEY environment variableinput_type parameter with v3 embedding models (search_document, search_query, classification, clustering)rerank() for improving RAG result quality — always rerank before presenting results to the LLMcheck_api_key() to validate credentials before making other API callsinput_type for v3 embedding models — this degrades embedding quality significantlyclient.generate() when client.chat() is more appropriate for conversational use casestop_n in rerank() — the default may return too many results| Resource | URL |
|---|---|
| Cohere Python SDK (PyPI) | https://pypi.org/project/cohere/ |
| Cohere API Reference | https://docs.cohere.com/reference/about |
| Cohere Embed Documentation | https://docs.cohere.com/docs/embed |
| Cohere Rerank Documentation | https://docs.cohere.com/docs/rerank |
| Cohere Chat with Tools | https://docs.cohere.com/docs/multi-step-tool-use |
| Cohere GitHub | https://github.com/cohere-ai/cohere-python |
| Skill | Purpose |
|---|---|
coding-openai-api | Alternative embedding and LLM provider |
coding-mistral-api | Alternative embedding and LLM provider |
coding-pinecone-api | Vector database for storing Cohere embeddings |
coding-langchain | LangChain integration with Cohere models |