Skip to content

Commit

Permalink
thoughts improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
fwbrasil committed Dec 26, 2023
1 parent a6df1ee commit fb41b09
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 95 deletions.
124 changes: 62 additions & 62 deletions kyo-llm-bench/shared/src/main/scala/kyo/llm/bench/BigBenchHard.scala
Original file line number Diff line number Diff line change
@@ -1,62 +1,62 @@
package kyo.llm.bench

import kyo._
import kyo.ios._
import kyo.files._
import kyo.llm.ais._
import kyo.llm.KyoLLMApp
import kyo.direct._
import kyo.seqs.Seqs
import kyo.llm.configs.Config
import scala.util.matching.Regex

object BigBenchHard extends KyoLLMApp {

val asnwer: Regex = "So the answer is (.*?)\\.".r

override def config: Config =
super.config.apiKey("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")

case class Task(input: String, target: String)

case class Bench(name: String, total: Int, successes: Int, percent: Double)

run {
Seqs.run {
for {
cotPrompts <- loadCotPrompts
(bench, tasks) <- Seqs.get(loadBenchs)
task <- Seqs.get(tasks)
output <- AIs.ask(cotPrompts(bench), task.input)
} yield {
bench ->
(if (output.takeRight(task.target.size + 5).contains(task.target))
1
else
0)
}
}.map { r =>
r.groupBy(_._1).map {
case (name, seq) =>
val total = seq.size
val successes = seq.map(_._2).sum
Bench(name, total, successes, successes.toDouble / total)
}
}
}

def loadBenchs =
Seqs.traverse(
Files("BIG-Bench-Hard/bbh").readAll("json").map(_.filter(_._1 == "boolean_expressions"))
) {
case (name, content) =>
case class Examples(examples: List[Task])
Json.decode[Examples](content).map { e =>
name -> e.examples
}
}

def loadCotPrompts =
Files("BIG-Bench-Hard/cot-prompts").readAll("txt").map(_.toMap)

}
// package kyo.llm.bench

// import kyo._
// import kyo.ios._
// import kyo.files._
// import kyo.llm.ais._
// import kyo.llm.KyoLLMApp
// import kyo.direct._
// import kyo.seqs.Seqs
// import kyo.llm.configs.Config
// import scala.util.matching.Regex

// object BigBenchHard extends KyoLLMApp {

// val asnwer: Regex = "So the answer is (.*?)\\.".r

// override def config: Config =
// super.config.apiKey("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")

// case class Task(input: String, target: String)

// case class Bench(name: String, total: Int, successes: Int, percent: Double)

// run {
// Seqs.run {
// for {
// cotPrompts <- loadCotPrompts
// (bench, tasks) <- Seqs.get(loadBenchs)
// task <- Seqs.get(tasks)
// output <- AIs.ask(cotPrompts(bench), task.input)
// } yield {
// bench ->
// (if (output.takeRight(task.target.size + 5).contains(task.target))
// 1
// else
// 0)
// }
// }.map { r =>
// r.groupBy(_._1).map {
// case (name, seq) =>
// val total = seq.size
// val successes = seq.map(_._2).sum
// Bench(name, total, successes, successes.toDouble / total)
// }
// }
// }

// def loadBenchs =
// Seqs.traverse(
// Files("BIG-Bench-Hard/bbh").readAll("json").map(_.filter(_._1 == "boolean_expressions"))
// ) {
// case (name, content) =>
// case class Examples(examples: List[Task])
// Json.decode[Examples](content).map { e =>
// name -> e.examples
// }
// }

// def loadCotPrompts =
// Files("BIG-Bench-Hard/cot-prompts").readAll("txt").map(_.toMap)

// }
36 changes: 23 additions & 13 deletions kyo-llm-macros/shared/src/main/scala/kyo/llm/json/JsonSchema.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,49 +28,59 @@ object Schema {
"description" -> Json.Str(v)
}.distinct.toList

def constDesc: List[(String, Json)] =
List("description" -> Json.Str(
"Constant field, use the value in 'const'."
))

