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

Port PromptTemplate #6

Merged
merged 3 commits into from
Apr 21, 2023
Merged

Port PromptTemplate #6

merged 3 commits into from
Apr 21, 2023

Conversation

nomisRev
Copy link
Contributor

@nomisRev nomisRev commented Apr 20, 2023

This PR ports the PromptTemplate from langchain4s.

fileSystem.source(templateFile).use { source ->
source.buffer().use { buffer ->
val template = buffer.readUtf8()
val config = Config.orThrow(template, inputVariables)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the smart-constructors for PromptTemplate are turning NonEmptyList<InvalidTemplate> into an exception, we can keep this as Raise<NonEmptyList<InvalidTemplate>>.

Perhaps better is Raise<InvalidTemplate>, where the Config smart-constructors is already formatting the final message from NonEmptyList<String>.

Then we can turn the signature into something like Raise<PromptTemplate.IncorrectConfig>, and create an error hierarchy if we need to with more errors.

WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed all the exceptions, and orThrow and pushed them to the smart-constructors. You can always wrap in either { }.getOrNull() or recover({ }) { null } or we can expose a Either<InvalidTemplate, A>.getOrThrow extension within the module so dealing with the error at least becomes explicit.

override suspend fun format(variables: Map<String, String>): String {
val mergedArgs = mergePartialAndUserVariables(variables, config.inputVariables)
return when (config.templateFormat) {
TemplateFormat.Jinja2 -> TODO()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is currently not implemented in lanchain4s and is resulting in NoSuchElementException, so I am keeping it here as TODO for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed, this from the enum and raised a PR for Scala to do the same. I think it's better to not have this until we actually implement it.

suspend fun fromFile(
templateFile: Path,
inputVariables: List<String>,
fileSystem: FileSystem
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only exists for JVM, Native & NodeJS.

If we drop the browser target then we can provide a default argument of FileSystem.SYSTEM in this function, the Okio library also provides the test instance as well. We can also keep it as is, since browser cannot provide an instance for this it cannot call the method.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the moment, we don't need to load templates from files. I think it's better to keep the browser support I suspect in the browser there is some kind of API to access local storage if you have been granted permission which we could use in place of speaking in terms of File

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@raulraja it currently still supports browser, but from a browser sourcceSet you cannot provide a FileSystem instance. square/okio#1070.

I'll provide overloads of this function from JVM, Native and NodeJS that remove that argument.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could only do this for native, and JVM for now. Cannot define a separate NodeJS sourceSet, since there is no commonizer between NodeJS and Browser. https://youtrack.jetbrains.com/issue/KT-47038

Super tiny issue though, still quite easy to call it from node by passing FileSystem.SYSTEM.

@nomisRev
Copy link
Contributor Author

@gutiory @franciscodr @realdavidvega PR is ready for review.

suspend fun fromFile(
templateFile: Path,
inputVariables: List<String>,
fileSystem: FileSystem
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the moment, we don't need to load templates from files. I think it's better to keep the browser support I suspect in the browser there is some kind of API to access local storage if you have been granted permission which we could use in place of speaking in terms of File

{ validate(template, placeholders.toSet() - variables.toSet(), "missing") },
{ validateDuplicated(template, placeholders) }
) { _, _, _ -> Config(variables, template) }
}.mapLeft { InvalidTemplate(it.joinToString(transform = InvalidTemplate::reason)) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know the transform argument to join a List of elements into a single String. It looks neat 😄

Copy link
Contributor

@franciscodr franciscodr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @nomisRev!

@nomisRev nomisRev merged commit 5d218d7 into main Apr 21, 2023
@nomisRev nomisRev deleted the prompt-template branch April 21, 2023 11:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants