Skip to content

Commit

Permalink
add other tools
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcheng1982 committed Oct 7, 2024
1 parent 71c7239 commit 2283155
Show file tree
Hide file tree
Showing 50 changed files with 945 additions and 9 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# LLM Agent Builder Toolkit - File
# LLM Agent Builder Tools

Tools in this toolkit:
Tools:

* `readLocalFile`
* `writeLocalFile`
* `writeLocalFile`
* `runSqlQuery`

30 changes: 30 additions & 0 deletions baidu-hot-search/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.github.llmagentbuilder</groupId>
<artifactId>tools</artifactId>
<version>0.2.0</version>
</parent>

<artifactId>tool-baidu-hot-search</artifactId>
<name>LLM Agent Builder :: Tool :: Baidu Hot Search</name>
<description>LLM Agent Builder :: Tool :: Baidu Hot Search</description>

<properties>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>io.github.llmagentbuilder</groupId>
<artifactId>web-scraper</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.github.llmagentbuilder.tool.baiduhotsearch

data class BaiduHotSearchConfig(val numberOfItems: Int? = null)

data class BaiduHotSearchRequest(val numberOfItems: Int? = null)

data class BaiduHotSearchResponse(val results: List<String>? = null)
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.github.llmagentbuilder.tool.baiduhotsearch

import io.github.llmagentbuilder.core.tool.ConfigurableAgentTool
import io.github.llmagentbuilder.webscraper.WebScraper
import org.slf4j.LoggerFactory

const val toolId = "baiduHotSearch"

