OpenAI.InteractiveMedia 1.0.64

OpenAI.InteractiveMedia

OpenAI.InteractiveMedia is a NuGet package designed for ChatGPT and image generation using OpenAI's APIs.

Registration in Application

To register the service in your application, use the following code snippet:

using OpenAI.InteractiveMedia.OpenAIMedia;

builder.Services.AddOpenAIMedia("your-api-key"); // Replace with your actual API key

Dynamic API Key Configuration

The API key can be set either during service registration or dynamically at runtime using the ApiKey property in OpenAIMediaService. This allows you to change the key without restarting the application:

// Dynamically set the API key
openAIMediaService.ApiKey = "new-api-key";

Default Configuration

Base URL

  • Property: BaseUrl
  • Default value: https://api.openai.com/
  • Description: The base URL for the OpenAI API.

API Version

  • Property: Version
  • Default value: v1
  • Description: The specific version of the OpenAI API you wish to use. This allows for compatibility with future changes in the API.

Chat Completions Endpoint

  • Property: ChatCompletionsEndpoint
  • Default value: chat/completions
  • Description: The endpoint for the chat completions API.

Image Generations Endpoint

  • Property: ImageGenerationsEndpoint
  • Default value: images/generations
  • Description: The endpoint for the image generations API.

HTTP Client Timeout

  • Property: HttpClientTimeout
  • Default value: 00:03:00 (3 minutes)
  • Description: Specifies the maximum amount of time the HTTP client will wait for a response from the OpenAI API before timing out.

Models Pricing

  • Property: ModelsPricing
  • Default value: An empty list new List<ModelPricing>(), ready for custom pricing configurations.
  • Description: A list of pricing configurations for each model, allowing you to define the cost per 1,000 tokens for input, cached input and output tokens. This configuration enables flexible management of token costs based on model usage.
  • Note: If using pricing, ensure to set the prices according to OpenAI Pricing.

Configuration Example

You can customize the configuration by modifying the properties in the OpenAIMediaOptions class.
To utilize the OpenAIMediaOptions with custom settings, use the following code snippet:

builder.Services.AddOpenAIMedia("your-api-key", options =>
{    
    options.BaseUrl = "your-custom-base-url"; 
    options.Version = "your-custom-version";
    options.ChatCompletionsEndpoint = "your-custom-chat-completions-endpoint";   
    options.ImageGenerationsEndpoint = "your-custom-image-generations-endpoint";
    options.HttpClientTimeout = TimeSpan.FromMinutes(5);
    options.ModelsPricing = new List<ModelPricing>
    {
        new ModelPricing
        {
            Model = OpenAIModel.Gpt4oMini, // Set the model using the appropriate enum value from OpenAIModel for chat
            InputTokensPrice_USD_1K = 0.00015m,
            CachedInputTokensPrice_USD_1K = 0.000075m,
            OutputTokensPrice_USD_1K = 0.0006m
        }
    };
});

In your application, you can bind configuration values from the appsettings.json file directly to the OpenAIMediaOptions class using the following:

builder.Services.AddOpenAIMedia("your-api-key", options =>
{
    builder.Configuration.GetSection("OpenAIMediaOptions").Bind(options);
});

Then, configure these settings in the appsettings.json file:

{
    "OpenAIMediaOptions": {
        "BaseUrl": "your-custom-base-url",
        "Version": "your-custom-version",
        "ChatCompletionsEndpoint": "your-custom-chat-completions-endpoint",
        "ImageGenerationsEndpoint": "your-custom-image-generations-endpoint",  
        "HttpClientTimeout": "00:05:00",
        "ModelsPricing": [
            {
                "Model": 0,  // Set the model using the appropriate numeric value from the Available Models in the OpenAIModel enum for chat
                "InputTokensPrice_USD_1K": 0.00015,
                "CachedInputTokensPrice_USD_1K": 0.000075,
                "OutputTokensPrice_USD_1K": 0.0006
            }
        ]
    }
}

Available Models

The following models are available in the OpenAIModel enum, which includes models for both chat and image generation.

Enum Value Description Numeric Value Usage
Gpt4oMini gpt-4o-mini 0 Chat
Gpt4o gpt-4o 1 Chat
Gpt4Turbo gpt-4-turbo 2 Chat
Gpt35Turbo gpt-3.5-turbo 3 Chat
o1Mini o1-mini 4 Chat
o1Preview o1-preview 5 Chat
DallE3 dall-e-3 6 Image Generation

This approach allows you to manage configuration directly in appsettings.json.

Usage Example

using OpenAI.InteractiveMedia.OpenAIMedia;
using OpenAI.InteractiveMedia.Chat.Enums;
using OpenAI.InteractiveMedia.Chat.Models;
using OpenAI.InteractiveMedia.Image.Enums;
using OpenAI.InteractiveMedia.Shared;
using OpenAI.InteractiveMedia.Exceptions;

internal sealed class ExampleClass
{
    // System message content, typically used to define the assistant's behavior or provide specific instructions.
    private readonly systemContents = new List<Content>
    {      
        new Content(MessageType.Text, Text: "You are an AI assistant that provides helpful and concise answers."),        
    };

