Skip to content

Commit

Permalink
Add simple question answering ui and llm interaction
Browse files Browse the repository at this point in the history
Also, remove some generated template example code
  • Loading branch information
fmueller committed Apr 28, 2024
1 parent 9530922 commit 0c8725d
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 77 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,41 @@ class OllamaService : Disposable {
}
}

// TODO make it non-blocking
fun ask(question: String): String = runBlocking {
// TODO check if model is available
// TODO if not download model
try {
val client = HttpClient.newHttpClient()
val request = HttpRequest.newBuilder()
.uri(URI.create("http://localhost:11434/api/chat"))
.POST(
HttpRequest.BodyPublishers.ofString(
"""
{
"model": "llama3",
"messages": [
{
"role": "user",
"content": "$question"
}
],
"stream": false
}
""".trimIndent()
)
)
.build()

val response = client.send(request, HttpResponse.BodyHandlers.ofString())
val body = response.body()
thisLogger().warn("Response: $body")
body
} catch (e: Exception) {
"Error: ${e.message}"
}
}

override fun dispose() {
process.stop()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package com.github.fmueller.jarvis.toolWindow

import com.github.fmueller.jarvis.MyBundle
import com.github.fmueller.jarvis.services.MyProjectService
import com.github.fmueller.jarvis.services.OllamaService
import com.intellij.openapi.components.service
import com.intellij.openapi.project.Project
import com.intellij.openapi.wm.ToolWindow
import com.intellij.openapi.wm.ToolWindowFactory
import com.intellij.ui.components.JBLabel
import com.intellij.ui.components.JBPanel
import com.intellij.ui.components.JBTextArea
import com.intellij.ui.content.ContentFactory
import javax.swing.JButton
import com.intellij.util.ui.components.BorderLayoutPanel
import java.awt.event.KeyAdapter
import java.awt.event.KeyEvent

class ConversationWindowFactory : ToolWindowFactory {

Expand All @@ -24,17 +24,27 @@ class ConversationWindowFactory : ToolWindowFactory {

class ConversationWindow(toolWindow: ToolWindow) {

private val ollama = toolWindow.project.service<OllamaService>()
private val service = toolWindow.project.service<MyProjectService>()

fun getContent() = JBPanel<JBPanel<*>>().apply {
val label = JBLabel(MyBundle.message("randomLabel", "?"))

add(label)
add(JButton(MyBundle.message("shuffle")).apply {
addActionListener {
label.text = MyBundle.message("randomLabel", service.getRandomNumber())
}
private val project = toolWindow.project
private val ollama = project.service<OllamaService>()

fun getContent() = BorderLayoutPanel().apply {
val label =
JBLabel("I am Jarvis, your personal coding assistant. I try to be helpful, but I am not perfect.")
val responseArea = JBTextArea()

addToTop(label)
addToCenter(responseArea)
addToBottom(JBTextArea().apply {
text = "Please enter your question"
addKeyListener(object : KeyAdapter() {
override fun keyPressed(e: KeyEvent) {
if (e.keyCode == KeyEvent.VK_ENTER) {
val question = text
text = ""
responseArea.text = ollama.ask(question)
}
}
})
})
}
}
Expand Down
39 changes: 0 additions & 39 deletions src/test/kotlin/com/github/fmueller/jarvis/MyPluginTest.kt

This file was deleted.

3 changes: 0 additions & 3 deletions src/test/testData/rename/foo.xml

This file was deleted.

3 changes: 0 additions & 3 deletions src/test/testData/rename/foo_after.xml

This file was deleted.

0 comments on commit 0c8725d

Please sign in to comment.