Ollama Client
This package provides a client for Ollama.
This package should be considered experimental. All bindings documented here are subject to change.
1 Reference
(require ollama) | package: ollama-lib |
1.1 Client
procedure
(ollama-client? v) → boolean?
v : any/c
procedure
(make-ollama-client [base #:auth auth]) → ollama-client?
base : string? = "http://127.0.0.1:11434"
auth : auth-procedure/c =
(λ (url headers params) (values headers params))
procedure
(ollama-start-chat c model str-or-messages [ #:options options #:format output-format #:tools tools #:response->history-entry response-converter])
→
chat-response/c chat-continuation/c c : ollama-client? model : string? str-or-messages : (or/c string? message? (listof (or/c string? message?))) options : jsexpr? = (hasheq) output-format : (or/c #f string?) = #f tools : (or/c #f (hash/c symbol? tool-info?)) = #f
response-converter : (-> jsexpr? (or/c #f message?)) =
(λ (data) (make-message #:role 'assistant (hash-ref (hash-ref data 'message) 'content)))
A procedure to return the next part of the model’s response. Returns eof when the end of response has been reached.
A procedure to continue the conversation by sending the LLM subsequent messages. Returns a new pair of chat response and chat continuation procedures. Continuing a chat preserves the chat history and passes it along to the LLM on every call.
The #:options argument can be used to pass arbitrary chat options to Ollama.
The #:format argument can be used to control the LLM’s output format. When output-format is 'json, the LLM is instructed to format its response as JSON (but you should also encourage it to do so in your message).
The #:tools argument can be used to supply the LLM with tools that it can call to perform actions.
The #:response->history-entry procedure can be used to alter LLM responses before they’re committed to history. For example, you can use this hook to filter out reasoning from an LLM response. When the result of applying this procedure is #f, no entry is stored for that response.
contract
chat-response/c : (-> (or/c jsexpr? eof-object?))
contract
:
(->* [(or/c string? message? (listof (or/c string? message?)))] [#:format (or/c #f 'json jsexpr?) #:tools (or/c #f (hash/c symbol? tool-info?))] (values chat-response/c (recursive-contract chat-continuation/c)))
1.2 Message
procedure
(make-message content [ #:role role #:images images]) → message? content : string? role : (or/c 'assistant 'system 'user 'tool) = 'user images : (listof bytes?) = null
1.3 Tool
Tools are Racket functions that an LLM can call.
procedure
(tool-info? v) → boolean?
v : any/c
syntax
(define-tool-definer definer-id #:getter getter-id #:caller caller-id)
Binds getter-id to a procedure that returns a hash of all the tools that have been defined so far via definer-id.
Binds caller-id to a procedure that, given LLM tool call data, calls one of the defined procedures and returns response that can be sent to the LLM.
The syntax of define-id is
syntax
syntax
(define-id (tool-name [arg-id : type-expr] ...) maybe-description maybe-example e ...+)
maybe-description =
| #:description description-expr maybe-example =
| #:example example-expr maybe-example
procedure
(Array elem) → type
elem : type
value
Boolean : type
procedure
(Enum option ...+) → type
option : string?
value
Null : type
value
Number : type
syntax
procedure
(Optional t) → type
t : type
procedure
(Or t ...+) → type
t : type
value
String : type
> (require ollama)
> (define-tool-definer define-tool #:getter get-tools #:caller call-tool)
> (define-tool (get-temperature [location : String]) "451 Fahrenheit") > (get-tools) '#hasheq((get-temperature . #<tool-info>))
> (call-tool (hasheq 'function (hasheq 'name "get-temperature" 'arguments (hasheq 'location "Los Angeles")))) "{\"function\":{\"arguments\":{\"location\":\"Los Angeles\"},\"name\":\"get-temperature\"},\"result\":\"451 Fahrenheit\"}"
The caller-id procedure raises an exception when a requested tool cannot be found or when it is called incorrectly.
procedure
(exn:fail:tool? v) → boolean?
v : any/c
procedure
v : any/c
procedure
(exn:fail:tool:call? v) → boolean?
v : any/c
A tool error can be converted to JSON by applying ->jsexpr to it. The conversion adds an 'error key to the original call data with the exception message.
procedure
(raise-tool-error format-string format-arg ... [ #:hints hints]) → void? format-string : string? format-arg : any/c hints : (listof string?) = null
1.4 JSON
interface
procedure
(to-jsexpr? v) → boolean?
v : any/c
procedure
v : to-jsexpr?