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:

export PHOSPHO_API_KEY="your_api_key"
export PHOSPHO_PROJECT_ID="your_project_id"

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+.

pip install --upgrade phospho

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.

3. Get insights in the dashboard

phospho run analytics pipelines on the messages logged. Discover the insights in the phospho dashboard.

Next steps