Create Multi-Turn Chatbot with Tools in .NET Applications
🎯 Purpose of the Sample
Multi-Turn Chat with Tools demonstrates LM-Kit.NET’s agentic tool-calling: during a conversation, the model can decide to call one or multiple tools (functions) to fetch data or run computations, pass JSON arguments that match each tool’s InputSchema, and use the tool’s JSON result to produce a grounded reply, while preserving full multi-turn context. Tools implement ITool and are managed by a registry; per-turn behavior can be shaped via ToolChoice.
Why tools in chatbots?
- Reliable, source-backed answers (weather, FX rates, conversions, business APIs).
- Agentic chaining: the assistant can call several tools in one turn and combine results.
- Determinism & safety: typed schemas, clear failure modes, and policy control via
ToolChoice. - Extensibility: add domain tools by implementing
ITool; keep logic auditable and testable. - Efficiency: offload math/lookup to tools; keep the model focused on reasoning and language.
👥 Target Audience
- Product & Platform Teams - add reliable lookups & actions to assistants
- DevOps & Internal Tools - quick weather, units, currency in chat
- B2B Apps - action-oriented assistants with auditable outputs
- Educators & Demos - minimal, clear example of the tool contract
🚀 Problem Solved
- Actionable answers: Calls a tool when facts matter.
- Determinism: Conversions/quotes come from defined sources & formulas.
- Multi-turn memory: Context survives across tool calls.
- Easy extensibility: Implement a small
ITooland register it.
💻 Sample Application Description
Console app that:
- Lets you choose a local model (or a custom URI).
- Registers three tools (currency, weather, unit conversion).
- Runs a multi-turn chat where the model decides when to call tools.
- Prints generation stats (tokens, stop reason, speed, context usage).
✨ Key Features
- 🔧 Tool Calling: Assistant invokes tools via JSON arguments.
- 🧠 Context Retention: Full dialogue memory.
- 📥 Progress Feedback: Download + load progress bars.
- 📝 Special Commands:
/reset,/continue,/regenerate. - 🧩 Multiple Tool Calls: The assistant can call several tools in one turn (and across turns) and chain results when needed.
🧰 Built-In Tools
| Tool name | Purpose | Online? | Notes |
|---|---|---|---|
convert_currency |
ECB rates via Frankfurter (latest or historical) + optional short trend | Yes | No API key; business days; rounding & date support |
get_weather |
Open-Meteo current weather + optional short hourly forecast | Yes | No API key; geocoding + metric/us/si |
convert_units |
Offline conversions (length, mass, temperature, speed, volume, area, pressure, energy) | No | Temperature is non-linear; can list supported units |
Tools implement
ITool:Name,Description,InputSchema(JSON Schema), andInvokeAsync(string json)returning JSON.
Extend with your own tool
chat.Tools.Register(new MyCustomTool()); // implements ITool
Unique, stable, lowercase snake_case names are recommended.
🧠 Supported Models
Pick per hardware (defaults provided):
- Mistral Nemo 2407 12.2B (~7.7 GB VRAM)
- Meta Llama 3.1 8B (~6 GB VRAM)
- Google Gemma 3 4B Medium (~4 GB VRAM)
- Microsoft Phi-4 Mini 3.82B (~3.3 GB VRAM)
- Alibaba Qwen-3 8B (~5.6 GB VRAM)
- Microsoft Phi-4 14.7B (~11 GB VRAM)
- IBM Granite 4 7B (~6 GB VRAM)
- Open-AI GPT OSS 20B (~16 GB VRAM)
Or provide a custom model URI (GGUF/LMK).
🛠️ Commands
- /reset - clear conversation
- /continue - continue last assistant message
- /regenerate - new answer for last user input
🗣️ Example Prompts
- “Convert 125 USD to EUR and show a 7-day trend.”
- “Weather in Toulouse next 6 hours (metric).”
- “Convert 65 mph to km/h.” / “List pressure units.”
- “Now 75 °F to °C, then 2 km to miles.”
⚙️ Behavior & Policies (quick reference)
- Tool selection policy: By default the sample lets the model decide. See
ToolChoiceto Auto / Require / Forbid / Force a specific tool per turn. - Multiple tool calls: The runtime supports several tool invocations per turn when the model deems it necessary; outputs are injected back into context.
- Schemas matter: Precise
InputSchema+ conciseDescriptionimprove argument construction. - Networking: Currency & weather require internet; unit conversion is offline.
- Errors: Clear exceptions for invalid inputs (units, dates, locations).
🛠️ Getting Started
📋 Prerequisites
- .NET Framework 4.6.2 or .NET 6.0
📥 Download
git clone https://github.com/LM-Kit/lm-kit-net-samples.git
cd lm-kit-net-samples/console_net/multi_turn_chat_with_tools
Project Link: multi_turn_chat_with_tools (same path as above)
▶️ Run
dotnet build
dotnet run
Then:
- Pick a model or paste a custom URI.
- Chat naturally; the assistant will call one or multiple tools as needed.
- Use
/reset,/continue,/regenerateanytime.