Setup logging in your app
You can setup the logging to phospho in your app in a few minutes.
1. Get your phospho API key and your project id
Go to the phospho platform. Login or create an account if you don't have one.
If this is your first time using phospho, a Default project has been created for you. On the main page, note down the project id and follow the link to create a new API key.
If you already have a project, go to Settings. Your project id is displayed on the top of the page. To create an API key, click on the Manage Organization & API keys button. Store your API key safely!
2. Setup phospho logging in your app
Add environment variables
In your code, add the following environment variables:
Log to phospho
The basic abstraction of phospho is the task. If you're a programmer, you can think of tasks like a function.
input (str)
: The text that goes into the system. Eg: the user message.output (Optional[str])
: The text that comes out of the system. Eg: the system response.
We prefer to use this abstraction because of its flexibility. You can log any text to a task, not just chat messages: call to an LLM, answering a question, searching in documents, summarizing a text, performing inference of a model, steps of a chain-of-thought...
Tasks can be grouped into sessions. Tasks and Sessions can be attached to users.
How to setup logging?
The phospho Python module in the easiest way to log to phospho. It is compatible with Python 3.9+.
To log tasks, use phospho.log
. The logged tasks are analyzed by the phospho analytics pipeline.
import phospho
#Â By default, phospho reads the PHOSPHO_API_KEY and PHOSPHO_PROJECT_ID from the environment variables
phospho.init()
# Example
input = "Hello! This is what the user asked to the system"
output = "This is the response showed to the user by the app."
# This is how you log a task to phospho
phospho.log(
input=input,
output=output,
# Optional: for chats, group tasks together in sessions
#Â session_id = "session_1",
#Â Optional: attach tasks to users
#Â user_id = "user_1",
#Â Optional: add metadata to the task
#Â metadata = {"system_prompt": "You are a helpful assistant."},
)
-
More about logging in Python
Did you know you could log OpenAI completions, streaming outputs and metadata? Learn more by clicking here.
The phospho JavaScript module is the easiest way to log to phospho. It is compatible with Node.js.
Types are available for your Typescript codebase.
To log tasks, use phospho.log
. The logged tasks are analyzed by the phospho analytics pipeline.
import { phospho } from "phospho";
//Â By default, phospho reads the PHOSPHO_API_ID and PHOSPHO_PROJECT_KEY from the environment variables
phospho.init();
// Example
const input = "Hello! This is what the user asked to the system";
const output = "This is the response showed to the user by the app.";
// This is how you log a task to phospho
phospho.log({
input,
output,
// Optional: for chats, group tasks together in sessions
// session_id: "session_1",
// Optional: attach tasks to users
// user_id: "user_1",
// Optional: add metadata to the task
// metadata: { system_prompt: "You are a helpful assistant." },
});
-
More about logging in Javascript
Did you know you could log OpenAI completions, streaming outputs and metadata? Learn more by clicking here.
You can directly log to phospho using the /log endpoint of the API.
curl -X POST https://api.phospho.ai/v2/log/$PHOSPHO_PROJECT_ID \
-H "Authorization: Bearer $PHOSPHO_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"batched_log_events": [
{
"input": "your_input",
"output": "your_output",
"session_id": "session_1",
"user_id": "user_1",
"metadata": {"system_prompt": "You are a helpful assistant."},
}
]
}'
Info
The session_id
, user_id
and metadata
fields are optional.
-
API reference
Create a tailored integration with the API. Learn more by clicking here.
We provide a Langchain callback in our Python module.
from phospho.integrations import PhosphoLangchainCallbackHandler
chain = ... #Â Your Langchain agent or chain
chain.invoke(
"Your chain input",
#Â Add the callback handler to the config
config={"callbacks": [PhosphoLangchainCallbackHandler()]},
)
-
Langchain guide
Customize what is logged to phospho by customizing the callback. Learn more by clicking here.
Integrate phospho to your Supabase app is as simple as using the phospho API.
Note
Follow the Supabase guide to leverage the power of product analytics in your Supabase app!
-
Read the supabase guide
Get started with Supabase and phospho. Learn more by clicking here.
3. Get insights in the dashboard
phospho run analytics pipelines on the messages logged. Discover the insights in the phospho dashboard.
Next steps
-
Automatic tagging
Automatically annotate your text data and be alerted. Take action.
-
Unsupervised clustering
Group users' messages based on their intention. Find out what your users are talking about.
-
AB Testing
Run experiments and iterate on your LLM app, while keeping track of performances. Keep shipping.
-
Flexible evaluation pipeline
Discover how to run and design a text analytics pipeline using natural language. No code needed.
-
User analytics
Detect user languages, sentiment, and more. Get to know power users.