"LangChain provides streaming support for LLMs. Currently, we only support streaming for the `OpenAI` and `ChatOpenAI` LLM implementation, but streaming support for other LLM implementations is on the roadmap. To utilize streaming, use a [`CallbackHandler`](https://github.com/hwchase17/langchain/blob/master/langchain/callbacks/base.py) that implements `on_llm_new_token`. In this example, we are using [`StreamingStdOutCallbackHandler`]()."
"LangChain provides streaming support for LLMs. Currently, we support streaming for the `OpenAI`, `ChatOpenAI`. and `Anthropic` implementations, but streaming support for other LLM implementations is on the roadmap. To utilize streaming, use a [`CallbackHandler`](https://github.com/hwchase17/langchain/blob/master/langchain/callbacks/base.py) that implements `on_llm_new_token`. In this example, we are using [`StreamingStdOutCallbackHandler`]()."
"resp = llm(\"Write me a song about sparkling water.\")"
"resp = llm(\"Write me a song about sparkling water.\")"
]
]
@ -86,7 +95,7 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 6,
"execution_count": 4,
"id": "a35373f1-9ee6-4753-a343-5aee749b8527",
"id": "a35373f1-9ee6-4753-a343-5aee749b8527",
"metadata": {
"metadata": {
"tags": []
"tags": []
@ -105,10 +114,10 @@
{
{
"data": {
"data": {
"text/plain": [
"text/plain": [
"LLMResult(generations=[[Generation(text='\\n\\nQ: What did the fish say when it hit the wall?\\nA: Dam!', generation_info={'finish_reason': None, 'logprobs': None})]], llm_output={'token_usage': {}})"
"LLMResult(generations=[[Generation(text='\\n\\nQ: What did the fish say when it hit the wall?\\nA: Dam!', generation_info={'finish_reason': None, 'logprobs': None})]], llm_output={'token_usage': {}, 'model_name': 'text-davinci-003'})"
]
]
},
},
"execution_count": 6,
"execution_count": 4,
"metadata": {},
"metadata": {},
"output_type": "execute_result"
"output_type": "execute_result"
}
}
@ -122,12 +131,12 @@
"id": "a93a4d61-0476-49db-8321-7de92bd74059",
"id": "a93a4d61-0476-49db-8321-7de92bd74059",
"metadata": {},
"metadata": {},
"source": [
"source": [
"Here's an example with `ChatOpenAI`:"
"Here's an example with the `ChatOpenAI` chat model implementation:"
]
]
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 3,
"execution_count": 6,
"id": "22665f16-e05b-473c-a4bd-ad75744ea024",
"id": "22665f16-e05b-473c-a4bd-ad75744ea024",
"metadata": {
"metadata": {
"tags": []
"tags": []
@ -137,49 +146,47 @@
"name": "stdout",
"name": "stdout",
"output_type": "stream",
"output_type": "stream",
"text": [
"text": [
"\n",
"\n",
"Verse 1:\n",
"Verse 1:\n",
"Bubbles rising to the top\n",
"Bubbles rising to the top\n",
"A refreshing drink that never stops\n",
"A refreshing drink that never stops\n",
"Clear and crisp, it's pure delight\n",
"Clear and crisp, it's oh so pure\n",
"A taste that's sure to excite\n",
"Sparkling water, I can't ignore\n",
"\n",
"\n",
"Chorus:\n",
"Chorus:\n",
"Sparkling water, oh so fine\n",
"Sparkling water, oh how you shine\n",
"A drink that's always on my mind\n",
"A taste so clean, it's simply divine\n",
"With every sip, I feel alive\n",
"You quench my thirst, you make me feel alive\n",
"Sparkling water, you're my vibe\n",
"Sparkling water, you're my favorite vibe\n",
"\n",
"\n",
"Verse 2:\n",
"Verse 2:\n",
"No sugar, no calories, just pure bliss\n",
"No sugar, no calories, just H2O\n",
"A drink that's hard to resist\n",
"A drink that's good for me, don't you know\n",
"It's the perfect way to quench my thirst\n",
"With lemon or lime, you're even better\n",
"A drink that always comes first\n",
"Sparkling water, you're my forever\n",
"\n",
"\n",
"Chorus:\n",
"Chorus:\n",
"Sparkling water, oh so fine\n",
"Sparkling water, oh how you shine\n",
"A drink that's always on my mind\n",
"A taste so clean, it's simply divine\n",
"With every sip, I feel alive\n",
"You quench my thirst, you make me feel alive\n",
"Sparkling water, you're my vibe\n",
"Sparkling water, you're my favorite vibe\n",
"\n",
"\n",
"Bridge:\n",
"Bridge:\n",
"From the mountains to the sea\n",
"You're my go-to drink, day or night\n",
"Sparkling water, you're the key\n",
"You make me feel so light\n",
"To a healthy life, a happy soul\n",
"I'll never give you up, you're my true love\n",
"A drink that makes me feel whole\n",
"Sparkling water, you're sent from above\n",
"\n",
"\n",
"Chorus:\n",
"Chorus:\n",
"Sparkling water, oh so fine\n",
"Sparkling water, oh how you shine\n",
"A drink that's always on my mind\n",
"A taste so clean, it's simply divine\n",
"With every sip, I feel alive\n",
"You quench my thirst, you make me feel alive\n",
"Sparkling water, you're my vibe\n",
"Sparkling water, you're my favorite vibe\n",
"\n",
"\n",
"Outro:\n",
"Outro:\n",
"Sparkling water, you're the one\n",
"Sparkling water, you're the one for me\n",
"A drink that's always so much fun\n",
"I'll never let you go, can't you see\n",
"I'll never let you go, my friend\n",
"You're my drink of choice, forevermore\n",
"Sparkling"
"Sparkling water, I adore."
]
]
}
}
],
],
@ -188,13 +195,59 @@
"resp = chat([HumanMessage(content=\"Write me a song about sparkling water.\")])"
"resp = chat([HumanMessage(content=\"Write me a song about sparkling water.\")])"
]
]
},
},
{
"cell_type": "markdown",
"id": "909ae48b-0f07-4990-bbff-e627f706c93e",
"metadata": {},
"source": [
"Here is an example with the `Anthropic` LLM implementation, which uses their `claude` model."
]
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"id": "eadae4ba-9f21-4ec8-845d-dd43b0edc2dc",
"id": "eadae4ba-9f21-4ec8-845d-dd43b0edc2dc",
"metadata": {},
"metadata": {
"outputs": [],
"tags": []
"source": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Sparkling water, bubbles so bright,\n",
"\n",
"Fizzing and popping in the light.\n",
"\n",
"No sugar or calories, a healthy delight,\n",
"\n",
"Sparkling water, refreshing and light.\n",
"\n",
"Carbonation that tickles the tongue,\n",
"\n",
"In flavors of lemon and lime unsung.\n",
"\n",
"Sparkling water, a drink quite all right,\n",
"\n",
"Bubbles sparkling in the light."
]
},
{
"data": {
"text/plain": [
"'\\nSparkling water, bubbles so bright,\\n\\nFizzing and popping in the light.\\n\\nNo sugar or calories, a healthy delight,\\n\\nSparkling water, refreshing and light.\\n\\nCarbonation that tickles the tongue,\\n\\nIn flavors of lemon and lime unsung.\\n\\nSparkling water, a drink quite all right,\\n\\nBubbles sparkling in the light.'"