-
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.
decompose class hierarchy of LLM implementations (#397)
* refactor hierarchy of LLM (first compiling version) * first compiling version after clean build; Split request of Chat and FunChat * small changes and fixes * modify hierarchy for GCP * adjust AutoClose and scope of provider client * small changes * small changes * some models had wrong capability * changes according to comments and feedback * storing full prompt and response messages (#440) * storing full prompt and response messages * updated the rest of the functions * fixed problem in messages order and added tests * Animal Example Fixed --------- Co-authored-by: Raúl Raja Martínez <[email protected]> Co-authored-by: José Carlos Montañez <[email protected]> Co-authored-by: Javi Pacheco <[email protected]>
- Loading branch information
1 parent
a50b96e
commit 774c796
Showing
36 changed files
with
848 additions
and
742 deletions.
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
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
3 changes: 0 additions & 3 deletions
3
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,11 +1,8 @@ | ||
package com.xebia.functional.xef.llm | ||
|
||
import com.xebia.functional.tokenizer.ModelType | ||
import com.xebia.functional.xef.llm.models.text.CompletionRequest | ||
import com.xebia.functional.xef.llm.models.text.CompletionResult | ||
|
||
interface Completion : LLM { | ||
val modelType: ModelType | ||
|
||
suspend fun createCompletion(request: CompletionRequest): CompletionResult | ||
} |
29 changes: 27 additions & 2 deletions
29
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,7 +1,32 @@ | ||
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 | ||
|
||
sealed interface LLM : AutoCloseable { | ||
val name: String | ||
|
||
override fun close() {} | ||
val modelType: ModelType | ||
|
||
@Deprecated("use modelType.name instead", replaceWith = ReplaceWith("modelType.name")) | ||
val name | ||
get() = modelType.name | ||
|
||
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 | ||
} | ||
|
||
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
Oops, something went wrong.