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

Scala prompt templating #157

Merged
merged 5 commits into from
Jun 2, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
@@ -0,0 +1,47 @@
package com.xebia.functional.xef.scala.auto

import com.xebia.functional.xef.scala.auto.*
import com.xebia.functional.xef.scala.prompt.*
import io.circe.Decoder

final case class Play(title: String, era: String) derives PromptTemplate, Decoder, ScalaSerialDescriptor

final case class Synopsis(summary: String) derives PromptTemplate, Decoder, ScalaSerialDescriptor

final case class Review(review: String) derives PromptTemplate, Decoder, ScalaSerialDescriptor

final case class Score(score: Double) derives PromptTemplate, Decoder, ScalaSerialDescriptor

@main def runSynopsisReview: Unit = {
def synopsisTemplate(play: Play): String =
s"""
|You are a playwright. Given the title of play and the era it is set in, it is your job to write a synopsis for that title.
|
|Title: ${play.title}.
|Era: ${play.era}.
|Playwright: This is a synopsis for the above play:
""".stripMargin

def reviewTemplate(synopsis: Synopsis): String =
s"""
|You are a play critic from the New York Times. Given the synopsis of play, it is your job to write a review for that play.
|
|Play Synopsis: ${synopsis.summary}.
|Review from a New York Times play critic of the above play:
""".stripMargin

def scoreTemplate(review: Review): String =
s"""
|You are an independent play critic scoring the best plays of the year.
|Given the review of the play, it is your job to score this play.
|
|Play Review: ${review.review}.
|Score the above play from 0 to 10:
""".stripMargin

val playScore = PromptTemplate[Play](Play("The power of Zuluastral", "Modern Era"))
.chain[Synopsis](play => synopsisTemplate(play))
.chain[Review](synopsis => reviewTemplate(synopsis))
.chain[Score](review => scoreTemplate(review))
println(s"Score (0 to 10): " + playScore.score)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.xebia.functional.xef.scala.prompt

import com.xebia.functional.xef.scala.auto.*
import io.circe.Decoder

trait PromptTemplate[A] {

def chain(template: String): A
Copy link
Contributor

Choose a reason for hiding this comment

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

This functionality should be developed in Kotlin multiplatform and mapped to other lang bindings. @juanpedromoreno @necosta What criteria are we following to decide what gets developed in Kotlin vs. Scala?

Copy link
Contributor

Choose a reason for hiding this comment

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

Hello @raulraja, I agree with your observation. Nelson and I have had an offline discussion regarding this matter. The Scala API was unable to create prompts beyond utilizing string interpolations, unlike Kotlin does here. Due to this limitation, we ended up creating different designs without considering the possible divergence. I suggest we work on unifying the APIs once again 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will try to move this logic to Kotlin. Scala should follow the Kotlin API yes, it was just overlooked.


extension (a: A) {
def chain[B: ScalaSerialDescriptor: Decoder](template: A => String): B =
ai(prompt[B](template(a)))
necosta marked this conversation as resolved.
Show resolved Hide resolved
}
}

object PromptTemplate:

def apply[A](instance: A): A = instance

inline final def derived[A: ScalaSerialDescriptor: Decoder]: PromptTemplate[A] = new PromptTemplate[A]:

def chain(template: String): A = ai(prompt[A](template))