Skip to content

Commit ed596e0

Browse files
authored
docs: launch week day 3 (langfuse#562)
1 parent 600273a commit ed596e0

File tree

6 files changed

+415
-1
lines changed

6 files changed

+415
-1
lines changed

cookbook/_routes.json

+4
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@
7171
"notebook": "js_tracing_example_vercel_ai_sdk.ipynb",
7272
"docsPath": "/docs/sdk/typescript/example-vercel-ai"
7373
},
74+
{
75+
"notebook": "example_decorator_openai_langchain.ipynb",
76+
"docsPath": null
77+
},
7478
{
7579
"notebook": "integration_instructor.ipynb",
7680
"docsPath": "docs/integrations/instructor"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"---\n",
8+
"description: LLM Rap Battle traced using the Langfuse Decorator, OpenAI & Langchain Integration\n",
9+
"category: Examples\n",
10+
"---"
11+
]
12+
},
13+
{
14+
"cell_type": "markdown",
15+
"metadata": {
16+
"id": "ZqZ8AN39K6kG"
17+
},
18+
"source": [
19+
"# Example: Langfuse Decorator + OpenAI Integration + Langchain Integration"
20+
]
21+
},
22+
{
23+
"cell_type": "code",
24+
"execution_count": null,
25+
"metadata": {
26+
"id": "pla_RO_7bP3u"
27+
},
28+
"outputs": [],
29+
"source": [
30+
"%pip install langfuse openai langchain_openai langchain"
31+
]
32+
},
33+
{
34+
"cell_type": "code",
35+
"execution_count": null,
36+
"metadata": {
37+
"id": "LQ4UnVpDbP3v"
38+
},
39+
"outputs": [],
40+
"source": [
41+
"import os\n",
42+
"\n",
43+
"# Get keys for your project from the project settings page\n",
44+
"# https://cloud.langfuse.com\n",
45+
"os.environ[\"LANGFUSE_PUBLIC_KEY\"] = \"\"\n",
46+
"os.environ[\"LANGFUSE_SECRET_KEY\"] = \"\"\n",
47+
"os.environ[\"LANGFUSE_HOST\"] = \"https://cloud.langfuse.com\" # 🇪🇺 EU region\n",
48+
"# os.environ[\"LANGFUSE_HOST\"] = \"https://us.cloud.langfuse.com\" # 🇺🇸 US region\n",
49+
"\n",
50+
"# Your openai key\n",
51+
"os.environ[\"OPENAI_API_KEY\"] = \"\""
52+
]
53+
},
54+
{
55+
"cell_type": "markdown",
56+
"metadata": {
57+
"id": "pJXWPL1Xg_HO"
58+
},
59+
"source": [
60+
"## Imports"
61+
]
62+
},
63+
{
64+
"cell_type": "code",
65+
"execution_count": null,
66+
"metadata": {
67+
"id": "FKB4e1cpl1lu"
68+
},
69+
"outputs": [],
70+
"source": [
71+
"import random"
72+
]
73+
},
74+
{
75+
"cell_type": "code",
76+
"execution_count": null,
77+
"metadata": {
78+
"id": "anEKybkhlwju"
79+
},
80+
"outputs": [],
81+
"source": [
82+
"from operator import itemgetter\n",
83+
"from langchain_openai import ChatOpenAI\n",
84+
"from langchain.prompts import ChatPromptTemplate\n",
85+
"from langchain.schema import StrOutputParser\n",
86+
"from langfuse.decorators import observe"
87+
]
88+
},
89+
{
90+
"cell_type": "code",
91+
"execution_count": null,
92+
"metadata": {
93+
"id": "itxfY1Cug9iE"
94+
},
95+
"outputs": [],
96+
"source": [
97+
"from langfuse.decorators import langfuse_context, observe\n",
98+
"\n",
99+
"# import openai\n",
100+
"from langfuse.openai import openai"
101+
]
102+
},
103+
{
104+
"cell_type": "markdown",
105+
"metadata": {
106+
"id": "Om5dYvIwg14h"
107+
},
108+
"source": [
109+
"## Example: LLM Rap Battle"
110+
]
111+
},
112+
{
113+
"cell_type": "code",
114+
"execution_count": null,
115+
"metadata": {
116+
"id": "UNOk7cElboz1"
117+
},
118+
"outputs": [],
119+
"source": [
120+
"@observe()\n",
121+
"def get_random_rap_topic():\n",
122+
" topics = [\n",
123+
" \"OSS software\",\n",
124+
" \"artificial general intelligence\"\n",
125+
" ]\n",
126+
" return random.choice(topics)"
127+
]
128+
},
129+
{
130+
"cell_type": "code",
131+
"execution_count": null,
132+
"metadata": {
133+
"id": "O-QmAjTGfX-Q"
134+
},
135+
"outputs": [],
136+
"source": [
137+
"@observe()\n",
138+
"def summarize_rap_langchain(rap):\n",
139+
" langfuse_handler = langfuse_context.get_current_langchain_handler()\n",
140+
"\n",
141+
" # Create chain\n",
142+
" prompt = ChatPromptTemplate.from_template(\"Summarrize this rap: {rap}\")\n",
143+
" model = ChatOpenAI()\n",
144+
" chain = prompt | model | StrOutputParser()\n",
145+
"\n",
146+
" # Pass handler to invoke\n",
147+
" summary = chain.invoke(\n",
148+
" {\"rap\": rap},\n",
149+
" config={\"callbacks\":[langfuse_handler]}\n",
150+
" )\n",
151+
"\n",
152+
" return summary"
153+
]
154+
},
155+
{
156+
"cell_type": "code",
157+
"execution_count": null,
158+
"metadata": {
159+
"id": "MXl9E_mNcWw_"
160+
},
161+
"outputs": [],
162+
"source": [
163+
"@observe()\n",
164+
"def rap_battle(turns: int = 5):\n",
165+
" topic = get_random_rap_topic()\n",
166+
"\n",
167+
" print(f\"Topic: {topic}\")\n",
168+
"\n",
169+
" langfuse_context.update_current_trace(\n",
170+
" metadata={\"topic\":topic},\n",
171+
" tags=[\"Launch Week 1\"]\n",
172+
" )\n",
173+
"\n",
174+
" messages = [\n",
175+
" {\"role\": \"system\", \"content\": \"We are all rap artist. When it is our turn, we drop a fresh line.\"},\n",
176+
" {\"role\": \"user\", \"content\": f\"Kick it off, today's topic is {topic}, here's the mic...\"}\n",
177+
" ]\n",
178+
"\n",
179+
" for turn in range(turns):\n",
180+
" completion = openai.chat.completions.create(\n",
181+
" model=\"gpt-3.5-turbo\",\n",
182+
" messages=messages,\n",
183+
" )\n",
184+
" rap_line = completion.choices[0].message.content\n",
185+
" messages.append({\"role\": \"assistant\", \"content\": rap_line})\n",
186+
" print(f\"\\nRap {turn}: {rap_line}\")\n",
187+
"\n",
188+
" summary = summarize_rap_langchain([message['content'] for message in messages])\n",
189+
"\n",
190+
" return summary"
191+
]
192+
},
193+
{
194+
"cell_type": "code",
195+
"execution_count": null,
196+
"metadata": {
197+
"id": "Vg8bc0mzd7uG"
198+
},
199+
"outputs": [],
200+
"source": [
201+
"rap_summary = rap_battle(turns=4)\n",
202+
"print(\"\\nSummary: \" + rap_summary)"
203+
]
204+
}
205+
],
206+
"metadata": {
207+
"colab": {
208+
"provenance": []
209+
},
210+
"kernelspec": {
211+
"display_name": "Python 3 (ipykernel)",
212+
"language": "python",
213+
"name": "python3"
214+
},
215+
"language_info": {
216+
"codemirror_mode": {
217+
"name": "ipython",
218+
"version": 3
219+
},
220+
"file_extension": ".py",
221+
"mimetype": "text/x-python",
222+
"name": "python",
223+
"nbconvert_exporter": "python",
224+
"pygments_lexer": "ipython3",
225+
"version": "3.9.18"
226+
}
227+
},
228+
"nbformat": 4,
229+
"nbformat_minor": 0
230+
}

