Log to phospho with Python
Collect interactions and tasks
Log tasks to phospho
Tasks are the basic bricks that make up your LLM apps. If you’re a programmer, you can think of tasks like functions.
A task is made of at least two things:
input (str)
: What goes into a task. Eg: what the user asks to the assistant.output (Optional[str])
: What goes out of the task. Eg: what the assistant replied to the user.
Example of tasks you can log to phospho:
- Call to an LLM (input = query, output = llm response)
- Answering a question (input = question, output = answer)
- Searching in documents (input = search query, output = document)
- Summarizing a text (input = text, output = summary)
- Performing inference of a model (input = X, output = y)
Install phospho module
The phospho Python module in the easiest way to log to phospho. It is compatible with Python 3.9+.
The phospho module is an open source work in progress. Your help is deeply appreciated!
Initialize phospho
In your app, initialize the phospho module. By default, phospho will look for PHOSPHO_API_KEY
and PHOSPHO_PROJECT_ID
environment variables.
Learn how to get your api key and project id by clicking here!
You can also pass the api_key
and project_id
parameters to phospho.init
.
Log with phospho.log
The most minimal way to log a task is to use phospho.log
.
Logging text inputs and outputs
Note that the output is optional. If you don’t pass an output, phospho will log None
.
Logging OpenAI queries and responses
phospho aims to be battery included. So if you pass something else than a str
to phospho.log
, phospho extracts what’s usually considered “the input” or “the output”.
For example, if you use the OpenAI API:
Result:
Custom extractors
Pass custom extractors to phospho.log
to extract the input and output from any object. The original object will be converted to a dict (if jsonable) or a string and stored in raw_input
and raw_output
.
Logging additional metadata
You can log additional data with each interaction (user id, version id,…) by passing arguments to phospho.log
.
Wrapping functions’ arguments and values
Wrap any function with phospho.wrap
to automatically log an interaction when they are called.
- The passed arguments are logged as input
- The returned value is logged as output
Use the phospho.wrap decorator
Like phospho.log, every extra keyword argument is logged as metadata.
How to log metadata with phospho.wrap?
Wrap an imported function with phospho.wrap
If you can’t change the function definition, you can wrap it this way.
If you want to wrap all calls to a function, you can override the function definition.
Streaming
phospho supports streamed outputs. This is useful when you want to log the output of a streaming API.
Example with phospho.log
Pass stream=True
to phospho.log
to handle streaming responses. When iterating over the response, phospho will automatically log each chunk until the iteration is completed.
For example, you can pass streaming OpenAI responses to phospho.log
the following way:
Example with phospho.wrap
- Pass
stream=True
. This tells phospho to concatenate the string outputs. - Pass a
stop
function, such thatstop(output) is True
when the streaming is finished and trigger the logging of the task.
Example with a generator
For generators, you need to wrap it with phospho.MutableGenerator
or phospho.MutableAsyncGenerator
.
Was this page helpful?