def convert(schema: ZSchema[_]): List[(String, Json)] = {
def desc = this.desc(schema.annotations)
schema match {

case ZSchema.Primitive(StandardType.StringType, Chunk(Const(v))) =>
constDesc ++ List("type" -> Json.Str("string"), "const" -> Json.Str(v.asInstanceOf[String]))
desc ++ List(
"type" -> Json.Str("string"),
"enum" -> Json.Arr(Json.Str(v.asInstanceOf[String]))
)

case ZSchema.Primitive(StandardType.StringType, _) =>
desc ++ List("type" -> Json.Str("string"))

case ZSchema.Primitive(StandardType.IntType, Chunk(Const(v))) =>
constDesc ++ List("type" -> Json.Str("integer"), "const" -> Json.Num(v.asInstanceOf[Int]))
desc ++ List(
"type" -> Json.Str("integer"),
"enum" -> Json.Arr(Json.Num(v.asInstanceOf[Int]))
)

case ZSchema.Primitive(StandardType.IntType, _) =>
desc ++ List("type" -> Json.Str("integer"), "format" -> Json.Str("int32"))

case ZSchema.Primitive(StandardType.LongType, Chunk(Const(v))) =>
constDesc ++ List("type" -> Json.Str("integer"), "const" -> Json.Num(v.asInstanceOf[Long]))
desc ++ List(
"type" -> Json.Str("integer"),
"enum" -> Json.Arr(Json.Num(v.asInstanceOf[Long]))
)

case ZSchema.Primitive(StandardType.LongType, _) =>
desc ++ List("type" -> Json.Str("integer"), "format" -> Json.Str("int64"))

case ZSchema.Primitive(StandardType.DoubleType, Chunk(Const(v))) =>
constDesc ++ List("type" -> Json.Str("number"), "const" -> Json.Num(v.asInstanceOf[Double]))
desc ++ List(
"type" -> Json.Str("number"),
"enum" -> Json.Arr(Json.Num(v.asInstanceOf[Double]))
)

case ZSchema.Primitive(StandardType.DoubleType, _) =>
desc ++ List("type" -> Json.Str("number"))

case ZSchema.Primitive(StandardType.FloatType, Chunk(Const(v))) =>
constDesc ++ List("type" -> Json.Str("number"), "const" -> Json.Num(v.asInstanceOf[Float]))
desc ++ List(
"type" -> Json.Str("number"),
"enum" -> Json.Arr(Json.Num(v.asInstanceOf[Float]))
)

case ZSchema.Primitive(StandardType.FloatType, _) =>
desc ++ List("type" -> Json.Str("number"), "format" -> Json.Str("float"))

case ZSchema.Primitive(StandardType.BoolType, Chunk(Const(v))) =>
constDesc ++ List(
"type" -> Json.Str("boolean"),
"const" -> Json.Bool(v.asInstanceOf[Boolean])
desc ++ List(
"type" -> Json.Str("boolean"),
"enum" -> Json.Arr(Json.Bool(v.asInstanceOf[Boolean]))
)

case ZSchema.Primitive(StandardType.BoolType, _) =>
Expand Down
18 changes: 9 additions & 9 deletions kyo-llm/shared/src/main/scala/kyo/llm/agents.scala
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,13 @@ package object agents {
"""
)
case class Request[Opening, T, Closing](
@desc("A short text to provide a status update to the user.")
// @desc("A short text to provide a status update to the user.")
actionNarrationToBeShownToTheUser: String,
openingThoughts: Request.OpeningThoughts[Opening],
`agentInput must be complete, it's only thing the user will see`: Boolean,
@desc("String fields are text, not json. Change the tone as if you're addressing the user. Analyze and leverage the thoughts so far.")
`List opening thoughts`: String,
`Strategy to adhere to all opening thoughts`: String,
`agentInput must be not empty and complete, it's only thing the user will see`: Boolean,
// @desc("Do not skip! String fields are text, not json. Change the tone as if you're addressing the user. Analyze and leverage the thoughts so far.")
agentInput: T,
closingThoughts: Request.ClosingThoughts[Closing]
)
Expand All @@ -115,20 +117,18 @@ package object agents {
case class OpeningThoughts[T](
strictlyFollowTheJsonSchema: Boolean,
`Even when the the field name is a text like here`: Boolean,
@desc("Make sure to generate all required fields by the json schema.")
// @desc("Make sure to generate all required fields by the json schema.")
`I understand text field names function as an inner-dialog reasoning mechanism`: Boolean,
additionalOpeningThoughts: T,
`Elaborate on how I can use the opening thoughts`: String,
`Do not output json in string fields`: Boolean,
`Now I'll proceed to generate a complete function call input`: Boolean,
`Opening thoughts finalized`: Boolean,
`I'll change the tone as if I'm addressing the user`: Boolean
`I'll change the tone as if I'm addressing the user`: Boolean,
`Next json schema fields to generate, which include agentInput`: String
)
case class ClosingThoughts[T](
`The opening thoughts were considered`: Boolean,
`The input is complete and follows the json schema`: Boolean,
additionalClosingThoughts: T,
@desc("There's a common issue where you output many new line characters after the json.")
// @desc("There's a common issue where you output many new line characters after the json.")
`I will not generate a sequence of several new line charaters`: Boolean
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ import kyo.llm.ais._
"All text and any other field from this point on must be complete and with **as much detail as possible**. Perfer long texts."
)
case class Elaborate(
@desc("Consider all thoughts so far and the user request")
`Strategy to elaborate`: String,
`Generate a json that is complete and as detailed as possible`: Boolean,
`Strategy to generate long texts`: String,
@desc("Generate bulleted lists with detailed items if appropiate.")
`I'll elaborate as much as possible and use bulleted lists if appropiate`: Boolean
`I'll generate a json that is complete and as detailed as possible`: Boolean,
`Stragegy to generate a complete and well elaborated output`: String
) extends Thought.Opening
18 changes: 18 additions & 0 deletions kyo-llm/shared/src/main/scala/kyo/llm/thoughts/Humorize.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package kyo.llm.thoughts

import kyo.llm.ais._

@desc(
p"""
The Humorize thought directs the LLM in crafting humor within a given context.
- Explores the context for potential comedic elements.
- Develops humor that is engaging, witty, and contextually relevant.
- Ensures that the humor is appropriate and sensitive to the audience.
"""
)
case class Humorize(
`Context exploration for humor`: String,
`Comedic elements identified`: String,
`Strategy for humorous content creation`: String,
`Appropriateness and audience sensitivity`: String
) extends Thought.Opening
6 changes: 6 additions & 0 deletions kyo-llm/shared/src/main/scala/kyo/llm/thoughts/RolePlay.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package kyo.llm.thoughts

case class RolePlay[Role <: String](
`The user has defined the following const string as my role`: Role,
`Strategy to assume the role`: String
) extends Thought.Opening
16 changes: 11 additions & 5 deletions kyo-llm/shared/src/test/scala/kyo/llm/thoughts/ttt.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@ import kyo._
import kyo.llm.ais._
import kyo.llm.thoughts._
import kyo.llm.KyoLLMApp
import kyo.llm.configs.Config
import kyo.llm.configs.Model

object ttt extends KyoLLMApp {

run {
for {
ai <- AIs.init
// _ <- ai.thought[Contextualize]
// _ <- ai.thought[Elaborate]
// _ <- ai.thought[Brainstorm]
} yield ai.gen[String]("what are the dangers of AI? what kind of apocalypse it could create?")
_ <- ai.thought[Contextualize]
_ <- ai.thought[Brainstorm]
// _ <- ai.thought[Humorize]
// _ <- ai.thought[Elaborate]
_ <- ai.thought[RolePlay["You're a funny person that uses suffer lingo"]]
} yield ai.gen[String](
"What could be the most unexpected things that could happen as AIs evolve?"
)
}
}

0 comments on commit fb41b09

Please sign in to comment.