Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions samples/langchain/sample_arise_langchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,41 @@

from openinference.instrumentation.langchain import LangChainInstrumentor

configure_azure_monitor(connection_string="InstrumentationKey=...") # TODO: This will be replaced with the opentelemetry distro
configure_azure_monitor( # Replace with the opentelemetry distro
connection_string="InstrumentationKey=..."
)

LangChainInstrumentor().instrument()

# If using Azure OpenAI endpoint and API KEY
endpoint = "<AZURE_OPENAI_ENDPOINT>"
model_name = "gpt-4.1"
api_key = "<AZURE_OPENAI_API_KEY>"
ENDPOINT = "<AZURE_OPENAI_ENDPOINT>"
MODEL_NAME = "gpt-4.1"
API_KEY = "<AZURE_OPENAI_API_KEY>"

# Otherwise, set the env variable OPENAI_API_KEY

image_url = "<IMAGE_URL>"
IMAGE_URL = "<IMAGE_URL>"

model = ChatOpenAI(model=model_name, api_key=api_key, base_url=endpoint) # Do not include api_key and base_url if using the OPENAI_API_KEY environment variable
image_data = base64.b64encode(httpx.get(image_url).content).decode("utf-8")
model = ChatOpenAI(
model=MODEL_NAME,
api_key=API_KEY,
base_url=ENDPOINT, # Do not include api_key and base_url if using the OPENAI_API_KEY environment variable
Comment thread
rads-1996 marked this conversation as resolved.
)
image_data = base64.b64encode(httpx.get(IMAGE_URL).content).decode("utf-8")

message = HumanMessage(
content=[
{"type": "text", "text": "describe this image"},
{
"type": "image_url",
"image_url": {"url": f"data:image/jpeg;base64,{image_data}", "detail": "low"},
"image_url": {
"url": f"data:image/jpeg;base64,{image_data}",
"detail": "low",
},
},
],
)

if __name__ == "__main__":
response = model.invoke([message])
print(response.content)
print(response.content)
51 changes: 35 additions & 16 deletions samples/langchain/sample_langchain_azure.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
"""Travel planner with nested agents — coordinator delegates to specialist agents."""

from __future__ import annotations

import os
import random
from uuid import uuid4

Expand All @@ -18,16 +15,15 @@
from langgraph.prebuilt import create_react_agent

# If using Azure OpenAI endpoint and API KEY
endpoint = "<AZURE_OPENAI_ENDPOINT>"
api_key = "<AZURE_OPENAI_API_KEY>"
model_name = "gpt-4.1"
ENDPOINT = "<AZURE_OPENAI_ENDPOINT>"
API_KEY = "<AZURE_OPENAI_API_KEY>"
MODEL_NAME = "gpt-4.1"

# Otherwise, set the env variable OPENAI_API_KEY

configure_azure_monitor( # TODO: This will be replaced with the opentelemetry distro
connection_string=
"InstrumentationKey=...",
)
configure_azure_monitor( # Replace with the opentelemetry distro
connection_string="InstrumentationKey=...",
)

tracer = AzureAIOpenTelemetryTracer(name="travel_planner", provider_name="openai")

Expand All @@ -53,17 +49,28 @@ def search_hotels(destination: str, check_in: str, check_out: str) -> str:
def search_activities(destination: str) -> str:
"""Find popular activities in a destination city."""
activities = {
"Paris": ["Eiffel Tower at sunset", "Seine dinner cruise", "Day trip to Versailles"],
"Paris": [
"Eiffel Tower at sunset",
"Seine dinner cruise",
"Day trip to Versailles",
],
"Tokyo": ["Sushi masterclass", "Ghibli Museum", "Hakone hot springs"],
"Rome": ["Colosseum tour", "Pasta masterclass", "Trastevere walk"],
}
items = activities.get(destination, ["Sightseeing", "Local cuisine", "Museum visit"])
items = activities.get(
destination, ["Sightseeing", "Local cuisine", "Museum visit"]
)
return "\n".join(f"- {a}" for a in items)


# --- Specialist Agents ---
def _make_llm(temperature: float = 0.3) -> ChatOpenAI:
return ChatOpenAI(model=model_name, api_key=api_key, base_url=endpoint, temperature=temperature) # Do not include api_key and base_url if using the OPENAI_API_KEY environment variable
return ChatOpenAI(
model=MODEL_NAME,
api_key=API_KEY,
base_url=ENDPOINT, # Do not include api_key and base_url if using the OPENAI_API_KEY environment variable
Comment thread
rads-1996 marked this conversation as resolved.
temperature=temperature,
)


