Skip to content

Commit

Permalink
feat: feat: add Groq tracer
Browse files Browse the repository at this point in the history
  • Loading branch information
Stainless Bot committed Aug 29, 2024
1 parent e1e2237 commit bdf3f36
Show file tree
Hide file tree
Showing 3 changed files with 484 additions and 1 deletion.
140 changes: 140 additions & 0 deletions examples/tracing/groq/groq_tracing.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "2722b419",
"metadata": {},
"source": [
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/openlayer-ai/openlayer-python/blob/main/examples/tracing/groq/groq_tracing.ipynb)\n",
"\n",
"\n",
"# <a id=\"top\">Groq tracing</a>\n",
"\n",
"This notebook illustrates how to trace Groq LLM calls with Openlayer."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "020c8f6a",
"metadata": {},
"outputs": [],
"source": [
"!pip install groq openlayer"
]
},
{
"cell_type": "markdown",
"id": "75c2a473",
"metadata": {},
"source": [
"## 1. Set the environment variables"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f3f4fa13",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"\n",
"# Groq env variables\n",
"os.environ[\"GROQ_API_KEY\"] = \"YOUR_GROQ_API_KEY_HERE\"\n",
"\n",
"# Openlayer env variables\n",
"os.environ[\"OPENLAYER_API_KEY\"] = \"YOUR_OPENLAYER_API_KEY_HERE\"\n",
"os.environ[\"OPENLAYER_INFERENCE_PIPELINE_ID\"] = \"YOUR_OPENLAYER_INFERENCE_PIPELINE_ID_HERE\""
]
},
{
"cell_type": "markdown",
"id": "9758533f",
"metadata": {},
"source": [
"## 2. Import the `trace_groq` function"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c35d9860-dc41-4f7c-8d69-cc2ac7e5e485",
"metadata": {},
"outputs": [],
"source": [
"import groq\n",
"from openlayer.lib import trace_groq\n",
"\n",
"groq_client = trace_groq(groq.Groq())"
]
},
{
"cell_type": "markdown",
"id": "72a6b954",
"metadata": {},
"source": [
"## 3. Use the traced Groq client normally"
]
},
{
"cell_type": "markdown",
"id": "76a350b4",
"metadata": {},
"source": [
"That's it! Now you can continue using the traced Groq client normally. The data is automatically published to Openlayer and you can start creating tests around it!"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e00c1c79",
"metadata": {},
"outputs": [],
"source": [
"chat_completion = groq_client.chat.completions.create(\n",
" messages=[\n",
" {\n",
" \"role\": \"system\",\n",
" \"content\": \"You are a helpful assistant.\"\n",
" },\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": \"Explain the importance of fast language models\",\n",
" }\n",
" ],\n",
" model=\"llama3-8b-8192\",\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bd2cd65d-1b22-4f5d-b5cb-7700e036b863",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.9.19"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
21 changes: 20 additions & 1 deletion src/openlayer/lib/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
"""Openlayer lib.
"""

__all__ = ["trace", "trace_anthropic", "trace_openai", "trace_openai_assistant_thread_run", "trace_mistral"]
__all__ = [
"trace",
"trace_anthropic",
"trace_openai",
"trace_openai_assistant_thread_run",
"trace_mistral",
"trace_groq",
]

# ---------------------------------- Tracing --------------------------------- #
from .tracing import tracer
Expand Down Expand Up @@ -51,3 +58,15 @@ def trace_mistral(client):
if not isinstance(client, mistralai.Mistral):
raise ValueError("Invalid client. Please provide a Mistral client.")
return mistral_tracer.trace_mistral(client)


def trace_groq(client):
"""Trace Groq queries."""
# pylint: disable=import-outside-toplevel
import groq

from .integrations import groq_tracer

if not isinstance(client, groq.Groq):
raise ValueError("Invalid client. Please provide a Groq client.")
return groq_tracer.trace_groq(client)
Loading

0 comments on commit bdf3f36

Please sign in to comment.