-
Notifications
You must be signed in to change notification settings - Fork 15
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
examples/scala/src/main/scala/com/xebia/functional/xef/scala/auto/SynopsisReview.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
22 changes: 22 additions & 0 deletions
22
scala/src/main/scala/com/xebia/functional/xef/scala/prompt/PromptTemplate.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
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)) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 👍
There was a problem hiding this comment.
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.