Skip to content

Commit

Permalink
v0.58.2 - improved formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
FlyingFathead committed Mar 2, 2024
1 parent 9ccd01c commit b5afeb0
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ timezonefinder>=6.4.0
- Use the `configmerger.py` to update old configuration files into a newer version's `config.ini`. You can do this by creating a copy of your existing config to i.e. a file named `myconfig.txt` and including in it the lines you want to keep for the newer version. Then, just run `python configmerger.py config.ini myconfig.txt` and all your existing config lines will be migrated to the new one. Works in most cases, but remember to be careful and double-check any migration issues with i.e. `diff`!

# Changelog
- v0.58.2 - improved formatting in pplx API calls
- v0.58.1 - improved markdown parsing in translated Perplexity API calls
- v0.58 - chunking, parsing and other small fixes
- v0.57.5 - changes made to Perplexity API handling; new sonar-online models
Expand Down
46 changes: 43 additions & 3 deletions api_perplexity_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

# Global variable for chunk size
# Set this value as needed
CHUNK_SIZE = 350
CHUNK_SIZE = 500

# Assuming you've set PERPLEXITY_API_KEY in your environment variables
PERPLEXITY_API_KEY = os.getenv("PERPLEXITY_API_KEY")
Expand Down Expand Up @@ -269,8 +269,13 @@ async def translate_response_chunked(bot, user_message, perplexity_response, con
# After building the full translated_response, apply the paragraph breaks adjustment
adjusted_response = add_paragraph_breaks_to_headers(translated_response)

# Then convert the markdown to HTML
html_response = markdown_to_html(adjusted_response)
# Apply the header formatting for Telegram before converting to HTML
telegram_formatted_response = format_headers_for_telegram(adjusted_response)

# Then convert the Telegram-formatted response to HTML
html_response = markdown_to_html(telegram_formatted_response)

logging.info(f"Parsed translated response: {html_response}")

return html_response

Expand Down Expand Up @@ -357,6 +362,41 @@ def add_paragraph_breaks_to_headers(translated_response):
adjusted_response = '\n'.join(adjusted_lines)
return adjusted_response

# formatting headers to look neater for TG and ensure proper breaks
def format_headers_for_telegram(translated_response):
# Split the translated response into lines
lines = translated_response.split('\n')

# Initialize a list to hold the formatted lines
formatted_lines = []

# Iterate over the lines, adding symbols before headers and ensuring proper line breaks
for i, line in enumerate(lines):
if line.startswith('###'):
# Add a newline before the header if it's not the first line and the previous line is not empty
if i > 0 and lines[i - 1].strip() != '':
formatted_lines.append('')
# Format the header and add a newline after
formatted_line = '• <b>' + line[3:].strip() + '</b>'
formatted_lines.append(formatted_line)
if i < len(lines) - 1 and lines[i + 1].strip() != '':
formatted_lines.append('')
elif line.startswith('##'):
# Add a newline before the main header if it's not the first line and the previous line is not empty
if i > 0 and lines[i - 1].strip() != '':
formatted_lines.append('')
# Format the main header and add a newline after
formatted_line = '➤ <b>' + line[2:].strip() + '</b>'
formatted_lines.append(formatted_line)
if i < len(lines) - 1 and lines[i + 1].strip() != '':
formatted_lines.append('')
else:
formatted_lines.append(line)

# Join the adjusted lines back into a single string
formatted_response = '\n'.join(formatted_lines)
return formatted_response

# smart chunking (v1.09)
""" def smart_chunk(text, chunk_size=CHUNK_SIZE):
chunks = []
Expand Down
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# https://github.com/FlyingFathead/TelegramBot-OpenAI-API
#
# version of this program
version_number = "0.58.1"
version_number = "0.58.2"

# experimental modules
import requests
Expand Down

0 comments on commit b5afeb0

Please sign in to comment.