    // User message content, including both text and image inputs.
    private readonly userContents = new List<Content>
    {        
        new Content(MessageType.Text, Text: "What are the main differences between these two images?"),
        new Content(MessageType.Image, Image: new ImageContainer("https://example.com/image1.jpg", Source.Url)), // URL of an image
        new Content(MessageType.Image, Image: new ImageContainer("base64_image", Source.Base64, MimeType.Png))   // Base64-encoded image
    };  

    // List of messages sent to the OpenAI chat model, including system and user roles.
    private readonly List<Message> messages = new()
    {
        new Message(Role.System, systemContents),
        new Message(Role.User, userContents)
    };
    
    // OpenAI media service for interacting with the OpenAI API.
    private readonly OpenAIMediaService _openAIMedia; 

    // Constructor to inject the OpenAIMediaService dependency.
    public ExampleClass(OpenAIMediaService openAIMedia)
    {
        _openAIMedia = openAIMedia;
    }    

    // ######################## Method to get a reply from chat ########################

    public async Task GetChatResponseAsync()
    {    
        try
        {
            var chatResponse = await _openAIMedia.Chat.GetResponseAsync(
                messages: messages, 
                model: OpenAIModel.Gpt4oMini, 
                temperature: 0.2f, 
                responseCount: 1,
                cancellationToken: default);
        
            Console.WriteLine($"Response Content: {chatResponse.Content[0]}\n" +
                              $"Model Used: {chatResponse.Model}\n" +
                              $"Created At: {chatResponse.CreatedAt}\n" +
                              $"Usage Details: {chatResponse.TokensUsage}");
        }
        catch (OpenAiException ex)
        {
            // Handle exception            
        }
    }

    // ######################## Method to get a reply from chat in the specified format (JSON Schema) ########################

    // The response will be returned in the following format defined by the 'responseFormat' object:
    var responseFormat = new
    {
        type = "json_schema",
        json_schema = new
        {
            name = "city_data",
            schema = new
            {
                type = "object",
                properties = new
                {
                    cities = new
                    {
                        type = "array",
                        items = new
                        {
                            type = "object",
                            properties = new
                            {
                                name = new { type = "string" },
                                country = new { type = "string" },
                                population = new { type = "integer" }
                            },
                            required = new[] { "name", "country", "population" },
                            additionalProperties = false
                        }
                    }
                },
                required = new[] { "cities" },
                additionalProperties = false
            },
            strict = true
        }
    };

    public async Task GetChatResponseAsync()
    {    
        try
        {
            var chatResponse = await _openAIMedia.Chat.GetResponseAsync(
                messages: [new(Role.User, [new(MessageType.Text, Text: "Write down the ten largest cities.")])], 
                model: OpenAIModel.Gpt4oMini, 
                temperature: 0.2f, 
                responseCount: 1,
                responseFormat: responseFormat,
                cancellationToken: default);
        
            Console.WriteLine($"Response Content: {chatResponse.Content[0]}\n" +
                              $"Model Used: {chatResponse.Model}\n" +
                              $"Created At: {chatResponse.CreatedAt}\n" +
                              $"Usage Details: {chatResponse.TokensUsage}");
        }
        catch (OpenAiException ex)
        {
            // Handle exception            
        }
    }

    // ######################## Method to stream a response from chat ########################

    public async Task StreamResponseAsync()
    {
        try
        {
            await foreach (var stream in _openAIMedia.Chat.StreamResponseEnumerableAsync(
                messages: messages, 
                model: OpenAIModel.Gpt4oMini, 
                temperature: 0.2f, 
                cancellationToken: default))
            {
                // Output each piece of streaming content as it is received
                Console.Write(stream.Content[0]);

                if (stream.TokensUsage is not null)
                {                
                    Console.WriteLine($"\nModel Used: {stream.Model}\n" +
                                      $"Created At: {stream.CreatedAt}\n" +
                                      $"Usage Details: {stream.TokensUsage}");
                }
            }  
        }
        catch (OpenAiException ex)
        {
            // Handle exception            
        }      
    }

    // ######################## Method to generate an image ########################

    public async Task GenerateImageAsync()
    {     
        try
        {
            var imageResponse = await _openAIMedia.Image.GenerateAsync(
                prompt: "user-prompt-placeholder",  
                quality: ImageQuality.Standard,
                size: ImageSize._1024x1024,
                cancellationToken: default);
        
            Console.WriteLine($"Image URL: {imageResponse.Url}\n" +
                              $"Revised Prompt: {imageResponse.RevisedPrompt}\n" +
                              $"Created At: {imageResponse.CreatedAt}");
        }
        catch (OpenAiException ex)
        {
            // Handle exception            
        }
    }
}

Notes

Ensure to replace the placeholders in the code snippets with actual values applicable to your application.

No packages depend on OpenAI.InteractiveMedia.

Version Downloads Last updated
1.0.64 37 11/22/2025