Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbbf8dd0

Browse files
authored
Sql extension docs + fixes (#1427)
1 parent68bb388 commitbbf8dd0

File tree

16 files changed

+307
-82
lines changed

16 files changed

+307
-82
lines changed
82.7 KB
Loading
Loading
Loading
Loading
49.5 KB
Loading

‎pgml-cms/docs/README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,18 @@ PostgresML allows you to take advantage of the fundamental relationship between
2121

2222
<figure><imgsrc=".gitbook/assets/ml_system.svg"alt="Machine Learning Infrastructure (2.0) by a16z"><figcaptionclass="mt-2"><p>PostgresML handles all of the functions <ahref="https://a16z.com/emerging-architectures-for-modern-data-infrastructure/">described by a16z</a></p></figcaption></figure>
2323

24-
These capabilities are primarily provided by two open-source software projects, that may be used independently, but are designed to be used with the rest of the Postgres ecosystem:
24+
These capabilities are primarily provided by two open-source software projects, that may be used independently, but are designed to be usedtogetherwith the rest of the Postgres ecosystem:
2525

26-
***pgml** - an open source extension for PostgreSQL. It adds support for GPUs and the latest ML & AI algorithms_inside_ the database with a SQL API and no additional infrastructure, networking latency, or reliability costs
27-
***PgCat** - an open source pooler for PostgreSQL. It abstracts the scalability and reliability concerns of managing a distributed cluster of Postgres databases. Client applications connect only to the pooler, which handles load balancing, sharding, and failover, outside of any single database server.
26+
*[**pgml**](/docs/api/sql-extension/) - an open source extension for PostgreSQL. It adds support for GPUs and the latest ML & AI algorithms_inside_ the database with a SQL API and no additional infrastructure, networking latency, or reliability costs.
27+
*[**PgCat**](/docs/product/pgcat/) - an open source connection pooler for PostgreSQL. It abstracts the scalability and reliability concerns of managing a distributed cluster of Postgres databases. Client applications connect only to the pooler, which handles load balancing, sharding, and failover, outside of any single database server.
2828

2929
<figure><imgsrc=".gitbook/assets/architecture.png"alt="PostgresML architectural diagram"><figcaption></figcaption></figure>
3030

31+
To learn more about how we designed PostgresML, take a look at our[architecture overview](/docs/resources/architecture/).
32+
3133
##Client SDK
3234

33-
The PostgresML team also provides[native language SDKs](https://github.com/postgresml/postgresml/tree/master/pgml-sdks/pgml) which implement best practices for common ML & AI applications. The JavaScript and Python SDKs are generated from the a core Rust library, which provides a uniform API, correctness and efficiency across all environments.
35+
The PostgresML team also provides[native language SDKs](/docs/api/client-sdk/) which implement best practices for common ML & AI applications. The JavaScript and Python SDKs are generated from the a core Rust library, which provides a uniform API, correctness and efficiency across all environments.
3436

3537
While using the SDK is completely optional, SDK clients can perform advanced machine learning tasks in a single SQL request, without having to transfer additional data, models, hardware or dependencies to the client application.
3638

‎pgml-cms/docs/SUMMARY.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,18 @@
1616

1717
*[Overview](api/apis.md)
1818
*[SQL extension](api/sql-extension/README.md)
19-
*[pgml.deploy()](api/sql-extension/pgml.deploy.md)
2019
*[pgml.embed()](api/sql-extension/pgml.embed.md)
20+
*[pgml.transform()](api/sql-extension/pgml.transform/README.md)
21+
*[Fill Mask](api/sql-extension/pgml.transform/fill-mask.md)
22+
*[Question Answering](api/sql-extension/pgml.transform/question-answering.md)
23+
*[Summarization](api/sql-extension/pgml.transform/summarization.md)
24+
*[Text Classification](api/sql-extension/pgml.transform/text-classification.md)
25+
*[Text Generation](api/sql-extension/pgml.transform/text-generation.md)
26+
*[Text-to-Text Generation](api/sql-extension/pgml.transform/text-to-text-generation.md)
27+
*[Token Classification](api/sql-extension/pgml.transform/token-classification.md)
28+
*[Translation](api/sql-extension/pgml.transform/translation.md)
29+
*[Zero-shot Classification](api/sql-extension/pgml.transform/zero-shot-classification.md)
30+
*[pgml.deploy()](api/sql-extension/pgml.deploy.md)
2131
*[pgml.chunk()](api/sql-extension/pgml.chunk.md)
2232
*[pgml.generate()](api/sql-extension/pgml.generate.md)
2333
*[pgml.predict()](api/sql-extension/pgml.predict/README.md)
@@ -29,16 +39,6 @@
2939
*[Data Pre-processing](api/sql-extension/pgml.train/data-pre-processing.md)
3040
*[Hyperparameter Search](api/sql-extension/pgml.train/hyperparameter-search.md)
3141
*[Joint Optimization](api/sql-extension/pgml.train/joint-optimization.md)
32-
*[pgml.transform()](api/sql-extension/pgml.transform/README.md)
33-
*[Fill Mask](api/sql-extension/pgml.transform/fill-mask.md)
34-
*[Question Answering](api/sql-extension/pgml.transform/question-answering.md)
35-
*[Summarization](api/sql-extension/pgml.transform/summarization.md)
36-
*[Text Classification](api/sql-extension/pgml.transform/text-classification.md)
37-
*[Text Generation](api/sql-extension/pgml.transform/text-generation.md)
38-
*[Text-to-Text Generation](api/sql-extension/pgml.transform/text-to-text-generation.md)
39-
*[Token Classification](api/sql-extension/pgml.transform/token-classification.md)
40-
*[Translation](api/sql-extension/pgml.transform/translation.md)
41-
*[Zero-shot Classification](api/sql-extension/pgml.transform/zero-shot-classification.md)
4242
*[pgml.tune()](api/sql-extension/pgml.tune.md)
4343
*[Client SDK](api/client-sdk/README.md)
4444
*[Collections](api/client-sdk/collections.md)
@@ -79,6 +79,8 @@
7979

8080
##Resources
8181

82+
*[Architecture](resources/architecture/README.md)
83+
*[Why PostgresML?](resources/architecture/why-postgresml.md)
8284
*[FAQs](resources/faqs.md)
8385
*[Data Storage & Retrieval](resources/data-storage-and-retrieval/tabular-data.md)
8486
*[Tabular data](resources/data-storage-and-retrieval/tabular-data.md)
@@ -97,8 +99,6 @@
9799
*[Contributing](resources/developer-docs/contributing.md)
98100
*[Distributed Training](resources/developer-docs/distributed-training.md)
99101
*[GPU Support](resources/developer-docs/gpu-support.md)
100-
*[Deploying PostgresML](resources/developer-docs/deploying-postgresml/README.md)
101-
*[Monitoring](resources/developer-docs/deploying-postgresml/monitoring.md)
102102
*[Self-hosting](resources/developer-docs/self-hosting/README.md)
103103
*[Pooler](resources/developer-docs/self-hosting/pooler.md)
104104
*[Building from source](resources/developer-docs/self-hosting/building-from-source.md)

‎pgml-cms/docs/api/sql-extension/pgml.embed.md

Lines changed: 77 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,48 +6,99 @@ description: >-
66

77
#pgml.embed()
88

9-
Embeddings are a numeric representation of text. They are used to represent words and sentences as vectors, an array of numbers. Embeddings can be used to find similar pieces of text, by comparing the similarity of the numeric vectors using a distance measure, or they can be used as input features for other machine learning models, since most algorithms can't use text directly.
10-
11-
Many pretrained LLMs can be used to generate embeddings from text within PostgresML. You can browse all the[models](https://huggingface.co/models?library=sentence-transformers) available to find the best solution on Hugging Face.
9+
The`pgml.embed()` function generates[embeddings](/docs/use-cases/embeddings/) from text, using in-database models downloaded from Hugging Face. Thousands of[open-source models](https://huggingface.co/models?library=sentence-transformers) are available and new and better ones are being published regularly.
1210

1311
##API
1412

1513
```sql
1614
pgml.embed(
17-
transformerTEXT,-- huggingface sentence-transformer name
18-
textTEXT,-- input to embed
19-
kwargs JSON-- optional arguments (see below)
15+
transformerTEXT,
16+
"text"TEXT,
17+
kwargs JSON
2018
)
2119
```
2220

23-
##Example
21+
| Argument| Description| Example|
22+
|----------|-------------|---------|
23+
| transformer| The name of a Hugging Face embedding model.|`intfloat/e5-large-v2`|
24+
| text| The text to embed. This can be a string or the name of a column from a PostgreSQL table.|`'I am your father, Luke'`|
25+
| kwargs| Additional arguments that are passed to the model.||
2426

25-
Let's use the`pgml.embed` function to generate embeddings for tweets, so we can find similar ones. We will use the`distilbert-base-uncased` model from :hugging: HuggingFace. This model is a small version of the`bert-base-uncased` model. It is a good choice for short texts like tweets. To start, we'll load a dataset that provides tweets classified into different topics.
27+
###Examples
2628

27-
```sql
28-
SELECTpgml.load_dataset('tweet_eval','sentiment');
29-
```
29+
####Generate embeddings from text
3030

31-
View some tweets and their topics.
31+
Creating an embedding from text is as simple as calling the function with the text you want to embed:
3232

33-
```sql
34-
SELECT*
35-
FROMpgml.tweet_eval
36-
LIMIT10;
33+
{% tabs %}
34+
{% tab title="SQL" %}
35+
36+
```postgresql
37+
SELECT * FROM pgml.embed(
38+
'intfloat/e5-small',
39+
'No, that''s not true, that''s impossible.'
40+
) AS star_wars_embedding;
3741
```
3842

39-
Get a preview of the embeddings for the first 10 tweets. This will also download the model and cache it for reuse, since it's the first time we've used it.
43+
{% endtab %}
44+
{% endtabs %}
4045

41-
```sql
42-
SELECTtext,pgml.embed('distilbert-base-uncased',text)
43-
FROMpgml.tweet_eval
44-
LIMIT10;
46+
####Generate embeddings from a table
47+
48+
SQL functions can be used as part of a query to insert, update, or even automatically generate column values of any table:
49+
50+
{% tabs %}
51+
{% tab title="SQL" %}
52+
53+
```postgresql
54+
CREATE TABLE star_wars_quotes (
55+
quote TEXT NOT NULL,
56+
embedding vector(384) GENERATED ALWAYS AS (
57+
pgml.embed('intfloat/e5-small', quote)
58+
) STORED
59+
);
60+
61+
INSERT INTO
62+
star_wars_quotes (quote)
63+
VALUES
64+
('I find your lack of faith disturbing'),
65+
('I''ve got a bad feeling about this.'),
66+
('Do or do not, there is no try.');
4567
```
4668

47-
It will take a few minutes to generate the embeddings for the entire dataset. We'll save the results to a new table.
69+
{% endtab %}
70+
{% endtabs %}
4871

49-
```sql
50-
CREATETABLEtweet_embeddingsAS
51-
SELECTtext,pgml.embed('distilbert-base-uncased',text)AS embedding
52-
FROMpgml.tweet_eval;
72+
In this example, we're using[generated columns](https://www.postgresql.org/docs/current/ddl-generated-columns.html) to automatically create an embedding of the`quote` column every time the column value is updated.
73+
74+
####Using embeddings in queries
75+
76+
Once you have embeddings, you can use them in queries to find text with similar semantic meaning:
77+
78+
{% tabs %}
79+
{% tab title="SQL" %}
80+
81+
```postgresql
82+
SELECT
83+
quote
84+
FROM
85+
star_wars_quotes
86+
ORDER BY
87+
pgml.embed(
88+
'intfloat/e5-small',
89+
'Feel the force!',
90+
) <=> embedding
91+
DESC
92+
LIMIT 1;
5393
```
94+
95+
{% endtab %}
96+
{% endtabs %}
97+
98+
This query will return the quote with the most similar meaning to`'Feel the force!'` by generating an embedding of that quote and comparing it to all other embeddings in the table, using vector cosine similarity as the measure of distance.
99+
100+
##Performance
101+
102+
First time`pgml.embed()` is called with a new model, it is downloaded from Hugging Face and saved in the cache directory. Subsequent calls will use the cached model, which is faster, and if the connection to the database is kept open, the model will be reused across multiple queries without being unloaded from memory.
103+
104+
If a GPU is available, the model will be automatically loaded onto the GPU and the embedding generation will be even faster.

‎pgml-cms/docs/api/sql-extension/pgml.transform/README.md

Lines changed: 111 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,37 +17,134 @@ layout:
1717

1818
#pgml.transform()
1919

20-
PostgresML integrates[🤗 Hugging Face Transformers](https://huggingface.co/transformers) to bring state-of-the-art models into the data layer. There are tens of thousands of pre-trainedmodels with pipelines to turn raw inputs into useful results. Many state of the art deep learning architectures have been publishedandmade available for download. You will wanttobrowse all the[models](https://huggingface.co/models) available to find the perfect solution foryour[dataset](https://huggingface.co/dataset) and[task](https://huggingface.co/tasks).
20+
The`pgml.transform()` function isthe most powerful feature of PostgresML. It integrates open-source large languagemodels, like Llama, Mixtral,andmany more, which allowstoperform complex tasks onyourdata.
2121

22-
We'll demonstrate some of the tasks thatareimmediately available to users of your database upon installation:[translation](https://github.com/postgresml/postgresml/blob/v2.7.12/pgml-dashboard/content/docs/guides/transformers/pre\_trained\_models.md#translation),[sentiment analysis](https://github.com/postgresml/postgresml/blob/v2.7.12/pgml-dashboard/content/docs/guides/transformers/pre\_trained\_models.md#sentiment-analysis),[summarization](https://github.com/postgresml/postgresml/blob/v2.7.12/pgml-dashboard/content/docs/guides/transformers/pre\_trained\_models.md#summarization),[question answering](https://github.com/postgresml/postgresml/blob/v2.7.12/pgml-dashboard/content/docs/guides/transformers/pre\_trained\_models.md#question-answering) and[textgeneration](https://github.com/postgresml/postgresml/blob/v2.7.12/pgml-dashboard/content/docs/guides/transformers/pre\_trained\_models.md#text-generation).
22+
The modelsaredownloaded from[🤗 Hugging Face](https://huggingface.co/transformers) which hosts tens of thousands ofpre-trained and fine-tuned models for various tasks like text generation,question answering, summarization,textclassification, and more.
2323

24-
###Examples
24+
##API
2525

26-
All of the tasks and models demonstrated here can be customized by passing additional arguments to the`Pipeline` initializer or call. You'll find additional links to documentation in the examples below.
26+
The`pgml.transform()` function comes in two flavors, task-based and model-based.
2727

28-
The Hugging Face[`Pipeline`](https://huggingface.co/docs/transformers/main\_classes/pipelines)API is exposed in Postgres via:
28+
###Task-basedAPI
2929

30-
```sql
30+
The task-based API automatically chooses a model to use based on the task:
31+
32+
```postgresql
33+
pgml.transform(
34+
task TEXT,
35+
args JSONB,
36+
inputs TEXT[]
37+
)
38+
```
39+
40+
| Argument| Description| Example|
41+
|----------|-------------|---------|
42+
| task| The name of a natural language processing task.|`text-generation`|
43+
| args| Additional kwargs to pass to the pipeline.|`{"max_new_tokens": 50}`|
44+
| inputs| Array of prompts to pass to the model for inference.|`['Once upon a time...']`|
45+
46+
####Example
47+
48+
{% tabs %}
49+
{% tab title="SQL" %}
50+
51+
```postgresql
52+
SELECT *
53+
FROM pgml.transform (
54+
'translation_en_to_fr',
55+
'How do I say hello in French?',
56+
);
57+
```
58+
59+
{% endtab %}
60+
{% endtabs %}
61+
62+
###Model-based API
63+
64+
The model-based API requires the name of the model and the task, passed as a JSON object, which allows it to be more generic:
65+
66+
```postgresql
3167
pgml.transform(
32-
taskTEXTORJSONB,-- task name or full pipeline initializer arguments
33-
call JSONB,-- additional call arguments alongside the inputs
34-
inputsTEXT[]ORBYTEA[]-- inputs for inference
68+
modelJSONB,
69+
args JSONB,
70+
inputs TEXT[]
3571
)
3672
```
3773

38-
This is roughly equivalent to the following Python:
74+
| Argument| Description| Example|
75+
|----------|-------------|---------|
76+
| task| Model configuration, including name and task.|`{"task": "text-generation", "model": "mistralai/Mixtral-8x7B-v0.1"}`|
77+
| args| Additional kwargs to pass to the pipeline.|`{"max_new_tokens": 50}`|
78+
| inputs| Array of prompts to pass to the model for inference.|`['Once upon a time...']`|
79+
80+
####Example
81+
82+
{% tabs %}
83+
{% tab title="SQL" %}
84+
85+
```postgresql
86+
SELECT pgml.transform(
87+
task => '{
88+
"task": "text-generation",
89+
"model": "TheBloke/zephyr-7B-beta-GPTQ",
90+
"model_type": "mistral",
91+
"revision": "main",
92+
}'::JSONB,
93+
inputs => ['AI is going to change the world in the following ways:'],
94+
args => '{
95+
"max_new_tokens": 100
96+
}'::JSONB
97+
);
98+
```
99+
100+
{% endtab %}
101+
102+
{% tab title="Equivalent Python" %}
39103

40104
```python
41105
import transformers
42106

43107
deftransform(task,call,inputs):
44108
return transformers.pipeline(**task)(inputs,**call)
109+
110+
transform(
111+
{
112+
"task":"text-generation",
113+
"model":"TheBloke/zephyr-7B-beta-GPTQ",
114+
"model_type":"mistral",
115+
"revision":"main",
116+
},
117+
{"max_new_tokens":100},
118+
['AI is going to change the world in the following ways:']
119+
)
45120
```
46121

47-
Most pipelines operate on`TEXT[]` inputs, but some require binary`BYTEA[]` data like audio classifiers.`inputs` can be`SELECT`ed from tables in the database, or they may be passed in directly with the query. The output of this call is a`JSONB` structure that is task specific. See the[Postgres JSON](https://www.postgresql.org/docs/14/functions-json.html) reference for ways to process this output dynamically.
122+
{% endtab %}
123+
{% endtabs %}
124+
125+
126+
###Supported tasks
127+
128+
PostgresML currently supports most NLP tasks available on Hugging Face:
129+
130+
| Task| Name| Description|
131+
|------|-------------|---------|
132+
|[Fill mask](fill-mask)|`key-mask`| Fill in the blank in a sentence.|
133+
|[Question answering](question-answering)|`question-answering`| Answer a question based on a context.|
134+
|[Summarization](summarization)|`summarization`| Summarize a long text.|
135+
|[Text classification](text-classification)|`text-classification`| Classify a text as positive or negative.|
136+
|[Text generation](text-generation)|`text-generation`| Generate text based on a prompt.|
137+
|[Text-to-text generation](text-to-text-generation)|`text-to-text-generation`| Generate text based on an instruction in the prompt.|
138+
|[Token classification](token-classification)|`token-classification`| Classify tokens in a text.|
139+
|[Translation](translation)|`translation`| Translate text from one language to another.|
140+
|[Zero-shot classification](zero-shot-classification)|`zero-shot-classification`| Classify a text without training data.|
141+
142+
143+
##Performance
48144

49-
!!! tip
145+
Much like`pgml.embed()`, the models used in`pgml.transform()` are downloaded from Hugging Face and cached locally. If the connection to the database is kept open, the model remains in memory, which allows for faster inference on subsequent calls. If you want to free up memory, you can close the connection.
50146

51-
Models will be downloaded and stored locally on disk after the first call. They are also cached per connection to improve repeated calls in a single session. To free that memory, you'll need to close your connection. You may want to establish dedicated credentials and connection pools via[pgcat](https://github.com/levkk/pgcat) or[pgbouncer](https://www.pgbouncer.org/) for larger models that have billions of parameters. You may also pass`{"cache": false}` in the JSON`call` args to prevent this behavior.
147+
##Additional resources
52148

53-
!!!
149+
-[Hugging Face datasets](https://huggingface.co/datasets)
150+
-[Hugging Face tasks](https://huggingface.co/tasks)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp