The HuggingFace SDK implements IChatClient and IEmbeddingGenerator<string, Embedding<float>> and provides AIFunction tool wrappers, all compatible with Microsoft.Extensions.AI.
Installation
1
dotnetaddpackagetryAGI.HuggingFace
Chat Completions
Chat completions use HuggingFaceInferenceClient, which implements IChatClient:
1 2 3 4 5 6 7 8 91011
usingMicrosoft.Extensions.AI;usingHuggingFace;IChatClientclient=newHuggingFaceInferenceClient(apiKey:Environment.GetEnvironmentVariable("HUGGINGFACE_API_KEY")!);varresponse=awaitclient.GetResponseAsync("What is the capital of France?",newChatOptions{ModelId="Qwen/Qwen2.5-Coder-32B-Instruct"});Console.WriteLine(response.Text);
Streaming
1 2 3 4 5 6 7 8 9101112
usingMicrosoft.Extensions.AI;usingHuggingFace;IChatClientclient=newHuggingFaceInferenceClient(apiKey:Environment.GetEnvironmentVariable("HUGGINGFACE_API_KEY")!);awaitforeach(varupdateinclient.GetStreamingResponseAsync("Explain quantum computing in simple terms.",newChatOptions{ModelId="Qwen/Qwen2.5-Coder-32B-Instruct"})){Console.Write(update.Text);}
usingMicrosoft.Extensions.AI;usingHuggingFace;IChatClientclient=newHuggingFaceInferenceClient(apiKey:Environment.GetEnvironmentVariable("HUGGINGFACE_API_KEY")!);vartool=AIFunctionFactory.Create((stringlocation)=>$"The weather in {location} is sunny, 22C.","GetWeather","Gets the current weather for a given location.");varoptions=newChatOptions{ModelId="Qwen/Qwen2.5-Coder-32B-Instruct",Tools=[tool],};varmessages=newList<ChatMessage>{new(ChatRole.User,"What is the weather in London?"),};while(true){varresponse=awaitclient.GetResponseAsync(messages,options);messages.AddRange(response.ToChatMessages());if(response.FinishReason==ChatFinishReason.ToolCalls){varresults=awaitresponse.CallToolsAsync(options);messages.AddRange(results);continue;}Console.WriteLine(response.Text);break;}
Text Embeddings
Embeddings use HuggingFaceEmbeddingClient, which implements IEmbeddingGenerator<string, Embedding<float>>: