translator

package
v0.2.0-rc1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 2, 2025 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LLMTokenUsage

type LLMTokenUsage struct {
	// InputTokens is the number of tokens consumed from the input.
	InputTokens uint32
	// OutputTokens is the number of tokens consumed from the output.
	OutputTokens uint32
	// TotalTokens is the total number of tokens consumed.
	TotalTokens uint32
}

LLMTokenUsage represents the token usage reported usually by the backend API in the response body.

type OpenAIChatCompletionTranslator added in v0.2.0

type OpenAIChatCompletionTranslator interface {
	// RequestBody translates the request body.
	// 	- `raw` is the raw request body.
	// 	- `body` is the request body parsed into the [openai.ChatCompletionRequest].
	//	- `onRetry` is true if this is a retry request.
	//	- This returns `headerMutation` and `bodyMutation` that can be nil to indicate no mutation.
	RequestBody(raw []byte, body *openai.ChatCompletionRequest, onRetry bool) (
		headerMutation *extprocv3.HeaderMutation,
		bodyMutation *extprocv3.BodyMutation,
		err error,
	)

	// ResponseHeaders translates the response headers.
	// 	- `headers` is the response headers.
	//	- This returns `headerMutation` that can be nil to indicate no mutation.
	ResponseHeaders(headers map[string]string) (
		headerMutation *extprocv3.HeaderMutation,
		err error,
	)

	// ResponseBody translates the response body. When stream=true, this is called for each chunk of the response body.
	// 	- `body` is the response body either chunk or the entire body, depending on the context.
	//	- This returns `headerMutation` and `bodyMutation` that can be nil to indicate no mutation.
	//  - This returns `tokenUsage` that is extracted from the body and will be used to do token rate limiting.
	ResponseBody(respHeaders map[string]string, body io.Reader, endOfStream bool) (
		headerMutation *extprocv3.HeaderMutation,
		bodyMutation *extprocv3.BodyMutation,
		tokenUsage LLMTokenUsage,
		err error,
	)
}

OpenAIChatCompletionTranslator translates the request and response messages between the client and the backend API schemas for /v1/chat/completion endpoint of OpenAI.

This is created per request and is not thread-safe.

func NewChatCompletionOpenAIToAWSBedrockTranslator

func NewChatCompletionOpenAIToAWSBedrockTranslator() OpenAIChatCompletionTranslator

NewChatCompletionOpenAIToAWSBedrockTranslator implements [Factory] for OpenAI to AWS Bedrock translation.

func NewChatCompletionOpenAIToAzureOpenAITranslator added in v0.2.0

func NewChatCompletionOpenAIToAzureOpenAITranslator(apiVersion string) OpenAIChatCompletionTranslator

NewChatCompletionOpenAIToAzureOpenAITranslator implements [Factory] for OpenAI to Azure OpenAI translations. Except RequestBody method requires modification to satisfy Microsoft Azure OpenAI spec https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#chat-completions, other interface methods are identical to NewChatCompletionOpenAIToOpenAITranslator's interface implementations.

func NewChatCompletionOpenAIToOpenAITranslator

func NewChatCompletionOpenAIToOpenAITranslator() OpenAIChatCompletionTranslator

NewChatCompletionOpenAIToOpenAITranslator implements [Factory] for OpenAI to OpenAI translation.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL