Skip to content

Commit 33f2ea0

Browse files
committed
refactor java executor code
1 parent 87ece27 commit 33f2ea0

File tree

10 files changed

+72
-79
lines changed

10 files changed

+72
-79
lines changed

container-image-generator/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
</parent>
1111

1212
<artifactId>container-image-generator</artifactId>
13+
<name>LLM Code Executor :: Container Image Generator</name>
1314

1415
<properties>
1516
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

core/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
</parent>
1111

1212
<artifactId>core</artifactId>
13+
<name>LLM Code Executor :: Core</name>
1314

1415
<properties>
1516
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

core/src/main/kotlin/com/javaaidev/llmcodeexecutor/core/LLMCodeExecutor.kt

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.github.dockerjava.api.async.ResultCallback
44
import com.github.dockerjava.api.exception.NotModifiedException
55
import com.github.dockerjava.api.model.*
66
import com.github.dockerjava.core.DefaultDockerClientConfig
7+
import com.github.dockerjava.core.DockerClientConfig
78
import com.github.dockerjava.core.DockerClientImpl
89
import com.github.dockerjava.httpclient5.ApacheDockerHttpClient
910
import org.slf4j.LoggerFactory
@@ -61,45 +62,27 @@ data class CodeExecutionResponse(
6162
val copiedFiles: List<CopiedFile>? = null,
6263
)
6364

