Files
langchain/docs/modules/llms/integrations/hf_text_generation.ipynb
Harrison Chase daff73b3f5 cr
2023-03-08 22:49:27 -08:00

144 lines
3.2 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "16ae872d",
"metadata": {},
"source": [
"# Hugging Face Text Generation\n",
"\n",
"This notebook shows how to use the Hugging Face Text Generation endpoint."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "aed972a5",
"metadata": {},
"outputs": [],
"source": [
"from langchain.llms import HFTextGeneration"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "09e693b8",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"os.environ[\"HUGGINGFACEHUB_API_TOKEN\"] = \"...\""
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "6ce9486b",
"metadata": {},
"outputs": [],
"source": [
"model = HFTextGeneration(repo_id=\"google/flan-ul2\", model_kwargs={\"temperature\":0.1, \"max_new_tokens\":200})"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "2af51b5e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"i'm a music man i'm a music man i'm a music man i'm a music man i'm a music man i'm a music man i'm a music man i'm a music man i'm a music man i'm a music man i'm a music man i'm a music man i'm a music man i'm a music man i'm a music man i'm a music man i'm a music man i'm a music man i'm a music man i'm a music man i'm a music man i'm a music man i'm a music man i'm a music man i'm a music man\""
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"model(\"write me a song about music\")"
]
},
{
"cell_type": "markdown",
"id": "d47e1e10",
"metadata": {},
"source": [
"## Streaming\n",
"This section shows how to using streaming with the HF text generation endpoint."
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "3ffe8d7e",
"metadata": {},
"outputs": [],
"source": [
"from langchain.callbacks.base import CallbackManager\n",
"from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "5a84bdf3",
"metadata": {},
"outputs": [],
"source": [
"model = HFTextGeneration(repo_id=\"bigscience/bloomz\", streaming=True, callback_manager=CallbackManager([StreamingStdOutCallbackHandler()]), verbose=True)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "6d460fe9",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"? Rayleigh scattering"
]
}
],
"source": [
"resp = model(\"why is the sky blue\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6ed74872",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.1"
}
},
"nbformat": 4,
"nbformat_minor": 5
}