mirror of
https://github.com/microsoft/semantic-kernel.git
synced 2026-10-12 02:19:06 +00:00
### Motivation and Context <!-- Thank you for your contribution to the semantic-kernel repo! Please help reviewers and future users, providing the following information: 1. Why is this change required? 2. What problem does it solve? 3. What scenario does it contribute to? 4. If it fixes an open issue, please link to the issue here. --> ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [ ] The code builds clean without any errors or warnings - [ ] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [ ] All unit tests pass, and I have added new tests where possible - [ ] I didn't break anyone 😄
73 lines
2.3 KiB
C#
73 lines
2.3 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
using System.Threading.Tasks;
|
|
using Microsoft.SemanticKernel;
|
|
using Microsoft.SemanticKernel.Connectors.OpenAI;
|
|
using Resources;
|
|
using Xunit;
|
|
using Xunit.Abstractions;
|
|
|
|
namespace Examples;
|
|
|
|
// ReSharper disable once InconsistentNaming
|
|
/// <summary>
|
|
/// Showcase usage of Open AI file-service.
|
|
/// </summary>
|
|
public sealed class Example79_OpenAIFiles : BaseTest
|
|
{
|
|
private const string ResourceFileName = "30-user-context.txt";
|
|
|
|
/// <summary>
|
|
/// Show how to utilize OpenAI file-service.
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task RunFileLifecycleAsync()
|
|
{
|
|
this.WriteLine("======== OpenAI File-Service ========");
|
|
|
|
if (TestConfiguration.OpenAI.ApiKey == null)
|
|
{
|
|
this.WriteLine("OpenAI apiKey not found. Skipping example.");
|
|
return;
|
|
}
|
|
|
|
// Initialize file-service
|
|
var kernel =
|
|
Kernel.CreateBuilder()
|
|
.AddOpenAIFiles(TestConfiguration.OpenAI.ApiKey)
|
|
.Build();
|
|
|
|
var fileService = kernel.GetRequiredService<OpenAIFileService>();
|
|
|
|
// Upload file
|
|
var fileContent = new BinaryContent(() => Task.FromResult(EmbeddedResource.ReadStream(ResourceFileName)!));
|
|
var fileReference =
|
|
await fileService.UploadContentAsync(
|
|
fileContent,
|
|
new OpenAIFileUploadExecutionSettings(ResourceFileName, OpenAIFilePurpose.Assistants));
|
|
|
|
WriteLine("SOURCE:");
|
|
WriteLine($"# Name: {fileReference.FileName}");
|
|
WriteLine("# Content:");
|
|
WriteLine(await fileContent.GetContentAsync());
|
|
|
|
try
|
|
{
|
|
// Retrieve file metadata for validation.
|
|
var copyReference = await fileService.GetFileAsync(fileReference.Id);
|
|
Assert.Equal(fileReference.Id, copyReference.Id);
|
|
WriteLine("REFERENCE:");
|
|
WriteLine($"# ID: {fileReference.Id}");
|
|
WriteLine($"# Name: {fileReference.FileName}");
|
|
WriteLine($"# Purpose: {fileReference.Purpose}");
|
|
}
|
|
finally
|
|
{
|
|
// Remove file
|
|
await fileService.DeleteFileAsync(fileReference.Id);
|
|
}
|
|
}
|
|
|
|
public Example79_OpenAIFiles(ITestOutputHelper output) : base(output) { }
|
|
}
|