-
Notifications
You must be signed in to change notification settings - Fork 208
Expand file tree
/
Copy pathmessages_parse.py
More file actions
36 lines (28 loc) · 1.17 KB
/
messages_parse.py
File metadata and controls
36 lines (28 loc) · 1.17 KB
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
35
36
from anthropic import Anthropic
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk import trace as trace_sdk
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
from pydantic import BaseModel, Field
from openinference.instrumentation.anthropic import AnthropicInstrumentor
# Configure AnthropicInstrumentor with Phoenix endpoint
endpoint = "http://127.0.0.1:6006/v1/traces"
tracer_provider = trace_sdk.TracerProvider()
tracer_provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter(endpoint)))
AnthropicInstrumentor().instrument(tracer_provider=tracer_provider)
client = Anthropic()
class SimpleResponse(BaseModel):
answer: str = Field(description="A brief answer to the question")
confidence: str = Field(description="Confidence level: high, medium, or low")
response = client.messages.parse(
max_tokens=16000,
messages=[
{
"role": "user",
"content": "What is 2 + 2? Provide a brief answer and your confidence level.",
}
],
model="claude-sonnet-4-6",
thinking={"type": "adaptive"},
output_format=SimpleResponse,
)
print(response.parsed_output)