Skip to content

Commit

Permalink
feat: Add plain chat command without code context
Browse files Browse the repository at this point in the history
  • Loading branch information
fmueller committed Sep 12, 2024
1 parent e4431e2 commit 80f792a
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 3 deletions.
11 changes: 9 additions & 2 deletions src/main/kotlin/com/github/fmueller/jarvis/ai/OllamaService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> { 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ object SlashCommandParser {
return NewConversationCommand()
}

if (trimmedMessage.startsWith("/plain ")) {
return PlainChatCommand()
}

return ChatCommand()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
)

Expand Down

0 comments on commit 80f792a

Please sign in to comment.