【OpenAI】API プロンプトトークン数(利用料金)をカウントする

通常のAPIリクエストではレスポンスボディに利用トークンのカウント数は含まれる。


"usage": {
   "prompt_tokens": 9,
   "completion_tokens": 12,
   "total_tokens": 21
 }

👉 API Reference - OpenAI API hatena-bookmark

しかし「Server-Sent Events(SSE)」では、それをレスポンスで返さない。



👉 【OpenAI】Kotlin OkHttp で Server-Sent Events (SSE) hatena-bookmark

従量制のAPIを利用している限り料金は気になりますよね。

プロンプトのトークンの数をアプリ側で独自にカウントすれば料金(利用トークン数から)は算定できるのでは?

 

🤖 JTokkit

Java のライブラリありますね。OpenAI公式でも紹介されているやつ。


👉 openai-cookbook/How_to_count_tokens_with_tiktoken.ipynb at main · openai/openai-cookbook · GitHub hatena-bookmark

豊富な Java ライブラリを使えるのは Kotlin の便利なところです。

👉 Counting Tokens for ChatML | JTokkit hatena-bookmark

とりあえずテストで動かしてみます。


dependencies {
  testImplementation 'com.knuddels:jtokkit:0.4.0'
}


@Test
fun test_jtokkit() {

  val messages = listOf(
    Message("user", "こんにちはと言ってください。"),
    Message("assistant", "こんにちは!")
  )

  val encoding = Encodings.newDefaultEncodingRegistry()
    .getEncodingForModel(ModelType.GPT_3_5_TURBO)

  val count = messages.sumOf { message ->

    // GPT_3_5_TURBO without name
    listOf(
      4, // per message
      encoding.countTokens(message.role),
      encoding.countTokens(message.content)
    ).sum()

  } + 3 // every reply is primed with <|start|>assistant<|message|>

  println(count)

}


@Serializable
data class Message(
  val role: String,
  val content: String
)

メッセージごと、リプライごとのカウント追加が間違ってるのか、なんなのか。

少しカウント数ずれるんだけども。

 

🤖 まとめ

てか、返せよ Usage。

従量制の有料サービスなんだから。

👉 How to get total_tokens from a stream of CompletionCreateRequests - API - OpenAI Developer Forum hatena-bookmark
👉 How do you get token count when streaming - API - OpenAI Developer Forum hatena-bookmark
👉 How to determine the token usage for a session when using stream:true? - API - OpenAI Developer Forum hatena-bookmark

まあ、Webページで確認すればいいけども。

👉 Usage - OpenAI API hatena-bookmark

大して使ってないからどうでもいいか。


【OpenAI】Kotlin OkHttp で Server-Sent Events (SSE)

ChatGPT API で stream 受信したいですよね。

stream boolean Optional Defaults to false

If set, partial message deltas will be sent, like in ChatGPT. Tokens will be sent as data-only server-sent events as they become available, with the stream terminated by a data: [DONE] message.

👉 API Reference - OpenAI API hatena-bookmark

簡単にできそうなのでやってみました。

 

📡 curl

curl で簡単に取得できる。


curl "https://api.openai.com/v1/chat/completions" \
  -H "accept: text/event-stream" \
  -H "authorization: Bearer $OPENAI_API_KEY" \
  -H "content-type: application/json" \
  -d '{
    "model": "gpt-3.5-turbo",
    "messages": [{"role": "user", "content": "Server-Sent Events とは"}],
    "stream": true
  }'


...

data: {"id":"chatcmpl-7OQ1xuBFfam8Ct","object":"chat.completion.chunk","created":1686054286,"model":"gpt-3.5-turbo","choices":[{"delta":{"content":"て"},"index":0,"finish_reason":null}]}

data: {"id":"chatcmpl-7OQ1xuBFfam8Ct","object":"chat.completion.chunk","created":1686054286,"model":"gpt-3.5-turbo","choices":[{"delta":{"content":"います"},"index":0,"finish_reason":null}]}

data: {"id":"chatcmpl-7OQ1xuBFfam8Ct","object":"chat.completion.chunk","created":1686054286,"model":"gpt-3.5-turbo","choices":[{"delta":{"content":"。"},"index":0,"finish_reason":null}]}

data: {"id":"chatcmpl-7OQ1xuBFfam8Ct","object":"chat.completion.chunk","created":1686054286,"model":"gpt-3.5-turbo","choices":[{"delta":{},"index":0,"finish_reason":"stop"}]}

