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

原文地址:【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中使用特征库。

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

你可能感兴趣的:(LangChain,langchain,prompt,人工智能)