Skip to content

Commit

Permalink
Adding pdf function as part of Scala DSL (#130)
Browse files Browse the repository at this point in the history
* Adding  function as part of Scala DSL

* Deleted unneeded import

* Update examples/scala/src/main/scala/com/xebia/functional/xef/scala/auto/PDFDocument.scala

Co-authored-by: Juan Pedro Moreno <4879373+juanpedromoreno@users.noreply.github.com>

---------

Co-authored-by: yago <Yawolf@users.noreply.github.com>
Co-authored-by: Juan Pedro Moreno <4879373+juanpedromoreno@users.noreply.github.com>
  • Loading branch information
3 people authored May 30, 2023
1 parent 84e33b6 commit 51b1d65
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
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"

@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)
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

}

0 comments on commit 51b1d65

Please sign in to comment.