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

🐛 fix knowledge king oom bug #150

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@ package tw.waterballsa.utopia.chatgpt

import com.knuddels.jtokkit.Encodings
import com.knuddels.jtokkit.api.Encoding
import com.knuddels.jtokkit.api.EncodingType
import com.knuddels.jtokkit.api.ModelType

/**
* use with OpenAI's GPT-3.5 models.
*/
class JTokkit {
private val registry = Encodings.newDefaultEncodingRegistry()
private val encodingType: EncodingType = EncodingType.CL100K_BASE
companion object {
private val registry = Encodings.newLazyEncodingRegistry()
private lateinit var encoding: Encoding
}

init {
encoding = registry.getEncodingForModel(ModelType.GPT_3_5_TURBO)
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Q:這裡不能直接初始化嗎?

companion object {
        private val registry = Encodings.newLazyEncodingRegistry()
        private val encoding = registry.getEncodingForModel(ModelType.GPT_3_5_TURBO)
    }

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

可以直接初始化,已修


fun measureNumOfTokens(string: String): Int {
val encoding: Encoding = registry.getEncoding(encodingType)
val encoded: List<Int> = encoding.encode(string)
return encoded.size
return encoding.countTokens(string)
}

Choose a reason for hiding this comment

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

🤖 OpenAI

The measureNumOfTokens function has been modified to use the countTokens method of the encoding object. This change improves the efficiency of token counting by utilizing the built-in functionality provided by the jtokkit library.

Suggested change
fun measureNumOfTokens(string: String): Int {
val encoding: Encoding = registry.getEncoding(encodingType)
val encoded: List<Int> = encoding.encode(string)
return encoded.size
return encoding.countTokens(string)
}
fun measureNumOfTokens(string: String): Int {
return encoding.countTokens(string)
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

fun measureNumOfTokens(string: String): Int = encoding.countTokens(string)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

已修正

}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
<javax.annotation-api.version>1.3.2</javax.annotation-api.version>
<jda-ktx.version>0.10.0-beta.1</jda-ktx.version>
<mockito-junit-jupiter.version>5.2.0</mockito-junit-jupiter.version>
<jtokkit.version>0.4.0</jtokkit.version>
<jtokkit.version>0.5.1</jtokkit.version>
<markdowngenerator.version>1.3.1.1</markdowngenerator.version>
<jacoco-maven-plugin.version>0.8.10</jacoco-maven-plugin.version>
</properties>
Expand Down