pages/blog/launch-week-1.mdx

+10
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ We teamed up with [PostHog](https://posthog.com) (OSS product analytics) to inte
7070

7171
We're excited to introduce the [**LLM Playground**](/docs/playground) to Langfuse. By making prompt engineering possible directly in Langfuse, we take another step in our mission to build a feature-complete LLM engineering platform that helps you along the full live cycle of your LLM application. With the LLM playground, you can now test and iterate your prompts directly in Langfuse. Either start from scratch or jump into the playground from an existing prompt in your project. See the [docs](/docs/playground) for more details and let us know what you think in the [GitHub discussion](https://github.com/orgs/langfuse/discussions/1170).
7272

73+
### Day 3: Decorator-based integration for Python
74+
75+
<CloudflareVideo
76+
videoId="8a1f9282ef64c2bd0d932c92d6185668"
77+
aspectRatio={16 / 9.37}
78+
title="Decorator Integration"
79+
/>
80+
81+
We're happy to share that the Decorator-based integration for Python now supports all Langfuse features and is the recommended way to use Langfuse in Python. The decorator makes integrating with Langfuse so much easier. Head over to the [Python Decorator docs](/docs/sdk/python/decorators) to learn more. All inputs, outputs, timings are captured automatically, and it works with all other [Langfuse integrations](/docs/integrations) (LangChain, LlamaIndex, OpenAI SDK, ...). To celebrate this milestone, we wrote a [blog post](/blog/2024-04-python-decorator) on the technical details and created the [example notebook](/guides/cookbook/example_decorator_openai_langchain) shown in the video as it demonstrates what's really cool about the decorator. Thanks again to [@lshalon](https://github.com/lshalon) and [@AshisGhosh](https://github.com/AshisGhosh) for your contributions to this!
82+
7383
## Launch Week Events
7484

7585
### Wednesday: First virtual town hall [#townhall]
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
date: 2024-04-24
3+
badge: Launch Week 1 🚀
4+
title: Decorator-based integration for Python
5+
description: The decorator now supports all Langfuse features and is the recommended way to use Langfuse in Python.
6+
ogImage: /images/changelog/2024-03-24-python-decorator.png
7+
showOgInHeader: false
8+
author: Marc
9+
---
10+
11+
import { ChangelogHeader } from "@/components/changelog/ChangelogHeader";
12+
13+
<ChangelogHeader />
14+
15+
<CloudflareVideo
16+
videoId="8a1f9282ef64c2bd0d932c92d6185668"
17+
aspectRatio={16 / 9.37}
18+
title="Decorator Integration"
19+
/>
20+
21+
On Day 3 of [Launch Week 1](/blog/launch-week-1), we're happy to share that the Decorator-based integration for Python now supports all Langfuse features and is the recommended way to use Langfuse in Python.
22+
23+
The decorator makes integrating with Langfuse so much easier. Head over to the [Python Decorator docs](/docs/sdk/python/decorators) to learn more. All inputs, outputs, timings are captured automatically, and it works with all other [Langfuse integrations](/docs/integrations) (LangChain, LlamaIndex, OpenAI SDK, ...).
24+
25+
To celebrate this milestone, we wrote a [blog post](/blog/2024-04-python-decorator) on the technical details and created the [example notebook](/guides/cookbook/example_decorator_openai_langchain) shown in the video as it demonstrates what's really cool about the decorator. Let us know what you think in the [GitHub discussion](https://github.com/orgs/langfuse/discussions/1009), and stay tuned for more updates during [Langfuse Launch Week 1](/blog/launch-week-1) 🚀
26+
27+
Thanks again to [@lshalon](https://github.com/lshalon) and [@AshisGhosh](https://github.com/AshisGhosh) for your contributions to this!
28+
29+
### Learn more
30+
31+
import { Book, FileCode } from "lucide-react";
32+
33+
<Cards num={2}>
34+
<Card
35+
title="Decorator docs"
36+
href="/docs/sdk/python/decorators"
37+
icon={<Book />}
38+
/>
39+
<Card
40+
title="Notebook demonstrating all features"
41+
href="/docs/sdk/python/example"
42+
icon={<FileCode />}
43+
/>
44+
<Card
45+
title="Rap battle example notebook"
46+
href="/guides/cookbook/example_decorator_openai_langchain"
47+
icon={<FileCode />}
48+
/>
49+
</Cards>

0 commit comments

Comments
 (0)