LangChain 03 - astream_events Streaming Output with FAISS Practice
This article mainly introduces two key technologies:
FAISS Introduction: Facebook AI Similarity Search is an efficient similarity search library developed by Meta, suitable for large-scale vector data processing, supports GPU acceleration, multiple index structures (L2 distance, inner product distance).
langchain astream_events: Used for real-time monitoring and processing intermediate events during language model generation, supports async streaming processing, provides event types like on_llm_start, on_llm_new_token, on_llm_end.
Install Dependencies
pip install --upgrade --quiet langchain-core langchain-community langchain-openai
pip install faiss # or for mac use faiss-cpu
Core Code Example
Use DocArrayInMemorySearch to create vector store, combine with ChatOpenAI and prompt template, implement streaming output through astream_events.
Running Result Example
|H|arrison| worked| at| Kens|ho|.||
Streaming output effect shows the progressive generation process of each token.