【LangChain系列 5】Prompt模版——特征库

news2024/10/7 7:23:39

原文地址:【LangChain系列 5】Prompt模版——特征库

本文速读:

  • Feast

  • Featureform

特征库(feature stores)是传统机器学习中的一个概念,本质上是数据,不同维度/属性的数据,而且可以保持同步更新,所以可以将最新的、相关的数据输入给模型,用它来训练模型。

在一些LLM应用开发中,我们也需要将最新的数据输入给LLM,特征库 非常适合这个场景,所以LangChain提供了链接特征库方式。常用的特征库有:

  • Feast

  • Tecton

  • Featureform

  • AxureML Managed Feature Stores

LangChain对于以上四种特征库都支持,下面我将介绍如何在LangChain中使用Feast和Featureform这两种特征库的。

01 Feast


Feast是比较常用的开源特征库,下面我介绍如何在LangChain中使用Feast特征库。

1. 加载特征库

from feast import FeatureStore

# You may need to update the path depending on where you stored it
feast_repo_path = "/path/my_feature_repo/feature_repo/"
store = FeatureStore(repo_path=feast_repo_path)

2. 创建模版

from langchain.prompts import PromptTemplate, StringPromptTemplate

template = """Given the driver's up to date stats, write them note relaying those stats to them.
If they have a conversation rate above .5, give them a compliment. Otherwise, make a silly joke about chickens at the end to make them feel better

Here are the drivers stats:
Conversation rate: {conv_rate}
Acceptance rate: {acc_rate}
Average Daily Trips: {avg_daily_trips}

Your response:"""
prompt = PromptTemplate.from_template(template)

3. 创建FeastPromptTemplate

class FeastPromptTemplate(StringPromptTemplate):
    def format(self, **kwargs) -> str:
        driver_id = kwargs.pop("driver_id")
        feature_vector = store.get_online_features(
            features=[
                "driver_hourly_stats:conv_rate",
                "driver_hourly_stats:acc_rate",
                "driver_hourly_stats:avg_daily_trips",
            ],
            entity_rows=[{"driver_id": driver_id}],
        ).to_dict()
        kwargs["conv_rate"] = feature_vector["conv_rate"][0]
        kwargs["acc_rate"] = feature_vector["acc_rate"][0]
        kwargs["avg_daily_trips"] = feature_vector["avg_daily_trips"][0]
        return prompt.format(**kwargs)

prompt_template = FeastPromptTemplate(input_variables=["driver_id"])
print(prompt_template.format(driver_id=1001))

输出结果为:

Given the driver's up to date stats, write them note relaying those stats to them.
If they have a conversation rate above .5, give them a compliment. Otherwise, make a silly joke about chickens at the end to make them feel better

Here are the drivers stats:
Conversation rate: 0.4745151400566101
Acceptance rate: 0.055561766028404236
Average Daily Trips: 936

Your response:

4. 基于chain

from langchain.chat_models import ChatOpenAI
from langchain.chains import LLMChain

chain = LLMChain(llm=ChatOpenAI(openai_api_key="xxx"), prompt=prompt_template)
chain.run(1001)

运行代码,结果是:

"Hi there! I wanted to update you on your current stats. Your acceptance rate is 0.055561766028404236 and your average daily trips are 936. While your conversation rate is currently 0.4745151400566101, I have no doubt that with a little extra effort, you'll be able to exceed that .5 mark! Keep up the great work! And remember, even chickens can't always cross the road, but they still give it their best shot."

02 Featureform


Featureform是一个开源的、企业级的特征库,它可以让你像使用Spark或者本地环境一样,定义特征转换。

下面介绍如何使用用Featureform。

1. 初始化Featureform​​​​​​​

import featureform as ff

client = ff.Client(host="demo.featureform.com")

2. 创建prompt,定义FeaturformPrompt模版​​​​​​​

from langchain.prompts import PromptTemplate, StringPromptTemplate

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

Here are the user's stats:
Average Amount per Transaction: ${avg_transcation}

