-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extend AiServices to support SystemMessageProvider (#25)
This pull request includes several updates to the `langchain4j-kotlin` project, focusing on dependency management, new features, and testing improvements. The most important changes include adding new dependencies, introducing a new interface for system message providers, and updating the `pom.xml` files to reflect these changes. ### Dependency Management: * Added `langchain4j` as an optional dependency in `langchain4j-kotlin/pom.xml` and updated the main `pom.xml` to include versions for `mockito` and `mockito-kotlin` dependencies. [[1]](diffhunk://#diff-988dd67f6a0de416687ee39291f2713a8229de37918339f305c4ec8ae58e539bR19-R23) [[2]](diffhunk://#diff-9c5fb3d1b7e3b0f54bc5c4182965c4fe1f9023d449017cece3005d3f90e8e4d8R104-R116) * Removed `langchain4j` from the test scope in `langchain4j-kotlin/pom.xml`. * Added `mockito-kotlin` and `mockito-junit-jupiter` as test dependencies in `langchain4j-kotlin/pom.xml`. ### New Features: * Introduced a new type alias `ChatMemoryId` in `TypeAliases.kt`. * Added `SystemMessageProvider` interface to provide system messages based on chat memory identifiers. * Created an extension function `systemMessageProvider` for `AiServices` to set the system message provider. ### Testing Improvements: * Added a new test class `ServiceWithSystemMessageProviderTest` to verify the functionality of the `SystemMessageProvider`. ### Documentation: * Updated the `README.md` to include the new `langchain4j` dependency. ### Miscellaneous: * Updated the project name in the main `pom.xml` from "Aggregator" to "Root". * Added additional configuration for Maven plugins in `pom.xml` to support new dependencies and improve build processes. [[1]](diffhunk://#diff-9c5fb3d1b7e3b0f54bc5c4182965c4fe1f9023d449017cece3005d3f90e8e4d8R177-R179) [[2]](diffhunk://#diff-9c5fb3d1b7e3b0f54bc5c4182965c4fe1f9023d449017cece3005d3f90e8e4d8L225-R274) --------- Co-authored-by: Konstantin Pavlov <{ID}+{username}@users.noreply.github.com>
- Loading branch information
Showing
7 changed files
with
137 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
langchain4j-kotlin/src/main/kotlin/me/kpavlov/langchain4j/kotlin/TypeAliases.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package me.kpavlov.langchain4j.kotlin | ||
|
||
typealias ChatMemoryId = Any |
13 changes: 13 additions & 0 deletions
13
...in4j-kotlin/src/main/kotlin/me/kpavlov/langchain4j/kotlin/service/AiServicesExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package me.kpavlov.langchain4j.kotlin.service | ||
|
||
import dev.langchain4j.service.AiServices | ||
|
||
/** | ||
* Sets the system message provider for the AI services. | ||
* | ||
* @param provider The SystemMessageProvider that supplies system messages based on chat memory identifiers. | ||
* @return The updated AiServices instance with the specified system message provider. | ||
*/ | ||
public fun <T> AiServices<T>.systemMessageProvider( | ||
provider: SystemMessageProvider, | ||
): AiServices<T> = this.systemMessageProvider(provider::getSystemMessage) |
16 changes: 16 additions & 0 deletions
16
...n4j-kotlin/src/main/kotlin/me/kpavlov/langchain4j/kotlin/service/SystemMessageProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package me.kpavlov.langchain4j.kotlin.service | ||
|
||
import me.kpavlov.langchain4j.kotlin.ChatMemoryId | ||
|
||
/** | ||
* Interface for providing LLM system messages based on a given chat memory identifier. | ||
*/ | ||
public interface SystemMessageProvider { | ||
/** | ||
* Provides a system message based on the given chat memory identifier. | ||
* | ||
* @param chatMemoryID Identifier for the chat memory used to generate the system message. | ||
* @return A system prompt string associated with the provided chat memory identifier, maybe `null` | ||
*/ | ||
public fun getSystemMessage(chatMemoryID: ChatMemoryId): String? | ||
} |
47 changes: 47 additions & 0 deletions
47
...test/kotlin/me/kpavlov/langchain4j/kotlin/service/ServiceWithSystemMessageProviderTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package me.kpavlov.langchain4j.kotlin.service | ||
|
||
import assertk.assertThat | ||
import assertk.assertions.isEqualTo | ||
import dev.langchain4j.data.message.AiMessage | ||
import dev.langchain4j.data.message.SystemMessage | ||
import dev.langchain4j.model.chat.ChatLanguageModel | ||
import dev.langchain4j.model.output.Response | ||
import dev.langchain4j.service.AiServices | ||
import dev.langchain4j.service.UserMessage | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.extension.ExtendWith | ||
import org.mockito.junit.jupiter.MockitoExtension | ||
|
||
@ExtendWith(MockitoExtension::class) | ||
class ServiceWithSystemMessageProviderTest { | ||
lateinit var model: ChatLanguageModel | ||
|
||
@Test | ||
fun `Should use SystemMessageProvider`() { | ||
model = | ||
ChatLanguageModel { | ||
assertThat(it.first()).isEqualTo(SystemMessage.from("You are helpful assistant")) | ||
Response.from(AiMessage.from("I'm fine, thanks")) | ||
} | ||
|
||
val assistant = | ||
AiServices | ||
.builder(Assistant::class.java) | ||
.systemMessageProvider( | ||
object : SystemMessageProvider { | ||
override fun getSystemMessage(chatMemoryID: Any): String = | ||
"You are helpful assistant" | ||
}, | ||
).chatLanguageModel(model) | ||
.build() | ||
|
||
val response = assistant.askQuestion("How are you") | ||
assertThat(response).isEqualTo("I'm fine, thanks") | ||
} | ||
|
||
private interface Assistant { | ||
fun askQuestion( | ||
@UserMessage question: String, | ||
): String | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters