{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "llm-0",
      "metadata": {},
      "source": "# Native language model connections\n\nUse the compatible native engine and extension 0.1.34 or newer. A running local Ollama service must already host `llama3.2:3b`; replace that identifier with your installed model. Request cells send their prompts to this local service. Repeating a cell sends another request. No credentials or saved output are included.\n\nOpaque client, message, response, and credential handles hide content in inspection. Plain prompt Strings and explicitly read answer text remain ordinary program data. Review output before sharing the notebook. Read [Language model connections](https://anerlang.org/docs/llm) and [Credentials](https://anerlang.org/docs/credentials).\n"
    },
    {
      "cell_type": "code",
      "id": "llm-1",
      "metadata": {},
      "source": [
        "import aner.llm\n",
        "\n",
        "let model = llm.client(provider: \"ollama\", model: \"llama3.2:3b\")\n",
        "print(model.provider())\n",
        "print(model.model())\n"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "id": "llm-2",
      "metadata": {},
      "source": "## A concise local request\n\nClient construction above made no request. The next cell requests one completion with the default 512 output token budget.\n"
    },
    {
      "cell_type": "code",
      "id": "llm-3",
      "metadata": {},
      "source": [
        "let answer = model.generate(\"Explain a linked list in three sentences.\")\n",
        "print(answer.text())\n",
        "print(answer.finish_reason())\n"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "id": "llm-4",
      "metadata": {},
      "source": "## Explicit message order and controls\n\nThese messages define the full conversation for this request. No prior request is inserted automatically. Token counts of `-1` mean the service omitted usage.\n"
    },
    {
      "cell_type": "code",
      "id": "llm-5",
      "metadata": {},
      "source": [
        "let messages = List(\n",
        "    llm.message(\"system\", \"Explain concepts precisely and concisely.\"),\n",
        "    llm.message(\"user\", \"Compare a linked list and a fixed array.\"))\n",
        "let detailed = model.chat(messages, max_tokens: 256, temperature: 0.2)\n",
        "print(detailed.text())\n",
        "print(detailed.model())\n",
        "print(detailed.finish_reason())\n",
        "print(detailed.input_tokens())\n",
        "print(detailed.output_tokens())\n"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "id": "llm-6",
      "metadata": {},
      "source": "## Cloud configuration remains explicit\n\nTo use a cloud provider, configure a key in Terminal using `aner secrets set openai` on a supported system store. Source then creates `llm.client(provider: \"openai\", model: \"gpt-4.1-mini\", key: secrets.stored(\"openai\"))`. On headless Linux choose `--store file` and `secrets.file(\"openai\")`, or use `secrets.env(\"OPENAI_API_KEY\")` with an injected environment. Running cloud request cells can incur provider charges. The separate cloud example shows the complete request.\n"
    }
  ],
  "metadata": {
    "aner": {
      "execution_mode": "persistent-session",
      "runtime_mode": "production"
    },
    "kernelspec": {
      "display_name": "Aner",
      "language": "aner",
      "name": "aner"
    },
    "language_info": {
      "name": "aner",
      "file_extension": ".aner"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
