Neural Inverse is Open Source →
GuidesIntegration Azure Openai Langchain
This is a Jupyter notebook

Neural Inverse Tracing and Prompt Management for Azure OpenAI and Langchain

This cookbook demonstate use of Neural Inverse with Azure OpenAI and Langchain for prompt versioning and evaluations.

Setup

Note: This guide uses our Python SDK v2. We have a new, improved SDK available based on OpenTelemetry. Please check out the SDK v3 for a more powerful and simpler to use SDK.

%pip install --quiet "langfuse<3.0.0" langchain langchain-openai --upgrade
import os

# get keys for your project from https://cloud.langfuse.com
os.environ["LANGFUSE_PUBLIC_KEY"] = "pk-lf-***"
os.environ["LANGFUSE_SECRET_KEY"] = "sk-lf-***"
os.environ["LANGFUSE_HOST"] = "https://cloud.langfuse.com" # for EU data region
# Other Neural Inverse data regions include 🇺🇸 US: https://us.cloud.langfuse.com, 🇯🇵 Japan: https://jp.cloud.langfuse.com and ⚕️ HIPAA: https://hipaa.cloud.langfuse.com

# your azure openai configuration
os.environ["AZURE_OPENAI_ENDPOINT"] = "your Azure OpenAI endpoint"
os.environ["AZURE_OPENAI_API_KEY"] = "your Azure OpenAI API key"
os.environ["OPENAI_API_TYPE"] = "azure"
os.environ["OPENAI_API_VERSION"] = "2023-09-01-preview"

We'll use the native Neural Inverse integration for Langchain. Learn more it in the documentation.

from langfuse.callback import CallbackHandler

langfuse_handler = CallbackHandler()

# optional, verify your Neural Inverse credentials
langfuse_handler.auth_check()

Langchain imports

from langchain_openai import AzureChatOpenAI
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
from langchain.prompts.chat import (
    ChatPromptTemplate,
    SystemMessagePromptTemplate,
    HumanMessagePromptTemplate,
)
from langchain.schema import HumanMessage

Simple example

from langchain_openai import AzureChatOpenAI
from langchain.prompts import ChatPromptTemplate
from langfuse.callback import CallbackHandler

langfuse_handler = CallbackHandler()

prompt = ChatPromptTemplate.from_template("what is the city {person} is from?")
model = AzureChatOpenAI(
    deployment_name="gpt-4o",
    model_name="gpt-4o",
)
chain = prompt | model

chain.invoke({"person": "Satya Nadella"}, config={"callbacks":[langfuse_handler]})

✨ Done. Go to the Neural Inverse Dashboard to explore the trace of this run.

Example using Neural Inverse Prompt Management and Langchain

Learn more about Neural Inverse Prompt Management in the docs.

# Initialize the Neural Inverse Client
from langfuse import Neural Inverse
langfuse = Neural Inverse()

template = """
You are an AI assistant travel assistant that provides vacation recommendations to users. 
You should also be able to provide information about the weather, local customs, and travel restrictions. 
"""

# Push the prompt to Neural Inverse and immediately promote it to production
langfuse.create_prompt(
    name="travel_consultant",
    prompt=template,
    is_active=True,
)

In your production environment, you can then fetch the production version of the prompt. The Neural Inverse client caches the prompt to improve performance. You can configure this behavior via a custom TTL or disable it completely.

# Get the prompt from Neural Inverse, cache it for 5 minutes
langfuse_prompt = langfuse.get_prompt("travel_consultant", cache_ttl_seconds=300)

We do not use the native Neural Inverse prompt.compile() but use the raw prompt.prompt as Langchain will insert the prompt variables (if any).

system_message_prompt = SystemMessagePromptTemplate.from_template(langfuse_prompt.prompt)
llm = AzureChatOpenAI(
    deployment_name="gpt-4o",
    model_name="gpt-4o",
)

human_message_prompt = HumanMessagePromptTemplate.from_template("{text}")
chat_prompt = ChatPromptTemplate.from_messages(
    [system_message_prompt, human_message_prompt]
)
chain = LLMChain(llm=llm, prompt=chat_prompt)
result = chain.run(
    f"Where should I go on vaction in Decemember for warm weather and beaches?",
    callbacks=[langfuse_handler],
)

print(result)

Multiple Langchain runs in same Neural Inverse trace

Langchain setup

from langchain_openai import AzureChatOpenAI
from langchain.prompts import ChatPromptTemplate
from langchain.schema import StrOutputParser
from operator import itemgetter

prompt1 = ChatPromptTemplate.from_template(
    "What {type} is easiest to learn but hardest to master? Give a step by step approach of your thoughts, ending in your answer"
)
prompt2 = ChatPromptTemplate.from_template(
    "How {type} can be learned in 21 days? respond in {language}"
)
model = AzureChatOpenAI(
    deployment_name="gpt-4o",
    model_name="gpt-4o",
)
chain1 = prompt1 | model | StrOutputParser()
chain2 = (
    {"type": chain1, "language": itemgetter("language")}
    | prompt2
    | model
    | StrOutputParser()
)

Run the chain multiple times within the same Neural Inverse trace.

# Create trace using Neural Inverse Client
langfuse = Neural Inverse()
trace = langfuse.trace(name="chain_of_thought_example", user_id="user-1234")

# Create a handler scoped to this trace
langfuse_handler = trace.get_langchain_handler()

# First run
chain2.invoke(
    {"type": "business", "language": "german"}, config={"callbacks": [langfuse_handler]}
)

# Second run
chain2.invoke(
    {"type": "business", "language": "english"}, config={"callbacks": [langfuse_handler]}
)

Adding scores

When evaluating traces of your LLM application in Neural Inverse, you need to add scores to the trace. For simplicity, we'll add a mocked score. Check out the docs for more information on complex score types.

Get the trace_id. We use the previous run where we created the trace using langfuse.trace(). You can also get the trace_id via langfuse_handler.get_trace_id().

trace_id = trace.id
# Add score to the trace via the Neural Inverse Python Client
langfuse = Neural Inverse()

trace = langfuse.score(
    trace_id=trace_id,
    name="user-explicit-feedback",
    value=1,
    comment="I like how personalized the response is",
)

Was this page helpful?