RAGToolkit.AI.DataRetrieval.Cohere 1.0.0

dotnet add package RAGToolkit.AI.DataRetrieval.Cohere --version 1.0.0
                    
NuGet\Install-Package RAGToolkit.AI.DataRetrieval.Cohere -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="RAGToolkit.AI.DataRetrieval.Cohere" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="RAGToolkit.AI.DataRetrieval.Cohere" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="RAGToolkit.AI.DataRetrieval.Cohere" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add RAGToolkit.AI.DataRetrieval.Cohere --version 1.0.0
                    
#r "nuget: RAGToolkit.AI.DataRetrieval.Cohere, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package RAGToolkit.AI.DataRetrieval.Cohere@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=RAGToolkit.AI.DataRetrieval.Cohere&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=RAGToolkit.AI.DataRetrieval.Cohere&version=1.0.0
                    
Install as a Cake Tool

RAGToolkit.AI.DataRetrieval.Cohere

Cohere Rerank API 的 IReranker 实现 —— 手写线格式,不引官方 Cohere SDK。

用法

using System.Net.Http.Headers;
using RAGToolkit.AI.DataRetrieval.Cohere;

var httpClient = new HttpClient { BaseAddress = new Uri("https://api.cohere.com") };
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);

var reranker = new CohereReranker(httpClient);

pipeline.ResultProcessors.Add(new RerankingResultProcessor(reranker));

认证归 HttpClient 管 —— 这个类不碰凭据。用 IHttpClientFactory 时把 BaseAddress 和 Authorization 配在注册里即可。

API

public sealed class CohereReranker(
    HttpClient httpClient,
    string model = "rerank-v3.5",
    string requestPath = "/v2/rerank",
    int maxDocumentsPerBatch = 1000) : IReranker

线格式

POST /v2/rerank,Authorization: Bearer <token>。

// 请求
{ "model": "rerank-v3.5", "query": "...", "documents": ["...", "..."] }

// 响应
{ "id": "...", "results": [ { "index": 3, "relevance_score": 0.999071 } ], "meta": { /* 忽略 */ } }

不发 top_n。 截断归调用方(TopNResultProcessor);而分批时按批设上限,会在两批合并之前 就把候选丢掉 —— 某批里排第 6、全局其实排第 3 的那条会直接消失。Rag.NET 踩过这个坑:他们的 TopN 一度默认 5,把管线的 TopK 悄悄截掉了(issue #94)。

分批

Cohere 建议单次不要超过 1,000 个文档,所以按 MaxDocumentsPerBatch 分批。 分数跨批可比(cross-encoder 是同一把尺子量每一对 query/document),合并就是一次降序排序, 不是 rank fusion。批内返回的下标会加回偏移量再落回调用方的列表。

没被打分的块保留原位和原分数。

与 TEI 那版的关系

RAGToolkit.AI.DataRetrieval.Tei 是同一套形状 —— HttpClient 进、分批、合并 —— 只是线格式不同。两个都是手写 HTTP:私有线格式才值得补 provider 包, 标准协议(/v1/chat/completions)就不必了,IChatClient 已经覆盖。

依赖

  • RAGToolkit.AI.DataRetrieval.Abstractions
  • 无第三方依赖

许可证

MIT —— 见 LICENSE。

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 41 10/9/2026