Skip to content

Commit

Permalink
Push works
Browse files Browse the repository at this point in the history
  • Loading branch information
cheroliv committed Sep 25, 2024
1 parent a220577 commit 72d3e9e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 23 deletions.
21 changes: 21 additions & 0 deletions buildSrc/src/main/kotlin/school/ai/AssistantManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package school.ai
import arrow.core.Either
import arrow.core.Either.Companion.catch
import arrow.core.Either.Left
import arrow.core.Either.Right
import arrow.core.getOrElse
import dev.langchain4j.data.message.AiMessage
import dev.langchain4j.model.StreamingResponseHandler
import dev.langchain4j.model.chat.StreamingChatLanguageModel
import dev.langchain4j.model.ollama.OllamaChatModel
import dev.langchain4j.model.ollama.OllamaStreamingChatModel
import dev.langchain4j.model.output.Response
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.suspendCancellableCoroutine
import org.gradle.api.Project
import java.io.File
Expand Down Expand Up @@ -95,4 +97,23 @@ object AssistantManager {
})
}
}

fun Project.runChat(model: String) {
createOllamaChatModel(model = model)
.run { generate(userMessage).let(::println) }
}

fun Project.runStreamChat(model: String) {
runBlocking {
createOllamaStreamingChatModel(model).run {
when (val answer = generateStreamingResponse(this, userMessage)) {
is Right -> "Complete response received:\n${
answer.value.content().text()
}".run(::println)

is Left -> "Error during response generation:\n${answer.value}".run(::println)
}
}
}
}
}
28 changes: 5 additions & 23 deletions buildSrc/src/main/kotlin/school/ai/AssistantPlugin.kt
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
package school.ai

import arrow.core.Either.Left
import arrow.core.Either.Right
import dev.langchain4j.model.openai.OpenAiChatModel
import kotlinx.coroutines.runBlocking
import org.gradle.api.Plugin
import org.gradle.api.Project
import school.ai.AssistantManager.apiKey
import school.ai.AssistantManager.createOllamaChatModel
import school.ai.AssistantManager.createOllamaStreamingChatModel
import school.ai.AssistantManager.generateStreamingResponse
import school.ai.AssistantManager.localModels
import school.ai.AssistantManager.runChat
import school.ai.AssistantManager.runStreamChat
import school.ai.AssistantManager.userMessage

class AssistantPlugin : Plugin<Project> {

override fun apply(project: Project) {

project.run {
task("displayAIPrompt") {
group = "school-ai"
Expand Down Expand Up @@ -46,10 +43,7 @@ class AssistantPlugin : Plugin<Project> {
task(taskName) {
group = "school-ai"
description = "Display the Ollama $model chatgpt prompt request."
doFirst {
createOllamaChatModel(model = model)
.run { generate(userMessage).let(::println) }
}
doFirst { runChat(model) }
}
}

Expand All @@ -58,19 +52,7 @@ class AssistantPlugin : Plugin<Project> {
task(taskName) {
group = "school-ai"
description = "Display the Ollama $model chatgpt stream prompt request."
doFirst {
runBlocking {
createOllamaStreamingChatModel(model).run {
when (val answer = generateStreamingResponse(this, userMessage)) {
is Right -> "Complete response received:\n${
answer.value.content().text()
}".run(::println)

is Left -> "Error during response generation:\n${answer.value}".run(::println)
}
}
}
}
doFirst { runStreamChat(model) }
}
}

Expand Down

0 comments on commit 72d3e9e

Please sign in to comment.