|
17 | 17 | {
|
18 | 18 | "cell_type": "markdown",
|
19 | 19 | "id": "ad0b5edf",
|
20 |
| - "metadata": {}, |
| 20 | + "metadata": { |
| 21 | + "tags": [] |
| 22 | + }, |
21 | 23 | "source": [
|
22 | 24 | "## Feast\n",
|
23 | 25 | "\n",
|
|
211 | 213 | "metadata": {},
|
212 | 214 | "outputs": [],
|
213 | 215 | "source": []
|
| 216 | + }, |
| 217 | + { |
| 218 | + "cell_type": "markdown", |
| 219 | + "id": "c4049990-651d-44d3-82b1-0cd122da55c1", |
| 220 | + "metadata": {}, |
| 221 | + "source": [ |
| 222 | + "## Tecton\n", |
| 223 | + "\n", |
| 224 | + "Above, we showed how you could use Feast, a popular open source and self-managed feature store, with LangChain. Our examples below will show a similar integration using Tecton. Tecton is a fully managed feature platform built to orchestrate the complete ML feature lifecycle, from transformation to online serving, with enterprise-grade SLAs." |
| 225 | + ] |
| 226 | + }, |
| 227 | + { |
| 228 | + "cell_type": "markdown", |
| 229 | + "id": "7bb4dba1-0678-4ea4-be0a-d353c0b13fc2", |
| 230 | + "metadata": { |
| 231 | + "tags": [] |
| 232 | + }, |
| 233 | + "source": [ |
| 234 | + "### Prerequisites\n", |
| 235 | + "\n", |
| 236 | + "* Tecton Deployment (sign up at [https://tecton.ai](https://tecton.ai))\n", |
| 237 | + "* `TECTON_API_KEY` environment variable set to a valid Service Account key" |
| 238 | + ] |
| 239 | + }, |
| 240 | + { |
| 241 | + "cell_type": "markdown", |
| 242 | + "id": "ac9eb618-8c52-4cd6-bb8e-9c99a150dfa6", |
| 243 | + "metadata": { |
| 244 | + "tags": [] |
| 245 | + }, |
| 246 | + "source": [ |
| 247 | + "### Define and Load Features\n", |
| 248 | + "\n", |
| 249 | + "We will use the user_transaction_counts Feature View from the [Tecton tutorial](https://docs.tecton.ai/docs/tutorials/tecton-fundamentals) as part of a Feature Service. For simplicity, we are only using a single Feature View; however, more sophisticated applications may require more feature views to retrieve the features needed for its prompt.\n", |
| 250 | + "\n", |
| 251 | + "```python\n", |
| 252 | + "user_transaction_metrics = FeatureService(\n", |
| 253 | + " name = \"user_transaction_metrics\",\n", |
| 254 | + " features = [user_transaction_counts]\n", |
| 255 | + ")\n", |
| 256 | + "```\n", |
| 257 | + "\n", |
| 258 | + "The above Feature Service is expected to be [applied to a live workspace](https://docs.tecton.ai/docs/applying-feature-repository-changes-to-a-workspace). For this example, we will be using the \"prod\" workspace." |
| 259 | + ] |
| 260 | + }, |
| 261 | + { |
| 262 | + "cell_type": "code", |
| 263 | + "execution_count": 60, |
| 264 | + "id": "32e9675d-a7e5-429f-906f-2260294d3e46", |
| 265 | + "metadata": { |
| 266 | + "tags": [] |
| 267 | + }, |
| 268 | + "outputs": [], |
| 269 | + "source": [ |
| 270 | + "import tecton\n", |
| 271 | + "\n", |
| 272 | + "workspace = tecton.get_workspace(\"prod\")\n", |
| 273 | + "feature_service = workspace.get_feature_service(\"user_transaction_metrics\")" |
| 274 | + ] |
| 275 | + }, |
| 276 | + { |
| 277 | + "cell_type": "markdown", |
| 278 | + "id": "29b7550c-0eb4-4bd1-a501-1c63fb77aa56", |
| 279 | + "metadata": {}, |
| 280 | + "source": [ |
| 281 | + "### Prompts\n", |
| 282 | + "\n", |
| 283 | + "Here we will set up a custom TectonPromptTemplate. This prompt template will take in a user_id , look up their stats, and format those stats into a prompt.\n", |
| 284 | + "\n", |
| 285 | + "Note that the input to this prompt template is just `user_id`, since that is the only user defined piece (all other variables are looked up inside the prompt template)." |
| 286 | + ] |
| 287 | + }, |
| 288 | + { |
| 289 | + "cell_type": "code", |
| 290 | + "execution_count": 61, |
| 291 | + "id": "6fb77ea4-64c6-4e48-a783-bd1ece021b82", |
| 292 | + "metadata": { |
| 293 | + "tags": [] |
| 294 | + }, |
| 295 | + "outputs": [], |
| 296 | + "source": [ |
| 297 | + "from langchain.prompts import PromptTemplate, StringPromptTemplate" |
| 298 | + ] |
| 299 | + }, |
| 300 | + { |
| 301 | + "cell_type": "code", |
| 302 | + "execution_count": 77, |
| 303 | + "id": "02a98fbc-8135-4b11-bf60-85d28e426667", |
| 304 | + "metadata": { |
| 305 | + "tags": [] |
| 306 | + }, |
| 307 | + "outputs": [], |
| 308 | + "source": [ |
| 309 | + "template = \"\"\"Given the vendor's up to date transaction stats, write them a note based on the following rules:\n", |
| 310 | + "\n", |
| 311 | + "1. If they had a transaction in the last day, write a short congratulations message on their recent sales\n", |
| 312 | + "2. If no transaction in the last day, but they had a transaction in the last 30 days, playfully encourage them to sell more.\n", |
| 313 | + "3. Always add a silly joke about chickens at the end\n", |
| 314 | + "\n", |
| 315 | + "Here are the vendor's stats:\n", |
| 316 | + "Number of Transactions Last Day: {transaction_count_1d}\n", |
| 317 | + "Number of Transactions Last 30 Days: {transaction_count_30d}\n", |
| 318 | + "\n", |
| 319 | + "Your response:\"\"\"\n", |
| 320 | + "prompt = PromptTemplate.from_template(template)" |
| 321 | + ] |
| 322 | + }, |
| 323 | + { |
| 324 | + "cell_type": "code", |
| 325 | + "execution_count": 78, |
| 326 | + "id": "a35cdfd5-6ccc-4394-acfe-60d53804be51", |
| 327 | + "metadata": { |
| 328 | + "tags": [] |
| 329 | + }, |
| 330 | + "outputs": [], |
| 331 | + "source": [ |
| 332 | + "class TectonPromptTemplate(StringPromptTemplate):\n", |
| 333 | + " \n", |
| 334 | + " def format(self, **kwargs) -> str:\n", |
| 335 | + " user_id = kwargs.pop(\"user_id\")\n", |
| 336 | + " feature_vector = feature_service.get_online_features(join_keys={\"user_id\": user_id}).to_dict()\n", |
| 337 | + " kwargs[\"transaction_count_1d\"] = feature_vector[\"user_transaction_counts.transaction_count_1d_1d\"]\n", |
| 338 | + " kwargs[\"transaction_count_30d\"] = feature_vector[\"user_transaction_counts.transaction_count_30d_1d\"]\n", |
| 339 | + " return prompt.format(**kwargs)" |
| 340 | + ] |
| 341 | + }, |
| 342 | + { |
| 343 | + "cell_type": "code", |
| 344 | + "execution_count": 79, |
| 345 | + "id": "d5915df0-fb16-4770-8a82-22f885b74d1a", |
| 346 | + "metadata": { |
| 347 | + "tags": [] |
| 348 | + }, |
| 349 | + "outputs": [], |
| 350 | + "source": [ |
| 351 | + "prompt_template = TectonPromptTemplate(input_variables=[\"user_id\"])" |
| 352 | + ] |
| 353 | + }, |
| 354 | + { |
| 355 | + "cell_type": "code", |
| 356 | + "execution_count": 80, |
| 357 | + "id": "a36abfc8-ea60-4ae0-a36d-d7b639c7307c", |
| 358 | + "metadata": { |
| 359 | + "tags": [] |
| 360 | + }, |
| 361 | + "outputs": [ |
| 362 | + { |
| 363 | + "name": "stdout", |
| 364 | + "output_type": "stream", |
| 365 | + "text": [ |
| 366 | + "Given the vendor's up to date transaction stats, write them a note based on the following rules:\n", |
| 367 | + "\n", |
| 368 | + "1. If they had a transaction in the last day, write a short congratulations message on their recent sales\n", |
| 369 | + "2. If no transaction in the last day, but they had a transaction in the last 30 days, playfully encourage them to sell more.\n", |
| 370 | + "3. Always add a silly joke about chickens at the end\n", |
| 371 | + "\n", |
| 372 | + "Here are the vendor's stats:\n", |
| 373 | + "Number of Transactions Last Day: 657\n", |
| 374 | + "Number of Transactions Last 30 Days: 20326\n", |
| 375 | + "\n", |
| 376 | + "Your response:\n" |
| 377 | + ] |
| 378 | + } |
| 379 | + ], |
| 380 | + "source": [ |
| 381 | + "print(prompt_template.format(user_id=\"user_469998441571\"))" |
| 382 | + ] |
| 383 | + }, |
| 384 | + { |
| 385 | + "cell_type": "markdown", |
| 386 | + "id": "f8d4b905-1051-4303-9c33-8eddb65c1274", |
| 387 | + "metadata": { |
| 388 | + "tags": [] |
| 389 | + }, |
| 390 | + "source": [ |
| 391 | + "### Use in a chain\n", |
| 392 | + "\n", |
| 393 | + "We can now use this in a chain, successfully creating a chain that achieves personalization backed by the Tecton Feature Platform" |
| 394 | + ] |
| 395 | + }, |
| 396 | + { |
| 397 | + "cell_type": "code", |
| 398 | + "execution_count": 81, |
| 399 | + "id": "ffb60cd0-8e3c-4c9d-b639-43d766e12c4c", |
| 400 | + "metadata": { |
| 401 | + "tags": [] |
| 402 | + }, |
| 403 | + "outputs": [], |
| 404 | + "source": [ |
| 405 | + "from langchain.chat_models import ChatOpenAI\n", |
| 406 | + "from langchain.chains import LLMChain" |
| 407 | + ] |
| 408 | + }, |
| 409 | + { |
| 410 | + "cell_type": "code", |
| 411 | + "execution_count": 82, |
| 412 | + "id": "3918abc7-00b5-466f-bdfc-ab046cd282da", |
| 413 | + "metadata": { |
| 414 | + "tags": [] |
| 415 | + }, |
| 416 | + "outputs": [], |
| 417 | + "source": [ |
| 418 | + "chain = LLMChain(llm=ChatOpenAI(), prompt=prompt_template)" |
| 419 | + ] |
| 420 | + }, |
| 421 | + { |
| 422 | + "cell_type": "code", |
| 423 | + "execution_count": 83, |
| 424 | + "id": "e7d91c4b-3e99-40cc-b3e9-a004c8c9193e", |
| 425 | + "metadata": { |
| 426 | + "tags": [] |
| 427 | + }, |
| 428 | + "outputs": [ |
| 429 | + { |
| 430 | + "data": { |
| 431 | + "text/plain": [ |
| 432 | + "'Wow, congratulations on your recent sales! Your business is really soaring like a chicken on a hot air balloon! Keep up the great work!'" |
| 433 | + ] |
| 434 | + }, |
| 435 | + "execution_count": 83, |
| 436 | + "metadata": {}, |
| 437 | + "output_type": "execute_result" |
| 438 | + } |
| 439 | + ], |
| 440 | + "source": [ |
| 441 | + "chain.run(\"user_469998441571\")" |
| 442 | + ] |
| 443 | + }, |
| 444 | + { |
| 445 | + "cell_type": "code", |
| 446 | + "execution_count": null, |
| 447 | + "id": "f752b924-caf9-4f7a-b78b-cb8c8ada8c2e", |
| 448 | + "metadata": {}, |
| 449 | + "outputs": [], |
| 450 | + "source": [] |
214 | 451 | }
|
215 | 452 | ],
|
216 | 453 | "metadata": {
|
|
229 | 466 | "name": "python",
|
230 | 467 | "nbconvert_exporter": "python",
|
231 | 468 | "pygments_lexer": "ipython3",
|
232 |
| - "version": "3.9.1" |
| 469 | + "version": "3.9.13" |
233 | 470 | }
|
234 | 471 | },
|
235 | 472 | "nbformat": 4,
|
|
0 commit comments