DEV Community

Cover image for มาสร้าง AI สามัญประจำเครื่องด้วย Microsoft Foundry Local
Watchara Sukka for silicon brain

Posted on • Edited on

มาสร้าง AI สามัญประจำเครื่องด้วย Microsoft Foundry Local

ที่งาน  Microsoft Build 2025 ทาง Microsoft ได้มีการเปิดตัว Foundry Local ซึ่งเป็นเครื่องมือในการนำ Language Model มารันบนเครื่องคอมได้(ซึ่งการทำงานจะเหมือน Ollama นั่นเอง)
โดยได้เตรียมเครื่องมือสำหรับใช้คือ

  • มี REST API ให้ใช้งาน ซึ่ง compatability กับ OpenAI
  • มี CLI และ SDK มาให้ใช้งาน
  • ใช้ ONNX runtime สำหรับการ run model ซึ่งสามารถงานหลากหลาย hardware และ device (CPU, GPU และ NPU)

โครงสร้างของ Foundry Local

การติดตั้ง

Foundry Local รองรับทั้งระบบปฏิบัติการ Windows และ MacOS
สำหรับ Windows ติดตั้งผ่าน Command Line โดยใช้คำสั่ง

winget install Microsoft.FoundryLocal
Enter fullscreen mode Exit fullscreen mode

สำหรับ MacOS ติดตั้งผ่าน Terminal โดยใช้คำสั่ง

brew tap microsoft/foundrylocal
brew install foundrylocal
Enter fullscreen mode Exit fullscreen mode

##การใช้งาน
เมื่อติดตั้งสำเร็จสามารถใช้งานผ่านคำสั่ง

foundry --help
Enter fullscreen mode Exit fullscreen mode

จะเป็นการแสดงว่า foundry มีคำสั่งอะไรบ้าง โดยจะถูกแบ่งเป็น 3 กลุ่ม

  1. Model สำหรับจัดการ และรัน AI Model
  2. Service สำหรับควบคุมการทำงาน Foundry Local Service
  3. Cache สำหรับจัดการพื้นที่จัดเก็บของ Model

ทำการแสดงรายชื่อ model ที่ Foundry Local รองรับผ่านคำสั่ง

foundry model list
Enter fullscreen mode Exit fullscreen mode

จะแสดงรายการ model ที่ Foundry รองรับทั้งหมด

llm model list

ใช้คำสั่ง

foundry model run qwen2.5-coder-1.5b
Enter fullscreen mode Exit fullscreen mode

จะเป็นการ run model เพื่อใช้งานในกรณีที่ไม่มี model อยู่ในเครื่องจะเป็นการ load model จาก internet ลงมาไว้ที่เครื่องเมื่อ load เสร็จจะ run model และขึ้น prompt สำหรับ run คำสั่งทดสอบให้แสดงตัวอย่าง code แสดง "hello world" ด้วยภาษา rust

run foundry model

เราสามารถตรวจสอบว่าบนเครื่องของเรามี model อะไรในเครื่องผ่านคำสั่ง

foundry cache ls
Enter fullscreen mode Exit fullscreen mode

จะแสดงรายการ model ที่เรา load จาก internet มาลงที่เครื่องเรา

list model in local

เราสามารถ run ได้มากกว่า 1 model ในเครื่องเราได้ผ่านคำสั่ง foundry run โดยสามารถตรวจสอบว่าในเรื่องเรามี model อะไรที่ทำงานอยู่ผ่านคำสั่ง

foundry service list
Enter fullscreen mode Exit fullscreen mode

จะแสดงรายการ model ที่ foundry run อยู่

list model runnig

หมายเหตุ
คุณสามารถระบุอาร์กิวเมนต์ของโมเดลโดยใช้ชื่อแทน(alias) หรือ Model ID ได้
การใช้ชื่อแทนจะช่วย:
เลือกโมเดลที่ดีที่สุดสำหรับฮาร์ดแวร์ที่มีอยู่ของคุณ ตัวอย่างเช่น หากคุณมี GPU CUDA ของ >Nvidia Foundry Local จะเลือกโมเดล CUDA หากคุณมี NPU ที่รองรับ Foundry Local >จะเลือกโมเดล NPU อนุญาตให้คุณใช้ชื่อที่สั้นกว่าโดยไม่ต้องจำ ID โมเดล
หากคุณต้องการเรียกใช้โมเดลเฉพาะ คุณสามารถใช้ ID โมเดลได้ ตัวอย่างเช่น
หากต้องการเรียกใช้ qwen2.5-0.5b บน CPU โดยไม่คำนึงถึงฮาร์ดแวร์ที่มีอยู่ของคุณ
ให้ใช้: foundry model run qwen2.5-0.5b-instruct-generic-cpu

