Configure an AI Provider

Run the same generation request through the default Aspose client and a caller-configured OpenAI-compatible endpoint. The complete project reads the endpoint, model identifier, API key and timeout from environment variables and saves separate editable results.

Prerequisites and input

Download the complete project. It includes every source file, SDK settings and locked dependencies. Install the pinned .NET SDK and set ASPOSE_LICENSE_PATH to your valid license file before running the full-content checks. See installation and licensing.

The example uses only synthetic prompts and creates its input presentation where needed. Generation and translation contact the selected model service. Set SLIDES_AI_BASE_URL, SLIDES_AI_MODEL and SLIDES_AI_API_KEY for your compatible service. Optional SLIDES_AI_TIMEOUT_SECONDS controls the HTTP timeout. Use --configured-only after -- when running only that provider.

Complete C# example

using System;
using System.Collections.Concurrent;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Aspose.Slides;
using Aspose.Slides.AI;
using Aspose.Slides.Export;

internal static class Program
{
    private const string Prompt = "Create a short English presentation about the water cycle. Cover evaporation, condensation and precipitation. Use ordinary text slides with a title and a short paragraph. No charts, tables or images. For every section in the JSON, use the integer 0 for its type field instead of a type name. Return raw JSON without Markdown code fences or commentary.";
    private static readonly ConcurrentBag<RequestResult> Requests = new();

    private static async Task<int> Main(string[] args)
    {
        try
        {
            string licensePath = Required("ASPOSE_LICENSE_PATH");
            new License().SetLicense(licensePath);
            string baseUrl = Required("SLIDES_AI_BASE_URL");
            string model = Required("SLIDES_AI_MODEL");
            string key = Required("SLIDES_AI_API_KEY");
            bool configuredOnly = args.Contains("--configured-only");
            int completed = 0;
            if (!configuredOnly)
            {
                using var defaultHttp = Http("default");
                using var defaultClient = new AsposeAIWebClient(defaultHttp);
                using IPresentation deck = await new SlidesAIAgent(defaultClient).GeneratePresentationAsync(Prompt, PresentationContentAmountType.Brief);
                VerifyGenerated(deck, "default-generated.pptx");
                completed++;
            }
            using var configuredHttp = Http("configured");
            using var configuredClient = new OpenAICompatibleWebClient(model, key, baseUrl, configuredHttp);
            using IPresentation configuredDeck = await new SlidesAIAgent(configuredClient).GeneratePresentationAsync(Prompt, PresentationContentAmountType.Brief);
            VerifyGenerated(configuredDeck, "configured-generated.pptx");
            completed++;
            Require(Requests.Any(r => r.Provider == "configured" && r.StatusCode == 200), "Configured provider did not return a model response.");
            Report(new { providers_checked = completed, configured_provider_succeeded = true, reopened_text_matches = true, requests = Requests.ToArray() });
            return 0;
        }
        catch (Exception error)
        {
            int exit = error is OperationCanceledException ? 4 : error is HttpRequestException ? 3 : 2;
            File.WriteAllText("error.json", JsonSerializer.Serialize(new { error_type = error.GetType().Name, requests = Requests.ToArray(), exit_code = exit }));
            Console.Error.WriteLine("AI example failed: " + error.GetType().Name + ". Check configuration and provider access.");
            return exit;
        }
    }

    private static string Required(string name)
    {
        string? value = Environment.GetEnvironmentVariable(name);
        if (string.IsNullOrWhiteSpace(value)) throw new InvalidOperationException("Set " + name + " before running this example.");
        return value;
    }

    private static HttpClient Http(string provider)
    {
        double timeout = 180;
        if (double.TryParse(Environment.GetEnvironmentVariable("SLIDES_AI_TIMEOUT_SECONDS"), System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out double configured)) timeout = configured;
        if (timeout <= 0) throw new InvalidOperationException("Timeout must be positive.");
        return new HttpClient(new TraceHandler(provider)) { Timeout = TimeSpan.FromSeconds(timeout) };
    }

    private static void Require(bool condition, string message)
    {
        if (!condition) throw new InvalidOperationException(message);
    }

    private static string AllText(IPresentation deck)
    {
        var text = new StringBuilder();
        void Read(IShapeCollection shapes)
        {
            foreach (IShape shape in shapes)
            {
                if (shape is IAutoShape auto && auto.TextFrame != null) text.AppendLine(auto.TextFrame.Text);
                if (shape is IGroupShape group) Read(group.Shapes);
                if (shape is ITable table)
                    for (int row = 0; row < table.Rows.Count; row++)
                        for (int column = 0; column < table.Columns.Count; column++) text.AppendLine(table[column, row].TextFrame.Text);
            }
        }
        foreach (ISlide slide in deck.Slides) Read(slide.Shapes);
        return text.ToString();
    }

    private static int VerifyGenerated(IPresentation deck, string path)
    {
        string live = AllText(deck);
        Require(deck.Slides.Count >= 2 && live.Length > 150 && live.Contains("water", StringComparison.OrdinalIgnoreCase), "Generated content did not describe the requested topic.");
        deck.Save(path, SaveFormat.Pptx);
        using var reopened = new Presentation(path);
        Require(reopened.Slides.Count == deck.Slides.Count && AllText(reopened) == live, "Generated editable text changed after reopening.");
        using var image = reopened.Slides[0].GetImage(new Size(640, 360));
        image.Save(Path.ChangeExtension(path, ".png"), Aspose.Slides.ImageFormat.Png);
        return reopened.Slides.Count;
    }

    private static void Report(object result)
    {
        string json = JsonSerializer.Serialize(result, new JsonSerializerOptions { WriteIndented = true });
        File.WriteAllText("result.json", json);
        Console.WriteLine(json);
    }

    private sealed record RequestResult(string Provider, int StatusCode);
    private sealed class TraceHandler : DelegatingHandler
    {
        private readonly string provider;
        public TraceHandler(string provider) : base(new HttpClientHandler()) { this.provider = provider; }
        protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken token)
        {
            HttpResponseMessage response = await base.SendAsync(request, token);
            Requests.Add(new RequestResult(provider, (int)response.StatusCode));
            return response;
        }
    }
}

Run and inspect the result

dotnet run --project Example.csproj --configuration Release

The program writes “default-generated.pptx”, “default-generated.png”, “configured-generated.pptx”, “configured-generated.png” and a result.json summary in the current directory. Open the PPTX files to edit their text and review the PNG previews at normal size. The source and the generated output are kept separate. Wording varies between model responses; successful execution checks structure and saved text, not a byte-identical deck.

Limitations

Provide a compatible service you are authorized to use and its real model identifier. The compatible client is an adapter, not a bundled AI model. Service authentication, request limits and model fees belong to the chosen provider. The program reports configuration, HTTP and cancellation failures without printing keys; –configured-only skips the default-service demonstration.

Continue with the detailed guide.