Skip to content

Commit d77a6c9

Browse files
szymondudyczManul from Pathway
authored and
Manul from Pathway
committed
Exposing llm components articles in ai pipelines guide (#8384)
GitOrigin-RevId: 2ceca8c0359eb7b315e2c1ed9668b114184c5eae
1 parent d0d32a3 commit d77a6c9

File tree

14 files changed

+131
-5
lines changed

14 files changed

+131
-5
lines changed

docs/2.developers/4.user-guide/50.llm-xpack/70.llm-chats.md docs/2.developers/4.user-guide/50.llm-xpack/.chats/llm-chats.md

+63-2
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,23 @@ keywords: ['LLM', 'GPT', 'OpenAI', 'Gemini', 'LiteLLM', 'Wrapper']
99

1010
# LLM Chats
1111

12-
Out of the box, the LLM xpack provides wrappers for text generation and embedding LLMs. For text generation, you can use native wrappers for the OpenAI chat model and HuggingFace models running locally. Many other popular models, including Azure OpenAI, HuggingFace (when using their API) or Gemini can be used with the [`LiteLLM`](/developers/user-guide/llm-xpack/llm-chats#litellm) wrapper. To check the full list of providers supported by LiteLLM check [LiteLLM documentation](https://docs.litellm.ai/docs/providers). Currently, Pathway provides wrappers for the following LLMs:
12+
Out of the box, the LLM xpack provides wrappers for text generation and embedding LLMs. For text generation, you can use native wrappers for the OpenAI chat model and HuggingFace models running locally. Many other popular models, including Azure OpenAI, HuggingFace (when using their API) or Gemini can be used with the [`LiteLLM`](/developers/user-guide/llm-xpack/llm-chats#litellm) wrapper. To check the full list of providers supported by LiteLLM check [LiteLLM documentation](https://docs.litellm.ai/docs/providers).
13+
::if{path="/llm-xpack/"}
14+
Currently, Pathway provides wrappers for the following LLMs:
1315
- [OpenAI](/developers/user-guide/llm-xpack/llm-chats#openaichat)
1416
- [LiteLLM](/developers/user-guide/llm-xpack/llm-chats#litellm)
1517
- [Hugging Face Pipeline](/developers/user-guide/llm-xpack/llm-chats#hugging-face-pipeline)
1618
- [Cohere](/developers/user-guide/llm-xpack/llm-chats#cohere)
19+
::
20+
::if{path="/ai-pipelines/"}
21+
Currently, Pathway provides wrappers for the following LLMs:
22+
- [OpenAI](/developers/user-guide/llm-xpack/llm-chats#openaichat)
23+
- [LiteLLM](/developers/user-guide/llm-xpack/llm-chats#litellm)
24+
- [Hugging Face Pipeline](/developers/user-guide/llm-xpack/llm-chats#hugging-face-pipeline)
25+
::
26+
1727

28+
::if{path="/llm-xpack/"}
1829
To use a wrapper, first create an instance of the wrapper, which you can then apply to a column containing prompts.
1930

2031
We create a Pathway table to be used in the examples below:
@@ -28,17 +39,21 @@ How many 'r' there are in 'strawberry'? | 400
2839
split_on_whitespace=False,
2940
)
3041
```
42+
::
3143

44+
::if{path="/llm-xpack/"}
3245
## UDFs
3346

3447
Each wrapper is a [UDF](/developers/api-docs/pathway#pathway.UDF) (User Defined Function), which allows users to define their own functions to interact with Pathway objects. A UDF, in general, is any function that takes some input, processes it, and returns an output. In the context of the Pathway library, UDFs enable seamless integration of custom logic, such as invoking LLMs for specific tasks.
3548

3649
In particular a UDF can serve as a wrapper for LLM calls, allowing users to pass prompts or other inputs to a model and retrieve the corresponding outputs. This design makes it easy to interact with Pathway tables and columns while incorporating the power of LLMs.
50+
::
3751

3852
## OpenAIChat
3953

4054
For OpenAI, you create a wrapper using the [`OpenAIChat` class](/developers/api-docs/pathway-xpacks-llm/llms#pathway.xpacks.llm.llms.OpenAIChat).
4155

56+
::if{path="/llm-xpack/"}
4257
```python
4358
from pathway.xpacks.llm import llms
4459

@@ -51,7 +66,17 @@ responses = queries.select(result=model(llms.prompt_chat_single_qa(pw.this.quest
5166
# Run the computations (including sending requests to OpenAI) and print the output table
5267
pw.debug.compute_and_print(responses)
5368
```
69+
::
70+
::if{path="/ai-pipelines/"}
71+
```yaml
72+
chat: !pw.xpacks.llm.llms.OpenAIChat
73+
model: "gpt-4o-mini
74+
api_key: $OPENAI_API_KEY
75+
```
76+
::
77+
5478
79+
::if{path="/llm-xpack/"}
5580
### Message format
5681
`OpenAIChat` expects messages to be in the format required by [OpenAI API](https://platform.openai.com/docs/api-reference/chat/create) - that is a list of dictionaries, where each dictionary is one message in the conversation so far. For asking a single question, you can use [`pw.xpacks.llm.llm.prompt_chat_single_qa`](/developers/api-docs/pathway-xpacks-llm/llms#pathway.xpacks.llm.llms.prompt_chat_single_qa) to wrap a string so that it matches the format expected by OpenAI API. Our example above presents that use case.
5782

@@ -88,10 +113,12 @@ responses = queries.select(result=model(llms.prompt_chat_single_qa(pw.this.quest
88113
responses = queries.select(result=model(llms.prompt_chat_single_qa(pw.this.questions), max_tokens(pw.this.max_tokens)))
89114
pw.debug.compute_and_print(responses)
90115
```
116+
::
91117

92118
## LiteLLM
93119
Pathway has a wrapper for LiteLLM - [`LiteLLMChat`](/developers/api-docs/pathway-xpacks-llm/llms#pathway.xpacks.llm.llms.LiteLLMChat). For example, to use Gemini with LiteLLM, create an instance of `LiteLLMChat` and then apply it to the column with messages to be sent over API.
94120

121+
::if{path="/llm-xpack/"}
95122
```python
96123
from pathway.xpacks.llm import llms
97124

@@ -103,12 +130,20 @@ model = llms.LiteLLMChat(
103130
responses = queries.select(result=model(llms.prompt_chat_single_qa(pw.this.questions)))
104131
pw.debug.compute_and_print(responses)
105132
```
133+
::
134+
::if{path="/ai-pipelines/"}
135+
```yaml
136+
llm: !pw.xpacks.llm.llms.LiteLLMChat
137+
model: "gemini/gemini-pro", # Choose the model you want
138+
```
139+
::
106140
107141
With the wrapper for LiteLLM, Pathway allows you to use many popular LLMs.
108142
109143
## Hugging Face pipeline
110144
For models from Hugging Face that you want to run locally, Pathway gives a separate wrapper called `HFPipelineChat` (for calling HuggingFace through API, use LiteLLM wrapper). When an instance of this wrapper is created, it initializes a HuggingFace `pipeline`, so any [arguments to the `pipeline`](https://huggingface.co/docs/transformers/en/main_classes/pipelines#transformers.pipeline) - including the name of the model - must be set during the initialization of `HFPipelineChat`. Any parameters to `pipeline.__call__` can be as before set during initialization or overridden during application.
111145

146+
::if{path="/llm-xpack/"}
112147
```python
113148
from pathway.xpacks.llm import llms
114149
@@ -118,10 +153,21 @@ model = llms.HFPipelineChat(
118153
responses = queries.select(result=model(pw.this.questions))
119154
pw.debug.compute_and_print(responses)
120155
```
156+
::
157+
::if{path="/ai-pipelines/"}
158+
```yaml
159+
llm: !pw.xpacks.llm.llms.HFPipelineChat
160+
model: "TinyLlama/TinyLlama-1.1B-Chat-v1.0", # Choose the model you want
161+
```
162+
::
121163

122164
Note that format of questions used in Hugging Face pipeline depends on the model. Some models, like [`gpt2`](https://huggingface.co/openai-community/gpt2), expect a prompt string, whereas conversation models also accept messages as a list of dicts. The model's prompt template will be used if a conversation with a list of dicts is passed.
165+
::if{path="/ai-pipelines/"}
166+
Note that Pathway AI pipelines expect conversation models, so models like `gpt2` cannot be used.
167+
::
123168
For more information, see [pipeline docs](https://huggingface.co/docs/transformers/en/main_classes/pipelines#transformers.TextGenerationPipeline.__call__.text_inputs).
124169

170+
::if{path="/llm-xpack/"}
125171
For example for model [`TinyLlama/TinyLlama-1.1B-Chat-v1.0`](https://huggingface.co/TinyLlama/TinyLlama-1.1B-Chat-v1.0), you can use it with:
126172

127173
```python
@@ -133,8 +179,9 @@ model = llms.HFPipelineChat(
133179
responses = queries.select(result=model(llms.prompt_chat_single_qa(pw.this.questions)))
134180
pw.debug.compute_and_print(responses)
135181
```
182+
::
136183

137-
184+
::if{path="/llm-xpack/"}
138185
## Cohere
139186
Pathway has a wrapper for the [`Cohere Chat Services`](https://docs.cohere.com/docs/command-beta). The wrapper allows for augmenting the query with documents. The result contains cited documents along with the response.
140187

@@ -163,6 +210,7 @@ r = queries_with_docs.select(
163210
parsed_table = r.select(response=pw.this.ret[0], citations=pw.this.ret[1])
164211
pw.debug.compute_and_print(parsed_table)
165212
```
213+
::
166214

167215
## Wrappers are asynchronous
168216
Wrapper for OpenAI and LiteLLM, both for chat and embedding, are asynchronous, and Pathway allows you to set three parameters to set their behavior. These are:
@@ -172,6 +220,7 @@ Wrapper for OpenAI and LiteLLM, both for chat and embedding, are asynchronous, a
172220

173221
These three parameters need to be set during the initialization of the wrapper. You can read more about them in the [UDFs guide](/developers/user-guide/data-transformation/user-defined-functions#asyncexecutor).
174222

223+
::if{path="/llm-xpack/"}
175224
```python
176225
model = llms.OpenAIChat(
177226
# maximum concurrent operations is 10
@@ -188,3 +237,15 @@ model = llms.OpenAIChat(
188237
responses = queries.select(result=model(prompt_chat_single_qa(pw.this.questions)))
189238
pw.debug.compute_and_print(responses)
190239
```
240+
::
241+
::if{path="/ai-pipelines/"}
242+
```yaml
243+
chat: !pw.xpacks.llm.llms.OpenAIChat
244+
model: "gpt-4o-mini
245+
capacity: 10
246+
retry_strategy: !pw.udfs.ExponentialBackoffRetryStrategy
247+
max_retries: 5
248+
initial_delay: 1000
249+
backoff_factor: 2
250+
```
251+
::

docs/2.developers/4.user-guide/50.llm-xpack/60.embedders.md docs/2.developers/4.user-guide/50.llm-xpack/.embedders/embedders.md

+34-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ The following embedding wrappers are available through the Pathway xpack:
2121
## OpenAIEmbedder
2222
The default model for [`OpenAIEmbedder`](/developers/api-docs/pathway-xpacks-llm/embedders/#pathway.xpacks.llm.embedders.OpenAIEmbedder) is `text-embedding-3-small`.
2323

24+
::if{path="/llm-xpack/"}
2425
```python
2526
import os
2627
import pathway as pw
@@ -44,10 +45,18 @@ documents = documents.select(text=pw.this.elements[0], metadata=pw.this.elements
4445
embedder = OpenAIEmbedder(api_key=os.environ["OPENAI_API_KEY"])
4546
embeddings = documents.select(embedding=embedder(pw.this.text))
4647
```
48+
::
49+
::if{path="/ai-pipelines/"}
50+
```yaml
51+
embedder: !pw.xpacks.llm.embedders.OpenAIEmbedder
52+
model: "text-embedding-3-small"
53+
```
54+
::
4755
4856
## LiteLLMEmbedder
4957
The model for [`LiteLLMEmbedder`](/developers/api-docs/pathway-xpacks-llm/embedders/#pathway.xpacks.llm.embedders.LiteLLMEmbedder) has to be specified during initialization. No default is provided.
5058

59+
::if{path="/llm-xpack/"}
5160
```python
5261
from pathway.xpacks.llm import embedders
5362
@@ -63,12 +72,20 @@ Here is some text
6372
)
6473
res = t.select(ret=embedder(pw.this.text_column))
6574
```
75+
::
76+
::if{path="/ai-pipelines/"}
77+
```yaml
78+
embedder: !pw.xpacks.llm.embedders.LiteLLMEmbedder
79+
model: "text-embedding-3-small"
80+
```
81+
::
6682

6783
## SentenceTransformerEmbedder
6884
This [`SentenceTransformerEmbedder`](/developers/api-docs/pathway-xpacks-llm/embedders/#pathway.xpacks.llm.embedders.SentenceTransformerEmbedder) embedder allows you to use the models from the Hugging Face Sentence Transformer models.
6985

7086
The model is specified during initialization. Here is a list of [`available models`](https://www.sbert.net/docs/sentence_transformer/pretrained_models.html).
7187

88+
::if{path="/llm-xpack/"}
7289
```python
7390
import pathway as pw
7491
from pathway.xpacks.llm import embedders
@@ -84,10 +101,18 @@ Some text to embed
84101
# Extract the embedded text
85102
t.select(ret=embedder(pw.this.txt))
86103
```
104+
::
105+
::if{path="/ai-pipelines/"}
106+
```yaml
107+
embedder: !pw.xpacks.llm.embedders.SentenceTransformerEmbedder
108+
model: "intfloat/e5-large-v2"
109+
```
110+
::
87111

88-
## GemeniEmbedder
89-
[`GemeniEmbedder`](/developers/api-docs/pathway-xpacks-llm/embedders/#pathway.xpacks.llm.embedders.GeminiEmbedder) is the embedder for Google's Gemeni Embedding Services. Available models can be found [`here`](https://ai.google.dev/gemini-api/docs/models/gemini#text-embedding-and-embedding).
112+
## GeminiEmbedder
113+
[`GeminiEmbedder`](/developers/api-docs/pathway-xpacks-llm/embedders/#pathway.xpacks.llm.embedders.GeminiEmbedder) is the embedder for Google's Gemini Embedding Services. Available models can be found [`here`](https://ai.google.dev/gemini-api/docs/models/gemini#text-embedding-and-embedding).
90114

115+
::if{path="/llm-xpack/"}
91116
```python
92117
import pathway as pw
93118
from pathway.xpacks.llm import embedders
@@ -102,3 +127,10 @@ Some text to embed
102127
103128
t.select(ret=embedder(pw.this.txt))
104129
```
130+
::
131+
::if{path="/ai-pipelines/"}
132+
```yaml
133+
embedder: !pw.xpacks.llm.embedders.GeminiEmbedder
134+
model: "models/text-embedding-004"
135+
```
136+
::

docs/2.developers/4.user-guide/50.llm-xpack/50.splitters.md docs/2.developers/4.user-guide/50.llm-xpack/.splitters/splitters.md

+25
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ A better method is to chunk the text by tokens, ensuring each chunk makes sense
1818
## TokenCountSplitter
1919
Pathway offers a [`TokenCountSplitter`](/developers/api-docs/pathway-xpacks-llm/splitters#pathway.xpacks.llm.splitters.TokenCountSplitter) for token-based chunking. Here's how to use it:
2020

21+
::if{path="/llm-xpack/"}
2122
```python
2223
from pathway.xpacks.llm.splitters import TokenCountSplitter
2324

@@ -27,6 +28,15 @@ text_splitter = TokenCountSplitter(
2728
encoding_name="cl100k_base"
2829
)
2930
```
31+
::
32+
::if{path="/ai-pipelines/"}
33+
```yaml
34+
splitter: pw.xpacks.llm.splitters.TokenCountSplitter
35+
min_tokes: 100
36+
max_tokens: 500
37+
encoding_name: "cl100k_base"
38+
```
39+
::
3040
3141
This configuration creates chunks of 100–500 tokens using the `cl100k_base` tokenizer, compatible with OpenAI's embedding models.
3242

@@ -42,6 +52,7 @@ However, the way it determines split points differs.
4252
The splitter continues this process until all chunks are smaller than `chunk_size`.
4353
Additionally, you can introduce overlapping chunks by setting the `chunk_overlap` parameter. This is particularly useful if you want to capture different contexts in your chunks. However, keep in mind that enabling overlap increases the total number of chunks retrieved, which could impact performance.
4454

55+
::if{path="/llm-xpack/"}
4556
```python
4657
splitter = RecursiveSplitter(
4758
chunk_size=400,
@@ -50,3 +61,17 @@ splitter = RecursiveSplitter(
5061
model_name="gpt-4o-mini",
5162
)
5263
```
64+
::
65+
::if{path="/ai-pipelines/"}
66+
```yaml
67+
splitter: pw.xpacks.llm.splitters.RecursiveSplitter
68+
chunk_size: 400
69+
chunk_overlap: 200
70+
separators:
71+
- "\n#
72+
- "\n##"
73+
- "\n\n"
74+
- "\n"
75+
model_name: "gpt-4o-mini"
76+
```
77+
::
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.splitters/splitters.md
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.embedders/embedders.md
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.chats/llm-chats.md
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.rerankers/rerankers.md
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../4.user-guide/50.llm-xpack/.parsers/parsers.md
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../4.user-guide/50.llm-xpack/.splitters/splitters.md
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../4.user-guide/50.llm-xpack/.embedders/embedders.md
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../4.user-guide/50.llm-xpack/.chats/llm-chats.md
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
title: 'Available Components'

docs/2.developers/6.ai-pipelines/70.parsers.md

-1
This file was deleted.

0 commit comments

Comments
 (0)