-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
334 additions
and
463 deletions.
There are no files selected for viewing
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
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
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
10 changes: 10 additions & 0 deletions
10
examples/scala/src/main/scala/com/xebia/functional/xef/examples/scala/Simple.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,10 @@ | ||
package com.xebia.functional.xef.examples.scala | ||
|
||
import com.xebia.functional.xef.scala.conversation.* | ||
|
||
@main def runBooks(): Unit = conversation: | ||
val topic = "functional programming" | ||
val topBook: String = promptMessage(s"Give me the top-selling book about $topic") | ||
println(topBook) | ||
val selectedBooks: List[String] = promptMessages(s"Give me a selection of books about $topic") | ||
println(selectedBooks.mkString("\n")) |
45 changes: 45 additions & 0 deletions
45
...scala/src/main/scala/com/xebia/functional/xef/examples/scala/context/serpapi/Simple.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,45 @@ | ||
package com.xebia.functional.xef.examples.scala.context.serpapi | ||
|
||
import com.xebia.functional.xef.conversation.llm.openai.* | ||
import com.xebia.functional.xef.reasoning.serpapi.* | ||
import com.xebia.functional.xef.scala.conversation.* | ||
import com.xebia.functional.xef.scala.serialization.* | ||
import io.circe.Decoder | ||
|
||
import java.text.SimpleDateFormat | ||
import java.util.Date | ||
|
||
val openAI: OpenAI = OpenAI.FromEnvironment | ||
|
||
val sdf = SimpleDateFormat("dd/M/yyyy") | ||
def currentDate: String = sdf.format(new Date) | ||
|
||
def setContext(query: String)(using conversation: ScalaConversation): Unit = | ||
addContext(Search(openAI.DEFAULT_CHAT, conversation, 3).search(query).get) | ||
|
||
case class BreakingNews(summary: String) derives SerialDescriptor, Decoder | ||
|
||
case class MarketNews(news: String, risingStockSymbols: List[String], fallingStockSymbols: List[String]) derives SerialDescriptor, Decoder | ||
|
||
case class Estimate(number: Long) derives SerialDescriptor, Decoder | ||
|
||
@main def runWeather(): Unit = conversation: | ||
setContext("Weather in Cádiz, Spain") | ||
val question = "Knowing this forecast, what clothes do you recommend I should wear if I live in Cádiz?" | ||
val answer = promptMessage(question) | ||
println(answer) | ||
|
||
@main def runBreakingNews(): Unit = conversation: | ||
setContext(s"$currentDate COVID News") | ||
val BreakingNews(summary) = prompt[BreakingNews](s"Write a summary of about 300 words given the provided context.") | ||
println(summary) | ||
|
||
@main def runMarketNews(): Unit = conversation: | ||
setContext(s"$currentDate Stock market results, rising stocks, falling stocks") | ||
val news = prompt[MarketNews]("Write a short summary of the stock market results given the provided context.") | ||
println(news) | ||
|
||
@main def runFermiEstimate(): Unit = conversation: | ||
setContext("Estimate the number of medical needles in the world") | ||
val Estimate(needlesInWorld) = prompt[Estimate]("Answer the question with an integer number given the provided context.") | ||
println(s"Needles in world: $needlesInWorld") |
20 changes: 20 additions & 0 deletions
20
.../src/main/scala/com/xebia/functional/xef/examples/scala/context/serpapi/UserQueries.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,20 @@ | ||
package com.xebia.functional.xef.examples.scala.context.serpapi | ||
|
||
import com.xebia.functional.xef.reasoning.pdf.PDF | ||
import com.xebia.functional.xef.scala.conversation.* | ||
import com.xebia.functional.xef.scala.serialization.* | ||
import io.circe.Decoder | ||
|
||
import scala.io.StdIn.readLine | ||
|
||
case class AIResponse(answer: String) derives SerialDescriptor, Decoder | ||
|
||
val PdfUrl = "https://people.cs.ksu.edu/~schmidt/705a/Scala/Programming-in-Scala.pdf" | ||
|
||
@main def runUserQueries(): Unit = conversation: | ||
val pdf = PDF(openAI.DEFAULT_CHAT, openAI.DEFAULT_SERIALIZATION, summon[ScalaConversation]) | ||
addContext(Array(pdf.readPDFFromUrl.readPDFFromUrl(PdfUrl).get)) | ||
while (true) | ||
println("Enter your question: ") | ||
val AIResponse(answer) = prompt[AIResponse](readLine()) | ||
println(s"$answer\n---\n") |
10 changes: 10 additions & 0 deletions
10
...ples/scala/src/main/scala/com/xebia/functional/xef/examples/scala/images/HybridCity.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,10 @@ | ||
package com.xebia.functional.xef.examples.scala.images | ||
|
||
import com.xebia.functional.xef.prompt.Prompt | ||
import com.xebia.functional.xef.scala.conversation.* | ||
import com.xebia.functional.xef.scala.serialization.* | ||
import io.circe.Decoder | ||
|
||
@main def runHybridCity(): Unit = conversation: | ||
val imageUrls = images(Prompt("A hybrid city of Cádiz, Spain and Seattle, US.")) | ||
println(imageUrls) |
20 changes: 20 additions & 0 deletions
20
.../scala/src/main/scala/com/xebia/functional/xef/examples/scala/iteration/AnimalStory.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,20 @@ | ||
package com.xebia.functional.xef.examples.scala.iteration | ||
|
||
import com.xebia.functional.xef.prompt.JvmPromptBuilder | ||
import com.xebia.functional.xef.scala.conversation.* | ||
import com.xebia.functional.xef.scala.serialization.* | ||
import io.circe.Decoder | ||
|
||
case class Animal(name: String, habitat: String, diet: String) derives SerialDescriptor, Decoder | ||
case class Invention(name: String, inventor: String, year: Int, purpose: String) derives SerialDescriptor, Decoder | ||
|
||
@main def runAnimalStory(): Unit = conversation: | ||
val animal = prompt[Animal]("A unique animal species") | ||
val invention = prompt[Invention]("A groundbreaking invention from the 20th century.") | ||
println(s"Animal: $animal") | ||
println(s"Invention: $invention") | ||
val builder = new JvmPromptBuilder() | ||
.addSystemMessage("You are a writer for a science fiction magazine.") | ||
.addUserMessage("Write a short story of 200 words that involves the animal and the invention.") | ||
val story = promptMessage(builder.build) | ||
println(story) |
52 changes: 52 additions & 0 deletions
52
...es/scala/src/main/scala/com/xebia/functional/xef/examples/scala/iteration/ChessGame.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,52 @@ | ||
package com.xebia.functional.xef.examples.scala.iteration | ||
|
||
import com.xebia.functional.xef.prompt.Prompt | ||
import com.xebia.functional.xef.scala.conversation.* | ||
import com.xebia.functional.xef.scala.serialization.* | ||
import io.circe.Decoder | ||
|
||
import scala.annotation.tailrec | ||
|
||
case class ChessMove(player: String, move: String) derives SerialDescriptor, Decoder | ||
case class ChessBoard(board: String) derives SerialDescriptor, Decoder | ||
case class GameState(ended: Boolean = false, winner: Option[String] = None) derives SerialDescriptor, Decoder | ||
|
||
@tailrec | ||
private def chessGame(moves: List[ChessMove] = Nil, gameState: GameState = new GameState)(using ScalaConversation): (String, ChessMove) = | ||
if !gameState.ended then | ||
val currentPlayer = if moves.size % 2 == 0 then "Player 1 (White)" else "Player 2 (Black)" | ||
val previousMoves = moves.map(m => m.player + ":" + m.move).mkString(", ") | ||
val movePrompt = moves match { | ||
case Nil => s""" | ||
|$currentPlayer, you are playing chess and it's your turn. | ||
|Make your first move: | ||
""".stripMargin | ||
case l => s""" | ||
|$currentPlayer, you are playing chess and it's your turn. | ||
|Here are the previous moves: $previousMoves | ||
|Make your next move: | ||
""".stripMargin | ||
} | ||
println(movePrompt) | ||
val move = prompt[ChessMove](movePrompt) | ||
println(s"Move is: $move") | ||
val boardPrompt = | ||
s""" | ||
|Given the following chess moves: $previousMoves, | ||
|generate a chess board on a table with appropriate emoji representations for each move and piece. | ||
|Add a brief description of the move and its implications. | ||
""".stripMargin | ||
val chessBoard = prompt[ChessBoard](boardPrompt) | ||
println(s"Current board:\n${chessBoard.board}") | ||
val gameStatePrompt = | ||
s""" | ||
|Given the following chess moves: ${moves.mkString(", ")}, | ||
|has the game ended (win, draw, or stalemate)? | ||
""".stripMargin | ||
val gameState = prompt[GameState](gameStatePrompt) | ||
chessGame(moves :+ move, gameState) | ||
else (gameState.winner.getOrElse("Something went wrong"), moves.last) | ||
|
||
@main def runChessGame(): Unit = conversation: | ||
val (winner, fMove) = chessGame() | ||
println(s"Game over. Final move: $fMove, Winner: $winner") |
18 changes: 18 additions & 0 deletions
18
...cala/src/main/scala/com/xebia/functional/xef/examples/scala/serialization/Annotated.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,18 @@ | ||
package com.xebia.functional.xef.examples.scala.serialization | ||
|
||
import com.xebia.functional.xef.prompt.Prompt | ||
import com.xebia.functional.xef.scala.conversation.* | ||
import com.xebia.functional.xef.scala.serialization.* | ||
import io.circe.Decoder | ||
|
||
@Description("A book") | ||
case class AnnotatedBook( | ||
@Description("The name of the book") name: String, | ||
@Description("The author of the book") author: String, | ||
@Description("A 50 word paragraph with a summary of this book") summary: String | ||
) derives SerialDescriptor, | ||
Decoder | ||
|
||
@main def runAnnotatedBook(): Unit = conversation: | ||
val book = prompt[AnnotatedBook]("To Kill a Mockingbird") | ||
println(book) |
Oops, something went wrong.