2023-05-25 20:42:44 +00:00
|
|
|
{
|
|
|
|
"cells": [
|
|
|
|
{
|
|
|
|
"attachments": {},
|
|
|
|
"cell_type": "markdown",
|
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
|
|
|
"# C Transformers\n",
|
|
|
|
"\n",
|
|
|
|
"The [C Transformers](https://github.com/marella/ctransformers) library provides Python bindings for GGML models.\n",
|
|
|
|
"\n",
|
|
|
|
"This example goes over how to use LangChain to interact with `C Transformers` [models](https://github.com/marella/ctransformers#supported-models)."
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"attachments": {},
|
|
|
|
"cell_type": "markdown",
|
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
|
|
|
"**Install**"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": null,
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"%pip install ctransformers"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"attachments": {},
|
|
|
|
"cell_type": "markdown",
|
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
|
|
|
"**Load Model**"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": null,
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"from langchain.llms import CTransformers\n",
|
|
|
|
"\n",
|
2023-06-16 18:52:56 +00:00
|
|
|
"llm = CTransformers(model=\"marella/gpt-2-ggml\")"
|
2023-05-25 20:42:44 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"attachments": {},
|
|
|
|
"cell_type": "markdown",
|
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
|
|
|
"**Generate Text**"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": null,
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
2023-06-16 18:52:56 +00:00
|
|
|
"print(llm(\"AI is going to\"))"
|
2023-05-25 20:42:44 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"attachments": {},
|
|
|
|
"cell_type": "markdown",
|
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
|
|
|
"**Streaming**"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": null,
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler\n",
|
|
|
|
"\n",
|
2023-06-16 18:52:56 +00:00
|
|
|
"llm = CTransformers(\n",
|
|
|
|
" model=\"marella/gpt-2-ggml\", callbacks=[StreamingStdOutCallbackHandler()]\n",
|
|
|
|
")\n",
|
2023-05-25 20:42:44 +00:00
|
|
|
"\n",
|
2023-06-16 18:52:56 +00:00
|
|
|
"response = llm(\"AI is going to\")"
|
2023-05-25 20:42:44 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"attachments": {},
|
|
|
|
"cell_type": "markdown",
|
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
|
|
|
"**LLMChain**"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": null,
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"from langchain import PromptTemplate, LLMChain\n",
|
|
|
|
"\n",
|
|
|
|
"template = \"\"\"Question: {question}\n",
|
|
|
|
"\n",
|
|
|
|
"Answer:\"\"\"\n",
|
|
|
|
"\n",
|
2023-06-16 18:52:56 +00:00
|
|
|
"prompt = PromptTemplate(template=template, input_variables=[\"question\"])\n",
|
2023-05-25 20:42:44 +00:00
|
|
|
"\n",
|
|
|
|
"llm_chain = LLMChain(prompt=prompt, llm=llm)\n",
|
|
|
|
"\n",
|
2023-06-16 18:52:56 +00:00
|
|
|
"response = llm_chain.run(\"What is AI?\")"
|
2023-05-25 20:42:44 +00:00
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"metadata": {
|
|
|
|
"language_info": {
|
|
|
|
"name": "python"
|
|
|
|
},
|
|
|
|
"orig_nbformat": 4
|
|
|
|
},
|
|
|
|
"nbformat": 4,
|
|
|
|
"nbformat_minor": 2
|
|
|
|
}
|