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.
プロンプトの作成、評価、改良は、AI エンジニアにとって中核となる作業です。
プロンプトの小さな変更が、アプリケーションの挙動に大きな影響を与えることがあります。
W&B Weave を使うと、プロンプトを作成して公開し、継続的に改善していくことができます。
このページでは、プロンプトオブジェクトを作成して公開する方法を説明します。公開済みプロンプトの参照、取得、バージョン管理については、プロンプトのバージョンの保存と追跡 を参照してください。
プロンプトの要件がシンプルな場合は、組み込みの weave.StringPrompt または weave.MessagesPrompt クラスを使用できます。より複雑な要件がある場合は、それらのクラス、または基底クラス weave.Prompt をサブクラス化し、format method をオーバーライドできます。
weave.publish でプロンプトを公開すると、Weave プロジェクトの Prompts ページ に表示されます。
StringPrompt は、システムメッセージ、ユーザーのクエリ、または LLM への単独のテキスト入力として使用できる単一文字列のプロンプトをログします。複数メッセージの会話のような複雑さが不要な場合に、個別のプロンプト文字列を管理するには StringPrompt を使用することをおすすめします。
import weave
weave.init('intro-example')
system_prompt = weave.StringPrompt("You speak like a pirate")
weave.publish(system_prompt, name="pirate_prompt")
from openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{
"role": "system",
"content": system_prompt.format()
},
{
"role": "user",
"content": "Explain general relativity in one paragraph."
}
],
)
import * as weave from 'weave';
import OpenAI from 'openai';
async function main() {
// weave.init はクライアントインスタンスを返します
const weaveClient = await weave.init('wandb/prompt-examples');
const systemPrompt = new weave.StringPrompt({
content: 'You speak like a pirate',
name: 'your-prompt',
description: 'A helpful description of your prompt',
});
// init から返されたクライアントを使用します
await weaveClient.publish(systemPrompt, 'pirate_prompt');
// OpenAI クライアントをラップして Weave で call をトラッキングします
const client = weave.wrapOpenAI(new OpenAI());
const response = await client.chat.completions.create({
model: "gpt-4o",
messages: [
{
role: "system",
content: systemPrompt.content
},
{
role: "user",
content: "Explain general relativity in one paragraph."
}
],
});
}
main();
MessagesPrompt を使うと、複数ターンの会話やチャットベースのプロンプトをログできます。これは、会話全体の流れを表すメッセージオブジェクトの配列 (“system”、“user”、“assistant” などのロールを含む) を保存します。複数のメッセージにまたがってコンテキストを維持する必要があるチャットベースの LLM や、特定の会話パターンを定義したい場合、再利用可能な会話テンプレートを作成したい場合に使用することを推奨します。
import weave
weave.init('intro-example')
prompt = weave.MessagesPrompt([
{
"role": "system",
"content": "You are a stegosaurus, but don't be too obvious about it."
},
{
"role": "user",
"content": "What's good to eat around here?"
}
])
weave.publish(prompt, name="dino_prompt")
from openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
model="gpt-4o",
messages=prompt.format(),
)
import * as weave from 'weave';
import OpenAI from 'openai';
async function main() {
// weave.init はクライアントインスタンスを返します
const weaveClient = await weave.init('wandb/prompt-examples');
const prompt = new weave.MessagesPrompt({
messages: [
{
"role": "system",
"content": "You are a stegosaurus, but don't be too obvious about it."
},
{
"role": "user",
"content": "What's good to eat around here?"
}
],
});
// init が返したクライアントを使用します
await weaveClient.publish(prompt, 'dino_prompt');
// OpenAI クライアントをラップして、Weave で call をトラッキングします
const client = weave.wrapOpenAI(new OpenAI());
const response = await client.chat.completions.create({
model: "gpt-4o",
messages: prompt.messages,
});
}
main();
StringPrompt と MessagesPrompt はどちらも、パラメーター化によって動的なコンテンツをサポートしています。これにより、プレースホルダー ({variable} 構文を使用) を含む柔軟で再利用可能なプロンプトテンプレートを作成し、実行時に異なる値を埋め込めます。これは、一貫した構造を保ちながら、異なる入力、ユーザーデータ、またはコンテキストに応じてプロンプトを適応させる必要があるスケーラブルなアプリケーションを構築する際に役立ちます。format() メソッドは、これらのプレースホルダーを実際の値に置き換えるためのキーと値のペアを受け取ります。
import weave
weave.init('intro-example')
prompt = weave.StringPrompt("Solve the equation {equation}")
weave.publish(prompt, name="calculator_prompt")
from openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{
"role": "user",
"content": prompt.format(equation="1 + 1 = ?")
}
],
)
import * as weave from 'weave';
import OpenAI from 'openai';
async function main() {
// weave.init はクライアントインスタンスを返します
const weaveClient = await weave.init('wandb/prompt-examples');
const prompt = new weave.StringPrompt({
content: 'Solve the equation {equation}',
});
// init から返されたクライアントを使用します
await weaveClient.publish(prompt, 'calculator_prompt');
// Weave で call をトラッキングするために OpenAI クライアントをラップします
const client = weave.wrapOpenAI(new OpenAI());
const response = await client.chat.completions.create({
model: "gpt-4o",
messages: [
{
role: "user",
content: prompt.format({ equation: "1 + 1 = ?" })
}
],
});
}
main();
これは MessagesPrompt でも同様に機能します。
import weave
weave.init('intro-example')
prompt = weave.MessagesPrompt([
{
"role": "system",
"content": "You will be provided with a description of a scene and your task is to provide a single word that best describes an associated emotion."
},
{
"role": "user",
"content": "{scene}"
}
])
weave.publish(prompt, name="emotion_prompt")
from openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
model="gpt-4o",
messages=prompt.format(scene="A dog is lying on a dock next to a fisherman."),
)
import * as weave from 'weave';
import OpenAI from 'openai';
async function main() {
// weave.init はクライアントインスタンスを返します
const weaveClient = await weave.init('wandb/prompt-examples');
const prompt = new weave.MessagesPrompt({
messages: [
{
"role": "system",
"content": "You will be provided with a description of a scene and your task is to provide a single word that best describes an associated emotion."
},
{
"role": "user",
"content": "{scene}"
}
]
});
// init から返されたクライアントを使用します
await weaveClient.publish(prompt, 'emotion_prompt');
// Weave で call をトラッキングするために OpenAI クライアントをラップします
const client = weave.wrapOpenAI(new OpenAI());
const response = await client.chat.completions.create({
model: "gpt-4o",
messages: prompt.format({ scene: "A dog is lying on a dock next to a fisherman." }),
});
}
main();