Skip to content

Commit

Permalink
Add yaml configuration to remaining llm apps (#7263)
Browse files Browse the repository at this point in the history
GitOrigin-RevId: 6120038cbad99905bc0867fdb00928c3bd3599e6
  • Loading branch information
szymondudycz authored and Manul from Pathway committed Sep 13, 2024
1 parent abce1f7 commit 7ddca2a
Show file tree
Hide file tree
Showing 25 changed files with 464 additions and 432 deletions.
5 changes: 4 additions & 1 deletion examples/pipelines/adaptive-rag/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ FROM pathwaycom/pathway:latest
WORKDIR /app

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

COPY requirements.txt .
RUN pip install -U --no-cache-dir -r requirements.txt

COPY . .

EXPOSE 8000
Expand Down
70 changes: 21 additions & 49 deletions examples/pipelines/adaptive-rag/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,71 +2,43 @@

import pathway as pw
from dotenv import load_dotenv
from pathway.udfs import DiskCache, ExponentialBackoffRetryStrategy
from pathway.xpacks.llm import embedders, llms, parsers, splitters
from pathway.xpacks.llm.question_answering import AdaptiveRAGQuestionAnswerer
from pathway.xpacks.llm.vector_store import VectorStoreServer
from pathway.xpacks.llm.question_answering import SummaryQuestionAnswerer
from pathway.xpacks.llm.servers import QASummaryRestServer
from pydantic import BaseModel, ConfigDict, InstanceOf

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

load_dotenv()

logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(name)s %(levelname)s %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)

load_dotenv()

if __name__ == "__main__":
path = "./data"

my_folder = pw.io.fs.read(
path=path,
format="binary",
with_metadata=True,
)

sources = [
my_folder
] # define the inputs (local folders, google drive, sharepoint, ...)

DEFAULT_GPT_MODEL = "gpt-3.5-turbo"

chat = llms.OpenAIChat(
model=DEFAULT_GPT_MODEL,
retry_strategy=ExponentialBackoffRetryStrategy(max_retries=6),
cache_strategy=DiskCache(),
temperature=0.0,
)

app_host = "0.0.0.0"
app_port = 8000
class App(BaseModel):
question_answerer: InstanceOf[SummaryQuestionAnswerer]
host: str = "0.0.0.0"
port: int = 8000

parser = parsers.ParseUnstructured()
text_splitter = splitters.TokenCountSplitter(max_tokens=400)
embedder = embedders.OpenAIEmbedder(cache_strategy=DiskCache())
with_cache: bool = True
terminate_on_error: bool = False

vector_server = VectorStoreServer(
*sources,
embedder=embedder,
splitter=text_splitter,
parser=parser,
)
def run(self) -> None:
server = QASummaryRestServer(self.host, self.port, self.question_answerer)
server.run(
with_cache=self.with_cache, terminate_on_error=self.terminate_on_error
)

app = AdaptiveRAGQuestionAnswerer(
llm=chat,
indexer=vector_server,
default_llm_name=DEFAULT_GPT_MODEL,
n_starting_documents=2,
factor=2,
max_iterations=4,
strict_prompt=True,
)
model_config = ConfigDict(extra="forbid")

app.build_server(host=app_host, port=app_port)

app.run_server(with_cache=True)
if __name__ == "__main__":
with open("app.yaml") as f:
config = pw.load_yaml(f)
app = App(**config)
app.run()
72 changes: 72 additions & 0 deletions examples/pipelines/adaptive-rag/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
$sources:
- !pw.io.fs.read
path: data
format: binary
with_metadata: true

# - !pw.xpacks.connectors.sharepoint.read
# url: $SHAREPOINT_URL
# tenant: $SHAREPOINT_TENANT
# client_id: $SHAREPOINT_CLIENT_ID
# cert_path: sharepointcert.pem
# thumbprint: $SHAREPOINT_THUMBPRINT
# root_path: $SHAREPOINT_ROOT
# with_metadata: true
# refresh_interval: 30

# - !pw.io.gdrive.read
# object_id: $DRIVE_ID
# service_user_credentials_file: gdrive_indexer.json
# name_pattern:
# - "*.pdf"
# - "*.pptx"
# object_size_limit: null
# with_metadata: true
# refresh_interval: 30

$llm: !pw.xpacks.llm.llms.OpenAIChat
model: "gpt-3.5-turbo"
retry_strategy: !pw.udfs.ExponentialBackoffRetryStrategy
max_retries: 6
cache_strategy: !pw.udfs.DiskCache
temperature: 0.05
capacity: 8

$embedder: !pw.xpacks.llm.embedders.OpenAIEmbedder
model: "text-embedding-ada-002"
cache_strategy: !pw.udfs.DiskCache

$splitter: !pw.xpacks.llm.splitters.TokenCountSplitter
max_tokens: 400

$parser: !pw.xpacks.llm.parsers.ParseUnstructured

