Chat with a local LLM
A terminal chat with a model running on your own machine through Ollama. It keeps the conversation history and prints the answer word by word as the model writes it. The whole program is about 50 lines of する, and it runs the same in the interpreter and as a native binary.
01Setup
Install Ollama and download a model. Then make a kumu project and add the HTTP library, suru-lang/http, which is not part of the standard library.
$ ollama pull llama3.2 $ go install github.com/suru-lang/suru/cmd/kumu@latest $ kumu 新規 チャット # new project “チャット” (chat) $ cd チャット $ kumu 追加 github.com/suru-lang/http # add the dependency (kumu add)
「チャット」を作りました(cd チャット してから kumu 実行) 依存に「github.com/suru-lang/http@0.2.0」を足しました 「github.com/suru-lang/http@0.2.0」を取ってきました
02The program
Replace 本体.suru (“main body”) with this:
# Chat with a local LLM (Ollama). Answers are printed as they arrive.
「標準」 を 読み込んで 使う
「JSON」 を 読み込んで 使う
「HTTP/クライアント」 を 読み込む
発言 とは 役 と 内容 の 組 です # 発言 = message: a record (組) of a 役 (role) and 内容 (content)
# The JSON we send: {"model":…, "messages":[{"role":…, "content":…}, …]}
発言 を 発言JSONにする は JSON を 返す # 発言JSONにする = make it into message JSON
m を 発言JSONにする とは ((「role」 と (m の 役 の JSON文字列) の JSON項目) と (「content」 と (m の 内容 の JSON文字列) の JSON項目)) の JSON物 です
発言 の リスト を 要求本文にする は 文字列 を 返す # 要求本文にする = make it into the request body
履歴 を 要求本文にする とは
((「model」 と (「llama3.2」 の JSON文字列) の JSON項目) と (「messages」 と ((履歴 の それぞれ を 発言JSONにする) の JSON配列) の JSON項目)) の JSON物 を JSON文字列にする
# Each line we get back is {"message":{"content":"…"}, …}: one piece of the answer
JSON を 文字値取得する は 文字列 を 返す # 文字値取得する = get the string value
(s の JSON文字列) を 文字値取得する とは s です
v を 文字値取得する とは 「」 です
(JSON と 文字列) の 成否 を 断片にする は 文字列 を 返す # 成否 = success or failure, 断片 = piece
(v の 成功) を 断片にする とは ((v から 「message」 を JSON取得する) から 「content」 を JSON取得する) を 文字値取得する
(e の 失敗) を 断片にする とは 「」 です
# For every line: print its piece without a newline, and add it to the answer so far
文字列 を 文字列 に 断片表示する は 文字列 を 返す 入出力動作 # 断片表示する = show a piece
答え を 行 に 断片表示する とは 以下の手順です
断片 とは 行 を JSON解析して 断片にする
断片 を 続表示する
答え + 断片 を 結果にする
以上です
文字列 の 有無 を 入力文にする は 文字列 を 返す # 有無 = maybe (有 = some, 無 = none), 入力文 = input line
(s の 有) を 入力文にする とは s です
無 を 入力文にする とは 「」 です
発言 の リスト を 会話する は 数 を 返す 入出力動作 # 会話する = talk
履歴 を 会話する とは 以下の手順です
質問 とは 「あなた> 」 を 入力確認した 結果 を 入力文にする
もし 質問 が 「」 ならば 改行 を 続表示する
そうでなければ 履歴 + ((役 は 「user」 で 内容 は 質問 の 発言) を 空 に 挿入する) を 回答する
以上です
発言 の リスト を 回答する は 数 を 返す 入出力動作 # 回答する = answer
履歴 を 回答する とは 以下の手順です
「AI> 」 を 続表示する
答え とは 「http://localhost:11434/api/chat」 に (履歴 を 要求本文にする) を 「」 から 断片表示すること で クライアント内の逐次投稿した 結果
「」 を 表示する
履歴 + ((役 は 「assistant」 で 内容 は 答え の 発言) を 空 に 挿入する) を 会話する
以上です
空 を 会話する # 空 = the empty list03Run it
kumu 実行 compiles the project to a native binary and starts it. Type a question after あなた> (“you”). An empty line or Ctrl+D ends the chat.
$ kumu 実行
「チャット」0.1.0 を組みました(出力/チャット) あなた> Say hello in three languages. AI> Hello! Bonjour! こんにちは! あなた> Which one is Japanese? AI> こんにちは is Japanese. あなた>
続表示する and 入力確認する.04How it works
A record for each message
発言 とは 役 と 内容 の 組 defines a record type 発言 (“utterance”) with two fields, 役 (“role”) and 内容 (“content”). 役 は 「user」 で 内容 は 質問 の 発言 builds one, and the history is simply a list of them.
Building JSON
The standard JSON library turns values into JSON with words like JSON文字列 (string), JSON項目 (key–value pair), JSON物 (object) and JSON配列 (array). それぞれ (“each”) maps 発言JSONにする over the whole history.
Definitions by cases
文字値取得する (“get the string value”) is defined twice: once for a JSON string, and once for anything else. The same style handles a line that fails to parse: JSON解析する returns a 成否 (success or failure), and 断片にする has one case for each. 入力確認する reads a line and returns a 有無 (“there is / there isn’t”), so the end of input needs no exception.
Streaming with a function value
URL に 本文 を 初期 から 処理 で 逐次投稿する POSTs the body and calls 処理 once for every line of the reply, as it arrives. Writing 断片表示すること (“the act of showing a piece”) passes our function itself as a value. It starts from the empty answer 「」, prints each piece with 続表示する (“keep printing”, no newline), and the final answer becomes the value of 答え.
Actions in a procedure
Printing, reading input and sending requests are 入出力動作 (input/output actions), and the type declarations say so with … を 返す 入出力動作. Between 以下の手順です and 以上です they run in order, and …した 結果 (“the result of having done …”) takes the value an action produced. 会話する and 回答する call each other, so the conversation is a loop without a loop statement.
05Going further
The longer version in the HTTP library takes the model and URL as arguments, also talks to OpenAI-compatible servers such as llama.cpp’s llama-server and LM Studio, and keeps going when a request fails.
Ohanashi grows this chat into a full web app: threads, rules, images and PDFs, a sandbox and GitHub, also written in する.