Skip to content

Commit

Permalink
try to test Open AI
Browse files Browse the repository at this point in the history
  • Loading branch information
VadimIgnatiev committed Dec 17, 2023
1 parent cea5919 commit 10dd236
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 11 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,17 @@ repositories {
mavenCentral()
}

// Dependencies are managed with Gradle version catalog - read more: https://docs.gradle.org/current/userguide/platforms.html#sub:version-catalog
//Dependencies are managed with Gradle version catalog - read more: https://docs.gradle.org/current/userguide/platforms.html#sub:version-catalog
dependencies {
// implementation(libs.annotations)
implementation(libs.openai.client)
implementation(libs.coroutines)
// implementation(libs.ktor.core)
// implementation(libs.ktor.cio)
implementation(libs.slf4j.api)
implementation(libs.slf4j.simple)
}


// Set the JVM language level used to build the project. Use Java 11 for 2020.3+, and Java 17 for 2022.2+.
kotlin {
@Suppress("UnstableApiUsage")
Expand Down Expand Up @@ -120,6 +126,9 @@ tasks {
password = environment("PRIVATE_KEY_PASSWORD")
}

runIde {
systemProperty("idea.version", "2023.3")
}
/*
publishPlugin {
dependsOn("patchChangelog")
Expand Down
4 changes: 0 additions & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,3 @@ org.gradle.caching = true

# Enable Gradle Kotlin DSL Lazy Property Assignment -> https://docs.gradle.org/current/userguide/kotlin_dsl.html#kotdsl:assignment
systemProp.org.gradle.unsafe.kotlin.assignment = true

runIde {
ideDir = 'C:\\Program Files\\JetBrains\\JetBrains Rider 2023.3'
}
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
annotations = "24.1.0"

# plugins
kotlin = "1.9.21"
kotlin = "1.9.0"
changelog = "2.2.0"
gradleIntelliJPlugin = "1.16.1"
qodana = "0.1.13"
Expand Down
33 changes: 33 additions & 0 deletions out/production/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!-- Plugin Configuration File. Read more: https://plugins.jetbrains.com/docs/intellij/plugin-configuration-file.html -->
<idea-plugin>
<id>com.github.vadymignatiev.octopusrider</id>
<name>OctopusRider Template</name>
<vendor>vadymignatiev</vendor>

<depends>com.intellij.modules.platform</depends>

<resource-bundle>messages.MyBundle</resource-bundle>

<extensions defaultExtensionNs="com.intellij">
<toolWindow factoryClass="com.github.vadymignatiev.octopusrider.toolWindow.MyToolWindowFactory" id="MyToolWindow"/>
</extensions>

<actions>

<action id="OrderFood" class="com.github.vadymignatiev.octopusrider.OrderFoodAction"
text="Order Food!">

<add-to-group group-id="ToolbarRunGroup" anchor="last"/>
</action>

<action id="ChatAction" class="com.github.vadymignatiev.octopusrider.ChatAction" text="Open Chat Window"
description="Opens the chat window">
<!-- <add-to-group group-id="ToolsMenu" anchor="last"/>-->
</action>

</actions>

<applicationListeners>
<listener class="com.github.vadymignatiev.octopusrider.listeners.MyApplicationActivationListener" topic="com.intellij.openapi.application.ApplicationActivationListener"/>
</applicationListeners>
</idea-plugin>
3 changes: 3 additions & 0 deletions out/production/resources/messages/MyBundle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
projectService=Project service: {0}
randomLabel=The random number is: {0}
shuffle=Shuffle
15 changes: 15 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,18 @@ plugins {
}

rootProject.name = "OctopusRider"

dependencyResolutionManagement {
versionCatalogs {
create("libs") {
library("openai-client", "com.aallam.openai:openai-client:3.6.1")
library("coroutines", "org.jetbrains.kotlinx:kotlinx-coroutines-swing:1.7.2")
// library("ktor-core", "io.ktor:ktor-client-core:2.3.7")
// library("ktor-cio", "io.ktor:ktor-client-cio:2.3.7")
library("slf4j-api", "org.slf4j:slf4j-api:1.7.35")
library("slf4j-simple", "org.slf4j:slf4j-simple:1.7.35")
}
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent

class ChatAction : AnAction("Chat Action") {
override fun actionPerformed(e: AnActionEvent) {
val chatWindow = ChatWindow()
chatWindow.isVisible = true
override fun actionPerformed(event: AnActionEvent) {
val presenter = ChatPresenter()
presenter.window.isVisible = true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.github.vadymignatiev.octopusrider

import com.aallam.openai.api.chat.*
import com.aallam.openai.api.model.ModelId
import com.aallam.openai.client.OpenAI
import kotlinx.serialization.json.*

Check warning on line 6 in src/main/kotlin/com/github/vadymignatiev/octopusrider/ChatPresenter.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused import directive

Unused import directive
import java.awt.Color
import javax.swing.SwingWorker

Check warning on line 8 in src/main/kotlin/com/github/vadymignatiev/octopusrider/ChatPresenter.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused import directive

Unused import directive
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class ChatPresenter {
private val token = "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"
private val openAI = OpenAI(token)
private val modelId = ModelId("gpt-3.5-turbo-1106")
private val chatMessages = mutableListOf<ChatMessage>()

val coroutineScope = CoroutineScope(Dispatchers.Main)

val window = ChatWindow(this)

init { window.isVisible = true }

fun sendMessage(userMessage: String) {
val userChatMessage = ChatMessage(ChatRole.User, userMessage)
chatMessages.add(userChatMessage)

coroutineScope.launch {
val serviceResponse = sendToService(userMessage)
window.appendMessage(serviceResponse, Color.BLUE)
val serviceChatMessage = ChatMessage(ChatRole.System, serviceResponse)
chatMessages.add(serviceChatMessage)
}
}

private suspend fun sendToService(message: String): String {
val request = chatCompletionRequest {
model = modelId
messages = chatMessages
}
val response = openAI.chatCompletion(request)
return response.choices.first().message.content.orEmpty()
}

}
Original file line number Diff line number Diff line change
@@ -1,24 +1,55 @@
package com.github.vadymignatiev.octopusrider

import java.awt.*
import javax.swing.*
import javax.swing.text.SimpleAttributeSet
import javax.swing.text.StyleConstants
import java.awt.BorderLayout
import java.awt.Color
import java.awt.event.WindowAdapter
import java.awt.event.WindowEvent
import kotlinx.coroutines.*
import kotlinx.coroutines.swing.Swing

Check warning on line 11 in src/main/kotlin/com/github/vadymignatiev/octopusrider/ChatWindow.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused import directive

Unused import directive
import com.aallam.openai.api.chat.*

Check warning on line 12 in src/main/kotlin/com/github/vadymignatiev/octopusrider/ChatWindow.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused import directive

Unused import directive
import com.aallam.openai.api.model.ModelId

Check warning on line 13 in src/main/kotlin/com/github/vadymignatiev/octopusrider/ChatWindow.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused import directive

Unused import directive
import com.aallam.openai.client.OpenAI

Check warning on line 14 in src/main/kotlin/com/github/vadymignatiev/octopusrider/ChatWindow.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused import directive

Unused import directive
import kotlinx.serialization.json.*

Check warning on line 15 in src/main/kotlin/com/github/vadymignatiev/octopusrider/ChatWindow.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused import directive

Unused import directive

class ChatWindow(val presenter: ChatPresenter) : JFrame() {

private val messageArea = JTextPane()

class ChatWindow : JFrame() {
init {
title = "Chat Window"
setSize(400, 300)
setSize(800, 600)
defaultCloseOperation = JFrame.DISPOSE_ON_CLOSE

Check warning on line 24 in src/main/kotlin/com/github/vadymignatiev/octopusrider/ChatWindow.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Redundant qualifier name

Redundant qualifier name
contentPane.layout = BorderLayout()
val messageArea = JTextArea()
messageArea.isEditable = false
contentPane.add(JScrollPane(messageArea), BorderLayout.CENTER)

val textField = JTextField()
textField.addActionListener {
val text = textField.text
messageArea.append(text + "\n")
appendMessage(text, Color.BLACK)
presenter.sendMessage(text)
textField.text = ""
}

contentPane.add(textField, BorderLayout.SOUTH)

addWindowListener(object : WindowAdapter() {
override fun windowClosing(e: WindowEvent?) {
presenter.coroutineScope.cancel()
super.windowClosing(e)
}
})

pack()
}

fun appendMessage(message: String, color: Color) {
val doc = messageArea.styledDocument
val style = SimpleAttributeSet()
StyleConstants.setForeground(style, color)
doc.insertString(doc.length, "$message\n", style)
}
}

0 comments on commit 10dd236

Please sign in to comment.