Skip to content

Conversation

@kpavlov
Copy link
Owner

@kpavlov kpavlov commented Nov 16, 2024

This pull request introduces several enhancements and new features to the LangChain4j Kotlin module, primarily focusing on prompt templates and configuration improvements. The most important changes include the addition of prompt template support, configuration management, and various type aliases for better code clarity.

Enhancements to Prompt Templates:

  • New Prompt Templates Guide: Added comprehensive documentation on using prompt templates with the AiServices module, including configuration, usage examples, and implementation details. (docs/PromptTemplates.md)
  • Prompt Template Classes: Introduced several new classes and interfaces for managing prompt templates, including PromptTemplate, SimplePromptTemplate, PromptTemplateSource, ClasspathPromptTemplateSource, PromptTemplateFactory, RenderablePromptTemplate, and TemplateRenderer. (langchain4j-kotlin/src/main/kotlin/me/kpavlov/langchain4j/kotlin/prompt/) [1] [2] [3] [4] [5] [6] [7]

Configuration Improvements:

  • Configuration Object: Added a Configuration object responsible for loading and providing access to application properties, including prompt templates and their renderers. (langchain4j-kotlin/src/main/kotlin/me/kpavlov/langchain4j/kotlin/Configuration.kt)

Codebase Simplification:

  • Type Aliases: Introduced several type aliases to provide clearer and more specific meanings in the codebase, such as ChatMemoryId, TemplateName, TemplateContent, and PromptContent. (langchain4j-kotlin/src/main/kotlin/me/kpavlov/langchain4j/kotlin/TypeAliases.kt)

Miscellaneous Changes:

  • Sensitive Data Logging: Replaced the PII marker with SENSITIVE for logging sensitive data. (langchain4j-kotlin/src/main/kotlin/me/kpavlov/langchain4j/kotlin/internal/Logging.kt, langchain4j-kotlin/src/main/kotlin/me/kpavlov/langchain4j/kotlin/model/chat/StreamingChatLanguageModelExtensions.kt) [1] [2] [3] [4]

Documentation Updates:

  • README Update: Enhanced the README with additional context and features related to Kotlin support for LangChain4j. (README.md)

These changes collectively enhance the functionality, clarity, and maintainability of the LangChain4j Kotlin module.

Konstantin Pavlov added 10 commits November 16, 2024 17:22
…and tests

Included a new SimpleTemplateRenderer implementing the TemplateRenderer interface with functionality to replace placeholders in templates with corresponding values from a variables map. Added comprehensive tests to validate single and multiple placeholder replacements, as well as undefined placeholders throwing exceptions.
Introduced a `Configuration` object to handle properties loading from `langchain4j-kotlin.properties` file. This ensures configurations are loaded at runtime, promoting better management and flexibility of application settings.
This change updates the .editorconfig file to explicitly unset the Kotlin import-on-demand rule. It ensures that Kotlin packages do not use import-on-demand by default, improving code readability and management.
Added specific configuration for Mustache files in .editorconfig to not insert a final newline. This ensures consistent formatting for Mustache template files across different editors.
Introduced methods to load and instantiate `PromptTemplateSource` and `TemplateRenderer` using configuration properties. Enhanced the configuration class to support dynamic instance creation by class name, which improves flexibility and maintainability.
Introduced the TemplateSystemMessageProvider class, which fetches and renders templates for system messages. Added corresponding unit tests to ensure proper functionality, including cases where templates are found and not found.
Converted SimpleTemplateRenderer from an object to a class to allow instance creation. Updated tests to create instances of the class and call the render method. This change provides more flexibility for future enhancements and testing.
Introduced `PromptTemplateFactory` along with supporting interfaces and classes to streamline prompt template creation and management. Renamed `ExtendedPromptTemplate` to `RenderablePromptTemplate` and added properties to the configuration file for template source and renderer.
Introduce `ServiceWithPromptTemplatesTest` to verify that system and user prompt templates work correctly. The test sets up a mock `ChatLanguageModel` and checks the messages against predefined templates.
@kpavlov kpavlov added the enhancement New feature or request label Nov 16, 2024
Konstantin Pavlov added 5 commits November 16, 2024 19:37
Standardized the use of type aliases for template-related types in various classes and improved logging practices by replacing println with a logger. This enhances type clarity and ensures uniform handling of sensitive information.
Added comprehensive Kotlin documentation (KDoc) comments to clarify the purpose and usage of key classes and interfaces in the langchain4j-kotlin module. This includes descriptions for classes, methods, properties, and type aliases to improve code readability and maintainability.
Specify the Logger type explicitly in PromptTemplateFactory to ensure type safety and readability. Introduce simplelogger.properties for configuring logging levels and formatting for better diagnostics and debugging during tests.
Created `docs/PromptTemplates.md` to guide users on configuring and using prompt templates within the AiServices module. Updated README to mention the new feature, including a link to the documentation and an overview of prompt template support.
Annotated the ChatLanguageModelIT test class with @EnabledIfEnvironmentVariable to run tests only when the OPENAI_API_KEY environment variable is set. This ensures that tests depending on the OpenAI API are skipped if the API key is not available.
@codacy-production
Copy link

Coverage summary from Codacy

See diff coverage on Codacy

Coverage variation Diff coverage
+50.41% (target: -1.00%) 90.11%
Coverage variation details
Coverable lines Covered lines Coverage
Common ancestor commit (25ca539) 33 6 18.18%
Head commit (55da3e5) 121 (+88) 83 (+77) 68.60% (+50.41%)

Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch: <coverage of head commit> - <coverage of common ancestor commit>

Diff coverage details
Coverable lines Covered lines Diff coverage
Pull request (#27) 91 82 90.11%

Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified: <covered lines added or modified>/<coverable lines added or modified> * 100%

See your quality gate settings    Change summary preferences

Codacy stopped sending the deprecated coverage status on June 5th, 2024. Learn more

@kpavlov kpavlov marked this pull request as ready for review November 16, 2024 18:35
@kpavlov kpavlov merged commit a7f6719 into main Nov 16, 2024
5 checks passed
@kpavlov kpavlov deleted the external-templates branch November 16, 2024 18:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants