Skip to content

Commit 7ddca2a

Browse files
szymondudyczManul from Pathway
authored andcommitted
Add yaml configuration to remaining llm apps (#7263)
GitOrigin-RevId: 6120038cbad99905bc0867fdb00928c3bd3599e6
1 parent abce1f7 commit 7ddca2a

File tree

25 files changed

+464
-432
lines changed

25 files changed

+464
-432
lines changed

examples/pipelines/adaptive-rag/Dockerfile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ FROM pathwaycom/pathway:latest
33
WORKDIR /app
44

55
RUN apt-get update \
6-
&& apt-get install -y python3-opencv \
6+
&& apt-get install -y python3-opencv tesseract-ocr-eng \
77
&& rm -rf /var/lib/apt/lists/* /var/cache/apt/archives/*
88

9+
COPY requirements.txt .
10+
RUN pip install -U --no-cache-dir -r requirements.txt
11+
912
COPY . .
1013

1114
EXPOSE 8000

examples/pipelines/adaptive-rag/app.py

Lines changed: 21 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,71 +2,43 @@
22

33
import pathway as pw
44
from dotenv import load_dotenv
5-
from pathway.udfs import DiskCache, ExponentialBackoffRetryStrategy
6-
from pathway.xpacks.llm import embedders, llms, parsers, splitters
7-
from pathway.xpacks.llm.question_answering import AdaptiveRAGQuestionAnswerer
8-
from pathway.xpacks.llm.vector_store import VectorStoreServer
5+
from pathway.xpacks.llm.question_answering import SummaryQuestionAnswerer
6+
from pathway.xpacks.llm.servers import QASummaryRestServer
7+
from pydantic import BaseModel, ConfigDict, InstanceOf
98

109
# To use advanced features with Pathway Scale, get your free license key from
1110
# https://pathway.com/features and paste it below.
1211
# To use Pathway Community, comment out the line below.
1312
pw.set_license_key("demo-license-key-with-telemetry")
1413

15-
load_dotenv()
16-
1714
logging.basicConfig(
1815
level=logging.INFO,
1916
format="%(asctime)s %(name)s %(levelname)s %(message)s",
2017
datefmt="%Y-%m-%d %H:%M:%S",
2118
)
2219

20+
load_dotenv()
2321

24-
if __name__ == "__main__":
25-
path = "./data"
26-
27-
my_folder = pw.io.fs.read(
28-
path=path,
29-
format="binary",
30-
with_metadata=True,
31-
)
32-
33-
sources = [
34-
my_folder
35-
] # define the inputs (local folders, google drive, sharepoint, ...)
36-
37-
DEFAULT_GPT_MODEL = "gpt-3.5-turbo"
38-
39-
chat = llms.OpenAIChat(
40-
model=DEFAULT_GPT_MODEL,
41-
retry_strategy=ExponentialBackoffRetryStrategy(max_retries=6),
42-
cache_strategy=DiskCache(),
43-
temperature=0.0,
44-
)
4522

46-
app_host = "0.0.0.0"
47-
app_port = 8000
23+
class App(BaseModel):
24+
question_answerer: InstanceOf[SummaryQuestionAnswerer]
25+
host: str = "0.0.0.0"
26+
port: int = 8000
4827

49-
parser = parsers.ParseUnstructured()
50-
text_splitter = splitters.TokenCountSplitter(max_tokens=400)
51-
embedder = embedders.OpenAIEmbedder(cache_strategy=DiskCache())
28+
with_cache: bool = True
29+
terminate_on_error: bool = False
5230

53-
vector_server = VectorStoreServer(
54-
*sources,
55-
embedder=embedder,
56-
splitter=text_splitter,
57-
parser=parser,
58-
)
31+
def run(self) -> None:
32+
server = QASummaryRestServer(self.host, self.port, self.question_answerer)
33+
server.run(
34+
with_cache=self.with_cache, terminate_on_error=self.terminate_on_error
35+
)
5936

60-
app = AdaptiveRAGQuestionAnswerer(
61-
llm=chat,
62-
indexer=vector_server,
63-
default_llm_name=DEFAULT_GPT_MODEL,
64-
n_starting_documents=2,
65-
factor=2,
66-
max_iterations=4,
67-
strict_prompt=True,
68-
)
37+
model_config = ConfigDict(extra="forbid")
6938

70-
app.build_server(host=app_host, port=app_port)
7139

72-
app.run_server(with_cache=True)
40+
if __name__ == "__main__":
41+
with open("app.yaml") as f:
42+
config = pw.load_yaml(f)
43+
app = App(**config)
44+
app.run()
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
$sources:
2+
- !pw.io.fs.read
3+
path: data
4+
format: binary
5+
with_metadata: true
6+
7+
# - !pw.xpacks.connectors.sharepoint.read
8+
# url: $SHAREPOINT_URL
9+
# tenant: $SHAREPOINT_TENANT
10+
# client_id: $SHAREPOINT_CLIENT_ID
11+
# cert_path: sharepointcert.pem
12+
# thumbprint: $SHAREPOINT_THUMBPRINT
13+
# root_path: $SHAREPOINT_ROOT
14+
# with_metadata: true
15+
# refresh_interval: 30
16+
17+
# - !pw.io.gdrive.read
18+
# object_id: $DRIVE_ID
19+
# service_user_credentials_file: gdrive_indexer.json
20+
# name_pattern:
21+
# - "*.pdf"
22+
# - "*.pptx"
23+
# object_size_limit: null
24+
# with_metadata: true
25+
# refresh_interval: 30
26+
27+
$llm: !pw.xpacks.llm.llms.OpenAIChat
28+
model: "gpt-3.5-turbo"
29+
retry_strategy: !pw.udfs.ExponentialBackoffRetryStrategy
30+
max_retries: 6
31+
cache_strategy: !pw.udfs.DiskCache
32+
temperature: 0.05
33+
capacity: 8
34+
35+
$embedder: !pw.xpacks.llm.embedders.OpenAIEmbedder
36+
model: "text-embedding-ada-002"
37+
cache_strategy: !pw.udfs.DiskCache
38+
39+
$splitter: !pw.xpacks.llm.splitters.TokenCountSplitter
40+
max_tokens: 400
41+
42+
$parser: !pw.xpacks.llm.parsers.ParseUnstructured
43+
44+
$retriever_factory: !pw.stdlib.indexing.BruteForceKnnFactory
45+
reserved_space: 1000
46+
embedder: $embedder
47+
metric: !pw.internals.yaml_loader.import_object
48+
path: pw.stdlib.indexing.BruteForceKnnMetricKind.COS
49+
dimensions: 1536
50+
51+
52+
$document_store: !pw.xpacks.llm.document_store.DocumentStore
53+
docs: $sources
54+
parser: $parser
55+
splitter: $splitter
56+
retriever_factory: $retriever_factory
57+
58+
question_answerer: !pw.xpacks.llm.question_answering.AdaptiveRAGQuestionAnswerer
59+
llm: $llm
60+
indexer: $document_store
61+
n_starting_documents: 2
62+
factor: 2
63+
max_iterations: 4
64+
strict_prompt: true
65+
66+
67+
# Change host and port by uncommenting these files
68+
# host: "0.0.0.0"
69+
# port: 8000
70+
71+
# with_cache: true
72+
# terminate_on_error: false
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python-dotenv==1.0.1

examples/pipelines/demo-document-indexing/.env

Lines changed: 0 additions & 1 deletion
This file was deleted.

examples/pipelines/demo-document-indexing/Dockerfile

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@ FROM pathwaycom/pathway:latest
33
WORKDIR /app
44

55
RUN apt-get update \
6-
&& apt-get install -y python3-opencv \
6+
&& apt-get install -y python3-opencv tesseract-ocr-eng \
77
&& rm -rf /var/lib/apt/lists/* /var/cache/apt/archives/*
88

99
COPY requirements.txt .
10-
11-
RUN pip install --pre -U --no-cache-dir -r requirements.txt
10+
RUN pip install -U --no-cache-dir -r requirements.txt
1211

1312
COPY . .
1413

1514
EXPOSE 8000
1615

17-
CMD ["python", "main.py"]
16+
CMD ["python", "app.py"]
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import logging
2+
3+
import pathway as pw
4+
from dotenv import load_dotenv
5+
from pathway.xpacks.llm.document_store import DocumentStore
6+
from pathway.xpacks.llm.servers import DocumentStoreServer
7+
from pydantic import BaseModel, ConfigDict, InstanceOf
8+
9+
# To use advanced features with Pathway Scale, get your free license key from
10+
# https://pathway.com/features and paste it below.
11+
# To use Pathway Community, comment out the line below.
12+
pw.set_license_key("demo-license-key-with-telemetry")
13+
14+
logging.basicConfig(
15+
level=logging.INFO,
16+
format="%(asctime)s %(name)s %(levelname)s %(message)s",
17+
datefmt="%Y-%m-%d %H:%M:%S",
18+
)
19+
20+
load_dotenv()
21+
22+
23+
class App(BaseModel):
24+
document_store: InstanceOf[DocumentStore]
25+
host: str = "0.0.0.0"
26+
port: int = 8000
27+
28+
with_cache: bool = True
29+
terminate_on_error: bool = False
30+
31+
def run(self) -> None:
32+
server = DocumentStoreServer(self.host, self.port, self.document_store)
33+
server.run(
34+
with_cache=self.with_cache, terminate_on_error=self.terminate_on_error
35+
)
36+
37+
model_config = ConfigDict(extra="forbid")
38+
39+
40+
if __name__ == "__main__":
41+
with open("app.yaml") as f:
42+
config = pw.load_yaml(f)
43+
app = App(**config)
44+
app.run()
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
$sources:
2+
- !pw.io.fs.read
3+
path: files-for-indexing
4+
format: binary
5+
with_metadata: true
6+
7+
# - !pw.xpacks.connectors.sharepoint.read
8+
# url: $SHAREPOINT_URL
9+
# tenant: $SHAREPOINT_TENANT
10+
# client_id: $SHAREPOINT_CLIENT_ID
11+
# cert_path: sharepointcert.pem
12+
# thumbprint: $SHAREPOINT_THUMBPRINT
13+
# root_path: $SHAREPOINT_ROOT
14+
# with_metadata: true
15+
# refresh_interval: 30
16+
17+
# - !pw.io.gdrive.read
18+
# object_id: $DRIVE_ID
19+
# service_user_credentials_file: gdrive_indexer.json
20+
# name_pattern:
21+
# - "*.pdf"
22+
# - "*.pptx"
23+
# object_size_limit: null
24+
# with_metadata: true
25+
# refresh_interval: 30
26+
27+
$llm: !pw.xpacks.llm.llms.OpenAIChat
28+
model: "gpt-3.5-turbo"
29+
retry_strategy: !pw.udfs.ExponentialBackoffRetryStrategy
30+
max_retries: 6
31+
cache_strategy: !pw.udfs.DiskCache
32+
temperature: 0.05
33+
capacity: 8
34+
35+
$embedding_model: "mixedbread-ai/mxbai-embed-large-v1"
36+
37+
$embedder: !pw.xpacks.llm.embedders.SentenceTransformerEmbedder
38+
model: $embedding_model
39+
call_kwargs:
40+
show_progress_bar: False
41+
42+
$splitter: !pw.xpacks.llm.splitters.TokenCountSplitter
43+
max_tokens: 400
44+
45+
$parser: !pw.xpacks.llm.parsers.ParseUnstructured
46+
47+
$retriever_factory: !pw.stdlib.indexing.BruteForceKnnFactory
48+
reserved_space: 1000
49+
embedder: $embedder
50+
metric: !pw.internals.yaml_loader.import_object
51+
path: pw.stdlib.indexing.BruteForceKnnMetricKind.COS
52+
dimensions: 1536
53+
54+
55+
document_store: !pw.xpacks.llm.document_store.DocumentStore
56+
docs: $sources
57+
parser: $parser
58+
splitter: $splitter
59+
retriever_factory: $retriever_factory
60+
61+
62+
# Change host and port by uncommenting these files
63+
# host: "0.0.0.0"
64+
# port: 8000
65+
66+
# with_cache: true
67+
# terminate_on_error: false

examples/pipelines/demo-document-indexing/docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ services:
44
build:
55
context: .
66
ports:
7-
- "8001:8000"
7+
- "8000:8000"
88
environment:
99
OPENAI_API_KEY: "${OPENAI_API_KEY}"
1010
volumes:

0 commit comments

Comments
 (0)