You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
langchain/libs/core/langchain_core
Eugene Yurtsev 177af65dc4
core[minor]: RFC Add astream_events to Runnables (#16172)
This PR adds `astream_events` method to Runnables to make it easier to
stream data from arbitrary chains.

* Streaming only works properly in async right now
* One should use `astream()` with if mixing in imperative code as might
be done with tool implementations
* Astream_log has been modified with minimal additive changes, so no
breaking changes are expected
* Underlying callback code / tracing code should be refactored at some
point to handle things more consistently (OK for now)

- ~~[ ] verify event for on_retry~~ does not work until we implement
streaming for retry
- ~~[ ] Any rrenaming? Should we rename "event" to "hook"?~~
- [ ] Any other feedback from community?
- [x] throw NotImplementedError for `RunnableEach` for now

## Example

See this [Example
Notebook](dbbc7fa0d6/docs/docs/modules/agents/how_to/streaming_events.ipynb)
for an example with streaming in the context of an Agent

## Event Hooks Reference

Here is a reference table that shows some events that might be emitted
by the various Runnable objects.
Definitions for some of the Runnable are included after the table.


| event | name | chunk | input | output |

|----------------------|------------------|---------------------------------|-----------------------------------------------|-------------------------------------------------|
| on_chat_model_start | [model name] | | {"messages": [[SystemMessage,
HumanMessage]]} | |
| on_chat_model_stream | [model name] | AIMessageChunk(content="hello")
| | |
| on_chat_model_end | [model name] | | {"messages": [[SystemMessage,
HumanMessage]]} | {"generations": [...], "llm_output": None, ...} |
| on_llm_start | [model name] | | {'input': 'hello'} | |
| on_llm_stream | [model name] | 'Hello' | | |
| on_llm_end | [model name] | | 'Hello human!' |
| on_chain_start | format_docs | | | |
| on_chain_stream | format_docs | "hello world!, goodbye world!" | | |
| on_chain_end | format_docs | | [Document(...)] | "hello world!,
goodbye world!" |
| on_tool_start | some_tool | | {"x": 1, "y": "2"} | |
| on_tool_stream | some_tool | {"x": 1, "y": "2"} | | |
| on_tool_end | some_tool | | | {"x": 1, "y": "2"} |
| on_retriever_start | [retriever name] | | {"query": "hello"} | |
| on_retriever_chunk | [retriever name] | {documents: [...]} | | |
| on_retriever_end | [retriever name] | | {"query": "hello"} |
{documents: [...]} |
| on_prompt_start | [template_name] | | {"question": "hello"} | |
| on_prompt_end | [template_name] | | {"question": "hello"} |
ChatPromptValue(messages: [SystemMessage, ...]) |


Here are declarations associated with the events shown above:

`format_docs`:

```python
def format_docs(docs: List[Document]) -> str:
    '''Format the docs.'''
    return ", ".join([doc.page_content for doc in docs])

format_docs = RunnableLambda(format_docs)
```

`some_tool`:

```python
@tool
def some_tool(x: int, y: str) -> dict:
    '''Some_tool.'''
    return {"x": x, "y": y}
```

`prompt`:

```python
template = ChatPromptTemplate.from_messages(
    [("system", "You are Cat Agent 007"), ("human", "{question}")]
).with_config({"run_name": "my_template", "tags": ["my_template"]})
```
9 months ago
..
_api core[patch]: add alternative_import to deprecated (#15781) 9 months ago
beta core[patch], community[patch]: mark runnable context, lc load as beta (#15603) 9 months ago
callbacks core[minor]: RFC Add astream_events to Runnables (#16172) 9 months ago
documents Propagate context vars in all classes/methods 9 months ago
example_selectors adding vectorstore_kwarg attribute to search_similarity function (#14604) 9 months ago
globals IMPROVEMENT: filter global warnings properly (#13754) 11 months ago
language_models core[patch]: Release 0.1.7 (#15610) 9 months ago
load core[patch]: support old core namespaces (#16155) 9 months ago
messages core[patch]: simple prompt pretty printing (#15968) 9 months ago
output_parsers core: update json output parser (#15079) 9 months ago
outputs manual mapping (#14422) 10 months ago
prompts core[patch]: simple prompt pretty printing (#15968) 9 months ago
pydantic_v1 Separate out langchain_core package (#13577) 11 months ago
runnables core[minor]: RFC Add astream_events to Runnables (#16172) 9 months ago
tracers core[minor]: RFC Add astream_events to Runnables (#16172) 9 months ago
utils google-vertexai[minor]: add function calling on VertexAI (#15822) 9 months ago
__init__.py core[patch], community[patch]: mark runnable context, lc load as beta (#15603) 9 months ago
agents.py manual mapping (#14422) 10 months ago
caches.py REFACTOR: Refactor langchain_core (#13627) 11 months ago
chat_history.py core(minor): Allow explicit types for ChatMessageHistory adds (#14967) 10 months ago
chat_sessions.py REFACTOR: Refactor langchain_core (#13627) 11 months ago
embeddings.py Propagate context vars in all classes/methods 9 months ago
env.py core[patch]: update langchain-core runtime library name (#14884) 10 months ago
exceptions.py REFACTOR: Refactor langchain_core (#13627) 11 months ago
memory.py REFACTOR: Refactor langchain_core (#13627) 11 months ago
prompt_values.py manual mapping (#14422) 10 months ago
py.typed core[minor], langchain[patch], experimental[patch]: Added missing `py.typed` to `langchain_core` (#14143) 10 months ago
retrievers.py Propagate context vars in all classes/methods 9 months ago
stores.py core[patch], langchain[patch]: ByteStore (#14312) 10 months ago
sys_info.py core(minor): Add a way to print out system information for debugging purposes. (#15718) 9 months ago
tools.py core[minor]: RFC Add astream_events to Runnables (#16172) 9 months ago
vectorstores.py Propagate context vars in all classes/methods 9 months ago