0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

C#で簡単なアプリを作ってみよう

要件
・画面に入力した内容に対してAIを用いて返事をしてくれる

環境
・VisualStudioCommunity
・Ollama

ollamaをインストール後
phi3をインストールする

まず[コンソールアプリ] (.NET)で新規のプロジェクトを作成し

以下のようなコードを記載

using System;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        //Ollamaはローカルで動くため、直接呼び出す方式を採用
        static readonly string url = "http://localhost:11434/api/generate";

        static async Task Main()
        {
            Console.WriteLine("チャット開始。exit で終了。");

            var http = new HttpClient();

            while (true)
            {
                Console.Write("\nあなた: ");
                var input = Console.ReadLine();
                if (input == "exit") break;

                var request = new
                {
                    model = "phi3",
                    prompt = input,
                    stream = false
                };

                var content = new StringContent(
                    JsonSerializer.Serialize(request),
                    Encoding.UTF8,
                    "application/json"
                );

                var response = await http.PostAsync(url, content);
                var json = await response.Content.ReadAsStringAsync();

                using (var doc = JsonDocument.Parse(json))
                {
                    var root = doc.RootElement;

                    // エラー
                    if (root.TryGetProperty("error", out var err))
                    {
                        Console.WriteLine("AI Error: " + err.GetString());
                        continue;
                    }

                    // 正常
                    if (root.TryGetProperty("response", out var res))
                    {
                        Console.WriteLine("AI: " + res.GetString());
                    }
                    else
                    {
                        Console.WriteLine("AI: (応答なし)");
                    }
                }
            }
        }
    }
}

Ollamaはローカルで動くAIサーバーであり
PC内でモデルを動かすのでAPIキー不要である
APIは http://localhost:11434 に公開されるため
.NETからはHTTP POSTで呼ぶだけで起動が可能である

ユーザー入力を受け取り
JSONでOllamaに送る
Ollamaがローカルでモデルを実行し
JSONで返ってくるので
C#がresponseを取り出して表示する
という基本的な流れになっている

この状態でVisualStudioを起動すると

チャット開始。exit で終了。

あなた: 

という文言がコンソール上で出力されるため

Hello
と入力するとローカルで起動している分少し時間はかかるが

チャット開始。exit で終了。

あなた: hello
AI: Hello! How can I assist you today?

あなた:

このようにローカルでモデルを実行して返事をしてくれる

これにより、ローカルで自分だけのAIと会話することができるようになる
簡単に連携ができ、ローカルで実行できるため
モデルをインストールさえすればあとは外部と切り離した状況で使用することができる

また、無料で使用できるため、AIとの会話をもとに進めるゲームなども
作ることができるかもしれない

ローカルでモデルを学習させることが可能なので
ある程度パーソナライズされたものなどはこれでいいのかも?

※プライベートで利用していたアカウントからの記事移動です

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?