-
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.
Co-authored-by: Raúl Raja Martínez <[email protected]> Co-authored-by: Javi Pacheco <[email protected]>
- Loading branch information
1 parent
db95ac4
commit bd7e3bc
Showing
7 changed files
with
104 additions
and
243 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
core/src/commonMain/kotlin/com/xebia/functional/xef/prompt/Builder.kt
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,21 @@ | ||
package com.xebia.functional.xef.prompt | ||
|
||
open class PromptBuilder { | ||
private val items = mutableListOf<Prompt>() | ||
|
||
val emptyLine: String = "" | ||
|
||
operator fun Prompt.unaryPlus() { | ||
items.add(this) | ||
} | ||
|
||
operator fun String.unaryPlus() { | ||
items.add(Prompt(this)) | ||
} | ||
|
||
open protected fun preprocess(elements: List<Prompt>): List<Prompt> = elements | ||
|
||
fun build(): Prompt = buildString { preprocess(items).forEach(this::append) }.let { Prompt(it) } | ||
} | ||
|
||
fun buildPrompt(block: PromptBuilder.() -> Unit): Prompt = PromptBuilder().apply { block() }.build() |
16 changes: 6 additions & 10 deletions
16
core/src/commonMain/kotlin/com/xebia/functional/xef/prompt/Prompt.kt
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 |
---|---|---|
@@ -1,23 +1,19 @@ | ||
package com.xebia.functional.xef.prompt | ||
|
||
import arrow.core.fold | ||
import kotlin.jvm.JvmInline | ||
|
||
fun Prompt(examples: List<String>, suffix: String, prefix: String): Prompt = | ||
Prompt( | ||
"""|$prefix | ||
| | ||
|${examples.joinToString(separator = "\n")} | ||
| | ||
|$suffix""" | ||
.trimMargin() | ||
) | ||
|
||
@JvmInline value class Prompt(val message: String) | ||
|
||
fun String.prompt(): Prompt = Prompt(this) | ||
|
||
fun Prompt.prepend(text: String) = Prompt(text + message) | ||
|
||
operator fun Prompt.plus(other: Prompt): Prompt = Prompt(message + other.message) | ||
|
||
operator fun Prompt.plus(text: String): Prompt = Prompt(message + text) | ||
|
||
fun Prompt.append(text: String) = this + text | ||
|
||
fun Prompt.format(variables: Map<String, String>): Prompt = | ||
Prompt(variables.fold(message) { acc, (key, value) -> acc.replace("{$key}", value) }) |
98 changes: 0 additions & 98 deletions
98
core/src/commonMain/kotlin/com/xebia/functional/xef/prompt/PromptTemplate.kt
This file was deleted.
Oops, something went wrong.
69 changes: 69 additions & 0 deletions
69
core/src/commonMain/kotlin/com/xebia/functional/xef/prompt/templates/templates.kt
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,69 @@ | ||
package com.xebia.functional.xef.prompt.templates | ||
|
||
import com.xebia.functional.xef.prompt.Prompt | ||
import com.xebia.functional.xef.prompt.PromptBuilder | ||
import com.xebia.functional.xef.prompt.prompt | ||
|
||
fun youAre(role: String, talkingTo: String): Prompt = | ||
"You are a $role talking with a $talkingTo".prompt() | ||
|
||
class StepsBuilder : PromptBuilder() { | ||
override fun preprocess(elements: List<Prompt>): List<Prompt> = | ||
elements.mapIndexed { ix, elt -> Prompt("${ix + 1} - ${elt.message}") } | ||
} | ||
|
||
fun steps(inside: PromptBuilder.() -> Unit): Prompt = StepsBuilder().apply { inside() }.build() | ||
|
||
fun writeSequenceOf(content: String): Prompt = | ||
""" | ||
Write a sequence of $content in the following format: | ||
Step 1 - ... | ||
Step 2 - ... | ||
... | ||
Step N - ... | ||
""" | ||
.trimIndent() | ||
.prompt() | ||
|
||
fun writeListOf(content: String): Prompt = | ||
""" | ||
Write a list of $content in the following format: | ||
1. ... | ||
2. ... | ||
n. ... | ||
""" | ||
.trimIndent() | ||
.prompt() | ||
|
||
fun code(code: String, delimiter: Delimiter?, name: String? = null): Prompt = | ||
""" | ||
${name ?: "" } | ||
${delimiter?.start() ?: ""} | ||
$code | ||
${delimiter?.end() ?: ""} | ||
""" | ||
.trimIndent() | ||
.prompt() | ||
|
||
enum class Delimiter { | ||
ThreeBackticks, | ||
ThreeQuotes; | ||
|
||
fun text(): String = | ||
when (this) { | ||
ThreeBackticks -> "triple backticks" | ||
ThreeQuotes -> "triple quotes" | ||
} | ||
|
||
fun start() = | ||
when (this) { | ||
ThreeBackticks -> "```" | ||
ThreeQuotes -> "\"\"\"" | ||
} | ||
|
||
fun end() = | ||
when (this) { | ||
ThreeBackticks -> "```" | ||
ThreeQuotes -> "\"\"\"" | ||
} | ||
} |
124 changes: 0 additions & 124 deletions
124
core/src/commonTest/kotlin/com/xebia/functional/xef/prompt/PromptTemplateSpec.kt
This file was deleted.
Oops, something went wrong.
9 changes: 4 additions & 5 deletions
9
...nctional/xef/prompt/FilePromptTemplate.kt → ...xebia/functional/xef/prompt/FilePrompt.kt
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 |
---|---|---|
@@ -1,17 +1,16 @@ | ||
package com.xebia.functional.xef.prompt | ||
|
||
import arrow.core.raise.Raise | ||
import com.xebia.functional.xef.io.DEFAULT | ||
import okio.FileSystem | ||
import okio.Path | ||
|
||
/** | ||
* Creates a PromptTemplate based on a Path | ||
* Creates a Prompt based on a Path | ||
*/ | ||
fun Raise<InvalidTemplate>.PromptTemplate( | ||
fun Prompt( | ||
path: Path, | ||
fileSystem: FileSystem = FileSystem.DEFAULT | ||
): PromptTemplate = | ||
): Prompt = | ||
fileSystem.read(path) { | ||
PromptTemplate.either(readUtf8()).bind() | ||
Prompt(readUtf8()) | ||
} |
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