Skip to content

OpenAICompatibleProvider

Namespace: Vercel.AI.OpenAICompatible

public class OpenAICompatibleProvider : ProviderBase

Provider that speaks the OpenAI Chat Completions, embeddings, and images APIs.

Inheritance: object → ProviderBase

Derived: AlibabaProvider, AssemblyAIProvider, AzureOpenAIProvider, BasetenProvider, BlackForestLabsProvider, ByteDanceProvider, CartesiaProvider, CerebrasProvider, DeepInfraProvider, DeepSeekProvider, DeepgramProvider, ElevenLabsProvider, FalProvider, FireworksProvider, FishAudioProvider, GladiaProvider, GmiCloudProvider, GroqProvider, HeyGenProvider, HuggingFaceProvider, HumeProvider, KlingAIProvider, LumaProvider, MistralProvider, MoonshotProvider, OpenAIProvider, PerplexityProvider, ProdiaProvider, QuiverAIProvider, ReplicateProvider, RevAIProvider, TogetherAIProvider, VoyageProvider, XaiProvider, ZaiProvider

View source

OpenAICompatibleProvider(OpenAICompatibleOptions, HttpClient)

Section titled “OpenAICompatibleProvider(OpenAICompatibleOptions, HttpClient)”
public OpenAICompatibleProvider(OpenAICompatibleOptions options, HttpClient httpClient)

Creates a provider. The caller owns <code class=“paramref”>httpClient</code>.

Parameters

Name Type Description
options OpenAICompatibleOptions
httpClient HttpClient

View source

public ProviderHttp Http { get; }

HTTP helper used by the models.

Value: ProviderHttp

View source

public OpenAICompatibleOptions Options { get; }

Options used to build requests.

Value: OpenAICompatibleOptions

View source

public Uri ChatUri(string modelId)

Chat Completions URL for a model. Azure deployment routes include the model id.

Parameters

Name Type Description
modelId string

Returns: Uri

View source

public virtual OpenAICompatibleCompletionLanguageModel CompletionModel(string modelId)

Creates a legacy Completions model.

Parameters

Name Type Description
modelId string

Returns: OpenAICompatibleCompletionLanguageModel

View source

public Uri CompletionsUri(string modelId)

Completions URL.

Parameters

Name Type Description
modelId string

Returns: Uri

View source

Create(OpenAICompatibleOptions, HttpMessageHandler?)

Section titled “Create(OpenAICompatibleOptions, HttpMessageHandler?)”
public static OpenAICompatibleProvider Create(OpenAICompatibleOptions options, HttpMessageHandler? handler = null)

Creates a provider with an HttpClient that uses <code class=“paramref”>handler</code>.

Parameters

Name Type Description
options OpenAICompatibleOptions
handler HttpMessageHandler

Returns: OpenAICompatibleProvider

View source

public virtual OpenAICompatibleLanguageModel CreateChatModel(string modelId)

Creates a chat model. Providers override this when support differs by model id.

Parameters

Name Type Description
modelId string

Returns: OpenAICompatibleLanguageModel

View source

public Dictionary<string, string?> CreateHeaders()

Resolves the API key and builds request headers.

Returns: Dictionary<string, string>

View source

CreateHeaders(IReadOnlyDictionary<string, string?>?)

Section titled “CreateHeaders(IReadOnlyDictionary<string, string?>?)”
public Dictionary<string, string?> CreateHeaders(IReadOnlyDictionary<string, string?>? callHeaders)

Builds headers and lets <code class=“paramref”>callHeaders</code> override them.

Parameters

Name Type Description
callHeaders IReadOnlyDictionary<string, string>

Returns: Dictionary<string, string>

View source

public override IEmbeddingModel EmbeddingModel(string modelId)

Returns an embedding model.

Parameters

Name Type Description
modelId string

Returns: IEmbeddingModel

View source

public Uri EmbeddingsUri(string modelId)

Embeddings URL.

Parameters

Name Type Description
modelId string

Returns: Uri

View source

GetBinaryAsync(Uri, IReadOnlyDictionary<string, string?>?, CancellationToken)

