claude

package
v6.0.16 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2025 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package claude provides request translation functionality for Anthropic to OpenAI API. It handles parsing and transforming Anthropic API requests into OpenAI Chat Completions API format, extracting model information, system instructions, message contents, and tool declarations. The package performs JSON data transformation to ensure compatibility between Anthropic API format and OpenAI API's expected format.

Package claude provides response translation functionality for OpenAI to Anthropic API. This package handles the conversion of OpenAI Chat Completions API responses into Anthropic API-compatible JSON format, transforming streaming events and non-streaming responses into the format expected by Anthropic API clients. It supports both streaming and non-streaming modes, handling text content, tool calls, and usage metadata appropriately.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertClaudeRequestToOpenAI

func ConvertClaudeRequestToOpenAI(modelName string, inputRawJSON []byte, stream bool) []byte

ConvertClaudeRequestToOpenAI parses and transforms an Anthropic API request into OpenAI Chat Completions API format. It extracts the model name, system instruction, message contents, and tool declarations from the raw JSON request and returns them in the format expected by the OpenAI API.

func ConvertOpenAIResponseToClaude

func ConvertOpenAIResponseToClaude(_ context.Context, _ string, originalRequestRawJSON, requestRawJSON, rawJSON []byte, param *any) []string

ConvertOpenAIResponseToClaude converts OpenAI streaming response format to Anthropic API format. This function processes OpenAI streaming chunks and transforms them into Anthropic-compatible JSON responses. It handles text content, tool calls, and usage metadata, outputting responses that match the Anthropic API format.

Parameters:

  • ctx: The context for the request.
  • modelName: The name of the model.
  • rawJSON: The raw JSON response from the OpenAI API.
  • param: A pointer to a parameter object for the conversion.

Returns:

  • []string: A slice of strings, each containing an Anthropic-compatible JSON response.

func ConvertOpenAIResponseToClaudeNonStream

func ConvertOpenAIResponseToClaudeNonStream(_ context.Context, _ string, originalRequestRawJSON, requestRawJSON, rawJSON []byte, _ *any) string

ConvertOpenAIResponseToClaudeNonStream converts a non-streaming OpenAI response to a non-streaming Anthropic response.

Parameters:

  • ctx: The context for the request.
  • modelName: The name of the model.
  • rawJSON: The raw JSON response from the OpenAI API.
  • param: A pointer to a parameter object for the conversion.

Returns:

  • string: An Anthropic-compatible JSON response.

Types

type ConvertOpenAIResponseToAnthropicParams

type ConvertOpenAIResponseToAnthropicParams struct {
	MessageID string
	Model     string
	CreatedAt int64
	// Content accumulator for streaming
	ContentAccumulator strings.Builder
	// Tool calls accumulator for streaming
	ToolCallsAccumulator map[int]*ToolCallAccumulator
	// Track if text content block has been started
	TextContentBlockStarted bool
	// Track finish reason for later use
	FinishReason string
	// Track if content blocks have been stopped
	ContentBlocksStopped bool
	// Track if message_delta has been sent
	MessageDeltaSent bool
}

ConvertOpenAIResponseToAnthropicParams holds parameters for response conversion

type ToolCallAccumulator

type ToolCallAccumulator struct {
	ID        string
	Name      string
	Arguments strings.Builder
}

ToolCallAccumulator holds the state for accumulating tool call data

Jump to

Keyboard shortcuts

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