跳到正文
GitHub·· 7 天前AI 评分12

soenneker.cohere.openapiclient:Kiota 生成的 Cohere .NET 客户端

soenneker/soenneker.cohere.openapiclient

AI 导读

soenneker.cohere.openapiclient 是一个 Kiota 生成的 .NET 客户端,封装 Cohere 的类型化 chat、embedding、reranking、generation 和模型管理 API,可通过 dotnet add package 安装。

正文

A Kiota-generated .NET client exposing Cohere's typed chat, embedding, reranking, generation, and model-management APIs.

Installation

dotnet add package Soenneker.Cohere.OpenApiClient

Recommended setup

Most applications should use Soenneker.Cohere.OpenApiClientUtil, which configures bearer authentication, base URL, reuse, and dependency injection:

using Soenneker.Cohere.OpenApiClientUtil.Registrars;

services.AddCohereOpenApiClientUtilAsSingleton();

Resolve ICohereOpenApiClientUtil and call Get to obtain CohereOpenApiClient.

Direct construction

Use this package directly when the application already owns its Kiota authentication and HTTP pipeline:

using Microsoft.Kiota.Abstractions.Authentication;
using Microsoft.Kiota.Http.HttpClientLibrary;
using Soenneker.Cohere.OpenApiClient;
using System.Net.Http.Headers;

var httpClient = new HttpClient
{
    BaseAddress = new Uri("https://api.cohere.com")
};

httpClient.DefaultRequestHeaders.Authorization =
    new AuthenticationHeaderValue("Bearer", apiKey);

var adapter = new HttpClientRequestAdapter(
    new AnonymousAuthenticationProvider(),
    httpClient: httpClient)
{
    BaseUrl = httpClient.BaseAddress.ToString().TrimEnd('/')
};

var client = new CohereOpenApiClient(adapter);

The root exposes V1 and V2 request builders. For example, client.V2.Chat, client.V2.Embed, and client.V2.Rerank accept their corresponding generated request models.

Generated request and response types mirror Cohere's schema. Nullable return values represent endpoints that may return no body, and API failures follow Kiota's generated exception behavior. Pass cancellation tokens to network operations and validate model-specific inputs before sending prompts, documents, or other potentially sensitive content.

Generated-code boundary

Prefer generated request builders and model properties over manually assembled URLs or JSON. Do not hand-edit generated source files; place retries, application policy, output validation, and data-handling controls around the client.

来源:GitHub · github.com