Skip to content

Commit a02010c

Browse files
committed
Cleanup
1 parent 3e774ed commit a02010c

File tree

7 files changed

+83
-72
lines changed

7 files changed

+83
-72
lines changed

langchain4j-kotlin/src/main/kotlin/me/kpavlov/langchain4j/kotlin/service/AiServiceOrchestrator.kt

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ import java.util.concurrent.Future
5555
import java.util.function.Supplier
5656
import kotlin.coroutines.CoroutineContext
5757

58-
5958
/**
6059
* AiServiceOrchestrator is an internal class responsible for coordinating interactions with AI services.
6160
* It handles the flow of messages, memory management, augmentation, moderation, and parsing outputs
@@ -87,7 +86,9 @@ import kotlin.coroutines.CoroutineContext
8786
*/
8887
@ApiStatus.Internal
8988
@Suppress("TooManyFunctions", "detekt:all")
90-
internal class AiServiceOrchestrator<T : Any> @JvmOverloads constructor(
89+
internal class AiServiceOrchestrator<T : Any>
90+
@JvmOverloads
91+
constructor(
9192
private val context: AiServiceContext,
9293
private val serviceOutputParser: ServiceOutputParser,
9394
private val tokenStreamAdapters: Collection<TokenStreamAdapter>,
@@ -145,14 +146,20 @@ internal class AiServiceOrchestrator<T : Any> @JvmOverloads constructor(
145146

146147
context.retrievalAugmentor?.let {
147148
val chatMemoryMessages = chatMemory?.messages()
148-
val metadata = Metadata.from(userMessage, memoryId, chatMemoryMessages)
149+
val metadata =
150+
Metadata.from(
151+
userMessage,
152+
memoryId,
153+
chatMemoryMessages,
154+
)
149155
val augmentationRequest = AugmentationRequest(userMessage, metadata)
150156
augmentationResult = it.augment(augmentationRequest)
151157
userMessage = augmentationResult?.chatMessage() as UserMessage
152158
}
153159

154160
val returnType = ReflectionHelper.getSuspendReturnType(method)
155-
val streaming = returnType == TokenStream::class.java || canAdaptTokenStreamTo(returnType)
161+
val streaming =
162+
returnType == TokenStream::class.java || canAdaptTokenStreamTo(returnType)
156163
val supportsJsonSchema = supportsJsonSchema()
157164
var jsonSchema: JsonSchema? = null
158165

@@ -261,10 +268,11 @@ internal class AiServiceOrchestrator<T : Any> @JvmOverloads constructor(
261268
.responseFormat(responseFormat)
262269
.build()
263270

264-
var chatResponse = context.chatModel.chat(coroutineContext) {
265-
this.messages = messages
266-
this.parameters = parameters
267-
}
271+
var chatResponse =
272+
context.chatModel.chat(coroutineContext) {
273+
this.messages = messages
274+
this.parameters = parameters
275+
}
268276

269277
AiServices.verifyModerationIfNeeded(moderationFuture)
270278

langchain4j-kotlin/src/main/kotlin/me/kpavlov/langchain4j/kotlin/service/AsyncAiServices.kt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,15 @@ import dev.langchain4j.spi.ServiceHelper
1414
import dev.langchain4j.spi.services.TokenStreamAdapter
1515
import java.util.concurrent.ExecutorService
1616
import java.util.concurrent.Executors
17-
import kotlin.coroutines.CoroutineContext
1817

1918
public class AsyncAiServices<T : Any>(
2019
context: AiServiceContext,
2120
) : AiServices<T>(context) {
22-
private val chatModelCoroutineContext: CoroutineContext? = null
2321
private val serviceOutputParser = ServiceOutputParser()
2422
private val tokenStreamAdapters =
2523
ServiceHelper.loadFactories<TokenStreamAdapter>(TokenStreamAdapter::class.java)
2624

27-
@Suppress("NestedBlockDepth")
25+
@Suppress("NestedBlockDepth", "LongMethod")
2826
override fun build(): T {
2927
performBasicValidation()
3028

@@ -76,16 +74,18 @@ public class AsyncAiServices<T : Any>(
7674
}
7775
}
7876

79-
val executor: ExecutorService = VirtualThreadUtils.createVirtualThreadExecutor {
80-
Executors.newCachedThreadPool()
81-
}!!
77+
val executor: ExecutorService =
78+
VirtualThreadUtils.createVirtualThreadExecutor {
79+
Executors.newCachedThreadPool()
80+
}!!
8281

83-
val handler = AiServiceOrchestrator<T>(
84-
context = context,
85-
serviceOutputParser = serviceOutputParser,
86-
tokenStreamAdapters = tokenStreamAdapters,
87-
executor = executor,
88-
)
82+
val handler =
83+
AiServiceOrchestrator<T>(
84+
context = context,
85+
serviceOutputParser = serviceOutputParser,
86+
tokenStreamAdapters = tokenStreamAdapters,
87+
executor = executor,
88+
)
8989
@Suppress("UNCHECKED_CAST", "unused")
9090
return ReflectionHelper.createSuspendProxy(context.aiServiceClass) { method, args ->
9191
return@createSuspendProxy handler.execute(method, args)

langchain4j-kotlin/src/main/kotlin/me/kpavlov/langchain4j/kotlin/service/ReflectionVariableResolver.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,12 @@ import java.util.Optional
2323
*/
2424
@Suppress("detekt:all")
2525
internal object ReflectionVariableResolver {
26-
2726
public fun findTemplateVariables(
2827
template: String,
2928
method: Method,
3029
args: Array<Any?>?,
31-
): MutableMap<String?, Any?> {
32-
return InternalReflectionVariableResolver.findTemplateVariables(template, method, args)
33-
}
30+
): MutableMap<String?, Any?> =
31+
InternalReflectionVariableResolver.findTemplateVariables(template, method, args)
3432

3533
public fun asString(arg: Any?): String? {
3634
if (arg == null) {

langchain4j-kotlin/src/test/kotlin/me/kpavlov/langchain4j/kotlin/service/ServiceWithPromptTemplatesTest.kt

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,45 +18,46 @@ import org.junit.jupiter.api.Test
1818

1919
internal class ServiceWithPromptTemplatesTest {
2020
@Test
21-
fun `Should use System and User Prompt Templates`() = runTest {
22-
val chatResponse =
23-
ChatResponse
24-
.builder()
25-
.aiMessage(AiMessage("I'm fine, thanks"))
26-
.build()
21+
fun `Should use System and User Prompt Templates`() =
22+
runTest {
23+
val chatResponse =
24+
ChatResponse
25+
.builder()
26+
.aiMessage(AiMessage("I'm fine, thanks"))
27+
.build()
2728

28-
val model =
29-
object : ChatModel {
30-
override fun chat(chatRequest: ChatRequest): ChatResponse {
31-
chatRequest.messages() shouldHaveSize 2
32-
val systemMessage =
33-
chatRequest.messages().first { it is SystemMessage } as SystemMessage
34-
val userMessage =
35-
chatRequest.messages().first {
36-
it is UserMessage
37-
} as UserMessage
29+
val model =
30+
object : ChatModel {
31+
override fun chat(chatRequest: ChatRequest): ChatResponse {
32+
chatRequest.messages() shouldHaveSize 2
33+
val systemMessage =
34+
chatRequest.messages().first { it is SystemMessage } as SystemMessage
35+
val userMessage =
36+
chatRequest.messages().first {
37+
it is UserMessage
38+
} as UserMessage
3839

39-
systemMessage.text() shouldBe
40-
"You are helpful assistant using chatMemoryID=default"
41-
userMessage.singleText() shouldBe "Hello, My friend! How are you?"
40+
systemMessage.text() shouldBe
41+
"You are helpful assistant using chatMemoryID=default"
42+
userMessage.singleText() shouldBe "Hello, My friend! How are you?"
4243

43-
return chatResponse
44+
return chatResponse
45+
}
4446
}
45-
}
4647

47-
val assistant =
48-
AiServices
49-
.builder(Assistant::class.java)
50-
.systemMessageProvider(
51-
TemplateSystemMessageProvider(
52-
"prompts/ServiceWithTemplatesTest/default-system-prompt.mustache",
53-
),
54-
).chatModel(model)
55-
.build()
48+
val assistant =
49+
AiServices
50+
.builder(Assistant::class.java)
51+
.systemMessageProvider(
52+
TemplateSystemMessageProvider(
53+
"prompts/ServiceWithTemplatesTest/default-system-prompt.mustache",
54+
),
55+
).chatModel(model)
56+
.build()
5657

57-
val response = assistant.askQuestion(userName = "My friend", question = "How are you?")
58-
assertThat(response).isEqualTo("I'm fine, thanks")
59-
}
58+
val response = assistant.askQuestion(userName = "My friend", question = "How are you?")
59+
assertThat(response).isEqualTo("I'm fine, thanks")
60+
}
6061

6162
@Suppress("unused")
6263
private interface Assistant {

samples/src/main/kotlin/me/kpavlov/langchain4j/kotlin/samples/AsyncAiServiceExample.kt

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,26 @@ import kotlinx.coroutines.runBlocking
66
import me.kpavlov.langchain4j.kotlin.service.AsyncAiServicesFactory
77
import me.kpavlov.langchain4j.kotlin.service.createAiService
88

9-
109
fun interface AsyncAssistant {
11-
suspend fun askQuestion(@UserMessage question: String): String
10+
suspend fun askQuestion(
11+
@UserMessage question: String,
12+
): String
1213
}
1314

1415
class AsyncAiServiceExample(
15-
private val model: ChatModel
16+
private val model: ChatModel,
1617
) {
1718
suspend fun callAiService(): String {
18-
val assistant = createAiService(
19-
serviceClass = AsyncAssistant::class.java,
20-
factory = AsyncAiServicesFactory(),
21-
).chatModel(model)
22-
.systemMessageProvider { "You are a helpful software engineer" }
23-
.build()
19+
val assistant =
20+
createAiService(
21+
serviceClass = AsyncAssistant::class.java,
22+
factory = AsyncAiServicesFactory(),
23+
).chatModel(model)
24+
.systemMessageProvider { "You are a helpful software engineer" }
25+
.build()
2426

2527
return assistant.askQuestion(
26-
"What's new in Kotlin/AI space in one sentence?"
28+
"What's new in Kotlin/AI space in one sentence?",
2729
)
2830
}
2931
}

samples/src/main/kotlin/me/kpavlov/langchain4j/kotlin/samples/Environment.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import me.kpavlov.finchly.BaseTestEnvironment
66

77
val testEnv = BaseTestEnvironment()
88

9-
val model: ChatModel = OpenAiChatModel.builder()
10-
.modelName("gpt-4o-nano")
11-
.apiKey(testEnv["OPENAI_API_KEY"])
12-
.build()
9+
val model: ChatModel =
10+
OpenAiChatModel
11+
.builder()
12+
.modelName("gpt-4o-nano")
13+
.apiKey(testEnv["OPENAI_API_KEY"])
14+
.build()

samples/src/main/kotlin/me/kpavlov/langchain4j/kotlin/samples/OpenAiChatModelExample.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import kotlinx.coroutines.runBlocking
88
import me.kpavlov.langchain4j.kotlin.model.chat.chatAsync
99

1010
class OpenAiChatModelExample(
11-
private val model: ChatModel
11+
private val model: ChatModel,
1212
) {
1313
suspend fun callChatAsync(): String {
1414
val response =

0 commit comments

Comments
 (0)