AI

Chat completions, conversations, tools, and agent loops — with zero extra dependencies. JWeb speaks the OpenAI wire format directly, so it works with OpenAI, Ollama (free local models), Groq, LM Studio, or any OpenAI-compatible endpoint.

Configuration

# application.yaml (disabled by default)
jweb:
  ai:
    enabled: true
    base-url: ${AI_BASE_URL:https://api.openai.com/v1}
    api-key: ${AI_API_KEY:}      # blank for local providers
    model: ${AI_MODEL:gpt-4o-mini}
Local development without a key: install Ollama, then set base-url: http://localhost:11434/v1 and model: llama3.2

One-liners & Conversations

String answer = AI.ask("Summarize this: " + text);

Chat chat = AI.chat().system("You are a concise support agent");
String a = chat.send("What is JWeb?");
String b = chat.send("Show me an example");   // history kept

Agents with Tools

Register Java functions as tools; the model loops — reason, call tools, see results, repeat — until it produces a final answer. Tool errors are fed back as text so the agent can recover; maxSteps bounds the loop.

Tool weather = Tool.of("get_weather", "Get the weather for a city")
    .param("city", "The city name")
    .handler(args -> weatherService.lookup((String) args.get("city")));

String result = AI.agent()
    .system("You are a travel assistant")
    .tools(weather, searchDocs)
    .maxSteps(8)
    .onStep((step, info) -> Log.info("step {}: {}", step, info))
    .run("Should I pack an umbrella for Paris this weekend?");

Drop-in Chat Widget

A styled chat component plus a ready endpoint (POST /jweb/ai/chat, active when jweb.ai.enabled=true) with per-session history and a 30-minute TTL.

body(
    ...,
    AiChatWidget.render("Ask JWeb")
)
Everything is testable without an API key — AiModuleTest scripts an OpenAI-compatible mock using the JDK's built-in HttpServer.