Skip to content

Commit

Permalink
feat: Add check for ollama installation
Browse files Browse the repository at this point in the history
  • Loading branch information
fmueller committed Jun 2, 2024
1 parent eddce12 commit 0ad9f33
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 70 deletions.
74 changes: 4 additions & 70 deletions src/main/kotlin/com/github/fmueller/jarvis/ai/OllamaService.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package com.github.fmueller.jarvis.ai

import com.github.fmueller.jarvis.conversation.Message
import com.intellij.openapi.Disposable
import com.intellij.openapi.components.Service
import com.intellij.openapi.diagnostic.thisLogger
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import kotlinx.serialization.Serializable
import kotlinx.serialization.decodeFromString
Expand All @@ -16,7 +12,6 @@ import java.net.URI
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse
import java.util.*

@Serializable
private data class ChatMessage(val role: String, val content: String)
Expand All @@ -32,38 +27,11 @@ private data class ChatRequest(
private data class ChatResponse(val message: ChatMessage)

@Service(Service.Level.PROJECT)
class OllamaService : Disposable {

private val process = OllamaProcess()

init {
// TODO remove this autostart logic and show messages in conversation panel instead
if (isOllamaRunning()) {
thisLogger().info("Ollama process is already running")
} else {
process.start()
runBlocking {
val startTime = System.currentTimeMillis()
while (System.currentTimeMillis() - startTime < 5000) {
if (isOllamaRunning()) {
thisLogger().info("Ollama process started")
break
} else {
delay(500)
}
}

if (!isOllamaRunning()) {
thisLogger().error("Failed to start Ollama process")
// TODO send message to user interface
}
}
}
}
class OllamaService {

suspend fun chat(messages: List<Message>): String = withContext(Dispatchers.IO) {
// TODO check if model is available
// TODO if not download model
// TODO if not, download model
try {
val client = HttpClient.newHttpClient()
val chatRequest = ChatRequest(
Expand Down Expand Up @@ -100,11 +68,7 @@ class OllamaService : Disposable {
}
}

override fun dispose() {
process.stop()
}

private fun isOllamaRunning(): Boolean {
fun isAvailable(): Boolean {
return try {
val client = HttpClient.newHttpClient()
val request = HttpRequest.newBuilder()
Expand All @@ -117,34 +81,4 @@ class OllamaService : Disposable {
false
}
}

private class OllamaProcess {

private var process: Process? = null

fun start() {
thisLogger().info("Starting Ollama process")
try {
process = ProcessBuilder(getOllamaCommand(), "serve").start()
} catch (e: UnsupportedOperationException) {
thisLogger().error("Unsupported operating system", e)
} catch (e: Exception) {
thisLogger().error("Failed to start Ollama process", e)
}
}

fun stop() {
thisLogger().info("Destroying Ollama process")
process?.destroy()
}

private fun getOllamaCommand(): String {
val os = System.getProperty("os.name").lowercase(Locale.getDefault())
return when {
os.contains("win") -> "ollama.exe"
os.contains("nix") || os.contains("nux") || os.contains("mac") -> "ollama"
else -> throw UnsupportedOperationException("Unsupported operating system: $os")
}
}
}
}
}
10 changes: 10 additions & 0 deletions src/main/kotlin/com/github/fmueller/jarvis/commands/ChatCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ import com.github.fmueller.jarvis.conversation.Role
class ChatCommand(private val ollamaService: OllamaService) : SlashCommand {

override suspend fun run(conversation: Conversation): Conversation {
if (!ollamaService.isAvailable()) {
conversation.addMessage(
Message(
Role.ASSISTANT,
"I can't access Ollama at ```http://localhost:11434```. You need to install it first and download the ```llama3``` model."
)
)
return conversation
}

val response = ollamaService.chat(conversation.messages).trim()
conversation.addMessage(Message(Role.ASSISTANT, response))
return conversation
Expand Down

0 comments on commit 0ad9f33

Please sign in to comment.