Repository navigation
Add langchain instrumentation #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Radhika Gupta (rads-1996)
merged 32 commits into
microsoft:main
from
rads-1996:add-langchain-instrumentation
Apr 10, 2026
Merged
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 75ad80a
Improve logic and add unit tests
rads-1996 6418623
Update src/genai/_langchain/_utils.py
rads-1996 724686a
Update src/genai/_langchain/_tracer.py
rads-1996 0309731
Fixed format
rads-1996 37eea68
Fix datetime issue
rads-1996 d5d81fd
Fix lint
rads-1996 ae751b4
Fix tests
rads-1996 556127c
Fix lint
rads-1996 a30f28d
Fix lint on tests
rads-1996 baf05bc
Fix imports on the sample
rads-1996 8f0a2f6
Remove unused import from sample
rads-1996 55de642
Add CHANGELOG
rads-1996 827d71a
Fix mypy errors
rads-1996 c70d174
Add copyright header to sample
rads-1996 2cc6953
Fix mypy error in utils
rads-1996 dd47627
Fix mypy error in test
rads-1996 31df017
Cleanup sample
rads-1996 a189e2b
Move genai folder under microsoft
rads-1996 1f985b3
Rename new path
rads-1996 e45078e
Add langchain instrumentation
rads-1996 c8b3fa8
Improve logic and add unit tests
rads-1996 d701531
Update src/genai/_langchain/_utils.py
rads-1996 e0b5a68
Update src/genai/_langchain/_tracer.py
rads-1996 b96b0a2
Fixed format
rads-1996 b028451
Fix lint
rads-1996 f3fed64
Fix lint
rads-1996 f370617
Fix mypy errors
rads-1996 55e2914
Fix mypy error in utils
rads-1996 628020d
Move genai folder under microsoft
rads-1996 4c9772c
Update entry path
rads-1996 51f1587
Fix test coverage
rads-1996 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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}, | ||
| } | ||
| ) | ||
|
|
||
| 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() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.