translator

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2025 License: Apache-2.0 Imports: 29 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var SJSONOptions = &sjson.Options{
	Optimistic:     true,
	ReplaceInPlace: true,
}

SJSONOptions are the options used for sjson operations in the translator. This is also used outside the package to share the same options for consistency.

Functions

This section is empty.

Types

type AnthropicMessagesTranslator added in v0.3.0

type AnthropicMessagesTranslator interface {
	// RequestBody translates the request body.
	// 	- `raw` is the raw request body.
	// 	- `body` is the request body parsed into the [anthropicschema.MessagesRequest].
	//	- `forceBodyMutation` is true if the translator should always mutate the body, even if no changes are made.
	//	- This returns `headerMutation` and `bodyMutation` that can be nil to indicate no mutation.
	RequestBody(raw []byte, body *anthropicschema.MessagesRequest, forceBodyMutation 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,
	)
}

AnthropicMessagesTranslator translates the request and response messages between the client and the backend API schemas for /v1/messages endpoint of Anthropic.

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

func NewAnthropicToGCPAnthropicTranslator added in v0.3.0

func NewAnthropicToGCPAnthropicTranslator(apiVersion string, modelNameOverride string) AnthropicMessagesTranslator

NewAnthropicToGCPAnthropicTranslator creates a translator for Anthropic to GCP Anthropic format. This is essentially a passthrough translator with GCP-specific modifications.

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].
	//	- `forceBodyMutation` is true if the translator should always mutate the body, even if no changes are made.
	//	- This returns `headerMutation` and `bodyMutation` that can be nil to indicate no mutation.
	RequestBody(raw []byte, body *openai.ChatCompletionRequest, forceBodyMutation 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, span tracing.ChatCompletionSpan) (
		headerMutation *extprocv3.HeaderMutation,
		bodyMutation *extprocv3.BodyMutation,
		tokenUsage LLMTokenUsage,
		err error,
	)

	// ResponseError translates the response error. This is called when the upstream response status code is not successful (2xx).
	// 	- `respHeaders` is the response headers.
	// 	- `body` is the response body that contains the error message.
	ResponseError(respHeaders map[string]string, body io.Reader) (headerMutation *extprocv3.HeaderMutation, bodyMutation *extprocv3.BodyMutation, 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(modelNameOverride string) OpenAIChatCompletionTranslator

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

func NewChatCompletionOpenAIToAzureOpenAITranslator added in v0.2.0

func NewChatCompletionOpenAIToAzureOpenAITranslator(apiVersion string, modelNameOverride 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 NewChatCompletionOpenAIToGCPAnthropicTranslator added in v0.3.0

func NewChatCompletionOpenAIToGCPAnthropicTranslator(apiVersion string, modelNameOverride string) OpenAIChatCompletionTranslator

NewChatCompletionOpenAIToGCPAnthropicTranslator implements [Factory] for OpenAI to GCP Anthropic translation. This translator converts OpenAI ChatCompletion API requests to GCP Anthropic API format.

func NewChatCompletionOpenAIToGCPVertexAITranslator added in v0.3.0

func NewChatCompletionOpenAIToGCPVertexAITranslator(modelNameOverride string) OpenAIChatCompletionTranslator

NewChatCompletionOpenAIToGCPVertexAITranslator implements [Factory] for OpenAI to GCP Gemini translation. This translator converts OpenAI ChatCompletion API requests to GCP Gemini API format.

func NewChatCompletionOpenAIToOpenAITranslator

func NewChatCompletionOpenAIToOpenAITranslator(apiVersion string, modelNameOverride string) OpenAIChatCompletionTranslator

NewChatCompletionOpenAIToOpenAITranslator implements [Factory] for OpenAI to OpenAI translation.

type OpenAIEmbeddingTranslator added in v0.3.0

type OpenAIEmbeddingTranslator interface {
	// RequestBody translates the request body.
	// 	- `raw` is the raw request body.
	// 	- `body` is the request body parsed into the [openai.EmbeddingRequest].
	//	- `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.EmbeddingRequest, 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.
	// 	- `body` is the response body.
	//	- 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,
	)

	// ResponseError translates the response error. This is called when the upstream response status code is not successful (2xx).
	// 	- `respHeaders` is the response headers.
	// 	- `body` is the response body that contains the error message.
	ResponseError(respHeaders map[string]string, body io.Reader) (headerMutation *extprocv3.HeaderMutation, bodyMutation *extprocv3.BodyMutation, err error)
}

OpenAIEmbeddingTranslator translates the request and response messages between the client and the backend API schemas for /v1/embeddings endpoint of OpenAI.

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

func NewEmbeddingOpenAIToOpenAITranslator added in v0.3.0

func NewEmbeddingOpenAIToOpenAITranslator(apiVersion string, modelNameOverride string) OpenAIEmbeddingTranslator

NewEmbeddingOpenAIToOpenAITranslator implements [Factory] for OpenAI to OpenAI translation for embeddings.

Jump to

Keyboard shortcuts

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