data: [DONE]

パースはだるそうですが、取得はできてるようです!

では、いざ、Kotlin で。

 

📡 okhttp-sse

壊れてるのか何なのか微妙。


testImplementation("com.squareup.okhttp3:okhttp-sse:4.11.0")

👉 okhttp/okhttp-sse at master · square/okhttp · GitHub hatena-bookmark

現在の安定版 4.11.0 ではあやしい。

5.0.0-alpha.11 ではまあまあ動いてるのでそれで。



SSEは、日本語で表現するとサーバ送信イベントと表現されるもので、サーバからクライアントに対してリアルタイムでイベントを送信することができる機能です。 コネクションを張っておいて、サーバからイベントを好きなタイミングで送れるイメージです

 

📡 まとめ

カンファレンスでの公演資料とか、時間が経つと、まあ動かないこと多いですよね!


👉 Server-Sent Events in Android (with Node.js) | by Rahul Ray | Jun, 2023 | ProAndroidDev hatena-bookmark


【OpenAI】API「That model is currently overloaded with other requests. You can retry your request, or contact us through our help center at help.openai.com if the error persists.」

最近、頻繁に出てます。レスポンスボディ。


{
  "error": {
    "message": "That model is currently overloaded with other requests. You can retry your request, or contact us through our help center at help.openai.com if the error persists. (Please include the request ID XXXXXXXXXXXXXXXXXXXXX in your message.)",
    "type": "server_error",
    "param": null,
    "code": null
  }
}

エラーコードが null なので、API クライアントの実装によっては影響あるか。

こんなの出ました。

Expected string literal but 'null' literal was found at path: $.error.code
Use 'coerceInputValues = true' in 'Json {}` builder to coerce nulls to default values.

👉 Kotlin Serialization ガイド 第5章 JSONの機能 - Qiita hatena-bookmark

Error codes - OpenAI API
👉 Error codes - OpenAI API hatena-bookmark

レスポンスボディでなく、レスポンスヘッダーから見るべきか。

👉 Status code 503: That model is currently overloaded with other requests - API - OpenAI Developer Forum hatena-bookmark

一応、サーバーステータスのページでも確認するといいですね。

OpenAI Status

👉 OpenAI Status hatena-bookmark


Dependency androidx.activity.* | androidx.core.* requires libraries and applications that depend on it to compile against codename "UpsideDownCake" of the Android APIs.

androidx.* の group = "androidx.activity"group = "androidx.core" などの基本的なモジュールの非 stable 版は、非 stable API では使えないようです。


3 issues were found when checking AAR metadata:

  1.  Dependency 'androidx.activity:activity-ktx:1.8.0-alpha03' requires libraries and applications that
      depend on it to compile against codename "UpsideDownCake" of the
      Android APIs.

      :app is currently compiled against android-33.

      Recommended action: Use a different version of dependency 'androidx.activity:activity-ktx:1.8.0-alpha03',
      or set compileSdkPreview to "UpsideDownCake" in your build.gradle
      file if you intend to experiment with that preview SDK.

  2.  Dependency 'androidx.activity:activity:1.8.0-alpha03' requires libraries and applications that
      depend on it to compile against codename "UpsideDownCake" of the
      Android APIs.

      :app is currently compiled against android-33.

      Recommended action: Use a different version of dependency 'androidx.activity:activity:1.8.0-alpha03',
      or set compileSdkPreview to "UpsideDownCake" in your build.gradle
      file if you intend to experiment with that preview SDK.

  3.  Dependency 'androidx.activity:activity-compose:1.8.0-alpha03' requires libraries and applications that
      depend on it to compile against codename "UpsideDownCake" of the
      Android APIs.

      :app is currently compiled against android-33.

      Recommended action: Use a different version of dependency 'androidx.activity:activity-compose:1.8.0-alpha03',
      or set compileSdkPreview to "UpsideDownCake" in your build.gradle
      file if you intend to experiment with that preview SDK.

Note: This version will only compile against the Android 14 (Upside Down Cake) Beta 1 SDK.

👉 Activity  |  Jetpack  |  Android Developers hatena-bookmark


