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

Soenneker.Groq.HttpClients:为 Groq OpenAI 兼容 API 提供缓存 HttpClient

soenneker/soenneker.groq.httpclients

AI 导读

Soenneker.Groq.HttpClients 发布,为 Groq 的 OpenAI 兼容 API 提供带缓存的 HttpClient,内置 base address 与认证头配置。

正文

Provides a cached HttpClient configured for Groq's OpenAI-compatible API, including its base address and authentication header.

Installation

dotnet add package Soenneker.Groq.HttpClients

Configuration

{
  "Groq": {
    "ApiKey": "gsk_..."
  }
}

Requests use https://api.groq.com by default. These optional settings override the endpoint or authentication format:

{
  "Groq": {
    "ClientBaseUrl": "https://api.groq.com",
    "AuthHeaderName": "Authorization",
    "AuthHeaderValueTemplate": "Bearer {token}"
  }
}

{token} is replaced with Groq:ApiKey when the client is created.

Registration and usage

using Soenneker.Groq.HttpClients.Abstract;
using Soenneker.Groq.HttpClients.Registrars;

services.AddGroqOpenApiHttpClientAsSingleton();

IGroqOpenApiHttpClient provider =
    serviceProvider.GetRequiredService<IGroqOpenApiHttpClient>();

HttpClient client = await provider.Get(cancellationToken);
HttpResponseMessage response = await client.GetAsync("/openai/v1/models", cancellationToken);
response.EnsureSuccessStatusCode();

Get() reuses the same cached client for the provider's lifetime. Dispose the provider, not the returned client, when you own the provider.

AddGroqOpenApiHttpClientAsScoped() is available when each dependency-injection scope should own and release an independent cached client. Consumers such as a scoped API utility can instead use the singleton registration to keep the transport alive across utility scopes.

来源:GitHub · github.com