2023-08-28 17:24:50 +00:00
# LLMonitor
2023-09-08 22:56:42 +00:00
[LLMonitor ](https://llmonitor.com?utm_source=langchain&utm_medium=py&utm_campaign=docs ) is an open-source observability platform that provides cost and usage analytics, user tracking, tracing and evaluation tools.
2023-08-28 17:24:50 +00:00
2023-08-28 17:26:33 +00:00
< video controls width = '100%' >
2023-08-28 17:24:50 +00:00
< source src = 'https://llmonitor.com/videos/demo-annotated.mp4' / >
< / video >
## Setup
2023-09-08 22:56:42 +00:00
Create an account on [llmonitor.com ](https://llmonitor.com?utm_source=langchain&utm_medium=py&utm_campaign=docs ), then copy your new app's `tracking id` .
2023-08-28 17:24:50 +00:00
Once you have it, set it as an environment variable by running:
2023-09-08 22:56:42 +00:00
2023-08-28 17:24:50 +00:00
```bash
export LLMONITOR_APP_ID="..."
```
If you'd prefer not to set an environment variable, you can pass the key directly when initializing the callback handler:
2023-09-08 22:56:42 +00:00
2023-08-28 17:24:50 +00:00
```python
from langchain.callbacks import LLMonitorCallbackHandler
handler = LLMonitorCallbackHandler(app_id="...")
```
## Usage with LLM/Chat models
2023-09-08 22:56:42 +00:00
2023-08-28 17:24:50 +00:00
```python
from langchain.llms import OpenAI
from langchain.chat_models import ChatOpenAI
from langchain.callbacks import LLMonitorCallbackHandler
2023-09-08 22:56:42 +00:00
handler = LLMonitorCallbackHandler()
2023-08-28 17:24:50 +00:00
llm = OpenAI(
callbacks=[handler],
)
2023-09-28 22:00:38 +00:00
chat = ChatOpenAI(callbacks=[handler])
llm("Tell me a joke")
2023-08-28 17:24:50 +00:00
```
2023-09-08 22:56:42 +00:00
## Usage with chains and agents
Make sure to pass the callback handler to the `run` method so that all related chains and llm calls are correctly tracked.
It is also recommended to pass `agent_name` in the metadata to be able to distinguish between agents in the dashboard.
Example:
```python
from langchain.chat_models import ChatOpenAI
from langchain.schema import SystemMessage, HumanMessage
from langchain.agents import OpenAIFunctionsAgent, AgentExecutor, tool
from langchain.callbacks import LLMonitorCallbackHandler
llm = ChatOpenAI(temperature=0)
handler = LLMonitorCallbackHandler()
@tool
def get_word_length(word: str) -> int:
"""Returns the length of a word."""
return len(word)
tools = [get_word_length]
prompt = OpenAIFunctionsAgent.create_prompt(
system_message=SystemMessage(
content="You are very powerful assistant, but bad at calculating lengths of words."
)
)
agent = OpenAIFunctionsAgent(llm=llm, tools=tools, prompt=prompt, verbose=True)
agent_executor = AgentExecutor(
agent=agent, tools=tools, verbose=True, metadata={"agent_name": "WordCount"} # < - recommended , assign a custom name
)
agent_executor.run("how many letters in the word educa?", callbacks=[handler])
```
Another example:
2023-08-28 17:24:50 +00:00
```python
from langchain.agents import load_tools, initialize_agent, AgentType
from langchain.llms import OpenAI
from langchain.callbacks import LLMonitorCallbackHandler
2023-09-08 22:56:42 +00:00
handler = LLMonitorCallbackHandler()
2023-08-28 17:24:50 +00:00
llm = OpenAI(temperature=0)
tools = load_tools(["serpapi", "llm-math"], llm=llm)
2023-09-08 22:56:42 +00:00
agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, metadata={ "agent_name": "GirlfriendAgeFinder" }) # < - recommended , assign a custom name
2023-08-28 17:24:50 +00:00
agent.run(
"Who is Leo DiCaprio's girlfriend? What is her current age raised to the 0.43 power?",
callbacks=[handler],
)
```
2023-09-28 22:00:38 +00:00
## User Tracking
User tracking allows you to identify your users, track their cost, conversations and more.
```python
from langchain.callbacks.llmonitor_callback import LLMonitorCallbackHandler, identify
with identify("user-123"):
llm("Tell me a joke")
with identify("user-456", user_props={"email": "user456@test.com"}):
agen.run("Who is Leo DiCaprio's girlfriend?")
```
2023-08-28 17:24:50 +00:00
## Support
2023-09-08 22:56:42 +00:00
2023-08-28 17:24:50 +00:00
For any question or issue with integration you can reach out to the LLMonitor team on [Discord ](http://discord.com/invite/8PafSG58kK ) or via [email ](mailto:vince@llmonitor.com ).