flight_agent = create_react_agent(_make_llm(0.3), tools=[search_flights])
Expand All @@ -76,7 +83,13 @@ def _make_llm(temperature: float = 0.3) -> ChatOpenAI:
def find_flights(origin: str, destination: str, date: str) -> str:
"""Delegate to the flight specialist agent to find flights."""
result = flight_agent.invoke(
{"messages": [HumanMessage(content=f"Find flights from {origin} to {destination} on {date}.")]},
{
"messages": [
HumanMessage(
content=f"Find flights from {origin} to {destination} on {date}."
)
]
},
config={"callbacks": [tracer]},
)
return result["messages"][-1].content
Expand All @@ -86,7 +99,13 @@ def find_flights(origin: str, destination: str, date: str) -> str:
def find_hotels(destination: str, check_in: str, check_out: str) -> str:
"""Delegate to the hotel specialist agent to find hotels."""
result = hotel_agent.invoke(
{"messages": [HumanMessage(content=f"Find a boutique hotel in {destination} from {check_in} to {check_out}.")]},
{
"messages": [
HumanMessage(
content=f"Find a boutique hotel in {destination} from {check_in} to {check_out}."
)
]
},
config={"callbacks": [tracer]},
)
return result["messages"][-1].content
Expand Down Expand Up @@ -134,4 +153,4 @@ def main():


if __name__ == "__main__":
main()
main()
40 changes: 21 additions & 19 deletions samples/langchain/sample_opentelemetry.py
Original file line number Diff line number Diff line change
@@ -1,51 +1,53 @@

from azure.monitor.opentelemetry import configure_azure_monitor
from opentelemetry.instrumentation.langchain import LangChainInstrumentor
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage
from langchain_openai import ChatOpenAI

# This sample demonstrates the current state of the opentelemetry-instrumentation-langchain package (unreleased) and how it can be integrated with the opentelemetry distro
# This sample demonstrates the current state of the opentelemetry-instrumentation-langchain
# package (unreleased) and how it can be integrated with the microsoftopentelemetry distro
Comment thread
rads-1996 marked this conversation as resolved.

# If using Azure OpenAI endpoint and API KEY
endpoint = "<AZURE_OPENAI_ENDPOINT>"
model_name = "gpt-4.1"
api_key = "<AZURE_OPENAI_API_KEY>"
ENDPOINT = "<AZURE_OPENAI_ENDPOINT>"
MODEL_NAME = "gpt-4.1"
API_KEY = "<AZURE_OPENAI_API_KEY>"

# Otherwise, set the env variable OPENAI_API_KEY

configure_azure_monitor() # TODO: This will be replaced with the opentelemetry distro
"""
configure_microsoft_opentelemetry(
connection_string="InstrumentationKey=...",
enable_genai_langchain=True,
)
"""
configure_azure_monitor() # Replace with the opentelemetry distro

# configure_microsoft_opentelemetry(
# connection_string="InstrumentationKey=...",
# enable_genai_langchain=True,
# )

LangChainInstrumentor().instrument()

# Two models with different configs — each call produces its own span
creative_llm = ChatOpenAI(
model=model_name,
model=MODEL_NAME,
temperature=0.9,
max_tokens=200,
top_p=0.95,
api_key=api_key, # Do not include if using the OPENAI_API_KEY environment variable
base_url=endpoint, # Do not include if using the OPENAI_API_KEY environment variable
api_key=API_KEY, # Do not include if using the OPENAI_API_KEY environment variable
base_url=ENDPOINT, # Do not include if using the OPENAI_API_KEY environment variable
Comment thread
rads-1996 marked this conversation as resolved.
)

precise_llm = ChatOpenAI(
model=model_name,
model=MODEL_NAME,
temperature=0.0,
max_tokens=100,
top_p=0.1,
frequency_penalty=0.5,
presence_penalty=0.5,
seed=42,
api_key=api_key, # Do not include if using the OPENAI_API_KEY environment variable
base_url=endpoint,
api_key=API_KEY, # Do not include if using the OPENAI_API_KEY environment variable
base_url=ENDPOINT, # Do not include if using the OPENAI_API_KEY environment variable
Comment thread
rads-1996 marked this conversation as resolved.
)


# --------------- Scenarios ---------------


def multi_turn_conversation():
"""Multi-turn chat — each invoke() is a separate instrumented span."""
print("=== Multi-turn Conversation ===")
Expand Down Expand Up @@ -83,4 +85,4 @@ def compare_temperatures():
multi_turn_conversation()
compare_temperatures()

LangChainInstrumentor().uninstrument()
LangChainInstrumentor().uninstrument()
Loading