A Session is a sequence of traces representing a single session (e.g. a session or a thread). Each response is represented as its own trace, but these traces are linked together by being part of the same session.
To associate traces together, you need to pass in a special metadata key where the value is the unique identifier for that thread.
Below is an example of logging conversations:
First make sure you have the required dependancies installed
pipinstallopeninfernce-instrumentation
Below is an example of how to use openinference.instrumentation to the traces created.
import uuidimport openaifrom openinference.instrumentation import using_sessionfrom openinference.semconv.trace import SpanAttributesfrom opentelemetry import traceclient = openai.Client()session_id =str(uuid.uuid4())tracer = trace.get_tracer(__name__)@tracer.start_as_current_span(name="agent", attributes={SpanAttributes.OPENINFERENCE_SPAN_KIND: "agent"})defassistant(messages: list[dict],session_id:str=str,): current_span = trace.get_current_span() current_span.set_attribute(SpanAttributes.SESSION_ID, session_id) current_span.set_attribute(SpanAttributes.INPUT_VALUE, messages[-1].get('content'))# Propagate the session_id down to spans crated by the OpenAI instrumentation# This is not strictly necessary, but it helps to correlate the spans to the same sessionwithusing_session(session_id): response = client.chat.completions.create( model="gpt-3.5-turbo", messages=[{"role": "system", "content": "You are a helpful assistant."}] + messages, ).choices[0].message current_span.set_attribute(SpanAttributes.OUTPUT_VALUE, response.content)return responsemessages = [{"role":"user","content":"hi! im bob"}]response =assistant( messages, session_id=session_id,)messages = messages + [ response,{"role":"user","content":"what's my name?"}]response =assistant( messages, session_id=session_id,)
The easiest way to add sessions to your application is to install @arizeai/openinfernce-core
npminstall@arizeai/openinference-core--save
You now can use either the session.id semantic attribute or the setSession utility function from openinference-core to associate traces with a particular session:
import { trace } from"@opentelemetry/api";import { SemanticConventions } from"@arizeai/openinference-semantic-conventions";import { context } from"@opentelemetry/api";import { setSession } from"@arizeai/openinference-core";consttracer=trace.getTracer("agent");constclient=newOpenAI({ apiKey:process.env["OPENAI_API_KEY"],// This is the default and can be omitted});asyncfunctionassistant(params: { messages: { role:string; content:string }[]; sessionId:string;}) {returntracer.startActiveSpan("agent",async (span:Span) => {span.setAttribute(SemanticConventions.OPENINFERENCE_SPAN_KIND,"agent");span.setAttribute(SemanticConventions.SESSION_ID,params.sessionId);span.setAttribute(SemanticConventions.INPUT_VALUE, messages[messages.length-1].content, );try {// This is not strictly necessary but it helps propagate the session ID// to all child spansreturncontext.with(setSession(context.active(), { sessionId:params.sessionId }),async () => {// Calls within this block will generate spans with the session ID setconstchatCompletion=awaitclient.chat.completions.create({ messages:params.messages, model:"gpt-3.5-turbo", });constresponse=chatCompletion.choices[0].message;span.setAttribute(SemanticConventions.OUTPUT_VALUE,response.content);span.end();return response; }, ); } catch (e) {span.error(e); } });}constsessionId=crypto.randomUUID();let messages = [{ role:"user", content:"hi! im Tim" }];constres=awaitassistant({ messages, sessionId: sessionId,});messages = [res, { role:"assistant", content:"What is my name?" }];awaitassistant({ messages, sessionId: sessionId,});