การเชื่อมต่อ Application ผ่าน SDK

เราสามารถใช้งาน AI Model ผ่านคำสั่งแล้วเรายังสามารถ ใช้งานผ่าน SDK ซึ่งรองรับภาษา JavaScript, Python, Rust และ C# โดยตัวอย่างนี้เราจะทำสอบการใช้งาน โดยใช้ภาษา C#

หมายเหตุ เนื่องจาก SDK ยังอยู่ในระหว่างการพัฒนา จึงยังไม่มี nuget package ให้ใช้งานจึงต้องมีการ build sdk จากนั้นใส่ไปยัง project ที่ใช้พัฒนา แทนที่จะ add nuget package ผ่านคำสั่งได้โดยตรง

1.ทำการ clone repo ของ Foundry Local จาก github

git clone https://github.com/microsoft/Foundry-Local.git
Enter fullscreen mode Exit fullscreen mode

2.ไปที่ โฟลเดอร์ sdk/cs

cd Foundry-Local/sdk/cs
Enter fullscreen mode Exit fullscreen mode

3.build sdk ด้วยคำสั่ง

dotnet build
Enter fullscreen mode Exit fullscreen mode

4.สรา้ง project console application ผ่านคำสั่ง

dotnet new console -o Foundry-ConsoleApp
Enter fullscreen mode Exit fullscreen mode

5.เข้าไปใน project Foundry-ConsoleApp ที่สรา้งขึ้นจากนั้นสรา้งไฟล์ nuget.config ซึ่งกำหนด path ชีไปที่ Project Foundry-Local sdk ที่เรา build เอาไว้

<configuration>
  <packageSources>
    <add key="foundry-local" value="C:\path\to\foundry-local\sdk\cs\bin\Debug" />
  </packageSources>
</configuration>
Enter fullscreen mode Exit fullscreen mode

6.เพิ่ม nuget package Foundry-Local เข้าไปใน project ของเราผ่านคำสั่ง

dotnet add package FoundryLocal --source foundry-local
Enter fullscreen mode Exit fullscreen mode

7.เพิ่ม nuget package OpenAI สำหรับใช้ OpenAI Client ในการเชื้่อมต่อกับ local model

dotnet add package OpenAI
Enter fullscreen mode Exit fullscreen mode

8.ที่ไฟล์ Program.cs ทำการใส่ code ตัวอย่างลงไป

using System.ClientModel;
using Microsoft.AI.Foundry.Local;

using OpenAI;
using OpenAI.Chat;

public class TestApp
{
    public static async Task Main(string[] args)
    {
        var app = new TestApp(); // Create an instance of TestApp  

        Console.WriteLine(new string('=', 80)); // Separator for clarity
        Console.WriteLine("Show catalog integration...");//ทดสอบแสดงรายการmodel ใน catalog
        await app.ShowCatalog();

        Console.WriteLine(new string('=', 80)); // Separator for clarity
        Console.WriteLine("Testing cache operations...");
        await app.TestCacheOperations(); // Call the instance method

        Console.WriteLine(new string('=', 80)); // Separator for clarity
        Console.WriteLine("Connect Model using  OpenAI integration (from stopped service)...");
        using var manager = new FoundryLocalManager();
        if (manager != null)
        {
            //ตรวจสอบว่าถ้ามี model ทำงานอยู่ให่ทำการหยุด
            await manager.StopServiceAsync();
        }
        //ทดสอบเรียนใช้ model และป้อนคำถาม
        await app.ConnectModel("qwen2.5-coder-1.5b");

        Console.WriteLine(new string('=', 80)); // Separator for clarity
        Console.WriteLine("Testing service operations");
        await app.ShowService(); // Call the instance method
        //ทดสอบ load/unload model
        Console.WriteLine(new string('=', 80)); // Separator for clarity
        Console.WriteLine("Testing model (un)loading");
        await app.LoadUnloadModel("qwen2.5-coder-1.5b"); // Call the instance method
        //ทดสอบ download ai model from internet
        Console.WriteLine(new string('=', 80)); // Separator for clarity
        Console.WriteLine("Testing downloading");
        await app.DownloadModel("qwen2.5-coder-1.5b"); // Call the instance method
        Console.WriteLine("Press any key to exit...");
        Console.ReadKey(true);
    }

    //fuction ในการแสดงรายชื่ model ใน catalog
    private async Task ShowCatalog()
    // 
    {
        using var manager = new FoundryLocalManager();
        foreach (var m in await manager.ListCatalogModelsAsync())
        {
            Console.WriteLine($"Model: {m.Alias} ({m.ModelId})");
        }
    }

