diff --git a/src/main/kotlin/com/github/fmueller/jarvis/ai/OllamaService.kt b/src/main/kotlin/com/github/fmueller/jarvis/ai/OllamaService.kt index 3e3e795..407d515 100644 --- a/src/main/kotlin/com/github/fmueller/jarvis/ai/OllamaService.kt +++ b/src/main/kotlin/com/github/fmueller/jarvis/ai/OllamaService.kt @@ -44,15 +44,22 @@ object OllamaService { } @OptIn(ExperimentalCoroutinesApi::class) - suspend fun chat(conversation: Conversation): String = withContext(Dispatchers.IO) { + suspend fun chat(conversation: Conversation, useCodeContext: Boolean): String = withContext(Dispatchers.IO) { // TODO check if model is available // TODO if not, download model + var lastUserMessage = if (conversation.getLastUserMessage() != null) + if (useCodeContext) + conversation.getLastUserMessage()!!.contentWithCodeContext() + else + conversation.getLastUserMessage()!!.contentWithClosedTrailingCodeBlock() + else "Tell me that there was no message provided." + val responseInFlight = StringBuilder() try { suspendCancellableCoroutine { continuation -> assistant - .chat(conversation.getLastUserMessage()?.contentWithCodeContext() ?: "Tell me that there was no message provided.") + .chat(lastUserMessage.removePrefix("/plain ")) .onNext { update -> responseInFlight.append(update) conversation.addToMessageBeingGenerated(update) diff --git a/src/main/kotlin/com/github/fmueller/jarvis/commands/ChatCommand.kt b/src/main/kotlin/com/github/fmueller/jarvis/commands/ChatCommand.kt index 3345356..41b5cc0 100644 --- a/src/main/kotlin/com/github/fmueller/jarvis/commands/ChatCommand.kt +++ b/src/main/kotlin/com/github/fmueller/jarvis/commands/ChatCommand.kt @@ -14,7 +14,7 @@ class ChatCommand : SlashCommand { return conversation } - val response = OllamaService.chat(conversation).trim() + val response = OllamaService.chat(conversation, true).trim() conversation.addMessage(Message.fromAssistant(response)) return conversation } diff --git a/src/main/kotlin/com/github/fmueller/jarvis/commands/PlainChatCommand.kt b/src/main/kotlin/com/github/fmueller/jarvis/commands/PlainChatCommand.kt new file mode 100644 index 0000000..011e75a --- /dev/null +++ b/src/main/kotlin/com/github/fmueller/jarvis/commands/PlainChatCommand.kt @@ -0,0 +1,21 @@ +package com.github.fmueller.jarvis.commands + +import com.github.fmueller.jarvis.ai.OllamaService +import com.github.fmueller.jarvis.conversation.Conversation +import com.github.fmueller.jarvis.conversation.Message + +class PlainChatCommand : SlashCommand { + + override suspend fun run(conversation: Conversation): Conversation { + if (!OllamaService.isAvailable()) { + conversation.addMessage( + Message.fromAssistant("I can't access Ollama at ```http://localhost:11434```. You need to install it first and download the ```llama3.1``` model.") + ) + return conversation + } + + val response = OllamaService.chat(conversation, false).trim() + conversation.addMessage(Message.fromAssistant(response)) + return conversation + } +} diff --git a/src/main/kotlin/com/github/fmueller/jarvis/commands/SlashCommandParser.kt b/src/main/kotlin/com/github/fmueller/jarvis/commands/SlashCommandParser.kt index 9141677..af54d77 100644 --- a/src/main/kotlin/com/github/fmueller/jarvis/commands/SlashCommandParser.kt +++ b/src/main/kotlin/com/github/fmueller/jarvis/commands/SlashCommandParser.kt @@ -12,6 +12,10 @@ object SlashCommandParser { return NewConversationCommand() } + if (trimmedMessage.startsWith("/plain ")) { + return PlainChatCommand() + } + return ChatCommand() } } diff --git a/src/main/kotlin/com/github/fmueller/jarvis/conversation/Conversation.kt b/src/main/kotlin/com/github/fmueller/jarvis/conversation/Conversation.kt index c59f276..7102ecb 100644 --- a/src/main/kotlin/com/github/fmueller/jarvis/conversation/Conversation.kt +++ b/src/main/kotlin/com/github/fmueller/jarvis/conversation/Conversation.kt @@ -37,6 +37,7 @@ data class Message( - ```/help``` or ```/?``` - Shows this help message - ```/new``` - Starts a new conversation + - ```/plain``` - Sends a chat message without code context """.trimIndent() )