ボルトオンのガバナンスAPIを使う¶
This notebook outlines how to use the Bolt-on Governance API with deployed LLM blueprints. LLM blueprints deployed from the playground implement the chat() hook in the custom model's custom.py file by default.
OpenAI APIの公式Pythonライブラリを使用して、DataRobot LLMブループリントデプロイにチャット補完リクエストを行うことができます。
!pip install openai
from openai import OpenAI
LLMブループリントのデプロイのIDとDataRobot APIトークンを指定します。
DEPLOYMENT_ID = "<SPECIFY_DEPLOYMENT_ID_HERE>"
DATAROBOT_API_TOKEN = "<SPECIFY_TOKEN_HERE>"
DEPLOYMENT_URL = f"https://app.datarobot.com/api/v2/deployments/{DEPLOYMENT_ID}"
OpenAIクライアントを作成するには、以下のコードを使用します。
client = OpenAI(base_url=DEPLOYMENT_URL, api_key=DATAROBOT_API_TOKEN)
チャット補完を要求するには、以下のコードを使用します。modelパラメーターの指定の詳細については、以下の注意事項を参照してください。リクエストにシステムメッセージを指定すると、LLMブループリントで設定されたシステムプロンプトがオーバーライドされます。リクエストで他の設定(max_completion_tokensなど)を指定すると、LLMブループリントの設定がオーバーライドされます。
completion = client.chat.completions.create(
model="datarobot-deployed-llm",
messages=[
{"role": "system", "content": "Answer with just a number."},
{"role": "user", "content": "What is 2+3?"},
{"role": "assistant", "content": "5"},
{"role": "user", "content": "Now multiply the result by 4."},
{"role": "assistant", "content": "20"},
{"role": "user", "content": "Now divide the result by 2."},
],
)
print(completion)
ストリーミングが無効な場合はChatCompletionオブジェクトを返し、ストリーミングが有効な場合はIterator[ChatCompletionChunk]を返します。ストリーミングレスポンスでのチャット補完を要求するには、以下のセルを使用します。
streaming_response = client.chat.completions.create(
model="datarobot-deployed-llm",
messages=[
{"role": "system", "content": "Explain your thoughts using at least 100 words."},
{"role": "user", "content": "What would it take to colonize Mars?"},
],
stream=True,
)
for chunk in streaming_response:
content = chunk.choices[0].delta.content
if content is not None:
print(content, end="")
citationsを返すには、デプロイされたLLMにベクターデータベースが関連付けられている必要があります。completionは、引用に関連し、カスタムモデルからアクセス可能なキーを返します。
関連付けIDとカスタム指標の指定¶
DataRobotにデプロイされたテキスト生成またはエージェントワークフローのカスタムモデルに対してチャットリクエストを行う場合、チャットリクエストにおいて、自動生成されたIDの代わりに、カスタムの関連付けIDをオプションで指定できます。これには、チャットリクエストのextra_bodyフィールドにdatarobot_association_idを設定します。extra_bodyフィールドにdatarobot_metricsを設定することによって、デプロイに対して定義された任意のカスタム指標の値を報告することもできます。これを行うには、チャットリクエストのオプションのextra_bodyフィールドでこれらの値を定義します。extra_bodyフィールドは、OpenAIチャットリクエストにパラメーターを追加する標準的な方法であり、チャットクライアントがモデル固有のパラメーターをLLMに渡すことができます。
datarobot_association_idフィールドがextra_bodyにある場合、DataRobotでは、自動的に生成された値ではなく、その値が使われます。datarobot_metricsフィールドがextra_bodyにある場合、DataRobotでは、その中にあるすべてのname:valueのペアのカスタム指標が報告されます。各名前に一致するカスタム指標が、デプロイですでに定義されている必要があります。この方法で報告されるカスタム指標値は、数値である必要があります。
extra_bodyが指定されているかどうかに関係なく、デプロイされたカスタムモデルには、DataRobotがチャットリクエストからカスタム指標を処理するために、関連付けID列が定義されている必要があります。指標を処理するためには、カスタムモデルに対してモデレーションを設定する必要があります。
extra_body = {
# These values pass through to the LLM
"llm_id": "azure-gpt-6",
# If set here, replaces the auto-generated association ID
"datarobot_association_id": "my_association_id_0001",
# DataRobot captures these for custom metrics
"datarobot_metrics": {
"field1": 24,
"field2": 25
}
}
completion = client.chat.completions.create(
model="datarobot-deployed-llm",
messages=[
{"role": "system", "content": "Explain your thoughts using at least 100 words."},
{"role": "user", "content": "What would it take to colonize Mars?"},
],
max_tokens=512,
extra_body=extra_body
)
print(completion.choices[0].message.content)
モデレーションとガードレール¶
モデレーションのガードレールは、組織がプロンプトインジェクションや、悪意のある、有害な、または不適切なプロンプトや回答をブロックするのに役立ちます。datarobot_moderationsを返すには、デプロイされたLLMが、モデレーションライブラリがインストールされている実行環境で実行されている必要があります。また、モデレーションを設定するためのmoderation_config.yamlがカスタムモデルコードディレクトリに含まれている必要があります。モデレーションが設定されたボルトオンのガバナンスAPIを使用する際は、以下の点に注意してください。
- 回答段階でガードがない場合、モデレーションライブラリは、LLMから取得した既存のストリームを返します。
- すべての回答ガードがチャンクに適用されるわけではありません。忠実度、rouge-1、nemoのガードはチャンクには適用されません。これらのガードは、評価のために回答全体が必要であるため、使用可能な場合は回答全体に適用されます。
- If moderation is enabled and the streaming response is requested, the first chunk will always contain the information about prompt guards (if configured) and response guards (excluding faithfulness, rouge-1, and nemo). Access the chunk via
chunk.datarobot_moderations. - 最後のチャンク以外の後続のチャンクごとに、(忠実度、rouge-1、NeMoを除く)回答ガードが適用され、
chunk.datarobot_moderationsからアクセスできます。 - The last chunk has all response guards (excluding faithfulness, rouge-1 and nemo) applied to the chunk. Faithfulness, rouge-1, and nemo are applied to the whole response.
- ストリーミングが集計の場合、以下のカスタム指標がレポートされます。
- latency:チャンクでガードを実行した際のレイテンシーの合計。
- score:中間チャンクがブロックされている場合はチャンクのスコア。そうでない(チャンクがブロックされていない)場合は、組み立てられた回答のスコア。
- enforced:各チャンクに適用された指標の論理和。
注意事項¶
ボルトオンのガバナンスAPIを使用する際は、以下の点に注意してください。
- If you implement the chat completion hook without modification, the
chat()hook behaves differently than thescore()hook. Specifically, the unmodifiedchat()hook passes in themodelparameter through thecompletion_create_paramsargument while thescore()hook specifies the model in the custom model code. - If you add a deployed LLM to the playground, the validation uses the value entered into the "Chat model ID" field as the
modelparameter value. Ensure the LLM deployment accepts this value as themodelparameter. Alternatively, you can modify the implementation of thechat()hook to override the value of themodelparameter, defining the intended model (for example, using a runtime parameter). For more information, see GenAI troubleshooting. - また、Workload APIを通じて、GPUを活用した推論も可能です。このAPIでは、Hugging Face HubからマネージドvLLMサービスとしてオープンソースLLMをデプロイします。