Skip to content

Commit

Permalink
Added simple Ollama example (#515)
Browse files Browse the repository at this point in the history
* Added simple Ollama example

* Added example to docs and updated cards

* Removed extra logo

* re order

* Cleared all outputs and resolved comments

* Added ollama's own example file

* Added generated example using tool

* Point examples to generated file

---------

Co-authored-by: Akhil Anand <[email protected]>
Co-authored-by: reibs <[email protected]>
Co-authored-by: Pratyush Shukla <[email protected]>
  • Loading branch information
4 people authored Nov 22, 2024
1 parent 882f371 commit 135cf8a
Show file tree
Hide file tree
Showing 6 changed files with 489 additions and 0 deletions.
Binary file added docs/images/external/ollama/ollama-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions docs/mint.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
"v1/integrations/langchain",
"v1/integrations/cohere",
"v1/integrations/anthropic",
"v1/integrations/ollama",
"v1/integrations/litellm",
"v1/integrations/multion",
"v1/integrations/rest"
Expand Down
3 changes: 3 additions & 0 deletions docs/v1/examples/examples.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ mode: "wide"
<Card title="MultiOn Example" icon="atom" href="/v1/examples/multion">
Create an autonomous browser agent capable of navigating the web and extracting information
</Card>
<Card title="Ollama Example" icon={<img src="/images/external/ollama/ollama-icon.png" alt="Ollama" />} iconType="image" href="/v1/examples/ollama">
Simple Ollama integration with AgentOps
</Card>
</CardGroup>

## Video Guides
Expand Down
123 changes: 123 additions & 0 deletions docs/v1/examples/ollama.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
---
title: 'Ollama Example'
description: 'Using Ollama with AgentOps'
mode: "wide"
---

{/* SOURCE_FILE: examples/ollama_examples/ollama_examples.ipynb */}# AgentOps Ollama Integration

This example demonstrates how to use AgentOps to monitor your Ollama LLM calls.

First let's install the required packages

> ⚠️ **Important**: Make sure you have Ollama installed and running locally before running this notebook. You can install it from [ollama.ai](https://ollama.com).

```python
%pip install -U ollama
%pip install -U agentops
%pip install -U python-dotenv
```

Then import them


```python
import ollama
import agentops
import os
from dotenv import load_dotenv

```

Next, we'll set our API keys. For Ollama, we'll need to make sure Ollama is running locally.
[Get an AgentOps API key](https://agentops.ai/settings/projects)

1. Create an environment variable in a .env file or other method. By default, the AgentOps `init()` function will look for an environment variable named `AGENTOPS_API_KEY`. Or...
2. Replace `<your_agentops_key>` below and pass in the optional `api_key` parameter to the AgentOps `init(api_key=...)` function. Remember not to commit your API key to a public repo!


```python
# Let's load our environment variables
load_dotenv()

AGENTOPS_API_KEY = os.getenv("AGENTOPS_API_KEY") or "<your_agentops_key>"
```


```python
# Initialize AgentOps with some default tags
agentops.init(AGENTOPS_API_KEY, default_tags=["ollama-example"])
```

Now let's make some basic calls to Ollama. Make sure you have pulled the model first, use the following or replace with whichever model you want to use.


```python
ollama.pull("mistral")
```


```python
# Basic completion,
response = ollama.chat(model='mistral',
messages=[{
'role': 'user',
'content': 'What are the benefits of using AgentOps for monitoring LLMs?',
}]
)
print(response['message']['content'])
```

Let's try streaming responses as well


```python
# Streaming Example
stream = ollama.chat(
model='mistral',
messages=[{
'role': 'user',
'content': 'Write a haiku about monitoring AI agents',
}],
stream=True
)

for chunk in stream:
print(chunk['message']['content'], end='')

```


```python
# Conversation Example
messages = [
{
'role': 'user',
'content': 'What is AgentOps?'
},
{
'role': 'assistant',
'content': 'AgentOps is a monitoring and observability platform for LLM applications.'
},
{
'role': 'user',
'content': 'Can you give me 3 key features?'
}
]

response = ollama.chat(
model='mistral',
messages=messages
)
print(response['message']['content'])
```

> 💡 **Note**: In production environments, you should add proper error handling around the Ollama calls and use `agentops.end_session("Error")` when exceptions occur.
Finally, let's end our AgentOps session


```python
agentops.end_session("Success")
```
150 changes: 150 additions & 0 deletions docs/v1/integrations/ollama.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
---
title: Ollama
description: "AgentOps provides first class support for Ollama"
---

import CodeTooltip from '/snippets/add-code-tooltip.mdx'
import EnvTooltip from '/snippets/add-env-tooltip.mdx'

<Note>
This is a living integration. Should you need any added functionality, message us on [Discord](https://discord.gg/UgJyyxx7uc)!
</Note>

<Card title="Ollama" icon={<img src="/images/external/ollama/ollama-icon.png" alt="Ollama" />} iconType="image" href="https://ollama.com">
First class support for Ollama
</Card>

<Steps>
<Step title="Install the AgentOps SDK">
<CodeGroup>
```bash pip
pip install agentops ollama
```
```bash poetry
poetry add agentops ollama
```
</CodeGroup>
</Step>
<Step title="Add 3 lines of code">
<CodeTooltip/>
<CodeGroup>
```python python
import agentops
import ollama

agentops.init(<INSERT YOUR API KEY HERE>)
agentops.start_session()

ollama.pull("<MODEL NAME>")

response = ollama.chat(model='mistral',
messages=[{
'role': 'user',
'content': 'What are the benefits of using AgentOps for monitoring LLMs?',
}]
)
print(response['message']['content'])
...
# End of program (e.g. main.py)
agentops.end_session("Success") # Success|Fail|Indeterminate
```
</CodeGroup>
<EnvTooltip />
<CodeGroup>
```python .env
# Alternatively, you can set the API key as an environment variable
AGENTOPS_API_KEY=<YOUR API KEY>
```
</CodeGroup>
Read more about environment variables in [Advanced Configuration](/v1/usage/advanced-configuration)
</Step>
<Step title="Run your Agent">
Execute your program and visit [app.agentops.ai/drilldown](https://app.agentops.ai/drilldown) to observe your Agent! 🕵️
<Tip>
After your run, AgentOps prints a clickable url to console linking directly to your session in the Dashboard
</Tip>
<div/>
</Step>
</Steps>

## Full Examples

<CodeGroup>
```python basic completion
import ollama
import agentops

agentops.init(<INSERT YOUR API KEY HERE>)

ollama.pull("<MODEL NAME>")
response = ollama.chat(
model="<MODEL NAME>",
max_tokens=1024,
messages=[{
"role": "user",
"content": "Write a haiku about AI and humans working together"
}]
)

print(response['message']['content'])
agentops.end_session('Success')
```

```python streaming
import agentops
import ollama

async def main():
agentops.init(<INSERT YOUR API KEY HERE>)
ollama.pull("<MODEL NAME>")

stream = ollama.chat(
model="<MODEL NAME>",
messages=[{
'role': 'user',
'content': 'Write a haiku about monitoring AI agents',
}],
stream=True
)

for chunk in stream:
print(chunk['message']['content'], end='')

agentops.end_session('Success')
```

```python conversation
import ollama
import agentops

agentops.init(<INSERT YOUR API KEY HERE>)
ollama.pull("<MODEL NAME>")

messages = [
{
'role': 'user',
'content': 'What is AgentOps?'
},
{
'role': 'assistant',
'content': 'AgentOps is a monitoring and observability platform for LLM applications.'
},
{
'role': 'user',
'content': 'Can you give me 3 key features?'
}
]

response = ollama.chat(
model="<MODEL NAME>",
messages=messages
)
print(response['message']['content'])
agentops.end_session('Success')
```
</CodeGroup>

<script type="module" src="/scripts/github_stars.js"></script>
<script type="module" src="/scripts/scroll-img-fadein-animation.js"></script>
<script type="module" src="/scripts/button_heartbeat_animation.js"></script>
<script type="css" src="/styles/styles.css"></script>
Loading

0 comments on commit 135cf8a

Please sign in to comment.