Code Integration
- Import the required modules, SDK components, and constants.
import os
from logspend_sdk.core import LogBuilder, LogSpendLogger
from logspend_sdk.constants import Provider
- Retrieve the API key from the environment variable.
api_key = os.environ.get('LOGSPEND_API_KEY')
if not api_key:
raise ValueError("Missing LogSpend API Key. Ensure the LOGSPEND_API_KEY environment variable is set.")
- Initialize the LogSpend Logger.
logger = LogSpendLogger(api_key=api_key, project_id="<YOUR_LOGSPEND_PROJECT_ID>")
- Prepare your input data and instantiate the
LogBuilder
before calling the LLM API. Input data is identical to what the OpenAI API expects.
input_data = {
"provider": Provider.OPENAI.value,
"model": "gpt-3.5-turbo-instruct",
"prompt": "Say this is a test", # Not needed for OpenAI integrations as everything is contained in the messages array below
"messages": [
{
"role": "system",
"content": "You are a helpful customer support assistant"
},
{
"role": "user",
"content": "Hello, can I get help with my booking"
},
{
"role": "assistant",
"content": "",
"function_call": {}
}
],
"functions": [{}],
"max_tokens": 7,
"temperature": 0
}
builder = LogBuilder(input_data)
- Set the identity on the
LogBuilder
instance. This helps to identify the user initiating the interactions. This must contain a session identifiersession_id
to encapsulate all interactions belonging to the same user session. This could be a chat ID, conversation ID, document ID or any relevant entity ID for your use case. Additionally, you can also include auser_id
to represent the user (this could be an internal identifier, an email address, device ID, cookie identifier or any relevant unique ID to identify the user or visitor). Same applies to the optionalip_address
field useful for enriching logs with user geographic information like city, country, and region.
identity_data = {
"session_id": "session-123",
"user_id": "123455",'
"ip_address": "0.0.0.0",
}
builder.set_identity(identity_data)
- Set any relevant custom properties on the
LogBuilder
instance. These custom properties can be used to filter on the data or create custom metrics and graphs on the LogSpend dashboard. Examples of custom properties aretask_name
,customer_id
, etc.
custom_properties_data = {
"task_name": "chatbot-qa",
"customer_id": "chatbot-qa",
}
builder.set_custom_properties(custom_properties_data)
- Make a call to OpenAI (or whichever provider you're using) to generate the
output_data
.
def call_openai(input_data):
# Placeholder for actual OpenAI call
# The fields below map well to the typical output of an OpenAI call with the exception of the http status code and optional error message
return {
"id": "chatcmpl-123",
"choices": [{
"index": 0,
"message": {
"role": "assistant",
"content": "\n\nHello there, I'm happy to assist you today. What is your preferred destination?",
},
"finish_reason": "stop"
}]
"usage": {
"prompt_tokens": 9,
"completion_tokens": 12,
"total_tokens": 21
},
"http_status_code": 200,
"http_error_message": "",
}
output_data = call_openai(input_data)
- Set the output on the
LogBuilder
instance only after the API call to the LLM. Setting this in this order enables us to reliably estimate the latency. Output data is almost identical to what the OpenAI API returns.
builder.set_output(output_data)
- Send the log to LogSpend.
logger.send(builder.build())