feat(llms): add streaming support to textgen (#9295)

- Description: Added streaming support to the textgen component in the
llms module.
  - Dependencies: websocket-client = "^1.6.1"
This commit is contained in:
Utku Ege Tuluk
2023-08-21 22:39:14 +08:00
committed by GitHub
parent a03003f5fd
commit bb4f7936f9
2 changed files with 163 additions and 16 deletions

View File

@@ -26,7 +26,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"metadata": {
"tags": []
},
@@ -61,6 +61,71 @@
"\n",
"llm_chain.run(question)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Streaming Version"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You should install websocket-client to use this feature.\n",
"`pip install websocket-client`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"model_url = \"ws://localhost:5005\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import langchain\n",
"from langchain import PromptTemplate, LLMChain\n",
"from langchain.llms import TextGen\n",
"from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler\n",
"\n",
"langchain.debug = True\n",
"\n",
"template = \"\"\"Question: {question}\n",
"\n",
"Answer: Let's think step by step.\"\"\"\n",
"\n",
"\n",
"prompt = PromptTemplate(template=template, input_variables=[\"question\"])\n",
"llm = TextGen(model_url=model_url, streaming=True, callbacks=[StreamingStdOutCallbackHandler()])\n",
"llm_chain = LLMChain(prompt=prompt, llm=llm)\n",
"question = \"What NFL team won the Super Bowl in the year Justin Bieber was born?\"\n",
"\n",
"llm_chain.run(question)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"llm = TextGen(\n",
" model_url = model_url,\n",
" streaming=True\n",
")\n",
"for chunk in llm.stream(\"Ask 'Hi, how are you?' like a pirate:'\",\n",
" stop=[\"'\",\"\\n\"]):\n",
" print(chunk, end='', flush=True)"
]
}
],
"metadata": {
@@ -79,7 +144,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.7"
"version": "3.10.4"
}
},
"nbformat": 4,