Skip to content

Instantly share code, notes, and snippets.

@btnguyen2k
Created May 11, 2023 13:49
Show Gist options
  • Save btnguyen2k/6da01c72bd86b2de8c9a30b5ad35fd71 to your computer and use it in GitHub Desktop.
Save btnguyen2k/6da01c72bd86b2de8c9a30b5ad35fd71 to your computer and use it in GitHub Desktop.
Count OpenAI tokens in different encodings
package main
import (
"fmt"
"github.com/btnguyen2k/oaiaux"
)
func CountTokens(text, lang, encoding string) {
numTokens := oaiaux.CountTokens(text, oaiaux.Option{Key: "encoding", Value: encoding})
fmt.Printf("Language: %10s / Encoding: %12s / Number of tokens: %4d\n", lang, encoding, numTokens)
}
func main() {
inputs := map[string]string{
"English": `OpenAI is an American artificial intelligence (AI) research laboratory consisting of the non-profit OpenAI Incorporated and its for-profit subsidiary corporation OpenAI Limited Partnership. OpenAI conducts AI research with the declared intention of promoting and developing a friendly AI. OpenAI systems run on an Azure-based supercomputing platform from Microsoft.
OpenAI was founded in 2015 by Ilya Sutskever, Greg Brockman, Trevor Blackwell, Vicki Cheung, Andrej Karpathy, Durk Kingma, John Schulman, Pamela Vagata, and Wojciech Zaremba, with Sam Altman and Elon Musk serving as the initial board members. Microsoft provided OpenAI LP with a $1 billion investment in 2019 and a $10 billion investment in 2023.`,
"Vietnamese": `OpenAI là một phòng thí nghiệm nghiên cứu trí tuệ nhân tạo (AI) của Mỹ bao gồm tổ chức phi lợi nhuận OpenAI Incorporated (OpenAI Inc.) và công ty con hoạt động vì lợi nhuận OpenAI Limited Partnership (OpenAI LP). OpenAI tiến hành nghiên cứu AI với mục đích đã tuyên bố là thúc đẩy và phát triển một AI thân thiện. Các hệ thống OpenAI chạy trên siêu máy tính mạnh thứ năm trên thế giới. Tổ chức được thành lập tại San Francisco vào năm 2015 bởi Sam Altman, Reid Hoffman, Jessica Livingston, Elon Musk, Ilya Sutskever, Peter Thiel và những người khác. Musk đã từ chức khỏi hội đồng quản trị vào năm 2018 nhưng vẫn là một nhà tài trợ. Microsoft đã cung cấp cho OpenAI LP khoản đầu tư 1 tỷ USD vào năm 2019 và khoản đầu tư thứ hai trong nhiều năm vào tháng 1 năm 2023, được báo cáo là 10 tỷ USD.`,
"French": `OpenAI (« AI » pour artificial intelligence, ou intelligence artificielle) est une entreprise spécialisée dans le raisonnement artificiel, à « but lucratif plafonné », dont le siège social est à San Francisco. Avant mars 2019, elle est reconnue association à but non lucratif. L'objectif de cette société est de promouvoir et de développer un raisonnement artificiel à visage humain qui profitera à toute l'humanité. Grâce à un fonds initial de 100 millions de dollars, OpenAI cherche à s'associer à quelques startups utilisant le raisonnement artificiel pour avoir un effet transformateur, par exemple dans les domaines des soins de santé, du changement climatique et de l'éducation et « où les outils d'IA peuvent autonomiser les gens en les aidant à être plus productifs ».
En 2023, la société OpenAI est valorisée à 29 milliards de dollars américains.`,
"Chinese": `OpenAI(开放人工智能)是美國一個人工智能研究實驗室,由非營利組織OpenAI Inc,和其營利組織子公司OpenAI LP所組成。OpenAI 進行 AI 研究的目的是促進和发展友好的人工智能,使人类整体受益。 OpenAI 系統運行在微軟基於 Azure 的超級計算平台上。該組織於2015年由萨姆·阿尔特曼、里德·霍夫曼、Jessica Livingston、伊隆·马斯克、伊爾亞·蘇茨克維、沃伊切赫·扎伦巴 (Wojciech Zaremba)、彼得·泰爾 等人在旧金山成立,他們共同認捐了$10億美元。 微軟在2019年向 OpenAI LP 提供了$10億美元的投資,並在2023年1月向其提供了第二筆多年投資,據報導為$100億美元, 用於獨家訪問GPT-4,這將為微軟自己的Bing Prometheus 模型提供支持。`,
"Japanese": `OpenAI(オープンエーアイ、オープンAI)とは、営利法人OpenAI LPとその親会社である非営利法人OpenAI Inc. からなるアメリカの人工知能(AI)の開発を行っている企業である。人類全体に利益をもたらす汎用人工知能(AGI)を普及・発展させることを目標に掲げ、AI分野の研究を行っている。
OpenAIは、サンフランシスコのミッション地区(英語版)にあるパイオニア・ビル(英語版)に本社を構えている。`,
"Korean": `오픈AI(OpenAI)는 프렌들리 AI를 제고하고 개발함으로써 전적으로 인류에게 이익을 주는 것을 목표로 하는 미국의 인공지능 연구소이다. 이윤을 목적으로 하는 기업 OpenAI LP와 그 모체 조직인 비영리 단체 OpenAI Inc로 구성되어 있다. 이 단체의 목적은 특허와 연구를 대중에 공개함으로써 다른 기관들 및 연구원들과 자유로이 협업하는 것이다. 설립자들(특히 일론 머스크, 샘 올트먼)은 인공 지능의 존재 문제의 염려에 부분적으로 동기를 받았다.`,
}
encodings := []string{"cl100k_base", "p50k_base", "r50k_base"}
for lang, text := range inputs {
for _, encoding := range encodings {
CountTokens(text, lang, encoding)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment