From 04168aba0e151e431b4a5a359e9c8eed475d4894 Mon Sep 17 00:00:00 2001 From: Arjun Verma Date: Wed, 1 May 2024 12:35:41 +0530 Subject: [PATCH] #373 Include documentation_helpers in module (#395) Co-authored-by: Philip Meier --- .gitignore | 1 + docs/assets/ragna.txt | 8 ------ docs/examples/gallery_streaming.py | 23 ++++++++------- docs/tutorials/gallery_python_api.py | 22 ++++++--------- docs/tutorials/gallery_rest_api.py | 28 ++++++++----------- .../_docs.py | 23 +++++++++++---- 6 files changed, 49 insertions(+), 56 deletions(-) delete mode 100644 docs/assets/ragna.txt rename docs/documentation_helpers.py => ragna/_docs.py (83%) diff --git a/.gitignore b/.gitignore index d0833ebd..d0e72889 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ ragna.toml ragna/_version.py +docs/**/ragna.txt # Byte-compiled / optimized / DLL files __pycache__/ diff --git a/docs/assets/ragna.txt b/docs/assets/ragna.txt deleted file mode 100644 index fa8a6b23..00000000 --- a/docs/assets/ragna.txt +++ /dev/null @@ -1,8 +0,0 @@ -Ragna is an open source project built by Quansight. It is designed to allow -organizations to explore the power of Retrieval-augmented generation (RAG) based -AI tools. Ragna provides an intuitive API for quick experimentation and built-in -tools for creating production-ready applications allowing you to quickly leverage -Large Language Models (LLMs) for your work. - -The Ragna website is https://ragna.chat/. The source code is available at -https://github.com/Quansight/ragna under the BSD 3-Clause license. diff --git a/docs/examples/gallery_streaming.py b/docs/examples/gallery_streaming.py index 87c51f9e..ca46d884 100644 --- a/docs/examples/gallery_streaming.py +++ b/docs/examples/gallery_streaming.py @@ -5,16 +5,6 @@ is performed using the Python and REST API. """ -# %% -# Before we start this example, we import some helpers. - -import sys -from pathlib import Path - -sys.path.insert(0, str(Path.cwd().parent)) - -import documentation_helpers - # %% # ## Setup streaming assistant # @@ -55,9 +45,18 @@ def answer(self, prompt, sources): # # Let's create and prepare a chat using the assistant we have defined above. +from pathlib import Path + +import ragna._docs as ragna_docs + from ragna import Rag, source_storages -document_path = documentation_helpers.assets / "ragna.txt" +print(ragna_docs.SAMPLE_CONTENT) + +document_path = Path.cwd() / "ragna.txt" + +with open(document_path, "w") as file: + file.write(ragna_docs.SAMPLE_CONTENT) chat = Rag().chat( documents=[document_path], @@ -98,7 +97,7 @@ def answer(self, prompt, sources): config = Config(assistants=[DemoStreamingAssistant]) -rest_api = documentation_helpers.RestApi() +rest_api = ragna_docs.RestApi() client = rest_api.start(config, authenticate=True) diff --git a/docs/tutorials/gallery_python_api.py b/docs/tutorials/gallery_python_api.py index 68635131..d1919bd3 100644 --- a/docs/tutorials/gallery_python_api.py +++ b/docs/tutorials/gallery_python_api.py @@ -8,16 +8,6 @@ This tutorial walks you through basic steps of using Ragnas Python API. """ -# %% -# Before we start this tutorial, we import some helpers. - -import sys -from pathlib import Path - -sys.path.insert(0, str(Path.cwd().parent)) - -import documentation_helpers - # %% # ## Step 1: Select relevant documents # @@ -25,10 +15,16 @@ # will be answered comes from documents that you provide. For this tutorial, let's use a # sample document that includes some information about Ragna. -document_path = documentation_helpers.assets / "ragna.txt" +from pathlib import Path + +import ragna._docs as ragna_docs + +print(ragna_docs.SAMPLE_CONTENT) + +document_path = Path.cwd() / "ragna.txt" -with open(document_path) as file: - print(file.read()) +with open(document_path, "w") as file: + file.write(ragna_docs.SAMPLE_CONTENT) # %% # !!! tip diff --git a/docs/tutorials/gallery_rest_api.py b/docs/tutorials/gallery_rest_api.py index 37f8bf4f..9d57d0b4 100644 --- a/docs/tutorials/gallery_rest_api.py +++ b/docs/tutorials/gallery_rest_api.py @@ -8,16 +8,6 @@ This tutorial walks you through basic steps of using Ragnas REST API. """ -# %% -# Before we start this tutorial, we import some helpers. - -import sys -from pathlib import Path - -sys.path.insert(0, str(Path.cwd().parent)) - -import documentation_helpers - # %% # ## Step 1: Start the REST API # @@ -43,11 +33,13 @@ # be using for this tutorial is equivalent of picking the first option the wizard # offers you, i.e. using only demo components. +import ragna._docs as ragna_docs + from ragna.deploy import Config config = Config() -rest_api = documentation_helpers.RestApi() +rest_api = ragna_docs.RestApi() _ = rest_api.start(config) # %% @@ -98,12 +90,14 @@ # %% # For simplicity, let's use a demo document with some information about Ragna -document_name = "ragna.txt" +from pathlib import Path + +print(ragna_docs.SAMPLE_CONTENT) -with open(documentation_helpers.assets / document_name, "rb") as file: - content = file.read() +document_path = Path.cwd() / "ragna.txt" -print(content.decode()) +with open(document_path, "w") as file: + file.write(ragna_docs.SAMPLE_CONTENT) # %% # The upload process in Ragna consists of two parts: @@ -116,7 +110,7 @@ # 2. Perform the actual upload with the information from step 1. response = client.post( - "/document", json={"name": document_name} + "/document", json={"name": document_path.name} ).raise_for_status() document_upload = response.json() print(json.dumps(response.json(), indent=2)) @@ -138,7 +132,7 @@ parameters["method"], parameters["url"], data=parameters["data"], - files={"file": content}, + files={"file": open(document_path, "rb")}, ).raise_for_status() # %% diff --git a/docs/documentation_helpers.py b/ragna/_docs.py similarity index 83% rename from docs/documentation_helpers.py rename to ragna/_docs.py index 9fde4d36..53f3993f 100644 --- a/docs/documentation_helpers.py +++ b/ragna/_docs.py @@ -11,13 +11,22 @@ from ragna._utils import timeout_after from ragna.deploy import Config -__all__ = ["assets", "RestApi"] +__all__ = ["SAMPLE_CONTENT", "RestApi"] -assets = Path(__file__).parent / "assets" +SAMPLE_CONTENT = """\ +Ragna is an open source project built by Quansight. It is designed to allow +organizations to explore the power of Retrieval-augmented generation (RAG) based +AI tools. Ragna provides an intuitive API for quick experimentation and built-in +tools for creating production-ready applications allowing you to quickly leverage +Large Language Models (LLMs) for your work. + +The Ragna website is https://ragna.chat/. The source code is available at +https://github.com/Quansight/ragna under the BSD 3-Clause license. +""" class RestApi: - def __init__(self): + def __init__(self) -> None: self._process: Optional[subprocess.Popen] = None def start(self, config: Config, *, authenticate: bool = False) -> httpx.Client: @@ -115,12 +124,14 @@ def _authenticate(self, client: httpx.Client) -> None: client.headers["Authorization"] = f"Bearer {token}" def stop(self, *, quiet: bool = False) -> None: + if self._process is None: + return + self._process.kill() stdout, _ = self._process.communicate() if not quiet: print(stdout.decode()) - def __del__(self): - if self._process is not None: - self.stop(quiet=True) + def __del__(self) -> None: + self.stop(quiet=True)