|
1 | 1 | import logging
|
2 |
| -import sys |
3 | 2 |
|
4 |
| -import click |
5 | 3 | import pathway as pw
|
6 |
| -import yaml |
7 | 4 | from dotenv import load_dotenv
|
8 |
| -from pathway.udfs import DiskCache, ExponentialBackoffRetryStrategy |
9 |
| -from pathway.xpacks.llm import embedders, llms, parsers, splitters |
| 5 | +from pathway.xpacks import llm |
10 | 6 | from pathway.xpacks.llm.question_answering import BaseRAGQuestionAnswerer
|
11 | 7 | from pathway.xpacks.llm.vector_store import VectorStoreServer
|
| 8 | +from pydantic import BaseModel, ConfigDict, InstanceOf |
| 9 | +from typing_extensions import TypedDict |
12 | 10 |
|
13 | 11 | # To use advanced features with Pathway Scale, get your free license key from
|
14 | 12 | # https://pathway.com/features and paste it below.
|
|
23 | 21 |
|
24 | 22 | load_dotenv()
|
25 | 23 |
|
| 24 | +host_config = TypedDict("host_config", {"host": str, "port": int}) |
26 | 25 |
|
27 |
| -def data_sources(source_configs) -> list[pw.Table]: |
28 |
| - sources = [] |
29 |
| - for source_config in source_configs: |
30 |
| - if source_config["kind"] == "local": |
31 |
| - source = pw.io.fs.read( |
32 |
| - **source_config["config"], |
33 |
| - format="binary", |
34 |
| - with_metadata=True, |
35 |
| - ) |
36 |
| - sources.append(source) |
37 |
| - elif source_config["kind"] == "gdrive": |
38 |
| - source = pw.io.gdrive.read( |
39 |
| - **source_config["config"], |
40 |
| - with_metadata=True, |
41 |
| - ) |
42 |
| - sources.append(source) |
43 |
| - elif source_config["kind"] == "sharepoint": |
44 |
| - try: |
45 |
| - import pathway.xpacks.connectors.sharepoint as io_sp |
46 |
| - |
47 |
| - source = io_sp.read(**source_config["config"], with_metadata=True) |
48 |
| - sources.append(source) |
49 |
| - except ImportError: |
50 |
| - print( |
51 |
| - "The Pathway Sharepoint connector is part of the commercial offering, " |
52 |
| - "please contact us for a commercial license." |
53 |
| - ) |
54 |
| - sys.exit(1) |
55 |
| - |
56 |
| - return sources |
57 |
| - |
58 |
| - |
59 |
| -@click.command() |
60 |
| -@click.option("--config_file", default="config.yaml", help="Config file to be used.") |
61 |
| -def run( |
62 |
| - config_file: str = "config.yaml", |
63 |
| -): |
64 |
| - with open(config_file) as config_f: |
65 |
| - configuration = yaml.safe_load(config_f) |
66 |
| - |
67 |
| - GPT_MODEL = configuration["llm_config"]["model"] |
68 |
| - |
69 |
| - embedder = embedders.OpenAIEmbedder( |
70 |
| - model="text-embedding-ada-002", |
71 |
| - cache_strategy=DiskCache(), |
72 |
| - ) |
73 |
| - |
74 |
| - chat = llms.OpenAIChat( |
75 |
| - model=GPT_MODEL, |
76 |
| - retry_strategy=ExponentialBackoffRetryStrategy(max_retries=6), |
77 |
| - cache_strategy=DiskCache(), |
78 |
| - temperature=0.05, |
79 |
| - ) |
80 |
| - |
81 |
| - host_config = configuration["host_config"] |
82 |
| - host, port = host_config["host"], host_config["port"] |
83 |
| - |
84 |
| - doc_store = VectorStoreServer( |
85 |
| - *data_sources(configuration["sources"]), |
86 |
| - embedder=embedder, |
87 |
| - splitter=splitters.TokenCountSplitter(max_tokens=400), |
88 |
| - parser=parsers.ParseUnstructured(), |
89 |
| - ) |
90 |
| - |
91 |
| - rag_app = BaseRAGQuestionAnswerer(llm=chat, indexer=doc_store) |
92 |
| - |
93 |
| - rag_app.build_server(host=host, port=port) |
94 |
| - |
95 |
| - rag_app.run_server(with_cache=True, terminate_on_error=False) |
| 26 | + |
| 27 | +class App(BaseModel): |
| 28 | + llm: InstanceOf[pw.UDF] |
| 29 | + embedder: InstanceOf[llm.embedders.BaseEmbedder] |
| 30 | + splitter: InstanceOf[pw.UDF] |
| 31 | + parser: InstanceOf[pw.UDF] |
| 32 | + |
| 33 | + sources: list[InstanceOf[pw.Table]] |
| 34 | + |
| 35 | + host_config: host_config |
| 36 | + |
| 37 | + def run(self, config_file: str = "config.yaml") -> None: |
| 38 | + # Unpack host and port from config |
| 39 | + host, port = self.host_config["host"], self.host_config["port"] |
| 40 | + |
| 41 | + doc_store = VectorStoreServer( |
| 42 | + *self.sources, |
| 43 | + embedder=self.embedder, |
| 44 | + splitter=self.splitter, |
| 45 | + parser=self.parser, |
| 46 | + ) |
| 47 | + |
| 48 | + rag_app = BaseRAGQuestionAnswerer(llm=self.llm, indexer=doc_store) |
| 49 | + |
| 50 | + rag_app.build_server(host=host, port=port) |
| 51 | + |
| 52 | + rag_app.run_server(with_cache=True, terminate_on_error=False) |
| 53 | + |
| 54 | + model_config = ConfigDict(extra="forbid") |
96 | 55 |
|
97 | 56 |
|
98 | 57 | if __name__ == "__main__":
|
99 |
| - run() |
| 58 | + with open("config.yaml") as f: |
| 59 | + config = pw.load_yaml(f) |
| 60 | + app = App(**config) |
| 61 | + app.run() |
0 commit comments