We use essential cookies to make our site work. With your consent, we may also use non-essential cookies to improve user experience and analyze website traffic…

DeepInfra raises $107M Series B to scale the inference cloud — read the announcement

Langchain improvements: async and streaming
Published on 2023.10.25 by Iskren Chernev
Langchain improvements: async and streaming

Starting from langchain v0.0.322 you can make efficient async generation and streaming tokens with deepinfra.

Async generation

The deepinfra wrapper now supports native async calls, so you can expect more performance (no more threads per invocation) from your async pipelines.

from langchain.llms.deepinfra import DeepInfra

async def async_predict():
    llm = DeepInfra(model_id="meta-llama/Llama-2-7b-chat-hf")
    output = await llm.apredict("What is 2 + 2?")
    print(output)
copy

Response streaming

Streaming lets you receive each token of the response as it gets generated. This is indispensable in user-facing applications.

def streaming():
    llm = DeepInfra(model_id="meta-llama/Llama-2-7b-chat-hf")
    for chunk in llm.stream("[INST] Hello [/INST] "):
        print(chunk, end='', flush=True)
    print()
copy

You can also use the asynchronous streaming API, natively implemented underneath.

async def async_streaming():
    llm = DeepInfra(model_id="meta-llama/Llama-2-7b-chat-hf")
    async for chunk in llm.astream("[INST] Hello [/INST] "):
        print(chunk, end='', flush=True)
    print()
copy
Related articles
Sandboxes: give your agents a safe place to run codeSandboxes: give your agents a safe place to run codeIsolated Linux microVMs you can spin up with one API call: run bash or Python, move files in and out, pause and resume, tear down when you're done. Built for agents and pipelines that need to execute untrusted code without you having to run the infrastructure yourself.
Best Kimi K3 SaaS Tools & API PlatformsBest Kimi K3 SaaS Tools & API Platforms<p>Kimi K3, with its 2.8 trillion parameters and 1M-token context window, represents a significant leap in large language model capabilities. However, deploying, accessing, and managing a model of this scale presents real infrastructure challenges. From optimizing inference latency and managing GPU compute costs to handling multimodal vision capabilities, selecting the right deployment platform is critical [&hellip;]</p>
GLM-5.1 Pricing Guide: API Cost Comparison & AnalysisGLM-5.1 Pricing Guide: API Cost Comparison & Analysis<p>Provider choice for GLM-5.1 is a real economic decision. Across 10 benchmarked API providers, blended pricing runs from $0.74 to $1.70 per 1M tokens, output speed from 33.8 to 175.2 t/s, and the fastest provider is 5.2x quicker than the slowest. For teams deploying at scale, that spread determines whether this model fits a production [&hellip;]</p>