From d84df2546690889986c9e5ad5520d2fbf8503ac4 Mon Sep 17 00:00:00 2001 From: Simba Khadder Date: Mon, 8 May 2023 10:32:17 -0700 Subject: [PATCH] Add example on how to use Featureform with langchain (#4337) Added an example on how to use Featureform to connecting_to_a_feature_store.ipynb . --- .../connecting_to_a_feature_store.ipynb | 146 ++++++++++++++++++ 1 file changed, 146 insertions(+) diff --git a/docs/modules/prompts/prompt_templates/examples/connecting_to_a_feature_store.ipynb b/docs/modules/prompts/prompt_templates/examples/connecting_to_a_feature_store.ipynb index ae58fbfd..955e7cf8 100644 --- a/docs/modules/prompts/prompt_templates/examples/connecting_to_a_feature_store.ipynb +++ b/docs/modules/prompts/prompt_templates/examples/connecting_to_a_feature_store.ipynb @@ -448,6 +448,152 @@ "metadata": {}, "outputs": [], "source": [] + }, + { + "cell_type": "markdown", + "id": "a0691cd9", + "metadata": {}, + "source": [ + "## Featureform\n", + "\n", + "Finally, we will use [Featureform](https://github.com/featureform/featureform) an open-source and enterprise-grade feature store to run the same example. Featureform allows you to work with your infrastructure like Spark or locally to define your feature transformations." + ] + }, + { + "cell_type": "markdown", + "id": "44320d68", + "metadata": {}, + "source": [ + "### Initialize Featureform\n", + "\n", + "You can follow in the instructions in the README to initialize your transformations and features in Featureform." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e64ada9d", + "metadata": {}, + "outputs": [], + "source": [ + "import featureform as ff\n", + "\n", + "client = ff.Client(host=\"demo.featureform.com\")" + ] + }, + { + "cell_type": "markdown", + "id": "b28914a2", + "metadata": {}, + "source": [ + "### Prompts\n", + "\n", + "Here we will set up a custom FeatureformPromptTemplate. This prompt template will take in the average amount a user pays per transactions.\n", + "\n", + "Note that the input to this prompt template is just avg_transaction, since that is the only user defined piece (all other variables are looked up inside the prompt template)." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "75d4a34a", + "metadata": {}, + "outputs": [], + "source": [ + "from langchain.prompts import PromptTemplate, StringPromptTemplate" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "88253bcb", + "metadata": {}, + "outputs": [], + "source": [ + "template = \"\"\"Given the amount a user spends on average per transaction, let them know if they are a high roller. Otherwise, make a silly joke about chickens at the end to make them feel better\n", + "\n", + "Here are the user's stats:\n", + "Average Amount per Transaction: ${avg_transcation}\n", + "\n", + "Your response:\"\"\"\n", + "prompt = PromptTemplate.from_template(template)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "61f72476", + "metadata": {}, + "outputs": [], + "source": [ + "class FeatureformPromptTemplate(StringPromptTemplate):\n", + " \n", + " def format(self, **kwargs) -> str:\n", + " user_id = kwargs.pop(\"user_id\")\n", + " fpf = client.features([(\"avg_transactions\", \"quickstart\")], {\"user\": user_id})\n", + " return prompt.format(**kwargs)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "994a644c", + "metadata": {}, + "outputs": [], + "source": [ + "prompt_template = FeatureformPrompTemplate(input_variables=[\"user_id\"])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "79b2b0cb", + "metadata": {}, + "outputs": [], + "source": [ + "print(prompt_template.format(user_id=\"C1410926\"))" + ] + }, + { + "cell_type": "markdown", + "id": "f09ddfdd", + "metadata": {}, + "source": [ + "### Use in a chain\n", + "\n", + "We can now use this in a chain, successfully creating a chain that achieves personalization backed by the Featureform Feature Platform" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5e89216f", + "metadata": {}, + "outputs": [], + "source": [ + "from langchain.chat_models import ChatOpenAI\n", + "from langchain.chains import LLMChain" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9d3d558c", + "metadata": {}, + "outputs": [], + "source": [ + "chain = LLMChain(llm=ChatOpenAI(), prompt=prompt_template)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b5412626", + "metadata": {}, + "outputs": [], + "source": [ + "chain.run(\"C1410926\")" + ] } ], "metadata": {