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/tests/unit_tests
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"]})
```
7 months ago
..
_api core[patch]: add alternative_import to deprecated (#15781) 8 months ago
callbacks core[patch], langchain[patch], community[patch]: Revert #15326 (#15546) 8 months ago
data Separate out langchain_core package (#13577) 9 months ago
documents REFACTOR: combine core documents files (#13733) 9 months ago
example_selectors BUG: more core fixes (#13665) 9 months ago
examples core:adds tests for partial_variables (#15427) 8 months ago
fake core[patch]: testing add chat model for unit-tests (#16209) 7 months ago
language_models [core: minor] fix getters (#15181) 8 months ago
load BUG: more core fixes (#13665) 9 months ago
messages Fixes for opengpts release (#13960) 9 months ago
output_parsers core: update json output parser (#15079) 8 months ago
outputs BUG: more core fixes (#13665) 9 months ago
prompts core:adds tests for partial_variables (#15427) 8 months ago
runnables core[minor]: RFC Add astream_events to Runnables (#16172) 7 months ago
tracers core[patch], langchain[patch], community[patch]: Revert #15326 (#15546) 8 months ago
utils core[patch]: Further restrict recursive URL loader (#15559) 8 months ago
__init__.py Separate out langchain_core package (#13577) 9 months ago
conftest.py core[patch], langchain[patch]: fix required deps (#14373) 9 months ago
prompt_file.txt Separate out langchain_core package (#13577) 9 months ago
test_globals.py Separate out langchain_core package (#13577) 9 months ago
test_imports.py infra: Fix test filesystem paths incompatible with windows (#14388) 8 months ago
test_messages.py \Fix tool_calls message merge (#14613) 9 months ago
test_outputs.py REFACTOR: Refactor langchain_core (#13627) 9 months ago
test_sys_info.py core(minor): Add a way to print out system information for debugging purposes. (#15718) 8 months ago
test_tools.py REFACTOR: Refactor langchain_core (#13627) 9 months ago