0 people like it.

Using local LLMs from F#

Download and install Ollama: https://ollama.com/ Ollama is a free common host for multiple different LLM-models like Google Gemma, OpenApi's GPT-OSS, Deepseek, etc.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
// #r "nuget:OllamaSharp"
open OllamaSharp
// For better Collections.Generic.IAsyncEnumerable<_> support for F#:
// #r "nuget:FSharp.Control.TaskSeq" 
open FSharp.Control

open System
open System.Threading.Tasks.Task

// See complete list of libraries: https://ollama.com/library
let model =
   "gemma3n:latest"       // Google's single device 5.2GB model
// "gpt-oss:latest"       // OpenAI 14GB model
// "deepseek-r1:latest"   // Deepseek 5.2GB
// "deepseek-v3.1:latest" // Deepseek 404GB

// Set up the client
let uri = Uri "http://localhost:11434"
use ollama = OllamaApiClient uri
ollama.SelectedModel <- model

// The model has to be installed locally first, and the download can be big.
ollama.PullModelAsync model
|> TaskSeq.iter(fun status -> printfn $"{status.Percent} %% {status.Status}")

// List of locally downloaded models (is empty before Ollama downloads the model)
let listModels =
    task {
        let! models = ollama.ListLocalModelsAsync() 
        models |> Seq.iter(fun x -> printf $"Model {x.Name} size {x.Size} bytes")
    }
listModels |> Async.AwaitTask |> Async.RunSynchronously

ollama.GenerateAsync "How are you today?"
|> TaskSeq.iter(fun stream -> printf $"{stream.Response}")

// Prints something like:
// "> I am doing well, thank you for asking! As a large language model, 
//    I don't experience feelings like humans do, but my systems are 
//    running smoothly and I'm ready to assist you."
namespace Microsoft.FSharp
namespace Microsoft.FSharp.Control
namespace System
namespace System.Threading
namespace System.Threading.Tasks
Multiple items
type Task =
  interface IAsyncResult
  interface IDisposable
  new : canceled: bool * creationOptions: TaskCreationOptions * ct: CancellationToken -> unit + 11 overloads
  member AddCompletionAction : action: ITaskCompletionAction -> unit + 1 overload
  member AddException : exceptionObject: obj -> unit + 1 overload
  member AddExceptionsFromChildren : props: ContingentProperties -> unit
  member AddNewChild : unit -> unit
  member AddTaskContinuation : tc: obj * addBeforeOthers: bool -> bool
  member AddTaskContinuationComplex : tc: obj * addBeforeOthers: bool -> bool
  member AssignCancellationToken : cancellationToken: CancellationToken * antecedent: Task * continuation: TaskContinuation -> unit
  ...

--------------------
type Task<'TResult> =
  inherit Task
  new : unit -> unit + 13 overloads
  member ConfigureAwait : continueOnCapturedContext: bool -> ConfiguredTaskAwaitable<'TResult>
  member ContinueWith : continuationAction: Action<Task<'TResult>> -> Task + 23 overloads
  member DangerousSetResult : result: 'TResult -> unit
  member GetAwaiter : unit -> TaskAwaiter<'TResult>
  member GetResultCore : waitCompletionNotification: bool -> 'TResult
  member InnerInvoke : unit -> unit
  member TrySetResult : result: 'TResult -> bool
  static member StartNew : parent: Task * function: Func<'TResult> * cancellationToken: CancellationToken * creationOptions: TaskCreationOptions * internalOptions: InternalTaskOptions * scheduler: TaskScheduler -> Task<'TResult> + 1 overload
  ...

--------------------
Threading.Tasks.Task(action: Action) : Threading.Tasks.Task
Threading.Tasks.Task(action: Action, cancellationToken: Threading.CancellationToken) : Threading.Tasks.Task
Threading.Tasks.Task(action: Action, creationOptions: Threading.Tasks.TaskCreationOptions) : Threading.Tasks.Task
Threading.Tasks.Task(action: Action<obj>, state: obj) : Threading.Tasks.Task
Threading.Tasks.Task(action: Action, cancellationToken: Threading.CancellationToken, creationOptions: Threading.Tasks.TaskCreationOptions) : Threading.Tasks.Task
Threading.Tasks.Task(action: Action<obj>, state: obj, cancellationToken: Threading.CancellationToken) : Threading.Tasks.Task
Threading.Tasks.Task(action: Action<obj>, state: obj, creationOptions: Threading.Tasks.TaskCreationOptions) : Threading.Tasks.Task
Threading.Tasks.Task(action: Action<obj>, state: obj, cancellationToken: Threading.CancellationToken, creationOptions: Threading.Tasks.TaskCreationOptions) : Threading.Tasks.Task

