• EnglishEspañol日本語한국어Português
  • ログイン今すぐ開始

この機械翻訳は、参考として提供されています。

In the event of any inconsistency between the English version and the translated version, the English versionwill take priority. Please visit this page for more information.

問題を作成する

set_llm_token_count_callback (Python エージェント API)

構文

newrelic.agent.set_llm_token_count_callback(callback, application=None)

大規模言語モデル(LLM) (LLM) イベントでのカウントの計算に使用されるバックコール関数を登録します。

要件

Python エージェント バージョン 9.8.0 以降。

説明

この API は、 LlmEmbeddingおよびLlmChatCompletionMessageイベントのトークン数を計算して保存するコールバックを登録します。

  • この関数は、 ai_monitoring.record_content.enabledfalseに設定されている場合に使用する必要があります。 この設定により、トークン数がサーバー側で付加される New Relic サーバーにエージェントが AI コンテンツを送信できなくなります。
  • それでも LLM イベントのトークン数を取得したい場合は、アプリコードにコールバックを実装してローカルでトークン数を特定し、この情報を New Relic に送信できます。

ほとんどの場合、この API は 1 回だけ呼び出されますが、この API を複数回呼び出すこともできます。 エンドポイントに対して新しい呼び出しが行われるたびに、以前に登録されたコールバックが、提供された新しいコールバックで上書きされます。 コールバックを完全に設定解除するには、元のコールバックの代わりにNoneを渡します。

API問題

パラメータ

説明

callback

呼び出し可能またはなし

必須。 トークン数を計算するコールバック。 現在のコールバックの設定を解除するには、コールバック関数の代わりにNoneを渡します。

application

物体

オプション。 APIコールを関連付ける特定のアプリケーションオブジェクト。 アプリケーション オブジェクトは、 newrelic.agent.application関数を使用して取得できます。

戻り値

なし。

コールバック要件

提供されたコールバックは、正の整数のトークン カウント値を返す必要があります。そうしないと、LLM イベントでトークン カウントがキャプチャされません。

コールバックパラメータ

パラメータ

説明

model

ストリング

必須。 LLM モデルの名前。

content

ストリング

必須。 メッセージの内容/プロンプトまたは埋め込み入力。

トークン数の計算とコールバックの登録

tiktokenの例:

import newrelic.agent
def token_count_callback(model, content):
"""
Calculate token counts locally based on the model being used and the content.
This callback will be invoked for each message sent or received during a LLM call.
If the application supports more than one model, it may require finding libraries for
each model to support token counts appropriately.
Arguments:
model -- name of the LLM model
content -- the LLM message content
"""
import tiktoken
try:
enc = tiktoken.encoding_for_model(model)
except KeyError:
return None # Unknown model
return len(enc.encode(content))
newrelic.agent.set_llm_token_count_callback(token_count_callback)

渡されたアプリケーション オブジェクトを使用した API の使用例:

application = newrelic.agent.register_application(timeout=10.0)
newrelic.agent.set_llm_token_count_callback(token_count_callback, application)
Copyright © 2024 New Relic株式会社。

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.