Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
acharneski committed Nov 12, 2023
1 parent 8488faa commit 8cd0de0
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.github.simiacryptus.aicoder.config.AppSettingsState
import com.github.simiacryptus.aicoder.util.UITools
import com.intellij.openapi.progress.ProgressIndicator
import com.intellij.openapi.project.Project
import com.simiacryptus.skyenet.webui.WebSocketServer
import com.simiacryptus.skyenet.sessions.WebSocketServer
import org.eclipse.jetty.server.Server
import org.eclipse.jetty.server.handler.ContextHandlerCollection
import org.eclipse.jetty.webapp.WebAppContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class CodeChatAction : BaseAction() {
val language = ComputerLanguage.getComputerLanguage(e)?.name ?: return
val server = AppServer.getServer(e.project)
val uuid = UUID.randomUUID().toString()
server.addApp("/$uuid", CodeChatServer(e.project!!, language, selectedText))
server.addApp("/$uuid", CodeChatServer(e.project!!, language, selectedText, api = api))
Thread {
Thread.sleep(500)
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,37 @@ package com.github.simiacryptus.aicoder.actions.dev
import com.github.simiacryptus.aicoder.config.AppSettingsState
import com.intellij.openapi.project.Project
import com.simiacryptus.openai.OpenAIClient
import com.simiacryptus.skyenet.webui.*
import com.simiacryptus.skyenet.sessions.*
import com.simiacryptus.skyenet.util.ClasspathResource
import com.simiacryptus.skyenet.util.MarkdownUtil
import org.eclipse.jetty.util.resource.Resource
import org.intellij.lang.annotations.Language

class CodeChatServer(
val project: Project,
val language: String,
val codeSelection: String,
) : BasicChat(
val api: OpenAIClient,
) : ChatApplicationBase(
applicationName = "Code Chat",
model = AppSettingsState.instance.defaultChatModel()
) {

private val rootOperationID = (0..5).map { ('a'..'z').random() }.joinToString("")
private var rootMessageTrail: String = ""

override fun newSession(sessionId: String): CodeChatSession {
val newSession = CodeChatSession(sessionId)
rootMessageTrail =
"""$rootOperationID,<div><h3>Code:</h3><pre><code class="language-$language">${htmlEscape(codeSelection)}</code></pre></div>"""
newSession.send(rootMessageTrail)
return newSession
}

private fun htmlEscape(html: String): String {
return html.replace("&", "&amp;").replace("<", "&lt;")
.replace(">", "&gt;").replace("\"", "&quot;")
.replace("'", "&#039;")
}

open inner class CodeChatSession(sessionId: String) : BasicChatSession(
parent = this@CodeChatServer,
model = model,
sessionId = sessionId
) {
override fun run(userMessage: String) {
var messageTrail = divInitializer()
send("""$messageTrail<div>$userMessage</div><div>$spinner</div>""")
messages += OpenAIClient.ChatMessage(OpenAIClient.ChatMessage.Role.user, userMessage)
val response = api.chat(chatRequest, model).choices.first().message?.content.orEmpty()
messages += OpenAIClient.ChatMessage(OpenAIClient.ChatMessage.Role.assistant, response)
messageTrail += MarkdownUtil.renderMarkdown(response)
send(messageTrail)
}

override val messages = listOf(
OpenAIClient.ChatMessage(
OpenAIClient.ChatMessage.Role.system, """
override fun newSession(sessionId: String): ChatSession {
val newSession = ChatSession(
sessionId = sessionId,
parent = this@CodeChatServer,
model = AppSettingsState.instance.defaultChatModel(),
api = api,
visiblePrompt = """
<div><h3>Code:</h3>
<pre><code class="language-$language">${htmlEscape(codeSelection)}</code></pre>
</div>
""".trimIndent().trim(),
hiddenPrompt = "",
systemPrompt = """
|You are a helpful AI that helps people with coding.
|
|You will be answering questions about the following code:
Expand All @@ -59,22 +43,35 @@ rootMessageTrail =
|```
|
|Responses may use markdown formatting.
""".trimMargin()
)
).toMutableList()
""".trimMargin(),
)
@Language("HTML") val html = """
<div><h3>Code:</h3>
<pre><code class="language-$language">${htmlEscape(codeSelection)}</code></pre>
</div>
""".trimIndent().trim()
rootMessageTrail = """$rootOperationID,$html"""
newSession.send(rootMessageTrail)
return newSession
}

private val chatRequest: OpenAIClient.ChatRequest
get() {
val chatRequest = OpenAIClient.ChatRequest()
val model = AppSettingsState.instance.defaultChatModel()
chatRequest.model = model.modelName
chatRequest.temperature = AppSettingsState.instance.temperature
chatRequest.messages = messages.toTypedArray()
return chatRequest
}
override fun processMessage(
sessionId: String,
userMessage: String,
session: PersistentSessionBase,
sessionDiv: SessionDiv,
socket: MessageWebSocket
) {
TODO("Not yet implemented")
}

private fun htmlEscape(html: String): String {
return html.replace("&", "&amp;").replace("<", "&lt;")
.replace(">", "&gt;").replace("\"", "&quot;")
.replace("'", "&#039;")
}

override val baseResource: Resource
get() = ClasspathResource(javaClass.classLoader.getResource(resourceBase))
get() = ClasspathResource(javaClass.classLoader.getResource(resourceBase)!!)

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class IdeaOpenAIClient : OpenAIClient(
) {
private val isInRequest = AtomicBoolean(false)

override fun incrementTokens(totalTokens: Int) {
AppSettingsState.instance.tokenCounter += totalTokens
override fun incrementTokens(model: Model?, tokens: Int) {
AppSettingsState.instance.tokenCounter += tokens
}

override fun authorize(request: HttpRequestBase) {
Expand Down

0 comments on commit 8cd0de0

Please sign in to comment.