localhost
.______ ______ __ __ __ __ ______ __ __ /__ _ / __ / / / / "-./ / __ / "-. /_/ / / _"-. -./ / -. _ _____ _ _ _ _ _____ _\"_ /_/ /_____/ /_//_/ /_/ /_/ /_____/ /_/ /_/ CLI-Dienstprogramm zur Überwachung Ihrer OpenAI-API-Token-Nutzung
tokmon
?? - CLI zur Überwachung der OpenAI-API-Nutzung tokmon
( Tok en Mon itor) können Sie die OpenAI-API-Token-Nutzung Ihres Programms verfolgen.
Sie können tokmon
genauso verwenden wie das time
, aber anstelle der Ausführungszeit erhalten Sie OpenAI-Nutzungs- und Kostenstatistiken.
pip install tokmon
Hinweis : tokmon funktioniert für
gpt-*
Modelle (gpt-3.5-turbo
,gpt-4
usw.). Wenn Sie Unterstützung für andere Modelle benötigen (z. B.davinci
), lesen Sie tokmon#6.
$ tokmon /path/to/your/ < your program > [arg1] [arg2] ...
tokmon cost report :
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Monitored invocation : python3 . / tests / python_example . py --prompt say 'hello, tokmon!'
Models : [ 'gpt-3.5-turbo-0301' ]
Total Usage : { 'total_prompt_tokens' : 26 , 'total_completion_tokens' : 12 , 'total_tokens' : 38}
Pricing : { 'gpt-3.5-turbo-0301' : { 'prompt_cost' : 0.002 , 'completion_cost' : 0.002 , 'per_tokens' : 1000}}
Total Cost : $0.000076
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Writing cost summary to JSON file : . / tokmon_usage_summary_1682039505 . json
Nachdem die Ausführung Ihres Programms abgeschlossen ist (oder Sie ctrl^C
gedrückt haben), druckt tokmon
eine Zusammenfassung aus, die wie oben aussieht. tokmon
generiert außerdem einen detaillierten Bericht und speichert ihn als JSON-Datei.
Sie können das Flag --beam <url>
verwenden, um Token-Nutzungsdaten an einen Server zu streamen. Weitere Informationen finden Sie unter tokmon --beam.
# Install tokmon
pip install tokmon
# Clone this repo and `cd` into it
git clone https://github.com/yagil/tokmon.git && cd tokmon/
# export your OpenAI API key. This will be used in the test program (source in ./tests/python_example.py)
export OPENAI_API_KEY= " YOUR_OPENAI_API_KEY "
# Run tokmon (save the JSON summary to the current folder)
# The test uses the openai python package. Install it if you don't have it: `pip install openai`
tokmon --json_out=. python3 ./tests/python_example.py --prompt " say 'hello, tokmon!' "
tokmon
unterstützt auch dies:tokmon
gleichzeitig ausführen. Bei jedem Aufruf wird ein separater Nutzungsbericht generiert.--json_out /your/path/report.json
um eine detaillierte Aufschlüsselung und einen Konversationsverlauf im JSON-Format zu erhalten. tokmon
mit Ihrer Anwendung oder Ihrem SkriptWarnung: Dies ist ein Debugging-Tool. Es ist nicht für die Verwendung in Folgesituationen vorgesehen. Nutzen Sie Ihr bestes Urteilsvermögen!
Stellen Sie tokmon
Ihrem normalen Programmaufruf wie folgt voran:
$ tokmon /path/to/your/ < your program > [arg1] [arg2] ...
Führen Sie Ihr Programm wie gewohnt aus und verwenden Sie es (Argumente und alles). Auch die interaktive Nutzung wird unterstützt.
Fügen Sie tokmon
einfach am Anfang Ihres regulären Aufrufs hinzu
$ tokmon python /path/to/your/script.py
Dies funktioniert für Skripte und lang laufende Programme wie Django-/Flask-/FastAPI-Server.
Für Skripte:
$ tokmon node /path/to/your/script.js
npm run
Bearbeiten Sie den „scripts“-Eintrag Ihrer package.json
, um tokmon
einzuschließen.
{
"scripts" : {
"dev" : "tokmon next dev" ,
...
}
}
{
"total_cost" : 0.0019199999999999998 ,
"total_usage" : {
"total_prompt_tokens" : 18 ,
"total_completion_tokens" : 23 ,
"total_tokens" : 41
},
"pricing_data" : " {'gpt-4-0314': {'prompt_cost': 0.03, 'completion_cost': 0.06, 'per_tokens': 1000}} " ,
"models" : [
" gpt-4-0314 "
],
"raw_data" : [
{
"model" : " gpt-4-0314 " ,
"usage" : {
"prompt_tokens" : 18 ,
"completion_tokens" : 23 ,
"total_tokens" : 41
},
"cost" : 0.0019199999999999998 ,
"messages" : [
{
"role" : " system " ,
"content" : " You're a helpful assistant. "
},
{
"role" : " user " ,
"content" : " hello "
},
{
"role" : " assistant " ,
"content" : " Hello! How can I help you today? If you have any questions or need assistance, feel free to ask. "
}
]
}
]
}
tokmon
verwendet die Mitmproxy-Bibliothek, um HTTP-Anfragen und -Antworten zwischen Ihrem Programm und der OpenAI-API abzufangen. Anschließend verarbeitet es die Anfrage- und Antwortdaten, um die Token-Nutzung und die Kosten auf der Grundlage von tokmon/openai-pricing.json zu berechnen.
tokmon
funktioniert für Programme inpython
/node
(unter Verwendung der OpenAI-Clients) odercurl
(direkt ausgeführt und nicht z. B. in einem Bash-Skript). Anweisungen zur Verwendungtokmon
mit Golang-Programmen finden Sie in den Golang-Anweisungen.
Wenn Sie das CA-Zertifikat von
mitmproxy
manuell installieren, sollte es für alle ausführbaren Dateien funktionieren (Hinweis: Dies wurde nicht getestet.)
In den meisten Fällen verlässt sich tokmon
für die Token-Zählung auf das Feld 'usage'
in den API-Antworten von OpenAI. Für Streaming-Anfragen nutzt tokmon
jedoch direkt die Tiktoken-Bibliothek von OpenAI, um die Tokens zu zählen. Zum Zeitpunkt des Schreibens gibt die API von OpenAI keine Nutzungsdaten für Streaming-Anfragen zurück (Referenz).
Die Preisdaten wurden mithilfe von ChatGPT von der Website von OpenAI extrahiert.
tokmon
verwendet tokmon/openai-pricing.json aus seinem Paket.
{
"last_updated" : " 2023-04-12 " ,
"data_sources" : [
" https://openai.com/pricing " ,
" https://platform.openai.com/docs/models/model-endpoint-compatibility "
],
"gpt-4" : { "prompt_cost" : 0.03 , "completion_cost" : 0.06 , "per_tokens" : 1000 },
"gpt-4-0314" : { "prompt_cost" : 0.03 , "completion_cost" : 0.06 , "per_tokens" : 1000 },
"gpt-4-32k" : { "prompt_cost" : 0.06 , "completion_cost" : 0.12 , "per_tokens" : 1000 },
"gpt-4-32k-0314" : { "prompt_cost" : 0.06 , "completion_cost" : 0.12 , "per_tokens" : 1000 },
"gpt-3.5-turbo" : { "prompt_cost" : 0.002 , "completion_cost" : 0.002 , "per_tokens" : 1000 },
"gpt-3.5-turbo-0301" : { "prompt_cost" : 0.002 , "completion_cost" : 0.002 , "per_tokens" : 1000 },
"text-davinci-003" : { "cost" : 0.02 , "per_tokens" : 1000 },
"text-curie-001" : { "cost" : 0.002 , "per_tokens" : 1000 },
"text-babbage-001" : { "cost" : 0.0005 , "per_tokens" : 1000 },
"text-ada-001" : { "cost" : 0.0004 , "per_tokens" : 1000 },
"text-embedding-ada-002" : { "cost" : 0.0004 , "per_tokens" : 1000 }
}
Sie können die Standardpreise überschreiben mit: tokmon --pricing /path/to/your/custom-openai-pricing.json ...
Dieser Preis-JSON ist unvollständig (DALL-E fehlt usw.), er ist möglicherweise falsch und möglicherweise veraltet.
Um optimale Ergebnisse zu erzielen, überprüfen Sie unbedingt, ob Sie über die neuesten Preise verfügen.
Hackiger Workaround für Golang-Programme. Fügen Sie dies Ihrem Programm hinzu:
// Import these packages
import (
"os"
"crypto/tls"
"crypto/x509"
"io/ioutil"
"net/http"
)
// Place this code somewhere in your go program *before* you make any calls to OpenAI's API.
certFile := os . Getenv ( "TOKMON_SSL_CERT_FILE" ) // This env variable will be set by tokmon
caCert , err := ioutil . ReadFile ( certFile )
if err == nil {
caCertPool , _ := x509 . SystemCertPool ()
caCertPool . AppendCertsFromPEM ( caCert )
http . DefaultTransport .( * http. Transport ). TLSClientConfig = & tls. Config {
RootCAs : caCertPool ,
}
}
tokmon
puffert Server-Sent Events (SSE), bis der data: [DONE]
empfangen wird. Wenn das überwachte Programm Event-Streaming nutzt, wird sein Verhalten geändert.gpt-3.5-turbo
, gpt-4
und Varianten)Wenn Sie zum Projekt beitragen möchten, befolgen Sie bitte diese Schritte:
tokmon
wird ohne jegliche Gewährleistung oder Garantie geliefert.tokmon
wurde nur auf macOS getestet. Auf anderen Plattformen funktioniert es möglicherweise nicht.openai-pricing.json
sind möglicherweise veraltet.