Your response:"""
prompt = PromptTemplate.from_template(template)

class FeatureformPromptTemplate(StringPromptTemplate):
    def format(self, **kwargs) -> str:
        user_id = kwargs.pop("user_id")
        fpf = client.features([("avg_transactions", "quickstart")], {"user": user_id})
        return prompt.format(**kwargs)

prompt_template = FeatureformPromptTemplate(input_variables=["user_id"])
print(prompt_template.format(user_id="C1410926"))

3. 使用prompt_template​​​​​​​

from langchain.chat_models import ChatOpenAI
from langchain.chains import LLMChain

chain = LLMChain(llm=ChatOpenAI(openai_api_key="xxx"), prompt=prompt_template)
chain.run("C1410926")

本文小结

本文主要介绍了可以LangChain中连接特征库,通过 特征库 将最新的、相关的信息输入给LLM,从而得到更加准确的回答;并以Feast和Featureform为示例介绍如何在LangChain中使用特征库。

更多最新文章,请关注公众号:大白爱爬山

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/989191.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

React中函数式组件与类组件有何不同?

Function Component 与 Class Component 有何不同 目录 Function Component 与 Class Component 有何不同 文章核心观点: 解释一下: 总结: 文章核心观点: Function components capture the rendered values.函数式组件捕获…

【PyQT5教程】-02-UI组件

1.按钮 QtWidgets模块提供了多种按钮类,让你可以轻松地创建各种类型的按钮 1.1 QPushButton(普通按钮) QPushButton是PyQt5中最常见的按钮类型之一,用于触发动作或执行操作。通过信号与槽机制,你可以将按钮的点击事…

性能测试 —— 生成html测试报告、参数化、jvm监控

1.生成HTML的测试报告 1.1配置 (1)找到jmeter 的安装目录,下的bin中的jmeter.properties(jmeter配置文件) (2) ctrl f ,搜索jmeter.save.saveservice.output_format,取消井号 并且 把等号后的xml改为csv,…

基于Intel优化的淡水养殖水质溯源方案

基于AI的淡水养殖水质溯源、优化系统方案 前言一、核心痛点及关键需求1.政策引导及产业升级2.紧跟时事及供给变化3.品牌打造,重拾消费者信赖4.特色生态新模式 二、方案设计1.水质溯源档案2.数字孪生系统3.基于intel AI 水质预测算法 三、实践案例1、方案简述2、数据…

聊聊低代码的全栈开发能力

一、前言 低代码的热度持续提升,最明显的举动就是资本真金白银的投资。 阿里推出“云钉一体”战略,为企业提供全生命周期的IT解决文案;腾讯将各个事业部的低代码平台进行整合,推出了OTeam平台。网易有数帆轻舟低代码平台&#xff…

CentOS7无法连接网络 右上角网络图标消失

在使用 linux 的过程中,有时会出现网络图标消失的问题,这时系统会没有网络。 有些 linux 的网络连接由 NetworkManager 管理, 问题应由它解决。 先执行一下 systemctl restart NetworkManager 看有没有效果。 原因一 :NetworkMan…

Kafka3.1部署和Topic主题数据生产与消费

文章目录 前言一、Kafka3.1X版本在Windows11主机部署二、Kafk生产Topic主题数据1.kafka生产数据2.JAVA kafka客户端消费数据 总结 前言 本章节主要讲述Kafka3.1X版本在Windows11主机下部署以及JAVA对Kafka应用: 一、Kafka3.1X版本在Windows11主机部署 1.安装JDK配…

Spring Boot 集成MQTT代码示例

文章目录 1. 简介使用场景 2. 搭建MQTT测试环境服务1. 先创建映射目录2. 创建两个文件2.1. mosquitto.conf 3. 启动 MQTT服务 Docker 容器3.1. 配置用户名和密码3.1.1. 创建密码文件3.1.2. 修改配置文件,追加密码文件 3.2. 重启mosquitto 容器服务 3. 编写测试程序3…

2023--9-8 高斯消元解线性方程组

题目链接&#xff1a;高斯消元解线性方程组 #include <iostream> #include <algorithm> #include <cmath>using namespace std;const int N 110; const double eps 1e-8;int n; double a[N][N];int gauss() {int c, r;for(c 0, r 0; c < n; c){// 找到…

(其他) 剑指 Offer 64. 求1+2+…+n ——【Leetcode每日一题】

❓ 剑指 Offer 64. 求12…n 难度&#xff1a;中等 求 12...n &#xff0c;要求不能使用乘除法、for、while、if、else、switch、case 等关键字及 条件判断语句&#xff08;A?B:C&#xff09;。 示例 1&#xff1a; 输入: n 3 输出: 6 示例 2&#xff1a; 输入: n 9 输出:…

LeetCode(力扣)491. 递增子序列Python

LeetCode491. 递增子序列 题目链接代码 题目链接 https://leetcode.cn/problems/non-decreasing-subsequences/ 代码 class Solution:def backtracking(self, nums, index, result, path):if len(path) > 1:result.append(path[:])uset set()for i in range(index, len…

小程序实现摄像头拍照 + 水印绘制

文章标题 01 功能说明02 使用方式 & 效果图2.1 基础用法2.2 拍照 底部定点水印 预览2.3 拍照 整体背景水印 预览 03 全部代码3.1 页面布局 html3.2 业务核心 js3.3 基础样式 css 01 功能说明 需求&#xff1a;小程序端需要调用前置摄像头进行拍照&#xff0c;并且将拍…

当面试官问你离职原因的时候怎么回答比较好?

所有的前提都是建立在有一定的物质基础&#xff0c;当你的一日三餐都成了问题&#xff0c;都需要家庭支持的时候我希望你可以找一份工作&#xff0c;靠自己的本事养活自己从来不丢人&#xff0c;我觉得死要面子活受罪才是真的让你看不起。 所有的建议都是建立在我们是普通打工人…

如何用Jmeter编写脚本压测?

随着商业业务不断扩张&#xff0c;调用adsearch服务频率越来越高&#xff0c;所以这次想做个压测&#xff0c;了解目前多少并发量可以到达adsearch服务的界值。 这次选用的jmeter压测工具&#xff0c;压测思路如图&#xff1a; 一、日志入参 日志选取的adsearch 的 getads部分…

电工-什么是电流

什么是电流&#xff1f;电流计算公式和单位换算及电流方向讲解 前面了解到电路的基本组成是包括&#xff1a;电能、负载、导线构成的&#xff0c;而这电路就是电流流通的路径&#xff0c;那么什么是电流呢&#xff1f;下面就讲讲电流形成的基本概念以及电流计算公式、单位和方…

2023-9-8 求组合数(二)

题目链接&#xff1a;求组合数 II #include <iostream> #include <algorithm>using namespace std;typedef long long LL; const int mod 1e9 7; const int N 100010;// 阶乘&#xff0c;阶乘的逆 int fact[N], infact[N];LL qmi(int a, int k, int p) {int res…

HTTPS 之fiddler抓包--jmeter请求

一、浅谈HTTPS 我们都知道HTTP并非是安全传输&#xff0c;在HTTPS基础上使用SSL协议进行加密构成的HTTPS协议是相对安全的。目前越来越多的企业选择使用HTTPS协议与用户进行通信&#xff0c;如百度、谷歌等。HTTPS在传输数据之前需要客户端&#xff08;浏览器&#xff09;与服…

selenium的Chrome116版驱动下载

这里写自定义目录标题 下载地址https://googlechromelabs.github.io/chrome-for-testing/#stable 选择chromedriver 对应的平台和版本 国内下载地址 https://download.csdn.net/download/dongtest/88314387

分享一个Python Django影片数据爬取与数据分析系统源码

&#x1f495;&#x1f495;作者&#xff1a;计算机源码社 &#x1f495;&#x1f495;个人简介&#xff1a;本人七年开发经验&#xff0c;擅长Java、Python、PHP、.NET、微信小程序、爬虫、大数据等&#xff0c;大家有这一块的问题可以一起交流&#xff01; &#x1f495;&…

数据结构——带头双向循环链表

数据结构——带头双向循环链表 一、带头双向循环链表的定义二、带头双向循环链表的实现2.1初始化创建带头双向循环链表的节点2.2申请新节点2.3节点的初始化2.4带头双向循环链表的尾插2.5带头双向循环链表的头插2.6判空函数2.7带头双向循环链表的打印函数2.8带头双向循环链表的尾…