user_input = "What was the capital of France in 1800s?"final_response = ""response_chunks = []with client.messages.stream( max_tokens=1024, messages=[{"role": "user", "content": user_input}], model=MODEL_NAME,) as stream: for text_chunk in stream.text_stream: # Collect streamed chunks response_chunks.append(text_chunk) # Print the streamed text chunk print(text_chunk, end="", flush=True) final_response = "".join(response_chunks)
from anthropic import Anthropicfrom maxim import Maximfrom maxim.logger.anthropic import MaximAnthropicClient# Make sure MAXIM_API_KEY and MAXIM_LOG_REPO_ID are set in env variableslogger = Maxim().logger()# Initialize MaximAnthropicClientclient = MaximAnthropicClient(client=Anthropic(api_key=ANTHROPIC_API_KEY), logger=logger)
response = client.messages.create( model="claude-3-5-sonnet-20241022", max_tokens=1024, messages=[ {"role": "user", "content": "What was the capital of France in 1800s?"} ], extra_headers={"x-maxim-trace-id": trace_id})# Extract response textresponse_text = response.content[0].textprint(response_text)
Keep adding LLM calls All LLM calls with extra header x-maxim-trace-id: trace_id will add it to the declared trace.
from anthropic import Anthropicfrom maxim import Maximfrom maxim.logger.anthropic import MaximAnthropicClient# Make sure MAXIM_API_KEY and MAXIM_LOG_REPO_ID are set in env variableslogger = Maxim().logger()# Initialize MaximAnthropicClientclient = MaximAnthropicClient(client=Anthropic(api_key=ANTHROPIC_API_KEY), logger=logger)
Create a new trace externally and add it to a session
Copy
Ask AI
from uuid import uuid4# use this session id to add multiple traces in one sessionsession_id = str(uuid4())trace_id = str(uuid4())trace = logger.trace({ "id": trace_id, "name": "Trace name", "session_id": session_id})
Make LLM calls and use this trace id
Copy
Ask AI
messages = [ {"role": "user", "content": "Hello, can you help me with Python programming?"}]response = client.messages.create( model="claude-3-5-sonnet-20241022", max_tokens=1024, messages=messages, extra_headers={"x-maxim-trace-id": trace_id})# Extract response textresponse_text = response.content[0].textprint(response_text)# Continue the conversation - add assistant's response to messagesmessages.append({"role": "assistant", "content": response_text})messages.append({"role": "user", "content": "Can you write a simple Python function?"})# Make another call with the same trace_id to continue the conversationresponse2 = client.messages.create( model="claude-3-5-sonnet-20241022", max_tokens=1024, messages=messages, extra_headers={"x-maxim-trace-id": trace_id})response_text2 = response2.content[0].textprint(response_text2)
Create additional traces in the same session To add more conversations to the same session, create new traces with the same session_id:
Copy
Ask AI
# Create another trace in the same sessiontrace_id_2 = str(uuid4())trace2 = logger.trace({ "id": trace_id_2, "name": "Second conversation", "session_id": session_id # Same session_id to group conversations})# Make calls with the new trace_idresponse3 = client.messages.create( model="claude-3-5-sonnet-20241022", max_tokens=1024, messages=[{"role": "user", "content": "Tell me about machine learning"}], extra_headers={"x-maxim-trace-id": trace_id_2})