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

DOC: Document how to pass callbacks with Runnable methods (e.g., .invoke / .batch) #16379

Closed
eyurtsev opened this issue Jan 22, 2024 · 1 comment
Labels
🤖:docs Changes to documentation and examples, like .md, .rst, .ipynb files. Changes to the docs/ folder good first issue Good for newcomers

Comments

@eyurtsev
Copy link
Collaborator

Issue with current documentation:

https://python.langchain.com/docs/modules/callbacks/#when-do-you-want-to-use-each-of-these

Idea or request for content:

Document how to pass callbacks via .invoke

Copy link

dosubot bot commented Jan 22, 2024

🤖

In the LangChain framework, callbacks are passed through the config parameter in the invoke, batch, ainvoke, and abatch methods. The config parameter is an instance of RunnableConfig or a list of RunnableConfig instances.

Here is an example of how you can pass a callback using the config parameter:

from langchain_core.tracers.root_listeners import RootListenersTracer

# Define your callbacks
def on_start(run):
    print(f"Start: {run}")

def on_end(run):
    print(f"End: {run}")

def on_error(run):
    print(f"Error: {run}")

# Create a RunnableConfig with your callbacks
config = {
    "callbacks": [
        RootListenersTracer(
            config=config,
            on_start=on_start,
            on_end=on_end,
            on_error=on_error,
        )
    ],
}

# Pass the config to the invoke method
output = runnable.invoke(input, config)

In this example, on_start, on_end, and on_error are callback functions that are called when the Runnable starts, ends, and encounters an error, respectively. These callbacks are wrapped in a RootListenersTracer instance, which is passed to the invoke method through the config parameter.

The same approach can be used for the batch, ainvoke, and abatch methods. You can find more details in the source code.

The invoke and batch methods of the LangChain framework accept the following parameters:

  1. invoke method:

    • input: Input: The input to the runnable.
    • config: Optional[RunnableConfig] = None: The config to use for the runnable.
    • **kwargs: Optional[Any]: Additional keyword arguments to pass to the runnable.
  2. batch method:

    • inputs: List[Input]: A list of inputs to the runnable.
    • config: Optional[Union[RunnableConfig, List[RunnableConfig]]] = None: The config to use for the runnable. It can be a single config or a list of configs.
    • return_exceptions: bool = False: A flag indicating whether to return exceptions or not.
    • **kwargs: Optional[Any]: Additional keyword arguments to pass to the runnable.

The RunnableConfig class in the LangChain framework accepts the following parameters:

  1. tags: This is a list of strings. Tags for this call and any sub-calls (e.g., a Chain calling an LLM). You can use these to filter calls.

  2. metadata: This is a dictionary where keys should be strings, and values should be JSON-serializable. Metadata for this call and any sub-calls (e.g., a Chain calling an LLM).

  3. callbacks: This is an optional parameter. Callbacks for this call and any sub-calls (e.g., a Chain calling an LLM). Tags are passed to all callbacks, metadata is passed to handle*Start callbacks.

  4. run_name: This is a string parameter. Name for the tracer run for this call. Defaults to the name of the class.

  5. max_concurrency: This is an optional integer parameter. Maximum number of parallel calls to make. If not provided, defaults to ThreadPoolExecutor's default.

  6. recursion_limit: This is an integer parameter. Maximum number of times a call can recurse. If not provided, defaults to 25.

  7. configurable: This is a dictionary. Runtime values for attributes previously made configurable on this Runnable, or sub-Runnables, through .configurable_fields() or .configurable_alternatives(). Check .output_schema() for a description of the attributes that have been made configurable.

I hope this helps! If you have any other questions, feel free to ask.

Sources


This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

@dosubot dosubot bot added the 🤖:docs Changes to documentation and examples, like .md, .rst, .ipynb files. Changes to the docs/ folder label Jan 22, 2024
@eyurtsev eyurtsev changed the title DOC: Passing callbacks with .invoke DOC: Document how to pass callbacks with Runnable methods (e.g., .invoke / .batch) Jan 22, 2024
@eyurtsev eyurtsev added the good first issue Good for newcomers label Jan 22, 2024
eyurtsev pushed a commit that referenced this issue Jan 22, 2024
The callbacks get started demo code was updated , replacing the
chain.run() command ( which is now depricated) ,with the updated
chain.invoke() command.
Solving the following issue : #16379
Twitter/X : @Hazxhx
eyurtsev pushed a commit that referenced this issue Mar 4, 2024
…8403)

## PR title
Docs: Updated callbacks/index.mdx adding example on runnable methods

## PR message
- **Description:** Updated callbacks/index.mdx adding an example on how
to pass callbacks to the runnable methods (invoke, batch, ...)
- **Issue:** #16379
- **Dependencies:** None
gkorland pushed a commit to FalkorDB/langchain that referenced this issue Mar 30, 2024
…ngchain-ai#18403)

## PR title
Docs: Updated callbacks/index.mdx adding example on runnable methods

## PR message
- **Description:** Updated callbacks/index.mdx adding an example on how
to pass callbacks to the runnable methods (invoke, batch, ...)
- **Issue:** langchain-ai#16379
- **Dependencies:** None
@dosubot dosubot bot added the stale Issue has not had recent activity or appears to be solved. Stale issues will be automatically closed label Apr 23, 2024
@dosubot dosubot bot closed this as not planned Won't fix, can't repro, duplicate, stale Apr 30, 2024
@dosubot dosubot bot removed the stale Issue has not had recent activity or appears to be solved. Stale issues will be automatically closed label Apr 30, 2024
pprados pushed a commit to pprados/langchain that referenced this issue May 27, 2024
The callbacks get started demo code was updated , replacing the
chain.run() command ( which is now depricated) ,with the updated
chain.invoke() command.
Solving the following issue : langchain-ai#16379
Twitter/X : @Hazxhx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤖:docs Changes to documentation and examples, like .md, .rst, .ipynb files. Changes to the docs/ folder good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant