A Prolog library for interfacing with LLMs

Lobsters Hottest Tools

Summary

A minimal SWI-Prolog library (pllm) that exposes an llm/2 predicate to send prompts to OpenAI-compatible chat/completions endpoints and unify responses, supporting configuration for different providers like OpenAI and Ollama.

<p><a href="https://lobste.rs/s/ad7cm6/prolog_library_for_interfacing_with_llms">Comments</a></p>
Original Article
View Cached Full Text

Cached at: 07/09/26, 03:40 PM

vagos/llmpl

Source: https://github.com/vagos/llmpl

llmpl

Use LLMs inside Prolog!

pllm is a minimal SWI-Prolog helper that exposes llm/2. The predicate posts a prompt to an HTTP LLM endpoint and unifies the model’s response text with the second argument.

The library currently supports any OpenAI-compatible chat/completions endpoint.

Installation

?- pack_install(pllm).

Configuration

Some services require an API key for authentication. Set the LLM_API_KEY environment variable to your API key. You can do the following in your shell before starting SWI-Prolog:

echo LLM_API_KEY="sk-..." >> .env
set -a && source .env && set +a

Configure the endpoint and default model before calling llm/2 or llm/3:

?- config("https://api.openai.com/v1/chat/completions", "gpt-4o-mini").

You can override the configured model per call with llm/3 options.

Usage

# Fill in .env with your settings
set -a && souce .env && set +a
swipl
?- [prolog/llm].
?- llm("Say hello in French.", Output).
Output = "Bonjour !".

?- llm("Say hello in French.", Output, [model("gpt-4o-mini"), timeout(30)]).
Output = "Bonjour !".

?- llm(Prompt, "Dog").
Prompt = "What animal is man's best friend?",
...

Providers

This library expects an OpenAI-compatible chat/completions endpoint. Below are common providers and endpoints you can try.

OpenAI

  • Endpoint: https://api.openai.com/v1/chat/completions
  • Example: ?- config("https://api.openai.com/v1/chat/completions", "gpt-4o-mini").

Ollama (local)

  • Endpoint: http://localhost:11434/v1/chat/completions
  • Example: ?- config("http://localhost:11434/v1/chat/completions", "llama3.1").

Reverse prompts

If you call llm/2 with an unbound first argument and a concrete response, the library first asks the LLM to suggest a prompt that would (ideally) produce that response, binds it to your variable, and then sends a second request that wraps the suggested prompt in a hard constraint ("answer only with ..."). This costs two API calls and is still best-effort; the model may ignore the constraint, in which case the predicate simply fails.

Similar Articles

PrologMCP: A Standardized Prolog Tool Interface for LLM Agents

arXiv cs.AI

Introduces PrologMCP, an open-source server that exposes Prolog as a stateful tool via the Model Context Protocol, enabling LLM agents to delegate reasoning to a symbolic solver. Evaluation shows competitive or superior accuracy on deductive reasoning tasks compared to frontier reasoning LLMs.

llm 0.31.1

Simon Willison's Blog

Release of llm 0.31.1, a command-line tool for interacting with large language models, by Simon Willison.

llm-chat-completions-server 0.1a0

Simon Willison's Blog

Simon Willison releases llm-chat-completions-server 0.1a0, a plugin that runs a local OpenAI Chat Completions-compatible server backed by any installed LLM models, using content-addressable logs for efficient conversation state.

llm 0.32a2

Simon Willison's Blog

The llm CLI tool version 0.32a2 has been released, adding support for OpenAI's /v1/responses endpoint to enable interleaved reasoning for GPT-5 class models.

llm 0.32a3

Simon Willison's Blog

Release of llm 0.32a3, an alpha version of Simon Willison's command-line tool for interacting with large language models.