Skip to content

Commit

Permalink
GitHub Action - Update examples in docs from notebooks
Browse files Browse the repository at this point in the history
  • Loading branch information
areibman authored and HowieG committed Dec 13, 2024
1 parent a03e45a commit 7e1e6b4
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 3 deletions.
179 changes: 178 additions & 1 deletion docs/v1/examples/langchain.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,181 @@ Finally, check your run on [AgentOps](https://app.agentops.ai)
Now if we look in the AgentOps dashboard, you will see a session recorded with the LLM calls and tool usage.

## Langchain V0.1 Example
You can find the example in the [notebook](https://github.com/AgentOps-AI/agentops/blob/main/examples/langchain_examples.ipynb).
This example is out of date. You can uncomment all the following cells and the example will run but AgentOps is deprecating support.


```python
# %pip install langchain==0.1.6
```


```python
# import os
# from langchain_openai import ChatOpenAI
# from langchain.agents import initialize_agent, AgentType
# from langchain.agents import tool
```

The only difference with using AgentOps is that we'll also import this special Callback Handler


```python
# from agentops.partners.langchain_callback_handler import (
# LangchainCallbackHandler as AgentOpsLangchainCallbackHandler,
# )
```

Next, we'll grab our two API keys.


```python
# from dotenv import load_dotenv

# load_dotenv()
```

This is where AgentOps comes into play. Before creating our LLM instance via Langchain, first we'll create an instance of the AO LangchainCallbackHandler. After the handler is initialized, a session will be recorded automatically.

Pass in your API key, and optionally any tags to describe this session for easier lookup in the AO dashboard.


```python
# AGENTOPS_API_KEY = os.environ.get("AGENTOPS_API_KEY")
# OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")

# agentops_handler = AgentOpsLangchainCallbackHandler(
# api_key=AGENTOPS_API_KEY, default_tags=["Langchain Example"]
# )

# llm = ChatOpenAI(
# openai_api_key=OPENAI_API_KEY, callbacks=[agentops_handler], model="gpt-3.5-turbo"
# )
```

You can also retrieve the `session_id` of the newly created session.


```python
# print("Agent Ops session ID: " + str(agentops_handler.current_session_ids))
```

Agents generally use tools. Let's define a simple tool here. Tool usage is also recorded.


```python
# @tool
# def find_movie(genre) -> str:
# """Find available movies"""
# if genre == "drama":
# return "Dune 2"
# else:
# return "Pineapple Express"


# tools = [find_movie]
```

For each tool, you need to also add the callback handler


```python
# for t in tools:
# t.callbacks = [agentops_handler]
```

Finally, let's use our agent! Pass in the callback handler to the agent, and all the actions will be recorded in the AO Dashboard


```python
# agent = initialize_agent(
# tools,
# llm,
# agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION,
# verbose=True,
# callbacks=[
# agentops_handler
# ], # You must pass in a callback handler to record your agent
# handle_parsing_errors=True,
# )
```


```python
# agent.invoke("What comedies are playing?", callbacks=[agentops_handler])
```

## Check your session
Finally, check your run on [AgentOps](https://app.agentops.ai)

# Async Agents

Several langchain agents require async callback handlers. AgentOps also supports this.


```python
# import os
# from langchain.chat_models import ChatOpenAI
# from langchain.agents import initialize_agent, AgentType
# from langchain.agents import tool
```


```python
# from agentops.partners.langchain_callback_handler import (
# AsyncLangchainCallbackHandler as AgentOpsAsyncLangchainCallbackHandler,
# )
```


```python
# from dotenv import load_dotenv

# load_dotenv()

# AGENTOPS_API_KEY = os.environ.get("AGENTOPS_API_KEY")
# OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
```


```python
# agentops_handler = AgentOpsAsyncLangchainCallbackHandler(
# api_key=AGENTOPS_API_KEY, tags=["Async Example"]
# )

# llm = ChatOpenAI(
# openai_api_key=OPENAI_API_KEY, callbacks=[agentops_handler], model="gpt-3.5-turbo"
# )

# print("Agent Ops session ID: " + str(await agentops_handler.session_id))
```


```python
# @tool
# def find_movie(genre) -> str:
# """Find available movies"""
# if genre == "drama":
# return "Dune 2"
# else:
# return "Pineapple Express"


# tools = [find_movie]

# for t in tools:
# t.callbacks = [agentops_handler]
```


```python
# agent = initialize_agent(
# tools,
# llm,
# agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION,
# verbose=True,
# handle_parsing_errors=True,
# callbacks=[agentops_handler],
# )

# await agent.arun("What comedies are playing?")
```
2 changes: 1 addition & 1 deletion docs/v1/examples/multion.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ _View All Notebooks on <a href={'https://github.com/AgentOps-AI/agentops/blob/ma
{/* SOURCE_FILE: examples/multion_examples/Autonomous_web_browsing.ipynb */}

# MultiOn Tracking Web Browse Actions
<img src="https://github.com/AgentOps-AI/agentops/blob/main/docs/logo/multion_integration.png?raw=true" width="250px" style="max-width: 100%; height: auto;"/>
<img src="https://github.com/AgentOps-AI/agentops/blob/b4aac2d4b9fb16d6aa0a25aa9018210a94f1bef2/docs/logo/multion_integration.png?raw=true" width="250px" style="max-width: 100%; height: auto;"/>

Agents using MultiOn can launch and control remote or local web browsers to perform actions and retrieve context using natural language commands. With AgentOps, MultiOn evens such as browse, retrieve, and step are automatically tracked.

Expand Down
2 changes: 1 addition & 1 deletion docs/v1/examples/simple_agent.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ if "hello" in str(response.choices[0].message.content).lower():
agentops.record(
ActionEvent(
action_type="Agent says hello",
params=str(message),
logs=str(message),
returns=str(response.choices[0].message.content),
)
)
Expand Down

0 comments on commit 7e1e6b4

Please sign in to comment.