Section titled “GetBinaryAsync(Uri, IReadOnlyDictionary<string, string?>?, CancellationToken)”
public Task<OpenAICompatibleBinaryResponse> GetBinaryAsync(Uri uri, IReadOnlyDictionary<string, string?>? headers, CancellationToken cancellationToken)

Downloads raw bytes.

Parameters

Name Type Description
uri Uri
headers IReadOnlyDictionary<string, string>
cancellationToken CancellationToken

Returns: Task<OpenAICompatibleBinaryResponse>

View source

public override IImageModel ImageModel(string modelId)

Returns an image model.

Parameters

Name Type Description
modelId string

Returns: IImageModel

View source

public Uri ImagesEditsUri()

Image edits URL.

Returns: Uri

View source

public Uri ImagesUri()

Images URL.

Returns: Uri

View source

public override ILanguageModel LanguageModel(string modelId)

Returns a language model.

Parameters

Name Type Description
modelId string

Returns: ILanguageModel

View source

PostBinaryAsync(Uri, string, IReadOnlyDictionary<string, string?>?, CancellationToken)

Section titled “PostBinaryAsync(Uri, string, IReadOnlyDictionary<string, string?>?, CancellationToken)”
public Task<OpenAICompatibleBinaryResponse> PostBinaryAsync(Uri uri, string json, IReadOnlyDictionary<string, string?>? headers, CancellationToken cancellationToken)

Posts JSON and returns the raw response bytes.

Parameters

Name Type Description
uri Uri
json string
headers IReadOnlyDictionary<string, string>
cancellationToken CancellationToken

Returns: Task<OpenAICompatibleBinaryResponse>

View source

PostJsonAsync(Uri, string, IReadOnlyDictionary<string, string?>?, CancellationToken)

Section titled “PostJsonAsync(Uri, string, IReadOnlyDictionary<string, string?>?, CancellationToken)”
public Task<ProviderTextResponse> PostJsonAsync(Uri uri, string json, IReadOnlyDictionary<string, string?>? headers, CancellationToken cancellationToken)

Posts JSON and rewrites the provider error message when one is available.

Parameters

Name Type Description
uri Uri
json string
headers IReadOnlyDictionary<string, string>
cancellationToken CancellationToken

Returns: Task<ProviderTextResponse>

View source

PostMultipartAsync(Uri, MultipartFormDataContent, IReadOnlyDictionary<string, string?>?, CancellationToken)

Section titled “PostMultipartAsync(Uri, MultipartFormDataContent, IReadOnlyDictionary<string, string?>?, CancellationToken)”
public Task<ProviderTextResponse> PostMultipartAsync(Uri uri, MultipartFormDataContent content, IReadOnlyDictionary<string, string?>? headers, CancellationToken cancellationToken)

Posts multipart form data and rewrites the provider error message when one is available.

Parameters

Name Type Description
uri Uri
content MultipartFormDataContent
headers IReadOnlyDictionary<string, string>
cancellationToken CancellationToken

Returns: Task<ProviderTextResponse>

View source

PostSseAsync(Uri, string, IReadOnlyDictionary<string, string?>?, CancellationToken)

Section titled “PostSseAsync(Uri, string, IReadOnlyDictionary<string, string?>?, CancellationToken)”
public Task<OpenAICompatibleSseResponse> PostSseAsync(Uri uri, string json, IReadOnlyDictionary<string, string?>? headers, CancellationToken cancellationToken)

Posts JSON and opens an SSE response. The caller disposes the result.

Parameters

Name Type Description
uri Uri
json string
headers IReadOnlyDictionary<string, string>
cancellationToken CancellationToken

Returns: Task<OpenAICompatibleSseResponse>

View source

public Uri RerankUri()

Rerank URL.

Returns: Uri

View source

protected virtual string? ResolveApiKey()

Resolves the API key. A missing key is omitted unless RequireApiKey is set.

Returns: string

View source

Apache-2.0 · Copyright 2023 Vercel, Inc. · unofficial community port, not an official Vercel product