Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
a161005
Add langchain instrumentation
rads-1996 Apr 9, 2026
75ad80a
Improve logic and add unit tests
rads-1996 Apr 9, 2026
6418623
Update src/genai/_langchain/_utils.py
rads-1996 Apr 9, 2026
724686a
Update src/genai/_langchain/_tracer.py
rads-1996 Apr 9, 2026
0309731
Fixed format
rads-1996 Apr 9, 2026
37eea68
Fix datetime issue
rads-1996 Apr 9, 2026
d5d81fd
Fix lint
rads-1996 Apr 9, 2026
ae751b4
Fix tests
rads-1996 Apr 9, 2026
556127c
Fix lint
rads-1996 Apr 10, 2026
a30f28d
Fix lint on tests
rads-1996 Apr 10, 2026
baf05bc
Fix imports on the sample
rads-1996 Apr 10, 2026
8f0a2f6
Remove unused import from sample
rads-1996 Apr 10, 2026
55de642
Add CHANGELOG
rads-1996 Apr 10, 2026
827d71a
Fix mypy errors
rads-1996 Apr 10, 2026
c70d174
Add copyright header to sample
rads-1996 Apr 10, 2026
2cc6953
Fix mypy error in utils
rads-1996 Apr 10, 2026
dd47627
Fix mypy error in test
rads-1996 Apr 10, 2026
31df017
Cleanup sample
rads-1996 Apr 10, 2026
a189e2b
Move genai folder under microsoft
rads-1996 Apr 10, 2026
1f985b3
Rename new path
rads-1996 Apr 10, 2026
e45078e
Add langchain instrumentation
rads-1996 Apr 9, 2026
c8b3fa8
Improve logic and add unit tests
rads-1996 Apr 9, 2026
d701531
Update src/genai/_langchain/_utils.py
rads-1996 Apr 9, 2026
e0b5a68
Update src/genai/_langchain/_tracer.py
rads-1996 Apr 9, 2026
b96b0a2
Fixed format
rads-1996 Apr 9, 2026
b028451
Fix lint
rads-1996 Apr 9, 2026
f3fed64
Fix lint
rads-1996 Apr 10, 2026
f370617
Fix mypy errors
rads-1996 Apr 10, 2026
55e2914
Fix mypy error in utils
rads-1996 Apr 10, 2026
628020d
Move genai folder under microsoft
rads-1996 Apr 10, 2026
4c9772c
Update entry path
rads-1996 Apr 10, 2026
51f1587
Fix test coverage
rads-1996 Apr 10, 2026
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### Features Added

- Add langchain instrumentation
([#26](https://github.com/microsoft/opentelemetry-distro-python/pull/26))
- Add Microsoft Opentelemetry Distro Configuration
([#9](https://github.com/microsoft/opentelemetry-distro-python/pull/9))
- Add langchain samples
Expand Down
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ dependencies = [
"opentelemetry-instrumentation-urllib3==0.61b0",
"opentelemetry-instrumentation-logging==0.61b0",
"opentelemetry-resource-detector-azure<1.0.0,>=0.1.5",
"langchain-core>=0.2.0",
"wrapt>=1.0",
"opentelemetry-util-genai>=0.3b0",
]

[project.optional-dependencies]
Expand All @@ -40,6 +43,9 @@ dev = [
"pytest-cov>=5.0",
]

[project.entry-points.opentelemetry_instrumentor]
langchain = "microsoft.genai._langchain._tracer_instrumentor:LangChainInstrumentor"

[tool.setuptools]
package-dir = {"" = "src"}

Expand Down
87 changes: 87 additions & 0 deletions samples/langchain/sample_langchain_instrumentation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

from microsoft.opentelemetry._distro import use_microsoft_opentelemetry
from langchain_core.messages import HumanMessage, SystemMessage
from langchain_core.tools import tool
from langchain_openai import ChatOpenAI
from langchain.agents import create_agent


endpoint = "<AZURE_OPENAI_ENDPOINT>"
model_name = "gpt-4.1"
deployment_name = "gpt-4.1"

api_key = "<AZURE_OPENAI_API_KEY>"

use_microsoft_opentelemetry(
sampling_ratio=1.0,
instrumentation_options={
"langchain": {"enabled": True},
Comment thread
rads-1996 marked this conversation as resolved.
}
)

def main():

# ChatOpenAI
llm = ChatOpenAI(
model="gpt-4.1",
temperature=0.1,
max_tokens=100,
top_p=0.9,
frequency_penalty=0.5,
presence_penalty=0.5,
stop_sequences=["\n", "Human:", "AI:"],
seed=100,
api_key=api_key, # Do not include api_key and base_url if using the OPENAI_API_KEY environment variable
base_url=endpoint,
)

messages = [
SystemMessage(content="You are a helpful assistant!"),
HumanMessage(content="What is the capital of France?"),
]

result = llm.invoke(messages)
print("LLM output:\n", result)

@tool
def get_population(city: str) -> str:
"""Get the population of a city."""
populations = {
"paris": "2.1 million",
"london": "8.9 million",
"tokyo": "14 million",
"new york": "8.3 million",
}
return populations.get(city.lower(), f"Population data not available for {city}.")

@tool
def get_famous_landmark(city: str) -> str:
"""Get the most famous landmark in a city."""
landmarks = {
"paris": "Eiffel Tower",
"london": "Big Ben",
"tokyo": "Tokyo Tower",
"new york": "Statue of Liberty",
}
return landmarks.get(city.lower(), f"No landmark info for {city}.")

agent = create_agent(
llm,
tools=[get_population, get_famous_landmark],
system_prompt="You are a helpful travel assistant. Use the tools to answer questions about cities.",
name="Travel_Assistant"
)

print("\n--- Agent run ---")
user_input = "What is the population of Paris and what is its most famous landmark?"
agent_result = agent.invoke({"messages": [("human", user_input)]})
output = agent_result["messages"][-1].content
print("Agent output:\n", output)




if __name__ == "__main__":
main()
2 changes: 2 additions & 0 deletions src/microsoft/genai/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
6 changes: 6 additions & 0 deletions src/microsoft/genai/_langchain/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

from microsoft.genai._langchain._tracer_instrumentor import LangChainInstrumentor

__all__ = ["LangChainInstrumentor"]
Loading
Loading