|
2 | 2 |
|
3 | 3 | import pathway as pw
|
4 | 4 | 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 |
9 | 8 |
|
10 | 9 | # To use advanced features with Pathway Scale, get your free license key from
|
11 | 10 | # https://pathway.com/features and paste it below.
|
12 | 11 | # To use Pathway Community, comment out the line below.
|
13 | 12 | pw.set_license_key("demo-license-key-with-telemetry")
|
14 | 13 |
|
15 |
| -load_dotenv() |
16 |
| - |
17 | 14 | logging.basicConfig(
|
18 | 15 | level=logging.INFO,
|
19 | 16 | format="%(asctime)s %(name)s %(levelname)s %(message)s",
|
20 | 17 | datefmt="%Y-%m-%d %H:%M:%S",
|
21 | 18 | )
|
22 | 19 |
|
| 20 | +load_dotenv() |
23 | 21 |
|
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 |
| - ) |
45 | 22 |
|
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 |
48 | 27 |
|
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 |
52 | 30 |
|
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 | + ) |
59 | 36 |
|
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") |
69 | 38 |
|
70 |
| - app.build_server(host=app_host, port=app_port) |
71 | 39 |
|
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() |
0 commit comments