-
Notifications
You must be signed in to change notification settings - Fork 336
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Workaround for HuggingFace streaming API
- Loading branch information
Showing
5 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
examples/209-dotnet-HuggingFace/209-dotnet-HuggingFace.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<OutputType>Exe</OutputType> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\service\Core\Core.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
// ReSharper disable InconsistentNaming | ||
|
||
using Microsoft.KernelMemory; | ||
|
||
internal static class Program | ||
{ | ||
internal static async Task Main() | ||
{ | ||
// Using HuggingFace text generation | ||
var huggingFaceConfig = new OpenAIConfig | ||
{ | ||
Endpoint = "https://api-inference.huggingface.co/models/NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO/v1", | ||
TextModel = "NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO", | ||
TextModelMaxTokenTotal = 4096, | ||
TextGenerationType = OpenAIConfig.TextGenerationTypes.Chat, | ||
APIKey = Environment.GetEnvironmentVariable("HF_API_KEY")! | ||
}; | ||
|
||
// Using OpenAI for embeddings | ||
var openAIEmbeddingConfig = new OpenAIConfig | ||
{ | ||
EmbeddingModel = "text-embedding-ada-002", | ||
EmbeddingModelMaxTokenTotal = 8191, | ||
APIKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY")! | ||
}; | ||
|
||
var memory = new KernelMemoryBuilder() | ||
// IMPORTANT: TopP = 0.01 is required by HuggingFace API | ||
.WithSearchClientConfig(new SearchClientConfig { TopP = 0.01, AnswerTokens = 100 }) | ||
.WithOpenAITextGeneration(huggingFaceConfig) // Hugging Face | ||
.WithOpenAITextEmbeddingGeneration(openAIEmbeddingConfig) // OpenAI | ||
.Build(); | ||
|
||
// Import some text - This will use OpenAI embeddings | ||
await memory.ImportTextAsync("Today is December 12th"); | ||
|
||
// Generate an answer - This uses OpenAI for embeddings and finding relevant data, and Hugging Face to generate an answer | ||
var answer = await memory.AskAsync("How many days to Christmas?"); | ||
Console.WriteLine(answer.Question); | ||
Console.WriteLine(answer.Result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Trace", | ||
"Microsoft.AspNetCore": "Trace" | ||
}, | ||
"Console": { | ||
"LogToStandardErrorThreshold": "Critical", | ||
"FormatterName": "simple", | ||
"FormatterOptions": { | ||
"TimestampFormat": "[HH:mm:ss.fff] ", | ||
"SingleLine": true, | ||
"UseUtcTimestamp": false, | ||
"IncludeScopes": false, | ||
"JsonWriterOptions": { | ||
"Indented": true | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters