DEV Community

Cover image for Cohere in .NET in 10 Minutes: Microsoft.Extensions.AI + the Cohere.Extensions.AI NuGet Package
Денис М
Денис М

Posted on

Cohere in .NET in 10 Minutes: Microsoft.Extensions.AI + the Cohere.Extensions.AI NuGet Package

TL;DR: connect Cohere to .NET in 10 minutes. I’ll show two approaches:

  1. direct call to the Chat API V1/V2
  2. using the unified Microsoft.Extensions.AI interface (IChatClient) - provider-agnostic code.

A short intro

I’m experimenting with Microsoft.Extensions.AI and want to share the results: I wrote a small Cohere adapter and published it to NuGet. Maybe someone will find it useful, and I’d love to get feedback.

With Microsoft.Extensions.AI it became easier to plug LLMs into .NET projects: we now have unified abstractions (IChatClient), DI and minimal vendor lock-in. Cohere is not supported out of the box, so I made a lightweight adapter and published it to NuGet. This article shows how to run a Cohere chat in .NET in ten minutes.


What is Cohere and why .NET developers should care

Cohere is an LLM provider focused on enterprise needs: data privacy, turnkey deployments, and practical company-oriented features (document QA, summarization, multilingual responses, etc.). Cohere provides a Chat API, embeddings, and Rerank for improving search and RAG pipelines.

Cohere develops the Command R / Command R+ family of models, optimized for long context and RAG (retrieval-augmented generation) scenarios. That makes them useful for document search, chatbots and analytics systems.

Why it matters: if you build on top of Microsoft.Extensions.AI you can integrate Cohere into .NET and easily switch to another provider later without rewriting your business logic.


Why consider Microsoft.Extensions.AI

Microsoft.Extensions.AI gives a common interface (IChatClient) for different LLMs. Benefits:

  • unified message model and call patterns (including streaming),
  • works nicely with DI / Options / ILogger,
  • minimal vendor lock-in - you can swap the provider without changing application code.

Installation

dotnet add package Cohere.Extensions.AI
Enter fullscreen mode Exit fullscreen mode

Get an API key from the Cohere dashboard: https://dashboard.cohere.com/api-keys. Provide the API key via an environment variable:

# Windows PowerShell
$env:COHERE_API_KEY="your_api_key"

# macOS/Linux (bash/zsh)
export COHERE_API_KEY="your_api_key"
Enter fullscreen mode Exit fullscreen mode

Option 1 - Direct call to Cohere Chat API V2

using Cohere.Client;
using Cohere.Client.Models.V2;

var client = new CohereClient(Environment.GetEnvironmentVariable("COHERE_API_KEY")!);

var resp = await client.ChatV2Async(new ChatRequestV2 {
    Model = "command-r-08-2024",
    Messages = [ new() { Role = "user", Content = "Hello" } ]
});

Console.WriteLine(resp.Text);
Enter fullscreen mode Exit fullscreen mode

Note: Cohere Chat API supports roles, tool-use and more - see the official docs for details.


Option 2 - Via Microsoft.Extensions.AI (the unified IChatClient)

If you prefer to write application code on top of common abstractions, use the adapter from the package (CohereChatClient):

using Cohere.Extensions.AI;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.DependencyInjection;

var services = new ServiceCollection();

services.AddCohereChatClient(null, opts =>
{
    opts.ModelId = "command-r-plus-08-2024";
});

await using var provider = services.BuildServiceProvider();
var chat = provider.GetRequiredService<IChatClient>();

var messages = new[]
{
    new ChatMessage(ChatRole.User, "Ping")
};

var response = await chat.GetResponseAsync(messages);
Console.WriteLine(response.Text);
Enter fullscreen mode Exit fullscreen mode

Now your business layer already works with IChatClient, and switching to another provider becomes trivial. (That’s exactly what ME.AI is designed for.)

Console output screenshot

The library also supports streaming responses via IAsyncEnumerable (SSE). This matters for UX: you can show text as it’s generated instead of waiting for the full answer.


What’s next

Planned features: extended parameters (temperature, max tokens) and OpenTelemetry integration. If you want to contribute - PRs are welcome.


Summary

This project was an experiment for me: I wanted to try combining .NET and Cohere using Microsoft’s new abstractions. I’d be glad if someone tries the package, stars the GitHub repo, or writes what’s missing.

Links


Top comments (0)