Package dyntabs.ai

Class ConversationBuilder

java.lang.Object
dyntabs.ai.ConversationBuilder

public class ConversationBuilder extends Object
Builder for creating Conversation instances.

Every setting is optional. At minimum, you just need a configured easyai.properties file on the classpath, then:


 Conversation chat = EasyAI.chat().build();
 

Available Options

MethodWhat it doesDefault
withMemory(int)How many messages to remember0 (no memory)
withSystemMessage(String)Set the AI's personality/rolenone
withModel(String)Override the model namefrom properties
withApiKey(String)Override the API keyfrom properties
withProvider(String)"openai" or "ollama"from properties
withBaseUrl(String)Custom API endpointprovider default
withTemperature(double)0.0=precise, 1.0=creativeprovider default
withMaxTokens(int)Max response lengthprovider default

Example: Using Local Ollama Instead of OpenAI


 Conversation chat = EasyAI.chat()
     .withProvider("ollama")
     .withModel("llama3")
     .withMemory(10)
     .build();
 
See Also:
  • Method Details

    • withMemory

      public ConversationBuilder withMemory(int maxMessages)
      Enables conversation memory. The AI will remember the last maxMessages messages (both user and AI messages count).

      Example: withMemory(20) means the AI sees the last 20 messages as context when generating a response.

      Parameters:
      maxMessages - number of messages to keep in memory (e.g. 10, 20, 50)
      Returns:
      this builder
    • withSystemMessage

      public ConversationBuilder withSystemMessage(String systemMessage)
      Sets a system message that defines the AI's behavior and personality.

      Examples:

      • "You are a helpful Java tutor"
      • "You are a customer support agent for an online store"
      • "Always respond in JSON format"
      Parameters:
      systemMessage - the system instruction for the AI
      Returns:
      this builder
    • withModel

      public ConversationBuilder withModel(String modelName)
      Overrides the model name from configuration.

      Examples: "gpt-4o", "gpt-4o-mini", "llama3"

      Parameters:
      modelName - the model name
      Returns:
      this builder
    • withApiKey

      public ConversationBuilder withApiKey(String apiKey)
      Overrides the API key from configuration.
      Parameters:
      apiKey - your API key
      Returns:
      this builder
    • withProvider

      public ConversationBuilder withProvider(String provider)
      Overrides the AI provider. Supported values: "openai", "ollama".
      Parameters:
      provider - the provider name
      Returns:
      this builder
    • withBaseUrl

      public ConversationBuilder withBaseUrl(String baseUrl)
      Overrides the API base URL.

      Useful for proxies, Azure OpenAI, or self-hosted endpoints.

      Parameters:
      baseUrl - the base URL (e.g. "http://localhost:11434/v1/")
      Returns:
      this builder
    • withTemperature

      public ConversationBuilder withTemperature(double temperature)
      Sets the temperature (creativity) of the AI responses.
      • 0.0 = deterministic, always picks the most likely word
      • 0.7 = balanced (good default)
      • 1.0 = very creative, more random
      Parameters:
      temperature - value between 0.0 and 1.0
      Returns:
      this builder
    • withMaxTokens

      public ConversationBuilder withMaxTokens(int maxTokens)
      Limits the maximum number of tokens in the AI response.

      Roughly: 1 token ~ 4 characters in English.

      Parameters:
      maxTokens - maximum tokens (e.g. 500, 1000, 4000)
      Returns:
      this builder
    • withChatModel

      public ConversationBuilder withChatModel(dev.langchain4j.model.chat.ChatModel model)
      Injects an externally created ChatModel.

      Useful for testing with a mock model, or when you need full control over model creation.

      Parameters:
      model - a pre-built ChatModel instance
      Returns:
      this builder
    • withEventListener

      public ConversationBuilder withEventListener(EasyAIListener eventListener)
      Registers a listener that receives a live EasyAIEvent stream around each Conversation.send(String): a STARTED event, a RESULT event carrying the reply, and a FINISHED event (or an ERROR event on failure).

      Familiar analogy: a read-receipt-plus-typing-indicator for your AI call — you can see it pick up the message, think, and answer, instead of only the final reply appearing.

      Parameters:
      eventListener - the listener to receive chat events (may be null)
      Returns:
      this builder
      See Also:
    • withActivityContext

      public ConversationBuilder withActivityContext(ActivityContext activityContext)
      Makes this conversation ambient-activity aware: before each Conversation.send(String) the given context is re-rendered and folded into the system message, so the model already knows what the user has recently been doing in the UI (and can resolve "this"/"that" without being told).

      Familiar analogy: handing your assistant a fresh one-line brief of "what you were just working on" right before every question — so you can say "cancel that order" and be understood.

      Typically you build the context from the per-tab activity store, e.g.:

      
       Conversation chat = EasyAI.chat()
           .withSystemMessage("You are an order-desk assistant.")
           .withActivityContext(ActivityContext.of(activityStore)
               .forSession(sessionId).forTab(tabId).build())
           .build();
       

      The context is combined with any withSystemMessage(String) value by SystemMessageComposer; passing null simply leaves the feature off.

      Parameters:
      activityContext - the ambient-activity descriptor to inject, or null to disable
      Returns:
      this builder
      See Also:
    • build

      public Conversation build()
      Builds and returns a ready-to-use Conversation.
      Returns:
      a new Conversation instance