人類学¶
ご紹介¶
Logfireは、Anthropicの呼び出しを検出するための追加のコード行をサポートしています。
import anthropic
import logfire
client = anthropic.Anthropic()
logfire.configure()
logfire.instrument_anthropic(client) # (1)!
response = client.messages.create(
max_tokens=1000,
model='claude-3-haiku-20240307',
system='You are a helpful assistant.',
messages=[{'role': 'user', 'content': 'Please write me a limerick about Python logging.'}],
)
print(response.content[0].text)
-
クライアントインスタンスにアクセスする権限がない場合は、クラス(
logfire.instrument_anthropic(anthropic.Anthropic)など)を渡すか、引数を渡さずに(logfire.instrument_anthropic()など)两者。人类学クラスと人类学。AsyncAnthropicクラスを検出できます。
詳細については、[instrument_anthropic() APIリファレンス][logfire. Logfire.device_anthropic]を参照してください。
それを得ることができます。
-
Anthropicへの呼び出しのスパンを中心に、呼び出しは期間を記録し、発生する可能性のある例外をキャプチャします
-
座席との会話の人間可読表示
-
使用されたトークンの数など、応答の詳細
カバーする方法¶
以下の人工的な方法をカバーしています:
client.messages.createclient.messages.stream(客户端.messages.stream)client.beta.tools.messages.create
すべてのメソッドは 人为的方法。人类学 と 人类学。异步人。 をカバーしています
ストリーミング応答¶
ストリーミングレスポンスを検出すると、Logfireは2つのスパンを作成します。
ここでは、リッチのLive とMarkdown タイプを使用して、ターミナルでレスポンスをリアルタイムでレンダリングします。:ダンサー:
import anthropic
import logfire
from rich.console import Console
from rich.live import Live
from rich.markdown import Markdown
client = anthropic.AsyncAnthropic()
logfire.configure()
logfire.instrument_anthropic(client)
async def main():
console = Console()
with logfire.span('Asking Anthropic to write some code'):
response = client.messages.stream(
max_tokens=1000,
model='claude-3-haiku-20240307',
system='Reply in markdown one.',
messages=[{'role': 'user', 'content': 'Write Python to show a tree of files 🤞.'}],
)
content = ''
with Live('', refresh_per_second=15, console=console) as live:
async with response as stream:
async for chunk in stream:
if chunk.type == 'content_block_delta':
content += chunk.delta.text
live.update(Markdown(content))
if __name__ == '__main__':
import asyncio
asyncio.run(main())
Logfireでは以下のようになります:
本文总阅读量次