Skip to content

Commit

Permalink
add ollama support
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcheng1982 committed Jan 1, 2025
1 parent 8aa61e8 commit 0ff40fb
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import java.util.function.Supplier
*/
interface AgentToolFactory<out T : AgentTool<*, *>> {
/**
* @param T Agent tool type
* @return Agent tool
*/
fun create(): T
Expand All @@ -21,7 +20,6 @@ interface AgentToolFactory<out T : AgentTool<*, *>> {
interface ConfigurableAgentToolFactory<CONFIG, out T : ConfigurableAgentTool<*, *, CONFIG>> :
AgentToolFactory<T> {
/**
* @param T Agent tool type
* @param config Tool configuration object
* @return Agent tool
*/
Expand Down
32 changes: 32 additions & 0 deletions llm/ollama/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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>llm</artifactId>
<version>0.4.2</version>
</parent>

<artifactId>llm-ollama</artifactId>
<name>LLM Adapter :: Ollama</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-ollama</artifactId>
</dependency>
<dependency>
<groupId>io.github.llmagentbuilder</groupId>
<artifactId>core</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.llm.ollama

import io.github.llmagentbuilder.core.ChatModelProvider
import io.github.llmagentbuilder.core.MapToObject
import org.springframework.ai.chat.model.ChatModel
import org.springframework.ai.model.function.FunctionCallbackResolver
import org.springframework.ai.ollama.OllamaChatModel
import org.springframework.ai.ollama.api.OllamaApi
import org.springframework.ai.ollama.api.OllamaModel
import org.springframework.ai.ollama.api.OllamaOptions

class OllamaChatModelProvider : ChatModelProvider {
override fun configKey(): String {
return "ollama"
}

override fun provideChatModel(
functionCallbackResolver: FunctionCallbackResolver,
config: Map<String, Any?>?
): ChatModel? {
val ollamaConfig = MapToObject.toObject<OllamaConfig>(config)
if (ollamaConfig?.enabled == false) {
return null
}
val model = ollamaConfig?.model ?: OllamaModel.PHI3.id()
return OllamaChatModel.builder()
.ollamaApi(OllamaApi())
.defaultOptions(OllamaOptions.builder().model(model).build())
.functionCallbackResolver(functionCallbackResolver)
.build()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.github.llmagentbuilder.llm.ollama

import io.github.llmagentbuilder.core.ChatOptionsConfigurer
import org.springframework.ai.chat.prompt.ChatOptions
import org.springframework.ai.ollama.api.OllamaOptions

class OllamaChatOptionsConfigurer : ChatOptionsConfigurer {
override fun supports(chatOptions: ChatOptions?): Boolean {
return chatOptions is OllamaOptions
}

override fun configure(
chatOptions: ChatOptions?,
config: ChatOptionsConfigurer.ChatOptionsConfig
): ChatOptions {
val stops = config.stopSequence ?: listOf()
return chatOptions?.let {
val options = OllamaOptions.fromOptions(it as OllamaOptions)
options.stop = ((options.stopSequences ?: listOf()) + stops)
return options
} ?: OllamaOptions.builder().stop(stops).build()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.github.llmagentbuilder.llm.ollama

data class OllamaConfig(
val enabled: Boolean? = true,
val model: String? = null,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.github.llmagentbuilder.llm.ollama.OllamaChatModelProvider
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.github.llmagentbuilder.llm.ollama.OllamaChatOptionsConfigurer
1 change: 1 addition & 0 deletions llm/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<packaging>pom</packaging>
<modules>
<module>openai</module>
<module>ollama</module>
</modules>

<properties>
Expand Down

0 comments on commit 0ff40fb

Please sign in to comment.