2 issues were found when checking AAR metadata:

  1.  Dependency 'androidx.core:core-ktx:1.12.0-alpha03' requires libraries and applications that
      depend on it to compile against codename "UpsideDownCake" of the
      Android APIs.

      :app is currently compiled against android-33.

      Recommended action: Use a different version of dependency 'androidx.core:core-ktx:1.12.0-alpha03',
      or set compileSdkPreview to "UpsideDownCake" in your build.gradle
      file if you intend to experiment with that preview SDK.

  2.  Dependency 'androidx.core:core:1.12.0-alpha03' requires libraries and applications that
      depend on it to compile against codename "UpsideDownCake" of the
      Android APIs.

      :app is currently compiled against android-33.

      Recommended action: Use a different version of dependency 'androidx.core:core:1.12.0-alpha03',
      or set compileSdkPreview to "UpsideDownCake" in your build.gradle
      file if you intend to experiment with that preview SDK.

👉 Core  |  Jetpack  |  Android Developers hatena-bookmark
👉 android - Issues were found when checking AAR metadata: androidx.core:core:1.12.0-alpha01 and androidx.core:core-ktx:1.12.0-alpha01 - Stack Overflow hatena-bookmark

 

🔗 まとめ

コアな Android SDK ライブラリの 非Stable版 は、APIバージョン Stable では使えない。


【Android Studio Flamingo】dependencies をバージョンアップデートするときに確認するべき3つの相性

ソフトウェアの開発において、依存関係は重要な要素の一つです。一つのライブラリが別のライブラリに依存している場合、最新バージョンのアップデートを行う際には、注意が必要です。

今回は、依存関係をアップデートする際に留意しておくべきことについて解説します。

で、[File] - [Project Structure] からのこの画面。

This project uses Gradle Version Catalogs. There are some limitations. Learn more.

【Android Studio Flamingo】dependencies をアップデートするときに確認しておくべきこと

[Suggestions] 画面だけで完全に更新できませんよね。

「プラグイン、ライブラリ同士の互換性は考慮されていないものがある。」

「提案されない表示されないライブラリがある。」

ということで、どこに注意しながら新しいものに更新していくとスムーズに進むか、という話です。

 

🔗 Android Studio と Android Gradle Plugin

Android Studio は、Android アプリ開発に必要な開発環境を提供する統合開発環境です。

Android Gradle Plugin (AGP) は、Android Studio のビルドシステムに使用されるツールであり、ビルドプロセスを自動化するために必要です。

Android Studio と Android Gradle Plugin
👉 Android Gradle plugin and Android Studio compatibility - Android Studio Flamingo | 2022.2.1  |  Android Developers hatena-bookmark

Android Studio で使用している Android Gradle Plugin のバージョンを確認するには、プロジェクトの build.gradle ファイルを開き、以下のように dependencies ブロック内に記述されている AGP のバージョン番号を確認します。


dependencies { 
  classpath 'com.android.tools.build:gradle:x.y.z' 
  // ... 
}

または、build.gradle に記述がない Version Catalog 記述の Android Gradle Plugin のバージョンは、以下の、どちらかのプラグインID のバージョン(共通) です。


com.android.application

com.android.library

👉 【Plugin DSL】Android Gradle Plugin のバージョンを調べる方法 hatena-bookmark
👉 【Plugin DSL】「com.android.tools.build:gradle」の記述は不要? hatena-bookmark

Gradle Version Catalogs では以下のようになります。


# libs.versions.toml 

[versions]
android-gradle-plugin = "8.0.1"

[plugins]
android-application = { id = "com.android.application", version.ref = "android-gradle-plugin" }


// buid.gradle

plugins {
  alias libs.plugins.android.application
}

また、最新の AGP バージョンを確認するには、Google's Maven Repository を参照します。
Google's Maven Repository
👉 com.android.application.com.android.application.gradle.plugin - Google's Maven Repository hatena-bookmark

 

🔗 Android Gradle Plugin と Gradle

Gradle は、オープンソースのビルドツールであり、Java や Kotlin、Groovy などのプログラミング言語に対応しています。Android Studio のビルドシステムには、Gradle が採用されています。

AGP は、Gradle に依存しており、AGP のバージョンアップに伴い、Gradle のバージョンもアップデートする必要があります。

また、Gradle のバージョンアップには、Android Studio のバージョンアップも伴う場合がありますので、注意が必要です。

Android Gradle Plugin は Gradle に依存しているため、AGP をアップデートする場合には、Gradle も同時にアップデートする必要があります。

Android Gradle Plugin と Gradle
👉 Update Gradle - Android Gradle plugin release notes  |  Android Studio  |  Android Developers hatena-bookmark

Gradle のバージョンを確認するには、プロジェクトの gradle/wrapper/gradle-wrapper.properties ファイルを開き、以下のように distributionUrl に記述されている Gradle のバージョン番号を確認します。


distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-bin.zip

また、最新の Gradle バージョンを確認するには、Gradle 公式サイトの Gradle Distributions を参照します。
Gradle Distributions
👉 Gradle Distributions hatena-bookmark

 

🔗 Kotlin と Jetpack Compose Compiler

Kotlin は、Android アプリ開発に必要なプログラミング言語の一つです。Jetpack Compose Compiler は、Kotlin をベースにした Jetpack Compose ライブラリのコンパイラです。

Jetpack Compose は、Android のユーザーインターフェースを作成するための新しい方法を提供するライブラリであり、Kotlin と密接に関連しています。

https://developer.android.com/jetpack/androidx/releases/compose-kotlin#pre-release_kotlin_compatibility
👉 Compose to Kotlin Compatibility Map  |  Android Developers hatena-bookmark

Jetpack Compose Compiler のバージョンをアップデートする場合には、Kotlin のバージョンも確認し、必要に応じてアップデートする必要があります。

Kotlin と Jetpack Compose Compiler の依存関係を確認するには、以下のように Kotlin と Compose Compiler のバージョン番号を確認します。


# libs.versions.toml 

[versions]
compose-compiler = "1.4.7"
kotlin = "1.8.21"

[plugins]
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
kotlin-kapt = { id = "org.jetbrains.kotlin.kapt", version.ref = "kotlin" }


// build.gradle

plugins {
  alias libs.plugins.kotlin.android  // 'kotlin-android'
  alias libs.plugins.kotlin.kapt     // 'kotlin-kapt'
}

android {
  composeOptions {
    kotlinCompilerExtensionVersion libs.versions.compose.compiler.get()
  }
}

また、最新の Kotlin と Jetpack Compose Compiler のバージョンを確認するには、それぞれ以下を参照します。

👉 Maven Repository: org.jetbrains.kotlin » kotlin-gradle-plugin hatena-bookmark
👉 androidx.compose.compiler.compiler - Google's Maven Repository hatena-bookmark

 

🔗 まとめ

Android Studio のビルドシステムには、Android Gradle Plugin が使用されており、AGP は Gradle に依存しています。

また、Kotlin と Jetpack Compose Compiler、そして SDK のライブラリ同士にも依存関係があります。

最新バージョンにアップデートする際には、それらのつながりを確認し、必要に応じて一括でアップデートすることをおすすめします。

コアな部分は Stable 最新バージョンで揃える、とすると、以下が、今現在ベストでしょう。


Android Studio
Flamingo | 2022.2.1

  ↕︎

Android Gradle Plugin
8.0.1

  ↕︎

Gradle
8.1.1


Kotlin
1.8.21

  ↕︎

Jetpack Compose Compiler
1.4.7

すぐ忘れるので libs.versions.toml にコメントで貼りつける。


[versions]
# Android Studio - AGP - Gradle
# Kotlin - Compose Compiler
#
# Android Studio version	Required plugin version
# Giraffe | 2022.3.1	3.2-8.1
# Flamingo | 2022.2.1	3.2-8.0
# https://developer.android.com/studio/releases#android_gradle_plugin_and_android_studio_compatibility
#
# Plugin version	Minimum required Gradle version
# 8.2	8.1
# 8.1	8.0
# 8.0	8.0
# https://developer.android.com/studio/releases/gradle-plugin#updating-gradle
#
# Android Studio :
# https://developer.android.com/studio
#
# Android Gradle Plugin :
# https://maven.google.com/web/index.html#com.android.application:com.android.application.gradle.plugin
#
# Gradle :
# https://services.gradle.org/distributions/
android-gradle-plugin = "8.0.1"

# Compose Compiler Version	Compatible Kotlin Version
# 1.4.7	1.8.21
# 1.4.6	1.8.20
# 1.4.5	1.8.20
# 1.4.4	1.8.10
# https://developer.android.com/jetpack/androidx/releases/compose-kotlin#pre-release_kotlin_compatibility
#
# compose-compiler :
# https://maven.google.com/web/index.html?authuser=0#androidx.compose.compiler:compiler
#
# kotlin :
# https://mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-gradle-plugin
# https://central.sonatype.com/artifact/org.jetbrains.kotlin/kotlin-gradle-plugin/
compose-compiler = "1.4.7"
kotlin = "1.8.21"

コメント内URLからリンクをブラウザを開くのは、⌘ (command) + click で。