Files
semantic-kernel/dotnet/samples/KernelSyntaxExamples/Example20_HuggingFace.cs
T
Roger Barreto b99b780eca .Net Kernel Examples as tests (#4526)
### Motivation and Context

Convert Kernel Examples as Integration Tests.
2024-01-16 19:16:24 +00:00

75 lines
3.0 KiB
C#

// Copyright (c) Microsoft. All rights reserved.
using System.Threading.Tasks;
using Microsoft.SemanticKernel;
using Xunit;
using Xunit.Abstractions;
namespace Examples;
// The following example shows how to use Semantic Kernel with HuggingFace API.
public class Example20_HuggingFace : BaseTest
{
/// <summary>
/// This example uses HuggingFace Inference API to access hosted models.
/// More information here: <see href="https://huggingface.co/inference-api"/>
/// </summary>
[Fact]
public async Task RunInferenceApiExampleAsync()
{
WriteLine("\n======== HuggingFace Inference API example ========\n");
Kernel kernel = Kernel.CreateBuilder()
.AddHuggingFaceTextGeneration(
model: TestConfiguration.HuggingFace.ModelId,
apiKey: TestConfiguration.HuggingFace.ApiKey)
.Build();
var questionAnswerFunction = kernel.CreateFunctionFromPrompt("Question: {{$input}}; Answer:");
var result = await kernel.InvokeAsync(questionAnswerFunction, new() { ["input"] = "What is New York?" });
WriteLine(result.GetValue<string>());
}
/// <summary>
/// This example uses HuggingFace Llama 2 model and local HTTP server from Semantic Kernel repository.
/// How to setup local HTTP server: <see href="https://github.com/microsoft/semantic-kernel/blob/main/samples/apps/hugging-face-http-server/README.md"/>.
/// <remarks>
/// Additional access is required to download Llama 2 model and run it locally.
/// How to get access:
/// 1. Visit <see href="https://ai.meta.com/resources/models-and-libraries/llama-downloads/"/> and complete request access form.
/// 2. Visit <see href="https://huggingface.co/meta-llama/Llama-2-7b-hf"/> and complete form "Access Llama 2 on Hugging Face".
/// Note: Your Hugging Face account email address MUST match the email you provide on the Meta website, or your request will not be approved.
/// </remarks>
/// </summary>
[Fact(Skip = "Requires local model or Huggingface Pro subscription")]
public async Task RunLlamaExampleAsync()
{
WriteLine("\n======== HuggingFace Llama 2 example ========\n");
// HuggingFace Llama 2 model: https://huggingface.co/meta-llama/Llama-2-7b-hf
const string Model = "meta-llama/Llama-2-7b-hf";
// HuggingFace local HTTP server endpoint
// const string Endpoint = "http://localhost:5000/completions";
Kernel kernel = Kernel.CreateBuilder()
.AddHuggingFaceTextGeneration(
model: Model,
//endpoint: Endpoint,
apiKey: TestConfiguration.HuggingFace.ApiKey)
.Build();
var questionAnswerFunction = kernel.CreateFunctionFromPrompt("Question: {{$input}}; Answer:");
var result = await kernel.InvokeAsync(questionAnswerFunction, new() { ["input"] = "What is New York?" });
WriteLine(result.GetValue<string>());
}
public Example20_HuggingFace(ITestOutputHelper output) : base(output)
{
}
}