Skip to main content

SmartHttpEndpoint

Namespace: Wisej.AI.Endpoints

Assembly: Wisej.AI

Represents an abstract base class for a smart HTTP endpoint, providing common functionality for HTTP-based communication.

public class SmartHttpEndpoint : SmartEndpoint

This class is designed to facilitate communication with HTTP endpoints by managing authentication, headers, and retry logic. It provides methods for sending requests and handling responses, including error detection and retry mechanisms.

Constructors

SmartHttpEndpoint()

Initializes a new instance of the SmartHttpEndpoint class.

Properties

ApiKey

String: Gets or sets the API key used for authentication. (Default: null)

Authentication

String: Gets or sets the authentication scheme used for requests. (Default: "Bearer")

Headers

NameValueCollection: Gets or sets the collection of additional headers to be included in requests.

MaxRetries

Int32: Gets or sets the maximum number of retry attempts for failed requests. (Default: 10)

RetryDelay

Int32: Gets or sets the delay between retry attempts in milliseconds. (Default: 10000)

URL

String: Gets or sets the base URL of the endpoint. (Default: null)

The URL should not end with a trailing slash. If a trailing slash is present, it will be removed.

Methods

AskAsync(session, messages)

Asynchronously sends a request to the endpoint and returns the response message.

ParameterTypeDescription
sessionSmartSessionThe session managing the AI interaction.
messagesIList<Message>The list of messages to be sent.

Returns: Task<Message>. A task representing the asynchronous operation, with a Message as the result.

info

The implementation of this method should call session.Usage.Add(assistantMessage.Usage) to keep track of the input and output tokens utilized at the session level.

Throws:

AskEmbeddingsAsync(inputs)

Asynchronously requests embeddings for the specified inputs.

ParameterTypeDescription
inputsString[]The array of input strings for which embeddings are requested.

Returns: Task<Embedding>. A task representing the asynchronous operation, with an Embedding as the result.

This method sends the inputs to the embeddings endpoint and returns the resulting embeddings. Throws:

BuildPayload(session, messages)

Builds the payload for the request.

ParameterTypeDescription
sessionSmartSessionThe session managing the AI interaction.
messagesIList<Message>The list of messages to be included in the payload.

Returns: Object. The constructed payload object.

Throws:

CreateContent(data)

Creates the HTTP content from the given data.

ParameterTypeDescription
dataObjectThe data to be serialized into the content.

Returns: HttpContent. The created HttpContent.

DetectContextLimitExceeded(response)

Detects if the context limit has been exceeded in the response.

ParameterTypeDescription
responseHttpResponseMessageThe HTTP response message.

Returns: Boolean. true if the context limit is exceeded; otherwise, false.

DetectRateLimitExceeded(response)

Detects if the rate limit has been exceeded in the response.

ParameterTypeDescription
responseHttpResponseMessageThe HTTP response message.

Returns: Boolean. true if the rate limit is exceeded; otherwise, false.

GetApiKey()

Gets the API key for the endpoint.

Returns: String. The API key as a string.

GetApiURL()

Gets the API URL for the endpoint.

Returns: String. The API URL as a string.

GetEmbeddingsURL()

Gets the URL for embeddings.

Returns: String. The embeddings URL as a string.

Throws:

PostAsync(url, data)

Asynchronously sends a POST request to the specified URL with the given data.

ParameterTypeDescription
urlStringThe URL to send the request to.
dataObjectThe data to be sent in the request body.

Returns: Task<Response>. A task representing the asynchronous operation, with a Response as the result.

This method handles retries and error detection, including token and rate limit exceeded exceptions. Throws:

ReadAssistantMessage(response, message)

Reads the assistant's message from the API response.

ParameterTypeDescription
responseResponseThe response received from the API.
messageMessageThe message object to populate with the response content.

Throws:

ReadUsage(message, reply)

Reads the usage statistics from the API response.

ParameterTypeDescription
messageMessageThe message object to update with usage data.
replyObjectThe dynamic object containing the API response data.

Inherited By

NameDescription
AmazonBedrockEndpointRepresents an endpoint for connecting to Amazon Bedrock services.
AnthropicEndpointRepresents an endpoint for connecting to Anthropic services.
AzureAIEndpointRepresents an endpoint for connecting to Azure AI services, specifically designed to interact with OpenAI models.
CerebrasEndpointRepresents a connection to Cerebras endpoints, inheriting from OpenAIEndpoint.
DeepSeekEndpointRepresents a connection to DeepSeek endpoints, inheriting from OpenAIEndpoint.
GoogleAIEndpointRepresents a connection to Google AI endpoints for generating content and embeddings.
GroqCloudEndpointhttps://console.groq.com/docs/openai
GroqCloudEndpointWhisperRepresents a connection to GroqCloud speech endpoints for audio transcription.
HuggingFaceEndpointRepresents a connection to HuggingFace serverless endpoints for model inference and embeddings.
LocalAIEndpointRepresents a connection to LocalAI endpoints, providing access to various AI models and services.
LocalAIEndpointImageGenRepresents an endpoint for connecting to LocalAI Image Generation services.
LocalAIEndpointTTSRepresents an endpoint for connecting to LocalAI's speech services.
LocalAIEndpointWhisperRepresents an endpoint for connecting to LocalAI's Whisper model for speech-to-text transcriptions.
NvidiaAIEndpointRepresents a connection to NVIDIA AI endpoints, providing access to various AI models and services.
OllamaEndpointRepresents an endpoint that connects to Ollama services, providing functionalities for chat and embeddings.
OpenAIEndpointRepresents an endpoint for connecting to OpenAI services.
OpenAIEndpointDallERepresents an endpoint for connecting to OpenAI DallE services.
OpenAIEndpointRealtimeRepresents a component that manages communication with OpenAI's real-time API endpoints.
OpenAIEndpointTTSRepresents an endpoint for connecting to OpenAI's speech services.
OpenAIEndpointWhisperRepresents an endpoint for connecting to OpenAI's Whisper model for speech-to-text transcriptions.
SambaNovaEndpointRepresents a connection to SambaNova endpoints, inheriting from OpenAIEndpoint.
TogetherAIEndpointRepresents a connection to TogetherAI endpoints, providing access to various AI models and services.
XAIEndpointRepresents a connection to X.AI endpoints, extending the functionality of OpenAIEndpoint.