ai_protocols

package
v0.1.0-alpha.2 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2026 License: Apache-2.0 Imports: 14 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	OpenAIChat = Protocol{
		OverrideKey: "openai-chat",
		RequestType: "ai_chat",
		Endpoint:    "/v1/chat/completions",
	}
	OpenAIResponses = Protocol{
		OverrideKey: "openai-responses",
		RequestType: "ai_responses",
		Endpoint:    "/v1/responses",
	}
	OpenAIEmbeddings = Protocol{
		OverrideKey: "openai-embeddings",
		RequestType: "ai_embeddings",
		Endpoint:    "/v1/embeddings",
	}
	AnthropicMessages = Protocol{
		OverrideKey: "anthropic-messages",
		RequestType: "ai_chat",
		Endpoint:    "/v1/messages",
	}
	BedrockConverse = Protocol{
		OverrideKey: "bedrock-converse",
		RequestType: "ai_chat",
		Endpoint:    "/converse",
	}
	Passthrough = Protocol{
		OverrideKey: "passthrough",
		RequestType: "ai_chat",
	}
)

Functions

func AppendBedrockEndpoint

func AppendBedrockEndpoint(endpoint string, model string, streaming bool) (string, error)

AppendBedrockEndpoint appends the model path to a Bedrock endpoint.

func AppendMessages

func AppendMessages(protocol Protocol, body map[string]any, messages []Message)

func AppendProtocolEndpoint

func AppendProtocolEndpoint(endpoint string, protocol Protocol) (string, error)

AppendProtocolEndpoint appends the protocol-specific path to an OpenAI-compatible endpoint.

func BuildDenyResponse

func BuildDenyResponse(protocol Protocol, model, text string) map[string]any

func BuildDenyWireResponse

func BuildDenyWireResponse(protocol Protocol, model, denyText string, streaming bool) ([]byte, string, error)

func BuildSimpleRequest

func BuildSimpleRequest(protocol Protocol, systemPrompt, userContent string, options map[string]any) map[string]any

func ConvertAnthropicHeadersToOpenAI

func ConvertAnthropicHeadersToOpenAI(headers http.Header)

func ConvertAnthropicMessagesToOpenAI

func ConvertAnthropicMessagesToOpenAI(body []byte) ([]byte, map[string]string, error)

func ConvertOpenAIChatToAnthropic

func ConvertOpenAIChatToAnthropic(body []byte, model string, toolNameMap map[string]string) ([]byte, error)

func ConvertOpenAIEmbeddingsToVertex

func ConvertOpenAIEmbeddingsToVertex(body []byte) ([]byte, error)

func ConvertVertexEmbeddingsToOpenAI

func ConvertVertexEmbeddingsToOpenAI(body []byte, model string) ([]byte, error)

func ExtractRequestContent

func ExtractRequestContent(protocol Protocol, body map[string]any) []string

func ExtractResponseText

func ExtractResponseText(protocol Protocol, body map[string]any) string

func ExtractStreamEventText

func ExtractStreamEventText(protocol Protocol, event map[string]any) string

func IsStreaming

func IsStreaming(protocol Protocol, body map[string]any) bool

func NumericUsage

func NumericUsage(value any, round bool) int64

NumericUsage converts a usage counter value. Fractional float64 values are rounded when round is true and truncated otherwise. Unsupported types return -1.

func PrependMessages

func PrependMessages(protocol Protocol, body map[string]any, messages []Message)

func ProviderUsesOpenAIChat

func ProviderUsesOpenAIChat(provider string) bool

ProviderUsesOpenAIChat reports whether the provider speaks the OpenAI chat protocol.

func RegisterLLMRequestVars

func RegisterLLMRequestVars(r *http.Request, requestDocument Document, protocol Protocol, responseDocument Document)

RegisterLLMRequestVars publishes the shared request variables for single- and multi-instance AI proxying.

func RegisterUsageContextDocumentVars

func RegisterUsageContextDocumentVars(r *http.Request, document Document, promptTokens int64, completionTokens int64)

RegisterUsageContextDocumentVars publishes the raw usage and token usage variables from a response document.

func RequestContent

func RequestContent(protocol Protocol, body map[string]any) any

func StringValue

func StringValue(value any) string

StringValue returns value when it is a string and an empty string otherwise.

Types

type Document

type Document struct {
	Raw map[string]any
}

Document is a decoded AI JSON payload. Callers should decode each request or non-streaming response once and pass the document to all extractors.

func ConvertAnthropicMessagesDocumentToOpenAI

func ConvertAnthropicMessagesDocumentToOpenAI(
	document Document,
) (Document, map[string]string, error)

func DecodeDocument

func DecodeDocument(body []byte) (Document, error)

func (Document) IsStreaming

func (d Document) IsStreaming(protocol Protocol) bool

func (Document) Model

func (d Document) Model() string

type Message

type Message struct {
	Role    string `json:"role"`
	Content string `json:"content"`
}

func ExtractMessages

func ExtractMessages(protocol Protocol, body map[string]any) []Message

type Protocol

type Protocol struct {
	OverrideKey string
	RequestType string
	Endpoint    string
}

func Detect

func Detect(requestPath string, body map[string]any) (Protocol, error)

type ResponseMetadata

type ResponseMetadata struct {
	Model            string
	PromptTokens     int64
	CompletionTokens int64
}

func ExtractResponseMetadata

func ExtractResponseMetadata(protocol Protocol, body []byte) ResponseMetadata

func ExtractResponseMetadataDocument

func ExtractResponseMetadataDocument(protocol Protocol, document Document) ResponseMetadata

Jump to

Keyboard shortcuts

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