Documentation Index
Fetch the complete documentation index at: https://wb-21fd5541-feature-automate-reference-docs-generation.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
Weaveは Anthropic Python library を通じて行われるLLM呼び出しを、 weave.init() が呼び出された後に自動的に追跡およびログに記録します。
トレース
開発中および本番環境の両方で、LLMアプリケーションのトレースを中央データベースに保存することが重要です。これらのトレースはデバッグに使用し、アプリケーションの改善に役立つデータセットとして活用します。
Weaveは anthropic-sdk-python のトレースを自動的にキャプチャします。通常通りライブラリを使用し、まず weave.init()を呼び出します:
import weave
# use the anthropic library as usual
import os
from anthropic import Anthropic
# highlight-next-line
weave.init("anthropic_project")
client = Anthropic(
api_key=os.environ.get("ANTHROPIC_API_KEY"),
)
message = client.messages.create(
max_tokens=1024,
messages=[
{
"role": "user",
"content": "Tell me a joke about a dog",
}
],
model="claude-3-opus-20240229",
)
print(message.content)
これでWeaveはAnthropicライブラリを通じて行われるすべてのLLM呼び出しを追跡し、ログに記録します。WeaveのWebインターフェースでトレースを確認できます。
私たちはanthropicの Messages.create メソッドにパッチを適用して、LLM呼び出しを追跡します。
これでWeaveはAnthropicを通じて行われるすべてのLLM呼び出しを追跡し、ログに記録します。WeaveのWebインターフェースでログとインサイトを確認できます。
独自のopsでラップする
Weave opsは結果を 再現可能 にします。実験中にコードを自動的にバージョン管理し、入力と出力をキャプチャします。単に @weave.op() でデコレートされた関数を作成して Anthropic.messages.create を呼び出すと、Weaveが入力と出力を追跡します。これをネストされた例で見てみましょう:
import weave
import os
from anthropic import Anthropic
# highlight-next-line
weave.init("anthropic_project")
client = Anthropic(
api_key=os.environ.get("ANTHROPIC_API_KEY"),
)
# highlight-next-line
@weave.op()
def call_anthropic(user_input:str, model:str) -> str:
message = client.messages.create(
max_tokens=1024,
messages=[
{
"role": "user",
"content": user_input,
}
],
model=model,
)
return message.content[0].text
# highlight-next-line
@weave.op()
def generate_joke(topic: str) -> str:
return call_anthropic(f"Tell me a joke about {topic}", model="claude-3-haiku-20240307")
print(generate_joke("chickens"))
print(generate_joke("cars"))
より簡単な実験のために Model を作成する
多くの要素がある場合、実験の整理は困難です。 Model クラスを使用することで、システムプロンプトや使用しているモデルなど、アプリの実験的な詳細をキャプチャして整理できます。これにより、アプリの異なるイテレーションを整理して比較するのに役立ちます。
コードのバージョン管理と入出力のキャプチャに加えて、 Model はアプリケーションの動作を制御する構造化されたパラメータをキャプチャし、どのパラメータが最適に機能したかを簡単に見つけることができます。また、Weave Modelsを serve、および Evaluation と一緒に使用することもできます。
以下の例では、 model と temperature を実験できます。これらのいずれかを変更するたびに、新しい バージョン の JokerModel が得られます。
import weave
# use the anthropic library as usual
import os
from anthropic import Anthropic
weave.init('joker-anthropic')
class JokerModel(weave.Model): # Change to `weave.Model`
model: str
temperature: float
@weave.op()
def predict(self, topic): # Change to `predict`
client = Anthropic()
message = client.messages.create(
max_tokens=1024,
messages=[
{
"role": "user",
"content": f"Tell me a joke about {topic}",
}
],
model=self.model,
temperature=self.temperature
)
return message.content[0].text
joker = JokerModel(
model="claude-3-haiku-20240307",
temperature = 0.1)
result = joker.predict("Chickens and Robots")
print(result)
ツール(関数呼び出し)
Anthropicは tools インターフェースを関数呼び出しのために提供しています。Weaveはこれらの関数呼び出しを自動的に追跡します。
message = client.messages.create(
max_tokens=1024,
messages=[
{
"role": "user",
"content": "What's the weather like in San Francisco?",
}
],
tools=[
{
"name": "get_weather",
"description": "Get the current weather in a given location",
"input_schema": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
}
},
"required": ["location"],
},
},
],
model=model,
)
print(message)
プロンプトで使用したツールを自動的にキャプチャし、バージョン管理します。
