-
Notifications
You must be signed in to change notification settings - Fork 15
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
405 abstract ModelType - inline ModelType #477
Closed
Closed
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
ebb0883
cherrypicked most changes from former branch
Intex32 9e71796
Merge branch 'main' into 405-abstract-modeltype-lento
Intex32 dc426c3
one step closer to make code compiling
Intex32 004f980
one step closer to make code compiling
Intex32 eea22e8
delete ModelType.kt
Intex32 3441197
rename MaxContextLength.kt
Intex32 0913f98
Merge branch 'main' into 405-abstract-modeltype-lento
Intex32 aea81bb
spotless
Intex32 a4eef4b
first compiling version (no clean build)
Intex32 3aa8be6
spotless
Intex32 a267c88
pls compile compadre
Intex32 f0075a4
small changes
Intex32 9f23295
small changes
Intex32 736912f
Merge branch 'main' into 405-abstract-modeltype-lento
Intex32 a251225
fixed stuff after merge
Intex32 96fe57c
Apply spotless formatting
Intex32 7a628e0
many small changes; introduces BaseChat
Intex32 c6b56e2
Apply spotless formatting
Intex32 1f19f61
removes some intermediary functions in LLM and adapts all usages
Intex32 253adba
fix potential bug with integer overflow in PromptCalculator
Intex32 066135e
fixes test (wrong model contxt length)
Intex32 295ed8c
Apply spotless formatting
Intex32 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
25 changes: 25 additions & 0 deletions
25
core/src/commonMain/kotlin/com/xebia/functional/xef/llm/BaseChat.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,25 @@ | ||
package com.xebia.functional.xef.llm | ||
|
||
import com.xebia.functional.xef.llm.models.MaxIoContextLength | ||
import com.xebia.functional.xef.llm.models.chat.Message | ||
|
||
interface BaseChat : LLM { | ||
|
||
val contextLength: MaxIoContextLength | ||
|
||
@Deprecated( | ||
"will be removed from LLM in favor of abstracting former ModelType, use contextLength instead" | ||
) | ||
val maxContextLength | ||
get() = | ||
(contextLength as? MaxIoContextLength.Combined)?.total | ||
?: error( | ||
"accessing maxContextLength requires model's context length to be of type MaxIoContextLength.Combined" | ||
) | ||
|
||
fun countTokens(text: String): Int | ||
|
||
fun truncateText(text: String, maxTokens: Int): String | ||
|
||
fun tokensFromMessages(messages: List<Message>): Int | ||
} |
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
7 changes: 7 additions & 0 deletions
7
core/src/commonMain/kotlin/com/xebia/functional/xef/llm/Completion.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,8 +1,15 @@ | ||
package com.xebia.functional.xef.llm | ||
|
||
import com.xebia.functional.xef.llm.models.MaxIoContextLength | ||
import com.xebia.functional.xef.llm.models.text.CompletionRequest | ||
import com.xebia.functional.xef.llm.models.text.CompletionResult | ||
|
||
interface Completion : LLM { | ||
val contextLength: MaxIoContextLength | ||
|
||
suspend fun createCompletion(request: CompletionRequest): CompletionResult | ||
|
||
fun countTokens(text: String): Int | ||
|
||
fun truncateText(text: String, maxTokens: Int): String | ||
} |
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
36 changes: 10 additions & 26 deletions
36
core/src/commonMain/kotlin/com/xebia/functional/xef/llm/LLM.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,38 +1,22 @@ | ||
package com.xebia.functional.xef.llm | ||
|
||
import com.xebia.functional.tokenizer.Encoding | ||
import com.xebia.functional.tokenizer.ModelType | ||
import com.xebia.functional.xef.llm.models.chat.Message | ||
import com.xebia.functional.xef.llm.models.ModelID | ||
|
||
sealed interface LLM : AutoCloseable { | ||
// sealed modifier temporarily removed as OAI's implementation of tokensFromMessages has to extend | ||
// and override LLM | ||
/*sealed */ interface LLM : AutoCloseable { | ||
|
||
val modelType: ModelType | ||
val modelID: ModelID | ||
|
||
@Deprecated("use modelType.name instead", replaceWith = ReplaceWith("modelType.name")) | ||
@Deprecated("use modelID.value instead", replaceWith = ReplaceWith("modelID.value")) | ||
val name | ||
get() = modelType.name | ||
get() = modelID.value | ||
|
||
/** | ||
* Copies this instance and uses [modelType] for [LLM.modelType]. Has to return the most specific | ||
* type of this instance! | ||
* Copies this instance and uses [modelID] for the new instances' [LLM.modelID]. Has to return the | ||
* most specific type of this instance! | ||
*/ | ||
fun copy(modelType: ModelType): LLM | ||
|
||
fun tokensFromMessages( | ||
messages: List<Message> | ||
): Int { // TODO: naive implementation with magic numbers | ||
fun Encoding.countTokensFromMessages(tokensPerMessage: Int, tokensPerName: Int): Int = | ||
messages.sumOf { message -> | ||
countTokens(message.role.name) + | ||
countTokens(message.content) + | ||
tokensPerMessage + | ||
tokensPerName | ||
} + 3 | ||
return modelType.encoding.countTokensFromMessages( | ||
tokensPerMessage = modelType.tokensPerMessage, | ||
tokensPerName = modelType.tokensPerName | ||
) + modelType.tokenPadding | ||
} | ||
fun copy(modelID: ModelID): LLM | ||
|
||
override fun close() = Unit | ||
} |
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
14 changes: 14 additions & 0 deletions
14
core/src/commonMain/kotlin/com/xebia/functional/xef/llm/models/MaxIoContextLength.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,14 @@ | ||
package com.xebia.functional.xef.llm.models | ||
|
||
/** | ||
* Describing the maximum context length a model with text input and output might have. | ||
* | ||
* Some models from VertexAI (in 2023/10) have both types of max context length. | ||
*/ | ||
sealed interface MaxIoContextLength { | ||
/** one total length of input and output combined */ | ||
data class Combined(val total: Int) : MaxIoContextLength | ||
|
||
/** two separate max lengths for input and output respectively */ | ||
data class Fix(val input: Int, val output: Int) : MaxIoContextLength | ||
} |
5 changes: 5 additions & 0 deletions
5
core/src/commonMain/kotlin/com/xebia/functional/xef/llm/models/ModelID.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,5 @@ | ||
package com.xebia.functional.xef.llm.models | ||
|
||
import kotlin.jvm.JvmInline | ||
|
||
@JvmInline value class ModelID(val value: String) |
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
4 changes: 2 additions & 2 deletions
4
core/src/commonMain/kotlin/com/xebia/functional/xef/store/MemoryUtils.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
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
6 changes: 3 additions & 3 deletions
6
core/src/commonMain/kotlin/com/xebia/functional/xef/textsplitters/TokenTextSplitter.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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As a side note, again - this is supposed to be an intermediary solution. Usages of this field that use an OAI model will still work as before. If this field is called on an instance of a (Google) model that doesn't use the combined context length an exception imminent.
I found this to be the best way to handle it, in favor to not change too much code in one PR.