Semantic Kernel + Ollama + LG EXAONE 3.5 모델을 결합한 로컬 LLM (feat. C# + LINQPad for macOS)

요즈음 생성형 AI 모델을 로컬 머신에서 구동하는 것을 고민하는 분들이 많이 있는데, 저도 호기심이 생겨 테스트를 몇 가지 해보았습니다.

최근에 출시된 LINQPad for macOS 덕분에 여러모로 편리한 개발 환경을 구축할 수 있었고, 여러 시행 착오 끝에 지금으로서는 Ollama를 사용하는 것이 가장 완성도 높고 오류가 적은 로컬 AI 사용을 지원한다고 판단하여 아래처럼 간단한 샘플 코드를 만들어보았습니다.

사용한 NuGet 패키지는 Microsoft.SemanticKernel.Connectors.Ollama (1.32.0-prealpha)입니다.

#pragma warning disable SKEXP0001
#pragma warning disable SKEXP0070

var builder = Kernel.CreateBuilder();

var modelName = "exaone3.5";
var apiClient = new OllamaApiClient(new Uri("http://127.0.0.1:11434", UriKind.Absolute));
await foreach (var eachResult in apiClient.PullModelAsync(modelName).ConfigureAwait(false))
{
	if (eachResult == null)
		continue;
	await Console.Out.WriteLineAsync($"{eachResult.Status}: {eachResult.Percent:#,#0.00}%".AsMemory()).ConfigureAwait(false);
}
apiClient.SelectedModel = modelName;

builder.AddOllamaChatCompletion(apiClient);
builder.Services.AddLogging(services => services.AddConsole());

// Build the kernel
var kernel = builder.Build();
var chatCompletionService = kernel.GetRequiredService<IChatCompletionService>();

// Create a history store the conversation
var history = new ChatHistory();

// Initiate a back-and-forth chat
var userInput = string.Empty;
do
{
	// Collect user input
	Console.Write("User > ");
	userInput = Console.ReadLine() ?? string.Empty;

	// Add user input
	history.AddUserMessage(userInput);

	// Get the response from the AI
	await foreach (var eachFragment in chatCompletionService.GetStreamingChatMessageContentsAsync(
		history,
		kernel: kernel).ConfigureAwait(false))
	{
		Console.Write(eachFragment.Content);
		history.AddMessage(eachFragment.Role.Value, eachFragment.Content ?? string.Empty);
	}
	
	Console.WriteLine();
	
} while (userInput is not null);

저는 맥북 에어 M2 CTO 버전 (메모리 용량을 16GB로 높인 버전)을 사용 중이고, Semantic Kernel에 Ollama Connector를 붙이고, LG EXAONE 3.5 모델을 가지고 테스트해보았습니다.

참고로 Ollama는 AI 추론에 특화되지 않은 1~2세대 이전 컴퓨터에서도 사용할 수 있도록 기능을 제공하고 있으니 여러 모델들을 두루 살펴보면서 테스트해보시면 더 재미있을 것 같습니다!

9개의 좋아요