Skip to content

Commit

Permalink
Ollama integration docs (#282)
Browse files Browse the repository at this point in the history
  • Loading branch information
jverre authored and Douglas Blank committed Oct 4, 2024
1 parent 7a290cd commit 874ea8d
Show file tree
Hide file tree
Showing 13 changed files with 429 additions and 15 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ The easiest way to get started is to use one of our integrations. Opik supports:
| OpenAI | Log traces for all OpenAI LLM calls | [Documentation](https://www.comet.com/docs/opik/tracing/integrations/openai/?utm_source=opik&utm_medium=github&utm_content=openai_link) | [![Open Quickstart In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/comet-ml/opik/blob/master/apps/opik-documentation/documentation/docs/cookbook/openai.ipynb) |
| LangChain | Log traces for all LangChain LLM calls | [Documentation](https://www.comet.com/docs/opik/tracing/integrations/langchain/?utm_source=opik&utm_medium=github&utm_content=langchain_link) | [![Open Quickstart In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/comet-ml/opik/blob/master/apps/opik-documentation/documentation/docs/cookbook/langchain.ipynb) |
| LlamaIndex | Log traces for all LlamaIndex LLM calls | [Documentation](https://www.comet.com/docs/opik/tracing/integrations/llama_index?utm_source=opik&utm_medium=github&utm_content=llama_index_link) | [![Open Quickstart In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/comet-ml/opik/blob/master/apps/opik-documentation/documentation/docs/cookbook/llama-index.ipynb) |
| Ollama | Log traces for all Ollama LLM calls | [Documentation](https://www.comet.com/docs/opik/tracing/integrations/ollama?utm_source=opik&utm_medium=github&utm_content=ollama_link) | [![Open Quickstart In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/comet-ml/opik/blob/master/apps/opik-documentation/documentation/docs/cookbook/ollama.ipynb) |
| Predibase | Fine-tune and serve open-source Large Language Models | [Documentation](https://www.comet.com/docs/opik/tracing/integrations/predibase?utm_source=opik&utm_medium=github&utm_content=predibase_link) | [![Open Quickstart In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/comet-ml/opik/blob/master/apps/opik-documentation/documentation/docs/cookbook/predibase.ipynb) |
| Ragas | Evaluation framework for your Retrieval Augmented Generation (RAG) pipelines | [Documentation](https://www.comet.com/docs/opik/tracing/integrations/ragas?utm_source=opik&utm_medium=github&utm_content=ragas_link) | [![Open Quickstart In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/comet-ml/opik/blob/master/apps/opik-documentation/documentation/docs/cookbook/ragas.ipynb) |

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ For this guide we will be evaluating the Hallucination metric included in the LL

## Creating an account on Comet.com

[Comet](https://www.comet.com/site) provides a hosted version of the Opik platform, [simply create an account](https://www.comet.com/signup?from=llm) and grab you API Key.
[Comet](https://www.comet.com/site?from=llm&utm_source=opik&utm_medium=colab&utm_content=eval_hall) provides a hosted version of the Opik platform, [simply create an account](https://www.comet.com/signup?from=llm&utm_source=opik&utm_medium=colab&utm_content=eval_hall) and grab you API Key.

> You can also run the Opik platform locally, see the [installation guide](https://www.comet.com/docs/opik/self-host/overview/) for more information.
> You can also run the Opik platform locally, see the [installation guide](https://www.comet.com/docs/opik/self-host/overview/?from=llm&utm_source=opik&utm_medium=colab&utm_content=eval_hall) for more information.

```python
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ For this guide we will be evaluating the Moderation metric included in the LLM E

## Creating an account on Comet.com

[Comet](https://www.comet.com/site) provides a hosted version of the Opik platform, [simply create an account](https://www.comet.com/signup?from=llm) and grab you API Key.
[Comet](https://www.comet.com/site?from=llm&utm_source=opik&utm_medium=colab&utm_content=eval_mod) provides a hosted version of the Opik platform, [simply create an account](https://www.comet.com/signup?from=llm&utm_source=opik&utm_medium=colab&utm_content=eval_mod) and grab you API Key.

> You can also run the Opik platform locally, see the [installation guide](https://www.comet.com/docs/opik/self-host/overview/) for more information.
> You can also run the Opik platform locally, see the [installation guide](https://www.comet.com/docs/opik/self-host/overview/?from=llm&utm_source=opik&utm_medium=colab&utm_content=eval_mod) for more information.

```python
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ We will highlight three different parts of the workflow:

## Creating an account on Comet.com

[Comet](https://www.comet.com/site) provides a hosted version of the Opik platform, [simply create an account](https://www.comet.com/signup?from=llm) and grab you API Key.
[Comet](https://www.comet.com/site?from=llm&utm_source=opik&utm_medium=colab&utm_content=langchain) provides a hosted version of the Opik platform, [simply create an account](https://www.comet.com/signup?from=llm&utm_source=opik&utm_medium=colab&utm_content=langchain) and grab you API Key.

> You can also run the Opik platform locally, see the [installation guide](https://www.comet.com/docs/opik/self-host/overview/) for more information.
> You can also run the Opik platform locally, see the [installation guide](https://www.comet.com/docs/opik/self-host/overview/?from=llm&utm_source=opik&utm_medium=colab&utm_content=langchain) for more information.

```python
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ For this guide we will be downloading the essays from Paul Graham and use them a

## Creating an account on Comet.com

[Comet](https://www.comet.com/site) provides a hosted version of the Opik platform, [simply create an account](https://www.comet.com/signup?from=llm) and grab you API Key.
[Comet](https://www.comet.com/site?from=llm&utm_source=opik&utm_medium=colab&utm_content=llamaindex) provides a hosted version of the Opik platform, [simply create an account](https://www.comet.com/signup?from=llm&utm_source=opik&utm_medium=colab&utm_content=llamaindex) and grab you API Key.

> You can also run the Opik platform locally, see the [installation guide](https://www.comet.com/docs/opik/self-host/overview/) for more information.
> You can also run the Opik platform locally, see the [installation guide](https://www.comet.com/docs/opik/self-host/overview/?from=llm&utm_source=opik&utm_medium=colab&utm_content=llamaindex) for more information.

```python
Expand Down
182 changes: 182 additions & 0 deletions apps/opik-documentation/documentation/docs/cookbook/ollama.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Using Opik with Ollama\n",
"\n",
"[Ollama](https://ollama.com/) allows users to run, interact with, and deploy AI models locally on their machines without the need for complex infrastructure or cloud dependencies.\n",
"\n",
"In this notebook, we will showcase how to log Ollama LLM calls using Opik by utilizing either the OpenAI or LangChain libraries.\n",
"\n",
"## Getting started\n",
"\n",
"### Configure Ollama\n",
"\n",
"In order to interact with Ollama from Python, we will to have Ollama running on our machine. You can learn more about how to install and run Ollama in the [quickstart guide](https://github.com/ollama/ollama/blob/main/README.md#quickstart).\n",
"\n",
"### Configuring Opik\n",
"\n",
"Opik is available as a fully open source local installation or using Comet.com as a hosted solution. The easiest way to get started with Opik is by creating a free Comet account at comet.com.\n",
"\n",
"If you'd like to self-host Opik, you can learn more about the self-hosting options [here](https://www.comet.com/docs/opik/self-host/overview).\n",
"\n",
"In addition, you will need to install and configure the Opik Python package:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%pip install --upgrade --quiet opik\n",
"\n",
"import opik\n",
"opik.configure()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Tracking Ollama calls made with OpenAI\n",
"\n",
"Ollama is compatible with the OpenAI format and can be used with the OpenAI Python library. You can therefore leverage the Opik integration for OpenAI to trace your Ollama calls:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from openai import OpenAI\n",
"from opik.integrations.openai import track_openai\n",
"\n",
"import os\n",
"os.environ['OPIK_PROJECT_NAME'] = 'ollama-integration'\n",
"\n",
"# Create an OpenAI client\n",
"client = OpenAI(\n",
" base_url='http://localhost:11434/v1/',\n",
"\n",
" # required but ignored\n",
" api_key='ollama',\n",
")\n",
"\n",
"# Log all traces made to with the OpenAI client to Opik\n",
"client = track_openai(client)\n",
"\n",
"# call the local ollama model using the OpenAI client\n",
"chat_completion = client.chat.completions.create(\n",
" messages=[\n",
" {\n",
" 'role': 'user',\n",
" 'content': 'Say this is a test',\n",
" }\n",
" ],\n",
" model='llama3.1',\n",
")\n",
"\n",
"print(chat_completion.choices[0].message.content)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Your LLM call is now traced and logged to the Opik platform."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Tracking Ollama calls made with LangChain\n",
"\n",
"In order to trace Ollama calls made with LangChain, you will need to first install the `langchain-ollama` package:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%pip install --quiet --upgrade langchain-ollama"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You will now be able to use the `OpikTracer` class to log all your Ollama calls made with LangChain to Opik:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from langchain_ollama import ChatOllama\n",
"from opik.integrations.langchain import OpikTracer\n",
"\n",
"# Create the Opik tracer\n",
"opik_tracer = OpikTracer(tags=[\"langchain\", \"ollama\"])\n",
"\n",
"# Create the Ollama model and configure it to use the Opik tracer\n",
"llm = ChatOllama(\n",
" model=\"llama3.1\",\n",
" temperature=0,\n",
").with_config({\"callbacks\": [opik_tracer]})\n",
"\n",
"# Call the Ollama model\n",
"messages = [\n",
" (\n",
" \"system\",\n",
" \"You are a helpful assistant that translates English to French. Translate the user sentence.\",\n",
" ),\n",
" (\n",
" \"human\",\n",
" \"I love programming.\",\n",
" ),\n",
"]\n",
"ai_msg = llm.invoke(messages)\n",
"ai_msg"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can now go to the Opik app to see the trace:\n",
"\n",
"![Ollama trace in Opik](https://raw.githubusercontent.com/comet-ml/opik/main/apps/opik-documentation/documentation/static/img/cookbook/ollama_cookbook.png)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "py312_llm_eval",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
111 changes: 111 additions & 0 deletions apps/opik-documentation/documentation/docs/cookbook/ollama.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# Using Opik with Ollama

[Ollama](https://ollama.com/) allows users to run, interact with, and deploy AI models locally on their machines without the need for complex infrastructure or cloud dependencies.

In this notebook, we will showcase how to log Ollama LLM calls using Opik by utilizing either the OpenAI or LangChain libraries.

## Getting started

### Configure Ollama

In order to interact with Ollama from Python, we will to have Ollama running on our machine. You can learn more about how to install and run Ollama in the [quickstart guide](https://github.com/ollama/ollama/blob/main/README.md#quickstart).

### Configuring Opik

Opik is available as a fully open source local installation or using Comet.com as a hosted solution. The easiest way to get started with Opik is by creating a free Comet account at comet.com.

If you'd like to self-host Opik, you can learn more about the self-hosting options [here](https://www.comet.com/docs/opik/self-host/overview).

In addition, you will need to install and configure the Opik Python package:


```python
%pip install --upgrade --quiet opik

import opik
opik.configure()
```

## Tracking Ollama calls made with OpenAI

Ollama is compatible with the OpenAI format and can be used with the OpenAI Python library. You can therefore leverage the Opik integration for OpenAI to trace your Ollama calls:



```python
from openai import OpenAI
from opik.integrations.openai import track_openai

import os
os.environ['OPIK_PROJECT_NAME'] = 'ollama-integration'

# Create an OpenAI client
client = OpenAI(
base_url='http://localhost:11434/v1/',

# required but ignored
api_key='ollama',
)

# Log all traces made to with the OpenAI client to Opik
client = track_openai(client)

# call the local ollama model using the OpenAI client
chat_completion = client.chat.completions.create(
messages=[
{
'role': 'user',
'content': 'Say this is a test',
}
],
model='llama3.1',
)

print(chat_completion.choices[0].message.content)
```

Your LLM call is now traced and logged to the Opik platform.

## Tracking Ollama calls made with LangChain

In order to trace Ollama calls made with LangChain, you will need to first install the `langchain-ollama` package:


```python
%pip install --quiet --upgrade langchain-ollama
```

You will now be able to use the `OpikTracer` class to log all your Ollama calls made with LangChain to Opik:


```python
from langchain_ollama import ChatOllama
from opik.integrations.langchain import OpikTracer

# Create the Opik tracer
opik_tracer = OpikTracer(tags=["langchain", "ollama"])

# Create the Ollama model and configure it to use the Opik tracer
llm = ChatOllama(
model="llama3.1",
temperature=0,
).with_config({"callbacks": [opik_tracer]})

# Call the Ollama model
messages = [
(
"system",
"You are a helpful assistant that translates English to French. Translate the user sentence.",
),
(
"human",
"I love programming.",
),
]
ai_msg = llm.invoke(messages)
ai_msg
```

You can now go to the Opik app to see the trace:

![Ollama trace in Opik](https://raw.githubusercontent.com/comet-ml/opik/main/apps/opik-documentation/documentation/static/img/cookbook/ollama_cookbook.png)
4 changes: 2 additions & 2 deletions apps/opik-documentation/documentation/docs/cookbook/openai.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ Opik integrates with OpenAI to provide a simple way to log traces for all OpenAI

## Creating an account on Comet.com

[Comet](https://www.comet.com/site) provides a hosted version of the Opik platform, [simply create an account](https://www.comet.com/signup?from=llm) and grab you API Key.
[Comet](https://www.comet.com/site?from=llm&utm_source=opik&utm_medium=colab&utm_content=openai) provides a hosted version of the Opik platform, [simply create an account](https://www.comet.com/signup?from=llm&utm_source=opik&utm_medium=colab&utm_content=openai) and grab you API Key.

> You can also run the Opik platform locally, see the [installation guide](https://www.comet.com/docs/opik/self-host/overview/) for more information.
> You can also run the Opik platform locally, see the [installation guide](https://www.comet.com/docs/opik/self-host/overview/?from=llm&utm_source=opik&utm_medium=colab&utm_content=openai) for more information.

```python
Expand Down
4 changes: 2 additions & 2 deletions apps/opik-documentation/documentation/docs/cookbook/ragas.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ There are two main ways to use Opik with Ragas:

## Creating an account on Comet.com

[Comet](https://www.comet.com/site) provides a hosted version of the Opik platform, [simply create an account](https://www.comet.com/signup?from=llm) and grab you API Key.
[Comet](https://www.comet.com/site?from=llm&utm_source=opik&utm_medium=colab&utm_content=openai) provides a hosted version of the Opik platform, [simply create an account](https://www.comet.com/signup?from=llm&utm_source=opik&utm_medium=colab&utm_content=ragas) and grab you API Key.

> You can also run the Opik platform locally, see the [installation guide](https://www.comet.com/docs/opik/self-host/overview/) for more information.
> You can also run the Opik platform locally, see the [installation guide](https://www.comet.com/docs/opik/self-host/overview/?from=llm&utm_source=opik&utm_medium=colab&utm_content=ragas) for more information.

```python
Expand Down
Loading

0 comments on commit 874ea8d

Please sign in to comment.