Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding pdf function as part of Scala DSL #130

Merged
merged 3 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
package com.xebia.functional.xef.scala.auto

import com.xebia.functional.xef.scala.auto.*
import com.xebia.functional.xef.scala.auto.ScalaSerialDescriptorContext.given
import io.circe.Decoder
import io.circe.parser.decode

import scala.io.StdIn.readLine

private final case class AIResponse(answer: String) derives ScalaSerialDescriptor, Decoder

val pdfUrl = "https://people.cs.ksu.edu/~schmidt/705a/Scala/Programming-in-Scala.pdf"
Yawolf marked this conversation as resolved.
Show resolved Hide resolved
@main def runPDFDocument: Unit =
ai {
while (true) {
println("Enter your question: ")
val line = scala.io.StdIn.readLine()
val response = prompt[AIResponse](line)
println(s"${response.answer}\n---\n")
contextScope(pdf(resource = pdfUrl)) {
while (true) {
println("Enter your question: ")
val line = scala.io.StdIn.readLine()
val response = prompt[AIResponse](line)
println(s"${response.answer}\n---\n")
}
}
}
2 changes: 2 additions & 0 deletions scala/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ plugins {

dependencies {
implementation(projects.xefCore)
implementation(projects.xefTokenizer)
implementation(projects.xefPdf)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO, these deps shouldn't be part of the core. However, let's tackle this dependencies/module organization in a separate effort, globally to the project

implementation(projects.kotlinLoom)
implementation(libs.kotlinx.coroutines)
implementation(libs.circe.parser)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ object KotlinXSerializers:

val char: KSerializer[Character] =
serializer(kotlin.jvm.internal.CharCompanionObject.INSTANCE)

val unit: KSerializer[kotlin.Unit] =
serializer(kotlin.Unit.INSTANCE)
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ object ScalaSerialDescriptor:
case _: (Int *: t) => KotlinXSerializers.int.getDescriptor :: getSerialDescriptor[t]
case _: (Long *: t) => KotlinXSerializers.long.getDescriptor :: getSerialDescriptor[t]
case _: (Short *: t) => KotlinXSerializers.short.getDescriptor :: getSerialDescriptor[t]
case _: (Unit *: t) => KotlinXSerializers.unit.getDescriptor :: getSerialDescriptor[t]
case _: (h *: t) => summonInline[ScalaSerialDescriptor[h]].serialDescriptor :: getSerialDescriptor[t]

inline final def derived[A](using inline m: Mirror.Of[A]): ScalaSerialDescriptor[A] = new ScalaSerialDescriptor[A]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import kotlin.jvm.internal.Reflection
import kotlin.reflect.KClass
import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.BuiltinSerializersKt
import kotlinx.serialization.descriptors.SerialDescriptor

import scala.compiletime.summonInline
import scala.reflect.ClassTag
Expand Down Expand Up @@ -60,3 +61,6 @@ object ScalaSerialDescriptorContext:

given ScalaSerialDescriptor[String] = new ScalaSerialDescriptor[String]:
def serialDescriptor = KotlinXSerializers.string.getDescriptor

given ScalaSerialDescriptor[Unit] = new ScalaSerialDescriptor[Unit]:
override def serialDescriptor: SerialDescriptor = KotlinXSerializers.unit.getDescriptor
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ import io.circe.parser.decode
import io.circe.{Decoder, Json}
import io.circe.parser.parse
import com.xebia.functional.xef.auto.{AIException, AIKt, AIScope as KtAIScope, Agent as KtAgent}
import com.xebia.functional.xef.textsplitters.TextSplitter
import com.xebia.functional.xef.textsplitters.TokenTextSplitterKt.TokenTextSplitter
import com.xebia.functional.xef.pdf.PDFLoaderKt
import com.xebia.functional.tokenizer.ModelType

import java.io.File
import scala.jdk.CollectionConverters.*

package object auto {
Expand Down Expand Up @@ -73,4 +78,15 @@ package object auto {
KtAgent.promptMessage(scope.kt, prompt, llmModel, user, echo, n, temperature, bringFromContext, minResponseTokens, _)
).asScala.toList

def pdf(
resource: String | File,
splitter: TextSplitter = TokenTextSplitter(ModelType.GPT_3_5_TURBO, 100, 50)
)(using scope: AIScope): List[String] =
LoomAdapter
.apply[java.util.List[String]](count =>
resource match
case url: String => PDFLoaderKt.pdf(url, splitter, count)
case file: File => PDFLoaderKt.pdf(file, splitter, count)
).asScala.toList

}