Home / File/ groq.py — langchain Source File

groq.py — langchain Source File

Architecture documentation for groq.py, a python file in the langchain codebase. 6 imports, 0 dependents.

File python CoreAbstractions MessageSchema 6 imports 6 functions

Entity Profile

Dependency Diagram

graph LR
  dd06d1a3_1164_37f6_7135_374817fb5652["groq.py"]
  7025b240_fdc3_cf68_b72f_f41dac94566b["json"]
  dd06d1a3_1164_37f6_7135_374817fb5652 --> 7025b240_fdc3_cf68_b72f_f41dac94566b
  67ec3255_645e_8b6e_1eff_1eb3c648ed95["re"]
  dd06d1a3_1164_37f6_7135_374817fb5652 --> 67ec3255_645e_8b6e_1eff_1eb3c648ed95
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  dd06d1a3_1164_37f6_7135_374817fb5652 --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  d758344f_537f_649e_f467_b9d7442e86df["langchain_core.messages"]
  dd06d1a3_1164_37f6_7135_374817fb5652 --> d758344f_537f_649e_f467_b9d7442e86df
  a1369c93_b21f_2edb_d15c_ec3e09ac1e42["langchain_core.messages.base"]
  dd06d1a3_1164_37f6_7135_374817fb5652 --> a1369c93_b21f_2edb_d15c_ec3e09ac1e42
  23170be8_e8a8_a728_6471_2a7fe6195245["langchain_core.messages.block_translators"]
  dd06d1a3_1164_37f6_7135_374817fb5652 --> 23170be8_e8a8_a728_6471_2a7fe6195245
  style dd06d1a3_1164_37f6_7135_374817fb5652 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Derivations of standard content blocks from Groq content."""

import json
import re
from typing import Any

from langchain_core.messages import AIMessage, AIMessageChunk
from langchain_core.messages import content as types
from langchain_core.messages.base import _extract_reasoning_from_additional_kwargs


def _populate_extras(
    standard_block: types.ContentBlock, block: dict[str, Any], known_fields: set[str]
) -> types.ContentBlock:
    """Mutate a block, populating extras."""
    if standard_block.get("type") == "non_standard":
        return standard_block

    for key, value in block.items():
        if key not in known_fields:
            if "extras" not in standard_block:
                # Below type-ignores are because mypy thinks a non-standard block can
                # get here, although we exclude them above.
                standard_block["extras"] = {}  # type: ignore[typeddict-unknown-key]
            standard_block["extras"][key] = value  # type: ignore[typeddict-item]

    return standard_block


def _parse_code_json(s: str) -> dict:
    """Extract Python code from Groq built-in tool content.

    Extracts the value of the 'code' field from a string of the form:
    {"code": some_arbitrary_text_with_unescaped_quotes}

    As Groq may not escape quotes in the executed tools, e.g.:
    ```
    '{"code": "import math; print("The square root of 101 is: "); print(math.sqrt(101))"}'
    ```
    """  # noqa: E501
    m = re.fullmatch(r'\s*\{\s*"code"\s*:\s*"(.*)"\s*\}\s*', s, flags=re.DOTALL)
    if not m:
        msg = (
            "Could not extract Python code from Groq tool arguments. "
            "Expected a JSON object with a 'code' field."
        )
        raise ValueError(msg)
    return {"code": m.group(1)}


def _convert_to_v1_from_groq(message: AIMessage) -> list[types.ContentBlock]:
    """Convert groq message content to v1 format."""
    content_blocks: list[types.ContentBlock] = []

    if reasoning_block := _extract_reasoning_from_additional_kwargs(message):
        content_blocks.append(reasoning_block)

    if executed_tools := message.additional_kwargs.get("executed_tools"):
        for idx, executed_tool in enumerate(executed_tools):
            args: dict[str, Any] | None = None
// ... (98 more lines)

Subdomains

Dependencies

  • json
  • langchain_core.messages
  • langchain_core.messages.base
  • langchain_core.messages.block_translators
  • re
  • typing

Frequently Asked Questions

What does groq.py do?
groq.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, MessageSchema subdomain.
What functions are defined in groq.py?
groq.py defines 6 function(s): _convert_to_v1_from_groq, _parse_code_json, _populate_extras, _register_groq_translator, translate_content, translate_content_chunk.
What does groq.py depend on?
groq.py imports 6 module(s): json, langchain_core.messages, langchain_core.messages.base, langchain_core.messages.block_translators, re, typing.
Where is groq.py in the architecture?
groq.py is located at libs/core/langchain_core/messages/block_translators/groq.py (domain: CoreAbstractions, subdomain: MessageSchema, directory: libs/core/langchain_core/messages/block_translators).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free