オントロジーの構築ファンクションオントロジーの編集ストリーミング関数

注: 以下の翻訳の正確性は検証されていません。AIPを利用して英語版の原文から機械的に翻訳されたものです。

ストリーミング関数

Python および TypeScript v2 の関数は、実行結果をチャンク単位でストリーミングできます。これは、データが時間の経過とともに利用可能になる場合に有用で、結果を利用するアプリケーションは、結果が完成する前に処理を開始できます。

レスポンスをストリーミングする関数を記述する

レスポンスをストリーミングするには、Python 関数に Iterable[T] の戻り値の型をアノテーションで指定し、yield キーワードを使って値が利用可能になった時点で出力する必要があります。TypeScript では、function キーワードの後に * 文字を追加して非同期ジェネレーター関数を宣言し、戻り値の型に AsyncIterable<T> を指定し、yield キーワードを使って値が利用可能になった時点で出力する必要があります。

次の例は、各整数の間に1秒の間隔を空けて、関数が整数のストリームを返す方法を示しています。

Copied!
1 2 3 4 5 6 7 8 9 from functions.api import function from typing import Iterable import time @function def my_lazy_number_generator(n: int) -> Iterable[int]: for i in range(n): time.sleep(1) yield i
Copied!
1 2 3 4 5 6 7 8 import { Integer } from "@osdk/functions"; export default async function* myLazyNumberGenerator(n: Integer): AsyncIterable<Integer> { for (let i = 0; i < n; i++) { await new Promise(resolve => setTimeout(resolve, 1_000)); yield i; } }

レスポンスをストリーミングする関数は、出力全体の生成に時間がかかる可能性がある言語モデルを扱う場合に有用です。モデルがレスポンスの各チャンクを生成するたびにそのチャンクを yield することで、レスポンス全体の完成を待たずにリアルタイムの体験を提供できます。関数内での言語モデルの呼び出しについて詳しくは、TypeScript v2 および Python 関数での言語モデルを参照してください。

次の例では、openai SDK を使って言語モデルを呼び出し、リクエストの stream フラグを有効にすることで、レスポンスをストリーミングで返します。

Copied!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 from openai import OpenAI from functions.api import function from functions.aliases import model from foundry_sdk.v2.language_models import ( get_openai_base_url, get_foundry_token, get_http_client, ) from typing import Iterable @function def create_chat_completion(prompt: str) -> Iterable[str]: client = OpenAI( api_key=get_foundry_token(preview=True), base_url=get_openai_base_url(preview=True), http_client=get_http_client(preview=True), ) stream = client.chat.completions.create( model=model("gpt55").rid, messages=[ { "role": "user", "content": prompt, }, ], stream=True ) for event in stream: if event.choices: content = event.choices[0].delta.content if content: yield content
Copied!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 import { PlatformClient } from "@osdk/client"; import OpenAI from "openai"; import { Aliases } from "@osdk/functions"; import { getFoundryToken, getOpenAiBaseUrl, createFetch } from "@osdk/language-models"; export default async function* createChatCompletion(client: PlatformClient, prompt: string): AsyncIterable<string> { const oaiClient = new OpenAI({ apiKey: await getFoundryToken(client), baseURL: getOpenAiBaseUrl(client), fetch: createFetch(client), }); const stream = await oaiClient.chat.completions.create({ model: Aliases.model("gpt55").rid, messages: [ { role: 'user', content: prompt }, ], stream: true }); for await (const event of stream) { const content = event.choices[0]?.delta?.content; if (content) { yield content; } } }

オントロジー SDK を通じてストリーミング関数を呼び出す

ストリーミング関数にタグを付けて公開すると、React アプリケーション、Workshop のカスタムウィジェット、または別の関数から、オントロジー SDK を通じて実行できます。

ベータ版

オントロジー SDK を通じた、レスポンスをストリーミングする関数の実行機能は、開発のベータ版段階にあります。開発が活発に進められている間は、機能が変更される可能性があります。

Copied!
1 2 3 4 5 6 from foundry_sdk_runtime import AllowBetaFeatures with AllowBetaFeatures(): with client.ontology.queries.create_chat_completion_streaming(prompt="Where is Mount Everest?") as stream: for text in stream: # ...
Copied!
1 2 3 4 5 6 7 8 9 10 11 12 import { __EXPERIMENTAL__NOT_SUPPORTED_YET__executeStreamingFunction } from "@osdk/api/unstable"; const stream = client(__EXPERIMENTAL__NOT_SUPPORTED_YET__executeStreamingFunction).executeStreamingFunction( createChatCompletion, { prompt: "Where is Mount Everest?", } ); for await (const text of stream) { // ... }