{"slug": "a-prolog-library-for-interfacing-with-llms", "title": "A Prolog library for interfacing with LLMs", "summary": "A new SWI-Prolog library called pllm allows developers to interface with large language models (LLMs) via OpenAI-compatible chat/completions endpoints. The library provides a simple predicate llm/2 that posts prompts and returns model responses, supporting services like OpenAI and local Ollama instances. It also offers a reverse-prompt feature that generates a prompt from a desired response.", "body_md": "Use LLMs inside Prolog!\n\n`pllm`\n\nis a minimal SWI-Prolog helper that exposes `llm/2`\n\n.\nThe predicate posts a prompt to an HTTP LLM endpoint and unifies the model's\nresponse text with the second argument.\n\nThe library currently supports any OpenAI-compatible chat/completions endpoint.\n\n```\n?- pack_install(pllm).\n```\n\nSome services require an API key for authentication.\nSet the `LLM_API_KEY`\n\nenvironment variable to your API key.\nYou can do the following in your shell before starting SWI-Prolog:\n\n```\necho LLM_API_KEY=\"sk-...\" >> .env\nset -a && source .env && set +a\n```\n\nConfigure the endpoint and default model before calling `llm/2`\n\nor `llm/3`\n\n:\n\n```\n?- config(\"https://api.openai.com/v1/chat/completions\", \"gpt-4o-mini\").\n```\n\nYou can override the configured model per call with `llm/3`\n\noptions.\n\n```\n# Fill in .env with your settings\nset -a && souce .env && set +a\nswipl\n?- [prolog/llm].\n?- llm(\"Say hello in French.\", Output).\nOutput = \"Bonjour !\".\n\n?- llm(\"Say hello in French.\", Output, [model(\"gpt-4o-mini\"), timeout(30)]).\nOutput = \"Bonjour !\".\n\n?- llm(Prompt, \"Dog\").\nPrompt = \"What animal is man's best friend?\",\n...\n```\n\nThis library expects an OpenAI-compatible chat/completions endpoint. Below are common providers and endpoints you can try.\n\nOpenAI\n\n- Endpoint:\n`https://api.openai.com/v1/chat/completions`\n\n- Example:\n`?- config(\"https://api.openai.com/v1/chat/completions\", \"gpt-4o-mini\").`\n\nOllama (local)\n\n- Endpoint:\n`http://localhost:11434/v1/chat/completions`\n\n- Example:\n`?- config(\"http://localhost:11434/v1/chat/completions\", \"llama3.1\").`\n\nIf you call `llm/2`\n\nwith an unbound first argument and a concrete response,\nthe library first asks the LLM to suggest a prompt that would (ideally)\nproduce that response, binds it to your variable, and then sends a *second*\nrequest that wraps the suggested prompt in a hard constraint (`\"answer only with ...\"`\n\n).\nThis costs two API calls and is still best-effort; the model may ignore the constraint, in which case the predicate simply fails.", "url": "https://wpnews.pro/news/a-prolog-library-for-interfacing-with-llms", "canonical_source": "https://github.com/vagos/llmpl", "published_at": "2026-07-09 13:52:12+00:00", "updated_at": "2026-07-09 14:06:42.136834+00:00", "lang": "en", "topics": ["large-language-models", "developer-tools"], "entities": ["pllm", "SWI-Prolog", "OpenAI", "Ollama", "GPT-4o-mini", "Llama 3.1"], "alternates": {"html": "https://wpnews.pro/news/a-prolog-library-for-interfacing-with-llms", "markdown": "https://wpnews.pro/news/a-prolog-library-for-interfacing-with-llms.md", "text": "https://wpnews.pro/news/a-prolog-library-for-interfacing-with-llms.txt", "jsonld": "https://wpnews.pro/news/a-prolog-library-for-interfacing-with-llms.jsonld"}}