-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add GPT3 tokenizer. Based on .NET implementation. --------- Co-authored-by: Luigi Montoya <[email protected]> Co-authored-by: joe-braley <[email protected]> Co-authored-by: Luigi96 <[email protected]> Co-authored-by: Mark Wallace <[email protected]> Co-authored-by: John Oliver <[email protected]> Co-authored-by: David Grieve <[email protected]>
- Loading branch information
1 parent
51c7569
commit e20b6f4
Showing
12 changed files
with
50,603 additions
and
1 deletion.
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
84 changes: 84 additions & 0 deletions
84
...e-code/src/main/java/com/microsoft/semantickernel/syntaxexamples/Example29_Tokenizer.java
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,84 @@ | ||
package com.microsoft.semantickernel.syntaxexamples; | ||
|
||
import com.microsoft.semantickernel.GPT3Tokenizer; | ||
public class Example29_Tokenizer { | ||
/** | ||
* This sample shows how to count tokens using GPT tokenizer. The number of tokens affects | ||
* API calls cost and each model has a maximum amount of tokens it can process and generate. | ||
* This example is specific to OpenAI models, which use the tokenization described here: | ||
* <a href="https://platform.openai.com/tokenizer">OpenAI tokenizer</a> | ||
* If you use Semantic Kernel with other models, the tokenization logic is most probably different, | ||
* and you should not use the GPT tokenizer. | ||
*/ | ||
public static void main(String[] args) { | ||
// Example 1 | ||
String sentence = "Some text on one line"; | ||
int tokenCount = GPT3Tokenizer.encode(sentence).size(); | ||
|
||
System.out.println("---"); | ||
System.out.println(sentence); | ||
System.out.println("Tokens: " + tokenCount); | ||
System.out.println("---\n\n"); | ||
|
||
// Example 2 | ||
sentence = "⭐⭐"; | ||
tokenCount = GPT3Tokenizer.encode(sentence).size(); | ||
|
||
System.out.println("The following example contains emojis which require several tokens."); | ||
System.out.println("---"); | ||
System.out.println(sentence); | ||
System.out.println("Tokens: " + tokenCount); | ||
System.out.println("---\n\n"); | ||
|
||
// Example 3 | ||
sentence = "Some text on\ntwo lines"; | ||
tokenCount = GPT3Tokenizer.encode(sentence).size(); | ||
|
||
System.out.println("The following example uses Unix '\\n' line separator."); | ||
System.out.println("---"); | ||
System.out.println(sentence); | ||
System.out.println("Tokens: " + tokenCount); | ||
System.out.println("---\n\n"); | ||
|
||
// Example 4 | ||
sentence = "Some text on\r\ntwo lines"; | ||
tokenCount = GPT3Tokenizer.encode(sentence).size(); | ||
|
||
System.out.println("The following example uses Windows '\\r\\n' line separator."); | ||
System.out.println("---"); | ||
System.out.println(sentence); | ||
System.out.println("Tokens: " + tokenCount); | ||
System.out.println("---\n\n"); | ||
|
||
/* | ||
Output: | ||
--- | ||
Some text on one line | ||
Tokens: 5 | ||
--- | ||
The following example contains emojis which require several tokens. | ||
--- | ||
⭐⭐ | ||
Tokens: 6 | ||
--- | ||
The following example uses Unix '\n' line separator. | ||
--- | ||
Some text on | ||
two lines | ||
Tokens: 6 | ||
--- | ||
The following example uses Windows '\r\n' line separator. | ||
--- | ||
Some text on | ||
two lines | ||
Tokens: 7 | ||
--- | ||
*/ | ||
} | ||
} |
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 |
---|---|---|
|
@@ -37,5 +37,4 @@ | |
<artifactId>jsr305</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>com.microsoft.semantic-kernel</groupId> | ||
<artifactId>semantickernel-parent</artifactId> | ||
<version>0.2.7-alpha-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>semantickernel-gpt3-tokenizer</artifactId> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.projectreactor</groupId> | ||
<artifactId>reactor-core</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.fasterxml.jackson.core</groupId> | ||
<artifactId>jackson-databind</artifactId> | ||
<scope>compile</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.microsoft.semantic-kernel</groupId> | ||
<artifactId>semantickernel-api</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
Oops, something went wrong.