class BaiduHotSearchTool(private val config: BaiduHotSearchConfig?) :
ConfigurableAgentTool<BaiduHotSearchRequest, BaiduHotSearchResponse, BaiduHotSearchConfig> {

private val logger = LoggerFactory.getLogger(javaClass)

init {
logger.info("Created with config: $config")
}

override fun apply(request: BaiduHotSearchRequest): BaiduHotSearchResponse {
return BaiduHotSearchResponse(
WebScraper.textList(
"https://top.baidu.com/board?tab=realtime",
".c-single-text-ellipsis"
).take((config?.numberOfItems ?: 10).coerceAtLeast(1))
)
}

override fun description(): String {
return "find hot search keywords in Baidu"
}

override fun id(): String {
return toolId
}

override fun name(): String {
return toolId
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.github.llmagentbuilder.tool.baiduhotsearch

import io.github.llmagentbuilder.core.tool.ConfigurableAgentToolFactory

class BaiduHotSearchToolFactory :
ConfigurableAgentToolFactory<BaiduHotSearchConfig, BaiduHotSearchTool> {
override fun toolId(): String {
return toolId
}

override fun create(config: BaiduHotSearchConfig?): BaiduHotSearchTool {
return BaiduHotSearchTool(config)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.github.llmagentbuilder.tool.baiduhotsearch.BaiduHotSearchToolFactory
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.github.llmagentbuilder.tool.baiduhotsearch

import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNotNull
import org.junit.jupiter.api.Test

class BaiduHotSearchToolTest {
@Test
fun find() {
val results = BaiduHotSearchTool(BaiduHotSearchConfig()).apply(
BaiduHotSearchRequest(10)
)
.results
assertNotNull(results)
assertEquals(10, results?.size)
}
}
23 changes: 23 additions & 0 deletions baidu-search/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.github.llmagentbuilder</groupId>
<artifactId>tools</artifactId>
<version>0.2.0</version>
</parent>

<artifactId>tool-baidu-search</artifactId>
<name>LLM Agent Builder :: Tool :: Baidu Search</name>
<description>LLM Agent Builder :: Tool :: Baidu Search</description>

<dependencies>
<dependency>
<groupId>io.github.llmagentbuilder</groupId>
<artifactId>web-scraper</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.github.llmagentbuilder.tool.baidusearch

data class BaiduSearchConfig(val numberOfItems: Int? = null)

data class BaiduSearchRequest(val keyword: String? = null)

data class BaiduSearchResultItem(
val title: String? = null,
val url: String? = null
)

data class BaiduSearchResponse(val items: List<BaiduSearchResultItem>? = null)
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.github.llmagentbuilder.tool.baidusearch

import io.github.llmagentbuilder.core.tool.ConfigurableAgentTool
import io.github.llmagentbuilder.webscraper.WebScraper
import org.slf4j.LoggerFactory

const val toolId = "baiduSearch"

class BaiduSearchTool(private val config: BaiduSearchConfig?) :
ConfigurableAgentTool<BaiduSearchRequest, BaiduSearchResponse, BaiduSearchConfig> {

private val logger = LoggerFactory.getLogger(javaClass)

init {
logger.info("Created with config: $config")
}

override fun apply(request: BaiduSearchRequest): BaiduSearchResponse {
return BaiduSearchResponse(
WebScraper.links(
"https://www.baidu.com/s?wd=${request.keyword}",
".c-container h3 a"
).map {
BaiduSearchResultItem(it.title, it.href)
}.take((config?.numberOfItems ?: 10).coerceAtLeast(1))
)
}

override fun description(): String {
return "Search web content using Baidu"
}

override fun id(): String {
return toolId
}

override fun name(): String {
return toolId
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.github.llmagentbuilder.tool.baidusearch

import io.github.llmagentbuilder.core.tool.ConfigurableAgentToolFactory

class BaiduSearchToolFactory :
ConfigurableAgentToolFactory<BaiduSearchConfig, BaiduSearchTool> {
override fun create(config: BaiduSearchConfig?): BaiduSearchTool {
return BaiduSearchTool(config)
}

override fun toolId(): String {
return toolId
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.github.llmagentbuilder.tool.baidusearch.BaiduSearchToolFactory
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.github.llmagentbuilder.tool.baidusearch

import org.junit.jupiter.api.Test
import kotlin.test.assertNotNull

class BaiduSearchToolTest {
@Test
fun testSearch() {
val tool = BaiduSearchTool(BaiduSearchConfig())
val items = tool.apply(BaiduSearchRequest("AI大模型")).items
assertNotNull(items)
}
}
24 changes: 24 additions & 0 deletions code-execution/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.github.llmagentbuilder</groupId>
<artifactId>tools</artifactId>
<version>0.2.0</version>
</parent>

<artifactId>code-execution</artifactId>
<name>LLM Agent Builder :: Code Execution</name>
<description>LLM Agent Builder :: Code Execution</description>

<dependencies>
<dependency>
<groupId>org.buildobjects</groupId>
<artifactId>jproc</artifactId>
<version>2.8.2</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package io.github.llmagentbuilder.codeexecution;
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.github.llmagentbuilder.codeexecution

import java.nio.file.Files
import kotlin.io.path.writeText

/**
* Code executor
*/
interface CodeExecutor {
fun execute(input: String): String
}

object CodeExecutionHelper {
fun inputToFile(input: String, suffix: String): String {
val file = Files.createTempFile("code-execution-", suffix)
file.writeText(input)
return file.toAbsolutePath().toString()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.github.llmagentbuilder.codeexecution.nonsandboxed

import io.github.llmagentbuilder.codeexecution.CodeExecutor
import org.buildobjects.process.ProcBuilder
import java.nio.file.Paths

abstract class NonSandboxedCodeExecutor : CodeExecutor {
override fun execute(input: String): String {
val result = ProcBuilder(executable())
.withArgs(*args(input).toTypedArray())
.withWorkingDirectory(
(workingDirectory() ?: ".")
.run { Paths.get(this).toAbsolutePath().normalize() }
.toFile())
.ignoreExitStatus()
.run()
return result.outputString.trim()
}

abstract fun executable(): String
abstract fun workingDirectory(): String?
abstract fun args(input: String): List<String>
}
23 changes: 23 additions & 0 deletions code-executor-java/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.github.llmagentbuilder</groupId>
<artifactId>tools</artifactId>
<version>0.2.0</version>
</parent>

<artifactId>tool-java-code-execution</artifactId>
<name>LLM Agent Builder :: Tool :: Java Code Execution</name>
<description>LLM Agent Builder :: Tool :: Java Code Execution</description>

<dependencies>
<dependency>
<groupId>io.github.llmagentbuilder</groupId>
<artifactId>code-execution</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.github.llmagentbuilder.tool.javacodeexecution;

public class JavaCodeExecutionConfig {

private String executable;

private String workingDirectory;

public String getExecutable() {
return executable;
}

public void setExecutable(String executable) {
this.executable = executable;
}

public String getWorkingDirectory() {
return workingDirectory;
}

public void setWorkingDirectory(String workingDirectory) {
this.workingDirectory = workingDirectory;
}

@Override
public String toString() {
return "JavaCodeExecutionConfig{" +
"executable='" + executable + '\'' +
", workingDirectory='" + workingDirectory + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.github.llmagentbuilder.tool.javacodeexecution;

public record JavaCodeExecutionRequest(String code) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.github.llmagentbuilder.tool.javacodeexecution;

public record JavaCodeExecutionResponse(String result) {

}
Loading

0 comments on commit 2283155

Please sign in to comment.