Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
OurBuilding Ambient Agents with LangGraph course is now available on LangChain Academy!
Open In ColabOpen on GitHub

MyScale

MyScale is a cloud-based database optimized for AI applications and solutions, built on the open-sourceClickHouse.

This notebook shows how to use functionality related to theMyScale vector database.

Setting up environments

%pip install--upgrade--quiet  clickhouse-connect langchain-community

We want to use OpenAIEmbeddings so we have to get the OpenAI API Key.

import getpass
import os

if"OPENAI_API_KEY"notin os.environ:
os.environ["OPENAI_API_KEY"]= getpass.getpass("OpenAI API Key:")
if"OPENAI_API_BASE"notin os.environ:
os.environ["OPENAI_API_BASE"]= getpass.getpass("OpenAI Base:")
if"MYSCALE_HOST"notin os.environ:
os.environ["MYSCALE_HOST"]= getpass.getpass("MyScale Host:")
if"MYSCALE_PORT"notin os.environ:
os.environ["MYSCALE_PORT"]= getpass.getpass("MyScale Port:")
if"MYSCALE_USERNAME"notin os.environ:
os.environ["MYSCALE_USERNAME"]= getpass.getpass("MyScale Username:")
if"MYSCALE_PASSWORD"notin os.environ:
os.environ["MYSCALE_PASSWORD"]= getpass.getpass("MyScale Password:")

There are two ways to set up parameters for myscale index.

  1. Environment Variables

    Before you run the app, please set the environment variable withexport:export MYSCALE_HOST='<your-endpoints-url>' MYSCALE_PORT=<your-endpoints-port> MYSCALE_USERNAME=<your-username> MYSCALE_PASSWORD=<your-password> ...

    You can easily find your account, password and other info on our SaaS. For details please refer tothis document

    Every attributes underMyScaleSettings can be set with prefixMYSCALE_ and is case insensitive.

  2. CreateMyScaleSettings object with parameters

    from langchain_community.vectorstoresimport MyScale, MyScaleSettings
    config= MyScaleSetting(host="<your-backend-url>", port=8443,...)
    index= MyScale(embedding_function, config)
    index.add_documents(...)
from langchain_community.document_loadersimport TextLoader
from langchain_community.vectorstoresimport MyScale
from langchain_openaiimport OpenAIEmbeddings
from langchain_text_splittersimport CharacterTextSplitter
from langchain_community.document_loadersimport TextLoader

loader= TextLoader("../../how_to/state_of_the_union.txt")
documents= loader.load()
text_splitter= CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
docs= text_splitter.split_documents(documents)

embeddings= OpenAIEmbeddings()
API Reference:TextLoader
for din docs:
d.metadata={"some":"metadata"}
docsearch= MyScale.from_documents(docs, embeddings)

query="What did the president say about Ketanji Brown Jackson"
docs= docsearch.similarity_search(query)
Inserting data...: 100%|██████████| 42/42 [00:15<00:00,  2.66it/s]
print(docs[0].page_content)
Tonight. I call on the Senate to: Pass the Freedom to Vote Act. Pass the John Lewis Voting Rights Act. And while you’re at it, pass the Disclose Act so Americans can know who is funding our elections.

Tonight, I’d like to honor someone who has dedicated his life to serve this country: Justice Stephen Breyer—an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. Justice Breyer, thank you for your service.

One of the most serious constitutional responsibilities a President has is nominating someone to serve on the United States Supreme Court.

And I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nation’s top legal minds, who will continue Justice Breyer’s legacy of excellence.

Get connection info and data schema

print(str(docsearch))

Filtering

You can have direct access to myscale SQL where statement. You can writeWHERE clause following standard SQL.

NOTE: Please be aware of SQL injection, this interface must not be directly called by end-user.

If you customized yourcolumn_map under your setting, you search with filter like this:

from langchain_community.document_loadersimport TextLoader
from langchain_community.vectorstoresimport MyScale

loader= TextLoader("../../how_to/state_of_the_union.txt")
documents= loader.load()
text_splitter= CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
docs= text_splitter.split_documents(documents)

embeddings= OpenAIEmbeddings()

for i, dinenumerate(docs):
d.metadata={"doc_id": i}

docsearch= MyScale.from_documents(docs, embeddings)
API Reference:TextLoader |MyScale
Inserting data...: 100%|██████████| 42/42 [00:15<00:00,  2.68it/s]

Similarity search with score

The returned distance score is cosine distance. Therefore, a lower score is better.

meta= docsearch.metadata_column
output= docsearch.similarity_search_with_relevance_scores(
"What did the president say about Ketanji Brown Jackson?",
k=4,
where_str=f"{meta}.doc_id<10",
)
for d, distin output:
print(dist, d.metadata, d.page_content[:20]+"...")
0.229655921459198 {'doc_id': 0} Madam Speaker, Madam...
0.24506962299346924 {'doc_id': 8} And so many families...
0.24786919355392456 {'doc_id': 1} Groups of citizens b...
0.24875116348266602 {'doc_id': 6} And I’m taking robus...

Deleting your data

You can either drop the table with.drop() method or partially delete your data with.delete() method.

# use directly a `where_str` to delete
docsearch.delete(where_str=f"{docsearch.metadata_column}.doc_id < 5")
meta= docsearch.metadata_column
output= docsearch.similarity_search_with_relevance_scores(
"What did the president say about Ketanji Brown Jackson?",
k=4,
where_str=f"{meta}.doc_id<10",
)
for d, distin output:
print(dist, d.metadata, d.page_content[:20]+"...")
0.24506962299346924 {'doc_id': 8} And so many families...
0.24875116348266602 {'doc_id': 6} And I’m taking robus...
0.26027143001556396 {'doc_id': 7} We see the unity amo...
0.26390212774276733 {'doc_id': 9} And unlike the $2 Tr...
docsearch.drop()

Related


[8]ページ先頭

©2009-2025 Movatter.jp