$retriever_factory: !pw.stdlib.indexing.BruteForceKnnFactory
reserved_space: 1000
embedder: $embedder
metric: !pw.internals.yaml_loader.import_object
path: pw.stdlib.indexing.BruteForceKnnMetricKind.COS
dimensions: 1536


$document_store: !pw.xpacks.llm.document_store.DocumentStore
docs: $sources
parser: $parser
splitter: $splitter
retriever_factory: $retriever_factory

question_answerer: !pw.xpacks.llm.question_answering.AdaptiveRAGQuestionAnswerer
llm: $llm
indexer: $document_store
n_starting_documents: 2
factor: 2
max_iterations: 4
strict_prompt: true


# Change host and port by uncommenting these files
# host: "0.0.0.0"
# port: 8000

# with_cache: true
# terminate_on_error: false
1 change: 1 addition & 0 deletions examples/pipelines/adaptive-rag/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python-dotenv==1.0.1
1 change: 0 additions & 1 deletion examples/pipelines/demo-document-indexing/.env

This file was deleted.

7 changes: 3 additions & 4 deletions examples/pipelines/demo-document-indexing/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ FROM pathwaycom/pathway:latest
WORKDIR /app

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

COPY requirements.txt .

RUN pip install --pre -U --no-cache-dir -r requirements.txt
RUN pip install -U --no-cache-dir -r requirements.txt

COPY . .

EXPOSE 8000

CMD ["python", "main.py"]
CMD ["python", "app.py"]
44 changes: 44 additions & 0 deletions examples/pipelines/demo-document-indexing/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import logging

import pathway as pw
from dotenv import load_dotenv
from pathway.xpacks.llm.document_store import DocumentStore
from pathway.xpacks.llm.servers import DocumentStoreServer
from pydantic import BaseModel, ConfigDict, InstanceOf

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

logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(name)s %(levelname)s %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)

load_dotenv()


class App(BaseModel):
document_store: InstanceOf[DocumentStore]
host: str = "0.0.0.0"
port: int = 8000

with_cache: bool = True
terminate_on_error: bool = False

def run(self) -> None:
server = DocumentStoreServer(self.host, self.port, self.document_store)
server.run(
with_cache=self.with_cache, terminate_on_error=self.terminate_on_error
)

model_config = ConfigDict(extra="forbid")


if __name__ == "__main__":
with open("app.yaml") as f:
config = pw.load_yaml(f)
app = App(**config)
app.run()
67 changes: 67 additions & 0 deletions examples/pipelines/demo-document-indexing/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
$sources:
- !pw.io.fs.read
path: files-for-indexing
format: binary
with_metadata: true

# - !pw.xpacks.connectors.sharepoint.read
# url: $SHAREPOINT_URL
# tenant: $SHAREPOINT_TENANT
# client_id: $SHAREPOINT_CLIENT_ID
# cert_path: sharepointcert.pem
# thumbprint: $SHAREPOINT_THUMBPRINT
# root_path: $SHAREPOINT_ROOT
# with_metadata: true
# refresh_interval: 30

# - !pw.io.gdrive.read
# object_id: $DRIVE_ID
# service_user_credentials_file: gdrive_indexer.json
# name_pattern:
# - "*.pdf"
# - "*.pptx"
# object_size_limit: null
# with_metadata: true
# refresh_interval: 30

$llm: !pw.xpacks.llm.llms.OpenAIChat
model: "gpt-3.5-turbo"
retry_strategy: !pw.udfs.ExponentialBackoffRetryStrategy
max_retries: 6
cache_strategy: !pw.udfs.DiskCache
temperature: 0.05
capacity: 8

$embedding_model: "mixedbread-ai/mxbai-embed-large-v1"

$embedder: !pw.xpacks.llm.embedders.SentenceTransformerEmbedder
model: $embedding_model
call_kwargs:
show_progress_bar: False

$splitter: !pw.xpacks.llm.splitters.TokenCountSplitter
max_tokens: 400

$parser: !pw.xpacks.llm.parsers.ParseUnstructured

$retriever_factory: !pw.stdlib.indexing.BruteForceKnnFactory
reserved_space: 1000
embedder: $embedder
metric: !pw.internals.yaml_loader.import_object
path: pw.stdlib.indexing.BruteForceKnnMetricKind.COS
dimensions: 1536


document_store: !pw.xpacks.llm.document_store.DocumentStore
docs: $sources
parser: $parser
splitter: $splitter
retriever_factory: $retriever_factory


# Change host and port by uncommenting these files
# host: "0.0.0.0"
# port: 8000

# with_cache: true
# terminate_on_error: false
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ services:
build:
context: .
ports:
- "8001:8000"
- "8000:8000"
environment:
OPENAI_API_KEY: "${OPENAI_API_KEY}"
volumes:
Expand Down
Loading

0 comments on commit 7ddca2a

Please sign in to comment.