avatarLaxfed Paulacy

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2095

Abstract

’s streaming support, the first step is to implement streaming support for the <code>OpenAI</code> implementation of <code>LLM</code>. Below is an example of how to use the <code>on_llm_new_token</code> callback when the <code>streaming</code> parameter of <code>OpenAI</code> is set to <code>True</code>:</p><div id="dc9f"><pre><span class="hljs-keyword">from</span> langchain <span class="hljs-keyword">import</span> OpenAI

<span class="hljs-comment"># Initialize OpenAI with streaming support</span> llm = OpenAI(streaming=<span class="hljs-literal">True</span>)

<span class="hljs-comment"># Implement the on_llm_new_token callback</span> <span class="hljs-keyword">def</span> <span class="hljs-title function_">on_llm_new_token</span>(<span class="hljs-params">token</span>): <span class="hljs-comment"># Handle the new token</span> <span class="hljs-built_in">print</span>(token)

<span class="hljs-comment"># Set the implemented callback</span> llm.set_callback(<span class="hljs-string">"on_llm_new_token"</span>, on_llm_new_token)</pre></div><h2 id="8506">Web Application Template</h2><p id="baf4">LangChain has also provided a sample web application template, <a href="https://github.com/hwchase17/chat-langchain?ref=blog.langchain.dev">chat-langchain</a>, to demonstrate best practices for integrating streaming and other LangChain features into a ready-to-deploy application that can support multiple users. The example below showcases the integration of LangChain streaming and the implementation of callback handlers for sending messages back to the client:</p><h2 id="b192">Streaming</h2><div id="c66c"><pre><span class="hljs-comment"># Initialize streaming support for the OpenAI implementation of LLM</span> llm = <span class="hljs-title class_">Open</span>AI(streaming=<span class="hljs-title class_">True</span>)

<span class="hljs-comment"># Implement the StreamingLLMCallbackHandler to send each token back to the client via websocket</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">StreamingLLMCallbackHandler</span>: <span class=

Options

"hljs-keyword">def</span> <span class="hljs-title function_">on_new_token</span>(<span class="hljs-params"><span class="hljs-variable language_">self</span>, token</span>): <span class="hljs-comment"># Send the token back to the client via websocket</span> websocket.send(token)</pre></div><h2 id="4b37">Async Execution</h2><p id="4945">The application also leverages asyncio support for select chains and LLMs to support concurrent execution without having to spawn multiple threads. This is essential when there are multiple client connections to the application:</p><div id="2c10"><pre><span class="hljs-comment"># Leveraging asyncio support</span> <span class="hljs-keyword">async</span> <span class="hljs-keyword">def</span> <span class="hljs-title function_">process_request</span>(<span class="hljs-params">request</span>): <span class="hljs-comment"># Processing the request asynchronously</span> ...</pre></div><h2 id="035b">Conclusion</h2><p id="138d">LangChain’s support for streaming provides developers with the tools to create more responsive and interactive LLM applications. By implementing streaming, developers can significantly enhance the user experience and reduce perceived latency in their applications. With continuous updates and improvements, LangChain is committed to providing developers with the necessary features to build best-in-class applications.</p><div id="002e" class="link-block"> <a href="https://readmedium.com/langchain-what-is-an-agent-toolkit-8ca3681ae2e2"> <div> <div> <h2>LANGCHAIN — What Is an Agent Toolkit?</h2> <div><h3>Software is like entropy: It is difficult to grasp, weighs nothing, and obeys the Second Law of Thermodynamics; i.e…</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*nu7ZXSdSXeo6aCLEJYoZpg.jpeg)"></div> </div> </div> </a> </div></article></body>

LANGCHAIN — LangChains Support for Streaming

Software is a great combination between artistry and engineering. — Bill Gates

LangChain has recently introduced streaming support, a feature that is essential in improving the user experience for LLM applications. With this update, developers can now leverage streaming to reduce perceived latency, making it possible to display progress to the user as the LLM generates tokens. In this article, we’ll explore LangChain’s streaming support and provide code snippets to help you get started.

Streaming Support Introduction

The LangChain team has made significant improvements to the LangChain platform, including the introduction of streaming support. With this update, developers can now implement streaming in their LLM applications, allowing for a more seamless and interactive user experience.

Motivation

Streaming helps reduce perceived latency by returning the output of the LLM token by token, instead of all at once. This means that as a token is generated by the LLM, it can be served immediately to the user, providing a more responsive user interface.

Usage

To start using LangChain’s streaming support, the first step is to implement streaming support for the OpenAI implementation of LLM. Below is an example of how to use the on_llm_new_token callback when the streaming parameter of OpenAI is set to True:

from langchain import OpenAI

# Initialize OpenAI with streaming support
llm = OpenAI(streaming=True)

# Implement the on_llm_new_token callback
def on_llm_new_token(token):
    # Handle the new token
    print(token)

# Set the implemented callback
llm.set_callback("on_llm_new_token", on_llm_new_token)

Web Application Template

LangChain has also provided a sample web application template, chat-langchain, to demonstrate best practices for integrating streaming and other LangChain features into a ready-to-deploy application that can support multiple users. The example below showcases the integration of LangChain streaming and the implementation of callback handlers for sending messages back to the client:

Streaming

# Initialize streaming support for the OpenAI implementation of LLM
llm = OpenAI(streaming=True)

# Implement the StreamingLLMCallbackHandler to send each token back to the client via websocket
class StreamingLLMCallbackHandler:
    def on_new_token(self, token):
        # Send the token back to the client via websocket
        websocket.send(token)

Async Execution

The application also leverages asyncio support for select chains and LLMs to support concurrent execution without having to spawn multiple threads. This is essential when there are multiple client connections to the application:

# Leveraging asyncio support
async def process_request(request):
    # Processing the request asynchronously
    ...

Conclusion

LangChain’s support for streaming provides developers with the tools to create more responsive and interactive LLM applications. By implementing streaming, developers can significantly enhance the user experience and reduce perceived latency in their applications. With continuous updates and improvements, LangChain is committed to providing developers with the necessary features to build best-in-class applications.

Support
ChatGPT
Langchains
Recommended from ReadMedium