Skip to content

Commit

Permalink
Docs: Grammar action, satisfaction! First 20 pages. (#1817)
Browse files Browse the repository at this point in the history
* update prompt
change model
fix chunking

* add sanity check and switch back to gpt 4

* fix lint
fix lint
fix grammar script
add offset limit functionailty for grammar checker

* first 20 pages

* Apply suggestions from code review

---------

Co-authored-by: Anton Burnashev <[email protected]>
  • Loading branch information
sh-rp and burnash authored Sep 19, 2024
1 parent ff1434b commit acf1e36
Show file tree
Hide file tree
Showing 23 changed files with 1,138 additions and 1,128 deletions.
68 changes: 58 additions & 10 deletions docs/tools/fix_grammar_gpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@

# constants
BASE_DIR = "../website/docs"
GPT_MODEL = "gpt-3.5-turbo-0125"
MAX_CHUNK_SIZE = 14000 # make sure that this is below the context window size of the model to not have cut off files
GPT_MODEL = "gpt-4-turbo"
MAX_CHUNK_SIZE = 4000 # make sure that this is below the context window size of the model to not have cut off files

SYSTEM_PROMPT = """\
You are a grammar checker. Every message you get will be a document that is to be grammarchecked and returned as such.
You will not change the markdown syntax. You will only fix the grammar. You will not change the code snippets except for the comments therein.
You will not modify the header section which is enclosed by two occurences of "---".
Make sure all headings use the Sentence case.
Never insert any codeblock start or end statements such as "```"
Do not change the spelling or casing of these words: dlt, sdf, dbt
"""

Expand Down Expand Up @@ -51,6 +53,22 @@
type=str,
)

parser.add_argument(
"-o",
"--offset",
help="File count offset from where to start fixing",
default=0,
type=int,
)

parser.add_argument(
"-l",
"--limit",
help="File count limit, how many files to process",
default=100000,
type=int,
)

# get args
args = parser.parse_args()

Expand All @@ -63,14 +81,26 @@

# run grammar check
count = 0
processed = 0
for file_path in markdown_files:
count += 1

if count <= args.offset:
continue

if processed >= args.limit:
break

processed += 1

fmt.note(f"Fixing grammar for file {file_path} ({count} of {len(markdown_files)})")

with open(file_path, "r", encoding="utf-8") as f:
doc = f.readlines()

with open(file_path, "r", encoding="utf-8") as f:
doc_length = len(f.read())

def get_chunk_length(chunk: List[str]) -> int:
count = 0
for line in chunk:
Expand All @@ -80,8 +110,11 @@ def get_chunk_length(chunk: List[str]) -> int:
# cut file into sections
sections: List[List[str]] = []
current_section: List[str] = []
is_in_code_block: bool = False
for line in doc:
if line.startswith("#"):
if "```" in line:
is_in_code_block = not is_in_code_block
if line.startswith("#") and not is_in_code_block:
if current_section:
sections.append(current_section)
current_section = [line]
Expand All @@ -106,27 +139,42 @@ def get_chunk_length(chunk: List[str]) -> int:
# sanity test, make sure we still have the full doc
assert doc == functools.reduce(lambda a, b: a + b, chunks)

fmt.note(f"Created {len(chunks)} chunks")
# count chars in doc
fmt.note(f"Created {len(chunks)} chunks for {doc_length} chars")

fixed_chunks: List[List[str]] = []
fixed_chunks: List[str] = []
for chunk in chunks:
client = OpenAI()
in_string = "".join(chunk)
response = client.chat.completions.create(
seed=123981298,
model=GPT_MODEL,
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": "".join(chunk)},
{"role": "user", "content": in_string},
],
temperature=0,
)

fixed_chunks.append(response.choices[0].message.content) # type: ignore
fixed_chunks.append(response.choices[0].message.content)

# here we check that no part of the doc was swallowed by gpt
fixed_doc_length = functools.reduce(
lambda count, chunk: count + len(chunk), fixed_chunks, 0
)
if fixed_doc_length / doc_length < 0.9:
fmt.error(
"Doc length reduced too much during processing, skipping saving, please check"
" manually"
)
continue

with open(file_path, "w", encoding="utf-8") as f:
for c in fixed_chunks:
f.writelines(c)
f.write(c)
f.write("\n")
f.write("\n")

if count == 0:
fmt.warning("No files selected for grammar check.")
else:
fmt.note(f"Fixed grammar for {count} files.")
fmt.note(f"Fixed grammar for {processed} files.")
3 changes: 2 additions & 1 deletion docs/website/docs/_book-onboarding-call.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
<a href="https://calendar.app.google/EMZRS6YhM11zTGQw7">book a call</a> with a dltHub Solutions Engineer
<a href="https://calendar.app.google/EMZRS6YhM11zTGQw7">Book a call</a> with a dltHub Solutions Engineer

Loading

0 comments on commit acf1e36

Please sign in to comment.