Example Decorator Openai Langchain
This is a Jupyter notebook

Example: Langfuse Decorator + OpenAI Integration + Langchain Integration

%pip install langfuse openai langchain_openai langchain
import os
# Get keys for your project from the project settings page
os.environ["LANGFUSE_PUBLIC_KEY"] = ""
os.environ["LANGFUSE_SECRET_KEY"] = ""
os.environ["LANGFUSE_HOST"] = "" # 🇪🇺 EU region
# os.environ["LANGFUSE_HOST"] = "" # 🇺🇸 US region
# Your openai key
os.environ["OPENAI_API_KEY"] = ""


import random
from operator import itemgetter
from langchain_openai import ChatOpenAI
from langchain.prompts import ChatPromptTemplate
from langchain.schema import StrOutputParser
from langfuse.decorators import observe
from langfuse.decorators import langfuse_context, observe
# import openai
from langfuse.openai import openai

Example: LLM Rap Battle

def get_random_rap_topic():
  topics = [
      "OSS software",
      "artificial general intelligence"
  return random.choice(topics)
def summarize_rap_langchain(rap):
    langfuse_handler = langfuse_context.get_current_langchain_handler()
    # Create chain
    prompt = ChatPromptTemplate.from_template("Summarrize this rap: {rap}")
    model = ChatOpenAI()
    chain = prompt | model | StrOutputParser()
    # Pass handler to invoke
    summary = chain.invoke(
        {"rap": rap},
    return summary
def rap_battle(turns: int = 5):
  topic = get_random_rap_topic()
  print(f"Topic: {topic}")
     tags=["Launch Week 1"]
  messages = [
      {"role": "system", "content": "We are all rap artist. When it is our turn, we drop a fresh line."},
      {"role": "user", "content": f"Kick it off, today's topic is {topic}, here's the mic..."}
  for turn in range(turns):
      completion =
      rap_line = completion.choices[0].message.content
      messages.append({"role": "assistant", "content": rap_line})
      print(f"\nRap {turn}: {rap_line}")
  summary = summarize_rap_langchain([message['content'] for message in messages])
  return summary
rap_summary = rap_battle(turns=4)
print("\nSummary: " + rap_summary)

Was this page useful?

Questions? We're here to help

Subscribe to updates