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
    • withChatLanguageModel

      public ConversationBuilder withChatLanguageModel(dev.langchain4j.model.chat.ChatLanguageModel model)
      Injects an externally created ChatLanguageModel.

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

      Parameters:
      model - a pre-built ChatLanguageModel instance
      Returns:
      this builder
    • build

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