-
Notifications
You must be signed in to change notification settings - Fork 34
Add tools support in langchain #37
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
Merged
Changes from 15 commits
Commits
Show all changes
43 commits
Select commit
Hold shift + click to select a range
3d60029
Added tools support
wrisa 2fb9234
Add tools support in langchain
wrisa f36d295
Merge branch 'tools-langchain' of github.com:wrisa/opentelemetry-pyth…
wrisa 93cf06a
added changelog
wrisa 0badaae
updated changelog
wrisa cdc1e86
fixed errors
wrisa 070c81e
fix: move imports to top-level to fix PLC0415 lint errors
wrisa 7d32fdd
Merge branch 'main' into tools-langchain
wrisa 08ff1c3
resolved conflicts
wrisa 436c230
Merge branch 'tools-langchain' of github.com:wrisa/opentelemetry-pyth…
wrisa 0ed8921
fixed errors
wrisa 8c5adad
fixed error
wrisa efb8264
fixed tool.type
wrisa 2a95a90
added conformance tests
wrisa e8194a9
Merge branch 'main' into tools-langchain
wrisa a89f6c7
addressed review comments
wrisa 2f87cdb
Merge branch 'tools-langchain' of github.com:wrisa/opentelemetry-pyth…
wrisa 459b1bb
fixed error
wrisa 4946d98
Merge branch 'main' into tools-langchain
wrisa 265ef8a
Merge branch 'main' into tools-langchain
wrisa 9bf1906
Merge branch 'main' into tools-langchain
wrisa f3c3627
Merge branch 'main' into tools-langchain
wrisa dcf64f2
Merge branch 'tools-langchain' of github.com:wrisa/opentelemetry-pyth…
wrisa f17dde6
Merge branch 'main' into tools-langchain
wrisa 72e7724
fixed conformance tests
wrisa 97772f0
Merge branch 'tools-langchain' of github.com:wrisa/opentelemetry-pyth…
wrisa ed6339c
Merge branch 'main' into tools-langchain
wrisa bd79d8c
fixed conformance tests and reverted change
wrisa b8ae7b6
Merge branch 'tools-langchain' of github.com:wrisa/opentelemetry-pyth…
wrisa 0c635d2
Merge branch 'main' into tools-langchain
wrisa 5324f69
Merge branch 'main' into tools-langchain
wrisa 3193db5
Merge branch 'main' into tools-langchain
wrisa 94f2af4
Merge branch 'main' into tools-langchain
wrisa 5d85bc9
addressed comment
wrisa 43760ca
resolved conflict
wrisa 3ac28de
fixed error
wrisa daab80a
fixed failures
wrisa 1c11d66
Merge branch 'main' into tools-langchain
wrisa 2d835c9
addressed review comments
wrisa 25c3b62
addressed review comments
wrisa 3213dab
fixed tests
wrisa e1eb409
tool result AnyValue
wrisa 897d8ce
fixed error
wrisa 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
1 change: 1 addition & 0 deletions
1
instrumentation/opentelemetry-instrumentation-genai-langchain/.changelog/37.added
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 @@ | ||
| Added tools span and tools definitions in inference span. | ||
116 changes: 116 additions & 0 deletions
116
instrumentation/opentelemetry-instrumentation-genai-langchain/examples/tools/main.py
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,116 @@ | ||
| # Copyright The OpenTelemetry Authors | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """ | ||
| Tool-calling example without agents, built with LangChain. | ||
|
|
||
| Uses ChatOpenAI with bind_tools to let the model call calculator tools directly, | ||
| then manually dispatches tool calls and feeds results back to the model. | ||
| OpenTelemetry LangChain instrumentation traces the LLM calls. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import json | ||
|
|
||
| from langchain_core.messages import HumanMessage, ToolMessage | ||
| from langchain_core.tools import tool | ||
| from langchain_openai import ChatOpenAI | ||
|
|
||
| from opentelemetry import _logs, metrics, trace | ||
| from opentelemetry.exporter.otlp.proto.grpc._log_exporter import ( | ||
| OTLPLogExporter, | ||
| ) | ||
| from opentelemetry.exporter.otlp.proto.grpc.metric_exporter import ( | ||
| OTLPMetricExporter, | ||
| ) | ||
| from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import ( | ||
| OTLPSpanExporter, | ||
| ) | ||
| from opentelemetry.instrumentation.langchain import LangChainInstrumentor | ||
|
wrisa marked this conversation as resolved.
Outdated
|
||
| from opentelemetry.sdk._logs import LoggerProvider | ||
| from opentelemetry.sdk._logs.export import BatchLogRecordProcessor | ||
| from opentelemetry.sdk.metrics import MeterProvider | ||
| from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader | ||
| from opentelemetry.sdk.trace import TracerProvider | ||
| from opentelemetry.sdk.trace.export import BatchSpanProcessor | ||
|
|
||
| # Configure tracing | ||
| trace.set_tracer_provider(TracerProvider()) | ||
| span_processor = BatchSpanProcessor(OTLPSpanExporter()) | ||
| trace.get_tracer_provider().add_span_processor(span_processor) | ||
|
|
||
| # Configure logging | ||
| _logs.set_logger_provider(LoggerProvider()) | ||
| _logs.get_logger_provider().add_log_record_processor( | ||
| BatchLogRecordProcessor(OTLPLogExporter()) | ||
| ) | ||
|
|
||
| # Configure metrics | ||
| metrics.set_meter_provider( | ||
| MeterProvider( | ||
| metric_readers=[ | ||
| PeriodicExportingMetricReader( | ||
| OTLPMetricExporter(), | ||
| ), | ||
| ] | ||
| ) | ||
| ) | ||
|
|
||
|
|
||
| @tool | ||
| def multiply(a: float, b: float) -> float: | ||
| """Multiply two numbers together.""" | ||
| return a * b | ||
|
|
||
|
|
||
| @tool | ||
| def add(a: float, b: float) -> float: | ||
| """Add two numbers together.""" | ||
| return a + b | ||
|
|
||
|
|
||
| TOOLS = [multiply, add] | ||
| TOOLS_BY_NAME = {t.name: t for t in TOOLS} | ||
|
|
||
|
|
||
| def main() -> None: | ||
| LangChainInstrumentor().instrument() | ||
|
wrisa marked this conversation as resolved.
Outdated
|
||
|
|
||
| llm = ChatOpenAI( | ||
| model="gpt-3.5-turbo", | ||
| temperature=0.1, | ||
| max_tokens=100, | ||
| top_p=0.9, | ||
| seed=100, | ||
| ) | ||
| llm_with_tools = llm.bind_tools(TOOLS) | ||
|
|
||
| messages = [HumanMessage(content="What is (3 * 4) + 7?")] | ||
|
|
||
| # First LLM call — model may request tool calls | ||
| response = llm_with_tools.invoke(messages) | ||
| messages.append(response) | ||
|
|
||
| # Dispatch tool calls until the model stops requesting them | ||
| while response.tool_calls: | ||
| for tool_call in response.tool_calls: | ||
| selected_tool = TOOLS_BY_NAME[tool_call["name"]] | ||
| tool_output = selected_tool.invoke(tool_call["args"]) | ||
| messages.append( | ||
| ToolMessage( | ||
| content=json.dumps(tool_output), | ||
| tool_call_id=tool_call["id"], | ||
| ) | ||
| ) | ||
|
|
||
| response = llm_with_tools.invoke(messages) | ||
| messages.append(response) | ||
|
|
||
| print("Final answer:", response.content) | ||
|
|
||
| LangChainInstrumentor().uninstrument() | ||
|
wrisa marked this conversation as resolved.
Outdated
|
||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
5 changes: 5 additions & 0 deletions
5
...rumentation/opentelemetry-instrumentation-genai-langchain/examples/tools/requirements.txt
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,5 @@ | ||
| langchain==0.3.21 | ||
| langchain_openai | ||
| langgraph | ||
| opentelemetry-sdk>=1.31.0 | ||
| opentelemetry-exporter-otlp-proto-grpc>=1.31.0 |
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
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.