Documentation
¶
Overview ¶
Package service provides shared utility functions for relay providers, adapted from LingRein's internal/service package with gin.Context dependencies removed.
This package bridges provider adaptors to the relaykit conversion system and provides common helpers for response handling and usage estimation.
Index ¶
- func CloseResponseBodyGracefully(resp *http.Response)
- func ConvertRequest(ctx context.Context, info *common.RelayInfo, target types.RelayFormat, ...) (*relayconvert.RequestResult, error)
- func ConvertRequestByID(ctx context.Context, info *common.RelayInfo, converter string, request any) (*relayconvert.RequestResult, error)
- func EstimateTokenByModel(modelName string, text string) int
- func GetBase64Data(source string, context string) (string, string, error)
- func GetImageFromUrl(url string) ([]byte, string, error)
- func IOCopyBytesGracefully(w http.ResponseWriter, resp *http.Response, data []byte)
- func ResponseText2Usage(responseText string, modelName string, promptTokens int) *dto.Usage
- func ShouldCopyUpstreamHeader(key string, values []string) bool
- func TaskErrorWrapper(err error, code string, statusCode int) *common.TaskError
- func TaskErrorWrapperLocal(err error, code string, statusCode int) *common.TaskError
- func ValidUsage(usage *dto.Usage) bool
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CloseResponseBodyGracefully ¶
CloseResponseBodyGracefully closes a response body, ignoring errors.
func ConvertRequest ¶
func ConvertRequest(ctx context.Context, info *common.RelayInfo, target types.RelayFormat, request any) (*relayconvert.RequestResult, error)
ConvertRequest converts a request to the target relay format using relaykit.
func ConvertRequestByID ¶
func ConvertRequestByID(ctx context.Context, info *common.RelayInfo, converter string, request any) (*relayconvert.RequestResult, error)
ConvertRequestByID converts a request using a named converter.
func EstimateTokenByModel ¶
EstimateTokenByModel estimates token count for a given model and text. Uses a simple heuristic: ~4 characters per token for English text.
func GetBase64Data ¶
GetBase64Data returns base64-encoded data from a source (URL or base64 string).
func GetImageFromUrl ¶
GetImageFromUrl downloads an image from a URL and returns its data, MIME type, and any error.
func IOCopyBytesGracefully ¶
func IOCopyBytesGracefully(w http.ResponseWriter, resp *http.Response, data []byte)
IOCopyBytesGracefully writes data to the ResponseWriter.
func ResponseText2Usage ¶
ResponseText2Usage estimates usage from response text when the provider doesn't return token counts. Uses a simple word-based estimation.
func ShouldCopyUpstreamHeader ¶
ShouldCopyUpstreamHeader reports whether a header should be copied from the upstream response to the client response.
func TaskErrorWrapper ¶
TaskErrorWrapper wraps an error into a TaskError with code and status code.
func TaskErrorWrapperLocal ¶
TaskErrorWrapperLocal wraps a local error into a TaskError.
func ValidUsage ¶
ValidUsage checks if a usage struct has non-zero values.
Types ¶
This section is empty.