64-
class LLMCodeExecutor(private val config: CodeExecutorConfig) {
65+
/**
66+
* Execute code for LLM
67+
*/
68+
class LLMCodeExecutor(
69+
private val config: CodeExecutorConfig,
70+
customDockerClientConfig: DockerClientConfig? = null
71+
) {
6572
private val logger = LoggerFactory.getLogger(LLMCodeExecutor::class.java)
66-
private val dockerClientConfig = DefaultDockerClientConfig.createDefaultConfigBuilder().build()
73+
private val dockerClientConfig =
74+
customDockerClientConfig ?: DefaultDockerClientConfig.createDefaultConfigBuilder().build()
6775
private val httpClient = ApacheDockerHttpClient.Builder()
6876
.dockerHost(dockerClientConfig.dockerHost)
6977
.sslConfig(dockerClientConfig.sslConfig)
7078
.maxConnections(100)
7179
.connectionTimeout(Duration.ofSeconds(30))
7280
.responseTimeout(Duration.ofSeconds(45))
7381
.build()
74-
private val dockerClient = DockerClientImpl.getInstance(dockerClientConfig, httpClient)
82+
private val dockerClient = DockerClientImpl.getInstance(customDockerClientConfig, httpClient)
7583

7684
fun execute(request: CodeExecutionRequest): CodeExecutionResponse {
77-
val pullImageCountDownLatch = CountDownLatch(1)
78-
dockerClient.pullImageCmd(config.containerImage)
79-
.exec(object : ResultCallback<PullResponseItem> {
80-
override fun close() {
81-
82-
}
83-
84-
override fun onStart(closeable: Closeable?) {
85-
86-
}
87-
88-
override fun onError(throwable: Throwable?) {
89-
logger.error("Failed to pull image", throwable)
90-
}
91-
92-
override fun onComplete() {
93-
pullImageCountDownLatch.countDown()
94-
}
95-
96-
override fun onNext(`object`: PullResponseItem?) {
97-
98-
}
99-
100-
})
101-
pullImageCountDownLatch.await(1, TimeUnit.MINUTES)
102-
logger.info("Image pulled successfully")
85+
pullImage()
10386

10487
val cmd = dockerClient.createContainerCmd(config.containerImage)
10588
config.volumes?.let { volumes ->
@@ -214,12 +197,41 @@ class LLMCodeExecutor(private val config: CodeExecutorConfig) {
214197
}
215198
}
216199

217-
// dockerClient.removeContainerCmd(containerId).exec()
200+
dockerClient.removeContainerCmd(containerId).exec()
218201
return CodeExecutionResponse(
219202
outputBuilder.toString(),
220203
errorBuilder.toString(),
221204
loadedFiles,
222205
copiedFiles
223206
)
224207
}
208+
209+
private fun pullImage() {
210+
val pullImageCountDownLatch = CountDownLatch(1)
211+
dockerClient.pullImageCmd(config.containerImage)
212+
.exec(object : ResultCallback<PullResponseItem> {
213+
override fun close() {
214+
215+
}
216+
217+
override fun onStart(closeable: Closeable?) {
218+
219+
}
220+
221+
override fun onError(throwable: Throwable?) {
222+
logger.error("Failed to pull image", throwable)
223+
}
224+
225+
override fun onComplete() {
226+
pullImageCountDownLatch.countDown()
227+
}
228+
229+
override fun onNext(`object`: PullResponseItem?) {
230+
231+
}
232+
233+
})
234+
pullImageCountDownLatch.await(1, TimeUnit.MINUTES)
235+
logger.info("Image pulled successfully")
236+
}
225237
}

core/src/test/kotlin/com/javaaidev/llmcodeexecutor/core/Manual.java

Lines changed: 0 additions & 14 deletions
This file was deleted.

executors/java/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
</parent>
1111

1212
<artifactId>executor-java</artifactId>
13+
<name>LLM Code Executor :: Executor :: Java</name>
1314

1415
<build>
1516
<sourceDirectory>src/main/kotlin</sourceDirectory>

executors/java/src/main/kotlin/com/javaaidev/llmcodeexecutor/executor/java/JavaCodeExecutor.kt

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,24 +42,4 @@ object JavaCodeExecutor {
4242
)
4343
)
4444
}
45-
}
46-
47-
fun main() {
48-
println(
49-
JavaCodeExecutor.execute(
50-
CodeExecutionRequest(
51-
"""
52-
public class Main {
53-
public static void main(String[] args) {
54-
System.out.println("hello");
55-
}
56-
}
57-
""".trimIndent(),
58-
OutputFileCollectionConfig(
59-
copyFiles = true,
60-
copiedFilesPath = "./target"
61-
)
62-
)
63-
)
64-
)
6545
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.javaaidev.llmcodeexecutor.executor.java
2+
3+
import com.javaaidev.llmcodeexecutor.core.CodeExecutionRequest
4+
import org.junit.jupiter.api.Test
5+
import kotlin.test.Ignore
6+
7+
class JavaCodeExecutorTest {
8+
@Test
9+
@Ignore
10+
fun basic() {
11+
val result = JavaCodeExecutor.execute(
12+
CodeExecutionRequest(
13+
"""
14+
public class Main {
15+
public static void main(String[] args) {
16+
System.out.println("hello");
17+
}
18+
}
19+
""".trimIndent()
20+
)
21+
)
22+
kotlin.test.assertEquals("Hello", result.output.trim())
23+
}
24+
}

executors/pom.xml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
</parent>
1111

1212
<artifactId>executors</artifactId>
13+
<name>LLM Code Executor :: Executors</name>
1314
<packaging>pom</packaging>
1415
<modules>
1516
<module>python</module>
@@ -22,13 +23,6 @@
2223
<kotlin.compiler.jvmTarget>1.8</kotlin.compiler.jvmTarget>
2324
</properties>
2425

25-
<repositories>
26-
<repository>
27-
<id>mavenCentral</id>
28-
<url>https://repo1.maven.org/maven2/</url>
29-
</repository>
30-
</repositories>
31-
3226
<build>
3327
<sourceDirectory>src/main/kotlin</sourceDirectory>
3428
<testSourceDirectory>src/test/kotlin</testSourceDirectory>

executors/python/pom.xml

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
</parent>
1111

1212
<artifactId>executor-python</artifactId>
13+
<name>LLM Code Executor :: Executor :: Python</name>
1314

1415
<build>
1516
<sourceDirectory>src/main/kotlin</sourceDirectory>
@@ -72,14 +73,6 @@
7273
<artifactId>kotlin-stdlib</artifactId>
7374
<version>2.1.10</version>
7475
</dependency>
75-
<dependency>
76-
<groupId>com.javaaidev.llmcodeexecutor</groupId>
77-
<artifactId>core</artifactId>
78-
<version>${project.version}</version>
79-
<classifier>tests</classifier>
80-
<type>test-jar</type>
81-
<scope>test</scope>
82-
</dependency>
8376
</dependencies>
8477

8578
</project>

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
<groupId>com.javaaidev.llmcodeexecutor</groupId>
88
<artifactId>parent</artifactId>
9+
<name>LLM Code Executor</name>
910
<version>0.1.0-SNAPSHOT</version>
1011
<packaging>pom</packaging>
1112
<modules>

0 commit comments

Comments
 (0)