Skip to content

Commit 04168ab

Browse files
arjxn-pypmeier
andauthored
#373 Include documentation_helpers in module (#395)
Co-authored-by: Philip Meier <github.pmeier@posteo.de>
1 parent f947f2d commit 04168ab

File tree

6 files changed

+49
-56
lines changed

6 files changed

+49
-56
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
ragna.toml
22
ragna/_version.py
3+
docs/**/ragna.txt
34

45
# Byte-compiled / optimized / DLL files
56
__pycache__/

docs/assets/ragna.txt

Lines changed: 0 additions & 8 deletions
This file was deleted.

docs/examples/gallery_streaming.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,6 @@
55
is performed using the Python and REST API.
66
"""
77

8-
# %%
9-
# Before we start this example, we import some helpers.
10-
11-
import sys
12-
from pathlib import Path
13-
14-
sys.path.insert(0, str(Path.cwd().parent))
15-
16-
import documentation_helpers
17-
188
# %%
199
# ## Setup streaming assistant
2010
#
@@ -55,9 +45,18 @@ def answer(self, prompt, sources):
5545
#
5646
# Let's create and prepare a chat using the assistant we have defined above.
5747

48+
from pathlib import Path
49+
50+
import ragna._docs as ragna_docs
51+
5852
from ragna import Rag, source_storages
5953

60-
document_path = documentation_helpers.assets / "ragna.txt"
54+
print(ragna_docs.SAMPLE_CONTENT)
55+
56+
document_path = Path.cwd() / "ragna.txt"
57+
58+
with open(document_path, "w") as file:
59+
file.write(ragna_docs.SAMPLE_CONTENT)
6160

6261
chat = Rag().chat(
6362
documents=[document_path],
@@ -98,7 +97,7 @@ def answer(self, prompt, sources):
9897

9998
config = Config(assistants=[DemoStreamingAssistant])
10099

101-
rest_api = documentation_helpers.RestApi()
100+
rest_api = ragna_docs.RestApi()
102101

103102
client = rest_api.start(config, authenticate=True)
104103

docs/tutorials/gallery_python_api.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,23 @@
88
This tutorial walks you through basic steps of using Ragnas Python API.
99
"""
1010

11-
# %%
12-
# Before we start this tutorial, we import some helpers.
13-
14-
import sys
15-
from pathlib import Path
16-
17-
sys.path.insert(0, str(Path.cwd().parent))
18-
19-
import documentation_helpers
20-
2111
# %%
2212
# ## Step 1: Select relevant documents
2313
#
2414
# Ragna uses the RAG technique to answer questions. The context in which the questions
2515
# will be answered comes from documents that you provide. For this tutorial, let's use a
2616
# sample document that includes some information about Ragna.
2717

28-
document_path = documentation_helpers.assets / "ragna.txt"
18+
from pathlib import Path
19+
20+
import ragna._docs as ragna_docs
21+
22+
print(ragna_docs.SAMPLE_CONTENT)
23+
24+
document_path = Path.cwd() / "ragna.txt"
2925

30-
with open(document_path) as file:
31-
print(file.read())
26+
with open(document_path, "w") as file:
27+
file.write(ragna_docs.SAMPLE_CONTENT)
3228

3329
# %%
3430
# !!! tip

docs/tutorials/gallery_rest_api.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,6 @@
88
This tutorial walks you through basic steps of using Ragnas REST API.
99
"""
1010

11-
# %%
12-
# Before we start this tutorial, we import some helpers.
13-
14-
import sys
15-
from pathlib import Path
16-
17-
sys.path.insert(0, str(Path.cwd().parent))
18-
19-
import documentation_helpers
20-
2111
# %%
2212
# ## Step 1: Start the REST API
2313
#
@@ -43,11 +33,13 @@
4333
# be using for this tutorial is equivalent of picking the first option the wizard
4434
# offers you, i.e. using only demo components.
4535

36+
import ragna._docs as ragna_docs
37+
4638
from ragna.deploy import Config
4739

4840
config = Config()
4941

50-
rest_api = documentation_helpers.RestApi()
42+
rest_api = ragna_docs.RestApi()
5143
_ = rest_api.start(config)
5244

5345
# %%
@@ -98,12 +90,14 @@
9890
# %%
9991
# For simplicity, let's use a demo document with some information about Ragna
10092

101-
document_name = "ragna.txt"
93+
from pathlib import Path
94+
95+
print(ragna_docs.SAMPLE_CONTENT)
10296

103-
with open(documentation_helpers.assets / document_name, "rb") as file:
104-
content = file.read()
97+
document_path = Path.cwd() / "ragna.txt"
10598

106-
print(content.decode())
99+
with open(document_path, "w") as file:
100+
file.write(ragna_docs.SAMPLE_CONTENT)
107101

108102
# %%
109103
# The upload process in Ragna consists of two parts:
@@ -116,7 +110,7 @@
116110
# 2. Perform the actual upload with the information from step 1.
117111

118112
response = client.post(
119-
"/document", json={"name": document_name}
113+
"/document", json={"name": document_path.name}
120114
).raise_for_status()
121115
document_upload = response.json()
122116
print(json.dumps(response.json(), indent=2))
@@ -138,7 +132,7 @@
138132
parameters["method"],
139133
parameters["url"],
140134
data=parameters["data"],
141-
files={"file": content},
135+
files={"file": open(document_path, "rb")},
142136
).raise_for_status()
143137

144138
# %%

docs/documentation_helpers.py renamed to ragna/_docs.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,22 @@
1111
from ragna._utils import timeout_after
1212
from ragna.deploy import Config
1313

14-
__all__ = ["assets", "RestApi"]
14+
__all__ = ["SAMPLE_CONTENT", "RestApi"]
1515

16-
assets = Path(__file__).parent / "assets"
16+
SAMPLE_CONTENT = """\
17+
Ragna is an open source project built by Quansight. It is designed to allow
18+
organizations to explore the power of Retrieval-augmented generation (RAG) based
19+
AI tools. Ragna provides an intuitive API for quick experimentation and built-in
20+
tools for creating production-ready applications allowing you to quickly leverage
21+
Large Language Models (LLMs) for your work.
22+
23+
The Ragna website is https://ragna.chat/. The source code is available at
24+
https://github.com/Quansight/ragna under the BSD 3-Clause license.
25+
"""
1726

1827

1928
class RestApi:
20-
def __init__(self):
29+
def __init__(self) -> None:
2130
self._process: Optional[subprocess.Popen] = None
2231

2332
def start(self, config: Config, *, authenticate: bool = False) -> httpx.Client:
@@ -115,12 +124,14 @@ def _authenticate(self, client: httpx.Client) -> None:
115124
client.headers["Authorization"] = f"Bearer {token}"
116125

117126
def stop(self, *, quiet: bool = False) -> None:
127+
if self._process is None:
128+
return
129+
118130
self._process.kill()
119131
stdout, _ = self._process.communicate()
120132

121133
if not quiet:
122134
print(stdout.decode())
123135

124-
def __del__(self):
125-
if self._process is not None:
126-
self.stop(quiet=True)
136+
def __del__(self) -> None:
137+
self.stop(quiet=True)

0 commit comments

Comments
 (0)