--------------------
Threading.Tasks.Task(function: Func<'TResult>) : Threading.Tasks.Task<'TResult>
Threading.Tasks.Task(function: Func<'TResult>, cancellationToken: Threading.CancellationToken) : Threading.Tasks.Task<'TResult>
Threading.Tasks.Task(function: Func<'TResult>, creationOptions: Threading.Tasks.TaskCreationOptions) : Threading.Tasks.Task<'TResult>
Threading.Tasks.Task(function: Func<obj,'TResult>, state: obj) : Threading.Tasks.Task<'TResult>
Threading.Tasks.Task(function: Func<'TResult>, cancellationToken: Threading.CancellationToken, creationOptions: Threading.Tasks.TaskCreationOptions) : Threading.Tasks.Task<'TResult>
Threading.Tasks.Task(function: Func<obj,'TResult>, state: obj, cancellationToken: Threading.CancellationToken) : Threading.Tasks.Task<'TResult>
Threading.Tasks.Task(function: Func<obj,'TResult>, state: obj, creationOptions: Threading.Tasks.TaskCreationOptions) : Threading.Tasks.Task<'TResult>
Threading.Tasks.Task(function: Func<obj,'TResult>, state: obj, cancellationToken: Threading.CancellationToken, creationOptions: Threading.Tasks.TaskCreationOptions) : Threading.Tasks.Task<'TResult>
val model : string
val uri : Uri
Multiple items
type Uri =
  interface ISerializable
  new : uriString: string -> unit + 7 overloads
  member AllowIdnStatic : syntax: UriParser * flags: Flags -> bool
  member Canonicalize : unit -> unit
  member CheckAuthorityHelper : pString: nativeptr<char> * idx: uint16 * length: uint16 * err: byref<ParsingError> * flags: byref<Flags> * syntax: UriParser * newHost: byref<string> -> uint16
  member CheckAuthorityHelperHandleAnyHostIri : pString: nativeptr<char> * startInput: int * end: int * iriParsing: bool * hasUnicode: bool * syntax: UriParser * flags: byref<Flags> * newHost: byref<string> * err: byref<ParsingError> -> unit
  member CheckAuthorityHelperHandleDnsIri : pString: nativeptr<char> * start: uint16 * end: int * startInput: int * iriParsing: bool * hasUnicode: bool * syntax: UriParser * userInfoString: string * flags: byref<Flags> * justNormalized: byref<bool> * newHost: byref<string> * err: byref<ParsingError> -> unit
  member CheckCanonical : str: nativeptr<char> * idx: byref<uint16> * end: uint16 * delim: char -> Check
  member CheckForEscapedUnreserved : data: string -> bool
  member CheckForUnicode : data: string -> bool
  ...

--------------------
Uri(uriString: string) : Uri
Uri(uriString: string, uriKind: UriKind) : Uri
Uri(baseUri: Uri, relativeUri: string) : Uri
Uri(baseUri: Uri, relativeUri: Uri) : Uri
val ollama : obj
val printfn : format:Printf.TextWriterFormat<'T> -> 'T
val listModels : Threading.Tasks.Task<obj>
module Seq

from Microsoft.FSharp.Collections
val iter : action:('T -> unit) -> source:seq<'T> -> unit
val printf : format:Printf.TextWriterFormat<'T> -> 'T
Multiple items
type Async =
  static member AsBeginEnd : computation:('Arg -> Async<'T>) -> ('Arg * AsyncCallback * obj -> IAsyncResult) * (IAsyncResult -> 'T) * (IAsyncResult -> unit)
  static member AwaitEvent : event:IEvent<'Del,'T> * ?cancelAction:(unit -> unit) -> Async<'T> (requires delegate and 'Del :> Delegate)
  static member AwaitIAsyncResult : iar:IAsyncResult * ?millisecondsTimeout:int -> Async<bool>
  static member AwaitTask : task:Task<'T> -> Async<'T> + 1 overload
  static member AwaitWaitHandle : waitHandle:WaitHandle * ?millisecondsTimeout:int -> Async<bool>
  static member CancelDefaultToken : unit -> unit
  static member Catch : computation:Async<'T> -> Async<Choice<'T,exn>>
  static member Choice : computations:seq<Async<'T option>> -> Async<'T option>
  static member FromBeginEnd : beginAction:(AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T> + 3 overloads
  static member FromContinuations : callback:(('T -> unit) * (exn -> unit) * (OperationCanceledException -> unit) -> unit) -> Async<'T>
  ...

--------------------
type Async<'T> =
static member Async.AwaitTask : task:Threading.Tasks.Task -> Async<unit>
static member Async.AwaitTask : task:Threading.Tasks.Task<'T> -> Async<'T>
static member Async.RunSynchronously : computation:Async<'T> * ?timeout:int * ?cancellationToken:Threading.CancellationToken -> 'T

More information

Link:http://fssnip.net/8aO
Posted:9 hours ago
Author:Tuomas Hietanen
Tags: llm