    //ตรวจสอบตำแหน่งการจัดเก็บ model ที่อยู่บนเครืี่องและตรวจสอบว่ามี model ไหนอยู่บนเครื่องบ้าง
    private async Task TestCacheOperations()
    {
        using var manager = new FoundryLocalManager();
        // แสดงตำแหน่งเก็บไฟล์ model
        Console.WriteLine($"Model cache location at {await manager.GetCacheLocationAsync()}");
        // แสดงรายการ model ที่อยู่ใน local
        var models = await manager.ListCachedModelsAsync();
        Console.WriteLine($"Found {models.Count} models in the cache:");
        foreach (var m in models)
        {
            Console.WriteLine($"Model: {m.Alias} ({m.ModelId})");
        }
    }

    //เชื่อมต่อกับ model
    private async Task ConnectModel(string aliasOrModelId)
    {

        var manager = await FoundryLocalManager.StartModelAsync(aliasOrModelId);
        //เลือก model จาก model ที่กำหนด 
        var model = await manager.GetModelInfoAsync(aliasOrModelId);
        var key = new ApiKeyCredential(manager.ApiKey); //ดึงค่า apikey จาก model
        //สรา้ง OpenAI client โดยเลือก Endpoint จาก model ที่เลือกไว้
        var client = new OpenAIClient(key, new OpenAIClientOptions
        {
            Endpoint = manager.Endpoint
        });
        //OpenAI Client เชื่อมต่อกับ Model
        var chatClient = client.GetChatClient(model?.ModelId);
        //เก็บคำตอบเมื่อถาม model ว่า "Why is the sky blue"
        CollectionResult<StreamingChatCompletionUpdate> completionUpdates = chatClient.CompleteChatStreaming("Why is the sky blue'");

        Console.Write($"[ASSISTANT]: ");
        foreach (StreamingChatCompletionUpdate completionUpdate in completionUpdates)
        {
            if (completionUpdate.ContentUpdate.Count > 0)
            {
                Console.Write(completionUpdate.ContentUpdate[0].Text);
            }
        }
    }
    //load or unload ai model
    private async Task LoadUnloadModel(string aliasOrModelId)
    {
        using var manager = new FoundryLocalManager();
        // Load a model
        var model = await manager.LoadModelAsync(aliasOrModelId);
        Console.WriteLine($"Loaded model: {model.Alias} ({model.ModelId})");
        // Unload the model
        await manager.UnloadModelAsync(aliasOrModelId);
        Console.WriteLine($"Unloaded model: {model.Alias} ({model.ModelId})");
    }

    //download ai model form internet
    private async Task DownloadModel(string aliasOrModelId)
    {
        using var manager = new FoundryLocalManager();

        // Download a model
        var model = await manager.DownloadModelAsync(aliasOrModelId, force: true);

        // test that the model can be loaded
        Console.WriteLine($"Downloaded model: {model!.Alias} ({model.ModelId})");
    }
    //แสดงรายละเอียด service ที่ทำงานอยู่
    private async Task ShowService()
    {
        using var manager = new FoundryLocalManager();
        await manager.StartServiceAsync();
        // Print out whether the service is running
        Console.WriteLine($"Service running (should be true): {manager.IsServiceRunning}");
        // Print out the service endpoint and API key
        Console.WriteLine($"Service Uri: {manager.ServiceUri}");
        Console.WriteLine($"Endpoint {manager.Endpoint}");
        Console.WriteLine($"ApiKey: {manager.ApiKey}");
        // stop the service
        await manager.StopServiceAsync();
        Console.WriteLine($"Service stopped");
        Console.WriteLine($"Service running (should be false): {manager.IsServiceRunning}");
    }

}
Enter fullscreen mode Exit fullscreen mode

เมื่อทดสอบ run โปรแกรมจะเป็นการแสดงรายชื่อ modelที่รองรับ, ตำแหน่งที่เก็บ model ใน local, ส่งคำถามและแสดงคำตอบจาก model , load และ unload model

หมายเหตุ code ตัวอย่างจะอยู่ที่ github

ส่งท้าย

ขณะนี้ Microsoft Foundry Local ยังเป็นรุ่น public preview ซึ่งทั้ง sdk กับ cli อาจมีการเปลี่ยนแปลง แต่ก็เป็นเครื่องมือสำหรับนำ AI Model มาใช้งานในเครื่องซึ่งเหมาะสำหรับการทดลองส่วนตัว หรือใช้ภายในองค์กร

Top comments (1)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.