Skip to content

Commit bdf3f36

Browse files
author
Stainless Bot
committed
feat: feat: add Groq tracer
1 parent e1e2237 commit bdf3f36

File tree

3 files changed

+484
-1
lines changed

3 files changed

+484
-1
lines changed
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "2722b419",
6+
"metadata": {},
7+
"source": [
8+
"[![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",
9+
"\n",
10+
"\n",
11+
"# <a id=\"top\">Groq tracing</a>\n",
12+
"\n",
13+
"This notebook illustrates how to trace Groq LLM calls with Openlayer."
14+
]
15+
},
16+
{
17+
"cell_type": "code",
18+
"execution_count": null,
19+
"id": "020c8f6a",
20+
"metadata": {},
21+
"outputs": [],
22+
"source": [
23+
"!pip install groq openlayer"
24+
]
25+
},
26+
{
27+
"cell_type": "markdown",
28+
"id": "75c2a473",
29+
"metadata": {},
30+
"source": [
31+
"## 1. Set the environment variables"
32+
]
33+
},
34+
{
35+
"cell_type": "code",
36+
"execution_count": null,
37+
"id": "f3f4fa13",
38+
"metadata": {},
39+
"outputs": [],
40+
"source": [
41+
"import os\n",
42+
"\n",
43+
"# Groq env variables\n",
44+
"os.environ[\"GROQ_API_KEY\"] = \"YOUR_GROQ_API_KEY_HERE\"\n",
45+
"\n",
46+
"# Openlayer env variables\n",
47+
"os.environ[\"OPENLAYER_API_KEY\"] = \"YOUR_OPENLAYER_API_KEY_HERE\"\n",
48+
"os.environ[\"OPENLAYER_INFERENCE_PIPELINE_ID\"] = \"YOUR_OPENLAYER_INFERENCE_PIPELINE_ID_HERE\""
49+
]
50+
},
51+
{
52+
"cell_type": "markdown",
53+
"id": "9758533f",
54+
"metadata": {},
55+
"source": [
56+
"## 2. Import the `trace_groq` function"
57+
]
58+
},
59+
{
60+
"cell_type": "code",
61+
"execution_count": null,
62+
"id": "c35d9860-dc41-4f7c-8d69-cc2ac7e5e485",
63+
"metadata": {},
64+
"outputs": [],
65+
"source": [
66+
"import groq\n",
67+
"from openlayer.lib import trace_groq\n",
68+
"\n",
69+
"groq_client = trace_groq(groq.Groq())"
70+
]
71+
},
72+
{
73+
"cell_type": "markdown",
74+
"id": "72a6b954",
75+
"metadata": {},
76+
"source": [
77+
"## 3. Use the traced Groq client normally"
78+
]
79+
},
80+
{
81+
"cell_type": "markdown",
82+
"id": "76a350b4",
83+
"metadata": {},
84+
"source": [
85+
"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!"
86+
]
87+
},
88+
{
89+
"cell_type": "code",
90+
"execution_count": null,
91+
"id": "e00c1c79",
92+
"metadata": {},
93+
"outputs": [],
94+
"source": [
95+
"chat_completion = groq_client.chat.completions.create(\n",
96+
" messages=[\n",
97+
" {\n",
98+
" \"role\": \"system\",\n",
99+
" \"content\": \"You are a helpful assistant.\"\n",
100+
" },\n",
101+
" {\n",
102+
" \"role\": \"user\",\n",
103+
" \"content\": \"Explain the importance of fast language models\",\n",
104+
" }\n",
105+
" ],\n",
106+
" model=\"llama3-8b-8192\",\n",
107+
")"
108+
]
109+
},
110+
{
111+
"cell_type": "code",
112+
"execution_count": null,
113+
"id": "bd2cd65d-1b22-4f5d-b5cb-7700e036b863",
114+
"metadata": {},
115+
"outputs": [],
116+
"source": []
117+
}
118+
],
119+
"metadata": {
120+
"kernelspec": {
121+
"display_name": "Python 3 (ipykernel)",
122+
"language": "python",
123+
"name": "python3"
124+
},
125+
"language_info": {
126+
"codemirror_mode": {
127+
"name": "ipython",
128+
"version": 3
129+
},
130+
"file_extension": ".py",
131+
"mimetype": "text/x-python",
132+
"name": "python",
133+
"nbconvert_exporter": "python",
134+
"pygments_lexer": "ipython3",
135+
"version": "3.9.19"
136+
}
137+
},
138+
"nbformat": 4,
139+
"nbformat_minor": 5
140+
}

src/openlayer/lib/__init__.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
"""Openlayer lib.
22
"""
33

4-
__all__ = ["trace", "trace_anthropic", "trace_openai", "trace_openai_assistant_thread_run", "trace_mistral"]
4+
__all__ = [
5+
"trace",
6+
"trace_anthropic",
7+
"trace_openai",
8+
"trace_openai_assistant_thread_run",
9+
"trace_mistral",
10+
"trace_groq",
11+
]
512

613
# ---------------------------------- Tracing --------------------------------- #
714
from .tracing import tracer
@@ -51,3 +58,15 @@ def trace_mistral(client):
5158
if not isinstance(client, mistralai.Mistral):
5259
raise ValueError("Invalid client. Please provide a Mistral client.")
5360
return mistral_tracer.trace_mistral(client)
61+
62+
63+
def trace_groq(client):
64+
"""Trace Groq queries."""
65+
# pylint: disable=import-outside-toplevel
66+
import groq
67+
68+
from .integrations import groq_tracer
69+
70+
if not isinstance(client, groq.Groq):
71+
raise ValueError("Invalid client. Please provide a Groq client.")
72+
return groq_tracer.trace_groq(client)

0 commit comments

Comments
 (0)