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

[#861mr47qr] Move all file related APIs to separate module #31

Merged
merged 2 commits into from
May 5, 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
3 changes: 2 additions & 1 deletion example/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ java {
}

dependencies {
implementation(project(":langchain4k-kotlin"))
implementation(projects.langchain4kKotlin)
implementation(projects.langchain4kFilesystem)
implementation(libs.kotlinx.serialization.json)
implementation(libs.logback)
implementation(libs.klogging)
Expand Down
5 changes: 1 addition & 4 deletions kotlin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,16 @@ kotlin {
sourceSets {
val commonMain by getting {
dependencies {
implementation(libs.bundles.arrow)
api(libs.bundles.arrow)
api(libs.bundles.ktor.client)
implementation(libs.kotlinx.serialization.json)
implementation(libs.okio)
implementation(libs.uuid)
implementation(libs.klogging)
}
}

commonTest {
dependencies {
implementation(libs.okio.fakefilesystem)
implementation(libs.kotest.property)
implementation(libs.kotest.framework)
implementation(libs.kotest.assertions)
Expand All @@ -88,7 +86,6 @@ kotlin {
val jsMain by getting {
dependencies {
api(libs.ktor.client.js)
implementation(libs.okio.nodefilesystem)
}
}

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.xebia.functional.prompt

import arrow.core.raise.Raise
import okio.FileSystem
import okio.Path

fun Raise<InvalidTemplate>.PromptTemplate(
examples: List<String>,
Expand All @@ -21,27 +19,6 @@ fun Raise<InvalidTemplate>.PromptTemplate(
fun Raise<InvalidTemplate>.PromptTemplate(template: String, variables: List<String>): PromptTemplate =
PromptTemplate(Config(template, variables))

/**
* Creates a PromptTemplate based on a Path
* JVM & Native have overloads for FileSystem.SYSTEM,
* on NodeJs you need to manually pass FileSystem.SYSTEM.
*
* This function can currently not be used on the browser.
*
* https://github.com/square/okio/issues/1070
* https://youtrack.jetbrains.com/issue/KT-47038
*/
suspend fun Raise<InvalidTemplate>.PromptTemplate(
path: Path,
variables: List<String>,
fileSystem: FileSystem
): PromptTemplate =
fileSystem.read(path) {
val template = readUtf8()
val config = Config(template, variables)
PromptTemplate(config)
}

interface PromptTemplate {
val inputKeys: List<String>
suspend fun format(variables: Map<String, String>): String
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import arrow.core.raise.either
import io.kotest.assertions.arrow.core.shouldBeLeft
import io.kotest.assertions.arrow.core.shouldBeRight
import io.kotest.core.spec.style.StringSpec
import okio.Path.Companion.toPath
import okio.fakefilesystem.FakeFileSystem

class PromptTemplateSpec : StringSpec({
"PromptTemplate(template, list) should fail if the template is not valid" {
Expand Down Expand Up @@ -70,22 +68,6 @@ class PromptTemplateSpec : StringSpec({
|What is a good name for a company that makes functional programming?""".trimMargin()
}

"should return a PromptTemplate instance with the contents of the specified file" {
val fileSystem = FakeFileSystem().apply {
val templates = "templates".toPath()
createDirectory(templates)
val example = templates / "example.txt"
write(example) { writeUtf8("My name is {name} and I'm {age} years old") }
}
val inputVariables = listOf("name", "age")
val variables = mapOf("name" to "Angela", "age" to "18")

either {
val prompt = PromptTemplate("templates/example.txt".toPath(), inputVariables, fileSystem)
prompt.format(variables)
} shouldBeRight "My name is Angela and I'm 18 years old"
}

"format should return the expected result for variables with functions" {
val template = "My name is {name} and I'm {age} years old"
fun getAge() = "21"
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

4 changes: 4 additions & 0 deletions langchain4k-filesystem/README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# langchain4k-filesystem

This module provides common _smart-constructors_ for all targets that support a `FileSystem`.
So `macOS`, `Linux`, `Windows`, `NodeJs` & `JVM`. All targets except `browser`.
63 changes: 63 additions & 0 deletions langchain4k-filesystem/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
plugins {
id(libs.plugins.kotlin.multiplatform.get().pluginId)
}

group = "com.xebia.functional.langchain4k"
version = "0.0.1-SNAPSHOT"

repositories {
mavenCentral()
}

kotlin {
jvm()
js(IR) {
nodejs()
}
linuxX64()
macosX64()
mingwX64()

sourceSets {
val commonMain by getting {
dependencies {
implementation(projects.langchain4kKotlin)
implementation(libs.okio)
implementation(libs.klogging)
}
}

val jsMain by getting {
dependencies {
implementation(libs.okio.nodefilesystem)
}
}

commonTest {
dependencies {
implementation(libs.okio.fakefilesystem)
implementation(libs.kotest.property)
implementation(libs.kotest.framework)
implementation(libs.kotest.assertions)
implementation(libs.kotest.assertions.arrow)
}
}

val jvmTest by getting {
dependencies {
implementation(libs.kotest.junit5)
}
}

val linuxX64Main by getting
val macosX64Main by getting
val mingwX64Main by getting

create("nativeMain") {
dependsOn(commonMain)
linuxX64Main.dependsOn(this)
macosX64Main.dependsOn(this)
mingwX64Main.dependsOn(this)
}
}
}
Loading