跳到主要內容
Open In ColabOpen on GitHub

MyScale

MyScale 是一個整合的向量資料庫。您可以使用 SQL 存取您的資料庫,也可以從這裡,LangChain 存取。MyScale 可以使用各種資料類型和函數進行篩選。無論您是要擴展您的數據還是將系統擴展到更廣泛的應用,它都將提升您的 LLM 應用程式。

在筆記本中,我們將演示 SelfQueryRetriever,它包裝在 MyScale 向量儲存周圍,並包含我們為 LangChain 貢獻的一些額外部分。

簡而言之,可以濃縮為 4 點

  1. 如果有多個元素匹配,則新增 contain 比較子以匹配任何列表
  2. 為日期時間匹配新增 timestamp 資料類型(ISO 格式或 YYYY-MM-DD)
  3. 為字串模式搜尋新增 like 比較子
  4. 新增任意函數功能

建立 MyScale 向量儲存

MyScale 已經整合到 LangChain 一段時間了。因此您可以按照此筆記本建立您自己的向量儲存,用於自我查詢檢索器。

注意: 所有自我查詢檢索器都要求您安裝 lark (pip install lark)。我們使用 lark 進行語法定義。在您繼續下一步之前,我們也想提醒您,與您的 MyScale 後端互動也需要 clickhouse-connect

%pip install --upgrade --quiet  lark clickhouse-connect

在本教學中,我們遵循其他範例的設定並使用 OpenAIEmbeddings。請記住取得 OpenAI API 金鑰以有效存取 LLM。

import getpass
import os

if "OPENAI_API_KEY" not in os.environ:
os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:")
if "MYSCALE_HOST" not in os.environ:
os.environ["MYSCALE_HOST"] = getpass.getpass("MyScale URL:")
if "MYSCALE_PORT" not in os.environ:
os.environ["MYSCALE_PORT"] = getpass.getpass("MyScale Port:")
if "MYSCALE_USERNAME" not in os.environ:
os.environ["MYSCALE_USERNAME"] = getpass.getpass("MyScale Username:")
if "MYSCALE_PASSWORD" not in os.environ:
os.environ["MYSCALE_PASSWORD"] = getpass.getpass("MyScale Password:")
from langchain_community.vectorstores import MyScale
from langchain_core.documents import Document
from langchain_openai import OpenAIEmbeddings

embeddings = OpenAIEmbeddings()

建立一些範例資料

如您所見,我們建立的資料與其他自我查詢檢索器相比有一些差異。我們將關鍵字 year 替換為 date,讓您可以更精細地控制時間戳記。我們也將關鍵字 gerne 的類型變更為字串列表,LLM 可以使用新的 contain 比較子來建構篩選器。我們還提供 like 比較子和任意函數支援來篩選器,這將在接下來的幾個儲存格中介紹。

現在我們先看看資料。

docs = [
Document(
page_content="A bunch of scientists bring back dinosaurs and mayhem breaks loose",
metadata={"date": "1993-07-02", "rating": 7.7, "genre": ["science fiction"]},
),
Document(
page_content="Leo DiCaprio gets lost in a dream within a dream within a dream within a ...",
metadata={"date": "2010-12-30", "director": "Christopher Nolan", "rating": 8.2},
),
Document(
page_content="A psychologist / detective gets lost in a series of dreams within dreams within dreams and Inception reused the idea",
metadata={"date": "2006-04-23", "director": "Satoshi Kon", "rating": 8.6},
),
Document(
page_content="A bunch of normal-sized women are supremely wholesome and some men pine after them",
metadata={"date": "2019-08-22", "director": "Greta Gerwig", "rating": 8.3},
),
Document(
page_content="Toys come alive and have a blast doing so",
metadata={"date": "1995-02-11", "genre": ["animated"]},
),
Document(
page_content="Three men walk into the Zone, three men walk out of the Zone",
metadata={
"date": "1979-09-10",
"director": "Andrei Tarkovsky",
"genre": ["science fiction", "adventure"],
"rating": 9.9,
},
),
]
vectorstore = MyScale.from_documents(
docs,
embeddings,
)

建立我們的自我查詢檢索器

就像其他檢索器一樣... 簡單又好用。

from langchain.chains.query_constructor.schema import AttributeInfo
from langchain.retrievers.self_query.base import SelfQueryRetriever
from langchain_openai import ChatOpenAI

metadata_field_info = [
AttributeInfo(
name="genre",
description="The genres of the movie. "
"It only supports equal and contain comparisons. "
"Here are some examples: genre = [' A '], genre = [' A ', 'B'], contain (genre, 'A')",
type="list[string]",
),
# If you want to include length of a list, just define it as a new column
# This will teach the LLM to use it as a column when constructing filter.
AttributeInfo(
name="length(genre)",
description="The length of genres of the movie",
type="integer",
),
# Now you can define a column as timestamp. By simply set the type to timestamp.
AttributeInfo(
name="date",
description="The date the movie was released",
type="timestamp",
),
AttributeInfo(
name="director",
description="The name of the movie director",
type="string",
),
AttributeInfo(
name="rating", description="A 1-10 rating for the movie", type="float"
),
]
document_content_description = "Brief summary of a movie"
llm = ChatOpenAI(temperature=0, model_name="gpt-4o")
retriever = SelfQueryRetriever.from_llm(
llm, vectorstore, document_content_description, metadata_field_info, verbose=True
)

使用自我查詢檢索器的現有功能進行測試

現在我們可以嘗試實際使用我們的檢索器了!

# This example only specifies a relevant query
retriever.invoke("What are some movies about dinosaurs")
# This example only specifies a filter
retriever.invoke("I want to watch a movie rated higher than 8.5")
# This example specifies a query and a filter
retriever.invoke("Has Greta Gerwig directed any movies about women")
# This example specifies a composite filter
retriever.invoke("What's a highly rated (above 8.5) science fiction film?")
# This example specifies a query and composite filter
retriever.invoke(
"What's a movie after 1990 but before 2005 that's all about toys, and preferably is animated"
)

等一下... 還有什麼?

具有 MyScale 的自我查詢檢索器可以做更多事情!讓我們來看看。

# You can use length(genres) to do anything you want
retriever.invoke("What's a movie that have more than 1 genres?")
# Fine-grained datetime? You got it already.
retriever.invoke("What's a movie that release after feb 1995?")
# Don't know what your exact filter should be? Use string pattern match!
retriever.invoke("What's a movie whose name is like Andrei?")
# Contain works for lists: so you can match a list with contain comparator!
retriever.invoke("What's a movie who has genres science fiction and adventure?")

篩選 k

我們也可以使用自我查詢檢索器來指定 k:要提取的文件數量。

我們可以透過將 enable_limit=True 傳遞給建構函式來做到這一點。

retriever = SelfQueryRetriever.from_llm(
llm,
vectorstore,
document_content_description,
metadata_field_info,
enable_limit=True,
verbose=True,
)
# This example only specifies a relevant query
retriever.invoke("what are two movies about dinosaurs")

此頁面是否對您有幫助?