Documentation Index
Fetch the complete documentation index at: https://wb-21fd5541-john-wbdocs-2044-rename-serverless-products.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
実行中のコードを Weave で詳細なトレースとして確認するには、calls を作成します。これには主に 3 つの方法があります。
Weave は、openai、anthropic、cohere、mistral、LangChain など、多くの一般的なインテグレーションやフレームワークと自動的に連携します。
LLM またはフレームワークのライブラリを import して Weave のプロジェクトを初期化すると、コードを追加・変更しなくても、LLM またはプラットフォームに対するすべてのcallが Weave によって自動的にプロジェクトへトレースされます。サポートされているライブラリインテグレーションの完全な一覧については、インテグレーション概要 を参照してください。
import weave
from openai import OpenAI
client = OpenAI()
# Weave トレース を初期化
weave.init('intro-example')
response = client.chat.completions.create(
model="gpt-4",
messages=[
{
"role": "user",
"content": "How are you?"
}
],
temperature=0.8,
max_tokens=64,
top_p=1,
)
import OpenAI from 'openai'
import * as weave from 'weave'
const client = new OpenAI()
// Weave トレース を初期化
await weave.init('intro-example')
const response = await client.chat.completions.create({
model: 'gpt-4',
messages: [
{
role: 'user',
content: 'How are you?',
},
],
temperature: 0.8,
max_tokens: 64,
top_p: 1,
});
JS / TS プロジェクト向けの完全なセットアップガイドについては、TypeScript SDK: サードパーティインテグレーションガイド を参照してください。
自動的な挙動をより細かく制御したい場合は、LLM call の自動トラッキングを設定する を参照してください。
LLM アプリケーションには、トラッキングしたい追加のロジック (前処理や後処理、プロンプトなど) が含まれていることがよくあります。
Weave では、@weave.op デコレータを使って、これらの calls を手動でトラッキングできます。例:import weave
# Weave トレース を初期化する
weave.init('intro-example')
# 関数をデコレートする
@weave.op
def my_function(name: str):
return f"Hello, {name}!"
# 関数を呼び出す -- Weave が入力と出力を自動的にトラッキングします
print(my_function("World"))
クラスの method もトラッキングできます。 Weave では、関数を weave.op でラップすることで、これらの calls を手動でトラッキングできます。例:import * as weave from 'weave'
await weave.init('intro-example')
function myFunction(name: string) {
return `Hello, ${name}!`
}
const myFunctionOp = weave.op(myFunction)
ラップをインラインで定義することもできます:const myFunctionOp = weave.op((name: string) => `Hello, ${name}!`)
これは関数だけでなく、クラスの method にも対応しています:class MyClass {
constructor() {
this.myMethod = weave.op(this.myMethod)
}
myMethod(name: string) {
return `Hello, ${name}!`
}
}
クラスとオブジェクトの method をトラッキングする
クラスやオブジェクトの method もトラッキングできます。weave.op で method をデコレートすると、クラス内の任意の method をトラッキングできます。
import weave
# Weave トレースを初期化する
weave.init("intro-example")
class MyClass:
# method をデコレートする
@weave.op
def my_method(self, name: str):
return f"Hello, {name}!"
instance = MyClass()
# method を呼び出す -- Weave が inputs と outputs を自動的にトラッキングします
print(instance.my_method("World"))
トレースするには、インスタンスmethod に @weave.op を適用できます。class Foo {
@weave.op
async predict(prompt: string) {
return "bar"
}
}
クラス内のユーティリティ関数を監視するために、static method にも @weave.op を適用できます。class MathOps {
@weave.op
static square(n: number): number {
return n * n;
}
}
並列 (マルチスレッド) の関数callをトレースする
デフォルトでは、並列に実行された calls はすべて、Weave で個別のルート call として表示されます。同じ親 Op の下に正しくネストするには、ThreadPoolExecutor を使用します。
次のコード例は、ThreadPoolExecutor の使用方法を示しています。
1 つ目の関数 func は、x を受け取って x+1 を返すシンプルな Op です。2 つ目の関数 outer は、入力のリストを受け取る別の Op です。
outer の内部で ThreadPoolExecutor と exc.map(func, inputs) を使用すると、func の各 call に同じ親トレースコンテキストが引き継がれます。import weave
@weave.op
def func(x):
return x+1
@weave.op
def outer(inputs):
with weave.ThreadPoolExecutor() as exc:
exc.map(func, inputs)
# Weave プロジェクト名を更新します
client = weave.init('my-weave-project')
outer([1,2,3,4,5])
この機能は、TypeScript SDK ではまだ利用できません。
Weave UI では、これにより 1 つの親 call の下に 5 つの子 calls がネストされた形で表示されるため、インクリメント処理が並列に実行されていても、完全な階層型トレースを取得できます。
API を直接使用して、手動で call を作成することもできます。
Python
TypeScript
HTTP API
import weave
# Weave トレースを初期化
client = weave.init('intro-example')
def my_function(name: str):
# call を開始
call = client.create_call(op="my_function", inputs={"name": name})
# ... 関数のコード ...
# call を終了
client.finish_call(call, output="Hello, World!")
# 関数を呼び出す
print(my_function("World"))
この機能は、TypeScript SDK ではまだ利用できません。
curl -L 'https://trace.wandb.ai/call/start' \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
-d '{
"start": {
"project_id": "string",
"id": "string",
"op_name": "string",
"display_name": "string",
"trace_id": "string",
"parent_id": "string",
"started_at": "2024-09-08T20:07:34.849Z",
"attributes": {},
"inputs": {},
"wb_run_id": "string"
}
}