Documentation
¶
Overview ¶
Package testopenai provides Azure OpenAI support for VCR cassette recording and playback.
Azure OpenAI cassettes follow a specific workflow to enable recording and playback while protecting sensitive information:
RECORDING FLOW:
- NewRequest builds Azure-format path from request body model: Input: endpoint="/chat/completions", body={"model":"gpt-4"} Output: path="/openai/deployments/gpt-4/chat/completions" Note: No api-version in the path built by NewRequest.
2. Server forwards to upstream Azure API:
- Reads AZURE_OPENAI_ENDPOINT (e.g., https://your-resource.eastus2.cognitiveservices.azure.com)
- Reads AZURE_OPENAI_DEPLOYMENT (deployment name configured in Azure portal)
- Reads OPENAI_API_VERSION (e.g., 2024-12-01-preview)
- Builds URL: {endpoint}/openai/deployments/{deployment}/{endpoint}?api-version={version}
- Example: https://your-resource.eastus2.cognitiveservices.azure.com/openai/deployments/prod-gpt4/chat/completions?api-version=2024-12-01-preview
3. VCR afterCaptureHook scrubs sensitive data from recorded cassette:
- Replaces actual hostname with "resource-name.cognitiveservices.azure.com"
- Replaces deployment name with model name from request body
- Strips api-version query parameter (not needed for playback matching)
- Result: https://resource-name.cognitiveservices.azure.com/openai/deployments/gpt-4/chat/completions
PLAYBACK FLOW: 1. NewRequest builds same Azure-format path (no api-version) 2. Server normalizes cassette URL by replacing hostname only 3. Request path matches cassette path exactly
ENVIRONMENT VARIABLES: Recording Azure cassettes requires: - AZURE_OPENAI_API_KEY: Authentication for Azure OpenAI - AZURE_OPENAI_ENDPOINT: Base URL (hostname is scrubbed in cassette) - AZURE_OPENAI_DEPLOYMENT: Deployment name (replaced with model in cassette) - OPENAI_API_VERSION: API version (stripped from cassette, only used upstream)
Recording OpenAI cassettes requires: - OPENAI_API_KEY: Authentication for OpenAI
Package testopenai provides a test OpenAI API server for testing. It uses VCR (Video Cassette Recorder) pattern to replay pre-recorded API responses, allowing deterministic testing without requiring actual API access or credentials.
Index ¶
Constants ¶
const CassetteNameHeader = "X-Cassette-Name"
CassetteNameHeader is the header used to specify which cassette to use for matching.
Variables ¶
This section is empty.
Functions ¶
func NewRequest ¶
NewRequest creates a new OpenAI request for the given cassette.
The returned request is an http.MethodPost with the body and CassetteNameHeader according to the pre-recorded cassette.
func ResponseBody ¶
ResponseBody is used in tests to avoid duplicating body content when the proxy serialization matches exactly the upstream (testopenai) server.
Types ¶
type Cassette ¶
type Cassette int
Cassette is an HTTP interaction recording.
Note: At the moment, our tests are optimized for single request/response pairs and do not include scenarios requiring multiple round-trips, such as `cached_tokens`.
const ( // CassetteChatBasic is the canonical OpenAI chat completion request. CassetteChatBasic Cassette = iota // CassetteChatJSONMode is a chat completion request with JSON response format. CassetteChatJSONMode // CassetteChatMultimodal is a multimodal chat request with text and image inputs. CassetteChatMultimodal // CassetteChatMultiturn is a multi-turn conversation with message history. CassetteChatMultiturn // CassetteChatNoMessages is a request missing the required messages field. CassetteChatNoMessages // CassetteChatParallelTools is a chat completion with parallel function calling enabled. CassetteChatParallelTools // CassetteChatStreaming is the canonical OpenAI chat completion request, // with streaming enabled. CassetteChatStreaming // CassetteChatTools is a chat completion request with function tools. CassetteChatTools // CassetteChatUnknownModel is a request with a non-existent model. CassetteChatUnknownModel // CassetteChatBadRequest is a request with multiple validation errors. CassetteChatBadRequest // CassetteChatReasoning tests capture of reasoning_tokens in completion_tokens_details for O1 models. CassetteChatReasoning // CassetteChatImageToText tests image input processing showing image token // count in usage details. CassetteChatImageToText // CassetteChatTextToImageTool tests image generation through tool calls since // chat completions cannot natively output images. CassetteChatTextToImageTool // CassetteChatAudioToText tests audio input transcription and audio_tokens // in prompt_tokens_details. CassetteChatAudioToText // CassetteChatTextToAudio tests audio output generation where the model // produces audio content, showing audio_tokens in completion_tokens_details. CassetteChatTextToAudio // CassetteChatDetailedUsage tests capture of all token usage detail fields in a single response. CassetteChatDetailedUsage // CassetteChatStreamingDetailedUsage tests capture of detailed token usage in streaming responses with include_usage. CassetteChatStreamingDetailedUsage // CassetteChatWebSearch tests OpenAI Web Search tool with a small URL response, including citations. CassetteChatWebSearch // CassetteChatStreamingWebSearch is CassetteChatWebSearch except with streaming enabled. CassetteChatStreamingWebSearch // CassetteChatOpenAIAgentsPython is a real request from OpenAI Agents Python library for financial research. // See https://github.com/openai/openai-agents-python/tree/main/examples/financial_research_agent CassetteChatOpenAIAgentsPython // CassetteCompletionBasic tests standard single-prompt code completion // requests typical of LoRA-tuned CodeLlama or Starcoder models deployed via // vLLM/llama.cpp. Uses fibonacci function as representative coding task for // model evaluation. // See: https://docs.vllm.ai/en/latest/serving/openai_compatible_server.html CassetteCompletionBasic // CassetteCompletionToken is CassetteCompletionBasic, but with cl100k_base // tokens as input instead of text strings. This simulates LoRA fine-tuning // workflows requiring precise tokenization control. CassetteCompletionToken // CassetteCompletionStreaming is CassetteCompletionBasic, with streaming // enabled to test real-time token delivery common in IDE. CassetteCompletionStreaming // CassetteCompletionStreamingUsage is CassetteCompletionStreaming, but // with include_usage enabled to test detailed token usage reporting. CassetteCompletionStreamingUsage // CassetteCompletionTextBatch tests multiple code completion variants // generated simultaneously, common in IDE autocomplete where users select // from LoRA model suggestions. Full vs truncated prompts simulate real // editing scenarios. // See: https://community.openai.com/t/n-argument-vs-batch-input/59121 CassetteCompletionTextBatch // CassetteCompletionTokenBatch is CassetteCompletionTextBatch, but with // cl100k_base tokens as input instead of text strings. This simulates LoRA // fine-tuning workflows requiring precise tokenization control. CassetteCompletionTokenBatch // CassetteCompletionSuffix tests the suffix parameter for fill-in-the-middle // completion tasks. The model generates code to insert between a prompt (partial // function definition) and a suffix (function call). Also tests logprobs for // confidence scoring and n for multiple completion variants. Only gpt-3.5-turbo-instruct // supports the suffix parameter. // See: https://platform.openai.com/docs/guides/completions/inserting-text CassetteCompletionSuffix // CassetteCompletionBadRequest is a request with multiple validation // errors. CassetteCompletionBadRequest // CassetteCompletionUnknownModel is a request with a non-existent model. CassetteCompletionUnknownModel // CassetteEmbeddingsBasic is the canonical OpenAI embeddings request with a single string input. CassetteEmbeddingsBasic // CassetteEmbeddingsBase64 tests base64 encoding format for embedding vectors. CassetteEmbeddingsBase64 // CassetteEmbeddingsTokens tests embeddings with token array input instead of text. CassetteEmbeddingsTokens // CassetteEmbeddingsLargeText tests embeddings with a longer text input. CassetteEmbeddingsLargeText // CassetteEmbeddingsUnknownModel tests error handling for non-existent model. CassetteEmbeddingsUnknownModel // CassetteEmbeddingsDimensions tests embeddings with specified output dimensions. CassetteEmbeddingsDimensions // CassetteEmbeddingsMixedBatch tests batch with varying text lengths. CassetteEmbeddingsMixedBatch // CassetteEmbeddingsMaxTokens tests input that approaches token limit. CassetteEmbeddingsMaxTokens // CassetteEmbeddingsWhitespace tests handling of various whitespace patterns. CassetteEmbeddingsWhitespace // CassetteEmbeddingsBadRequest tests request with multiple validation errors. CassetteEmbeddingsBadRequest // CassetteImageGenerationBasic is a basic image generation request with model and prompt. CassetteImageGenerationBasic // CassetteAzureChatBasic is the same as CassetteChatBasic, except using // Azure OpenAI Service authentication and endpoint format. CassetteAzureChatBasic )
func ChatCassettes ¶
func ChatCassettes() []Cassette
ChatCassettes returns a slice of all cassettes for chat completions. Unlike image generation—which *requires* an image_generation tool call— audio synthesis is natively supported.
func CompletionCassettes ¶ added in v0.4.0
func CompletionCassettes() []Cassette
CompletionCassettes returns a slice of all cassettes for the /completions endpoint.
func EmbeddingsCassettes ¶
func EmbeddingsCassettes() []Cassette
EmbeddingsCassettes returns a slice of all cassettes for embeddings.
func ImageCassettes ¶ added in v0.4.0
func ImageCassettes() []Cassette
ImageCassettes returns a slice of all cassettes for image generation.