Skip to content
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

feat(langchain): [MLOB-1972] update langchain to handle vertex and gemini llm calls #11642

Merged
merged 1 commit into from
Dec 10, 2024

Conversation

ncybul
Copy link
Contributor

@ncybul ncybul commented Dec 9, 2024

This PR updates the Langchain integration to handle LLM calls instrumented in the Vertex AI and Gemini integrations by checking for their respective provider names (vertexai and google_palm) and instrumenting the wrapper Langchain calls as workflow spans if these provider names are detected.

Importantly, because of the way Langchain invokes chat generations for Vertex AI and Gemini, our integrations will not capture these inner LLM calls (in both cases, the prediction client is called directly with the input instead of using the chat.send_message method which we have instrumented in both cases). Therefore, we will only capture the Langchain LLM call and hence leave it as an LLM span for chat generations.

Testing

As we work on a more stable way to test our langchain integration, I opted to manually verify these changes by submitting traces to staging. Below I have included the code that I ran and the resulting trace that appeared in the product. It is expected that LLM calls have a langchain workflow span with a Gemini/Vertex AI child LLM span whereas Chat calls have only the Langchain LLM span instrumented (see above for details on why this is the case).

Gemini LLM call

from langchain_google_genai import GoogleGenerativeAI
llm = GoogleGenerativeAI(model="gemini-pro")
print(
    llm.invoke(
        "What are some tips for improving sleep quality?"
    )
)

image

Gemini Chat call

from langchain_google_genai import ChatGoogleGenerativeAI
llm = ChatGoogleGenerativeAI(model="gemini-pro")
resp = llm.invoke("Tell me a joke.")
print(resp)

image

Vertex AI LLM call

from langchain_google_vertexai import VertexAI
model = VertexAI(model_name="gemini-pro")
message = "What is the optimal temperature for sleeping?"
model.invoke(message)

image

Vertex AI Chat call

from langchain_google_vertexai import ChatVertexAI
llm = ChatVertexAI(
    model="gemini-1.5-flash-001",
    temperature=0,
    max_tokens=None,
    max_retries=6,
    stop=None,
)
messages = [
    (
        "system",
        "You are a helpful assistant that translates English to French. Translate the user sentence.",
    ),
    ("human", "I love programming."),
]
ai_msg = llm.invoke(messages)
print(ai_msg)

image

Checklist

  • PR author has checked that all the criteria below are met
  • The PR description includes an overview of the change
  • The PR description articulates the motivation for the change
  • The change includes tests OR the PR description describes a testing strategy
  • The PR description notes risks associated with the change, if any
  • Newly-added code is easy to change
  • The change follows the library release note guidelines
  • The change includes or references documentation updates if necessary
  • Backport labels are set (if applicable)

Reviewer Checklist

  • Reviewer has checked that all the criteria below are met
  • Title is accurate
  • All changes are related to the pull request's stated goal
  • Avoids breaking API changes
  • Testing strategy adequately addresses listed risks
  • Newly-added code is easy to change
  • Release note makes sense to a user of the library
  • If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment
  • Backport labels are set in a manner that is consistent with the release branch maintenance policy

Copy link
Contributor

github-actions bot commented Dec 9, 2024

CODEOWNERS have been resolved as:

ddtrace/llmobs/_integrations/langchain.py                               @DataDog/ml-observability

@ncybul ncybul changed the title [MLOB-1949] update langchain to handle vertex and gemini llm calls feat(langchain): [MLOB-1949] update langchain to handle vertex and gemini llm calls Dec 9, 2024
@datadog-dd-trace-py-rkomorn
Copy link

datadog-dd-trace-py-rkomorn bot commented Dec 9, 2024

Datadog Report

Branch report: nicole-cybul/add-langchain-tests2
Commit report: e4d51b7
Test service: dd-trace-py

✅ 0 Failed, 20 Passed, 1448 Skipped, 1m 9.81s Total duration (35m 36.91s time saved)

@pr-commenter
Copy link

pr-commenter bot commented Dec 9, 2024

Benchmarks

Benchmark execution time: 2024-12-09 15:54:20

Comparing candidate commit e4d51b7 in PR branch nicole-cybul/add-langchain-tests2 with baseline commit 7320b6f in branch main.

Found 0 performance improvements and 2 performance regressions! Performance is the same for 392 metrics, 2 unstable metrics.

scenario:iast_aspects-ospathbasename_aspect

  • 🟥 execution_time [+367.818ns; +418.590ns] or [+10.984%; +12.500%]

scenario:iast_aspects-ospathdirname_aspect

  • 🟥 execution_time [+320.994ns; +389.810ns] or [+8.635%; +10.487%]

@ncybul ncybul changed the title feat(langchain): [MLOB-1949] update langchain to handle vertex and gemini llm calls feat(langchain): [MLOB-1972] update langchain to handle vertex and gemini llm calls Dec 9, 2024
@ncybul ncybul added the changelog/no-changelog A changelog entry is not required for this PR. label Dec 9, 2024
@ncybul ncybul marked this pull request as ready for review December 10, 2024 14:41
@ncybul ncybul requested a review from a team as a code owner December 10, 2024 14:41
@ncybul ncybul merged commit e04c8ac into main Dec 10, 2024
220 of 237 checks passed
@ncybul ncybul deleted the nicole-cybul/add-langchain-tests2 branch December 10, 2024 19:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog/no-changelog A changelog entry is not required for this PR.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants