Skip to content

Commit

Permalink
Encapsulate content retrieval in get_content function.
Browse files Browse the repository at this point in the history
  • Loading branch information
yaph committed Jul 22, 2024
1 parent d0f6431 commit ade33e9
Showing 1 changed file with 33 additions and 29 deletions.
62 changes: 33 additions & 29 deletions charla/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,30 @@ def generate(model: str, prompt: str, context: list, output: list, system=None)
return chunk['context'] if isinstance(chunk, Mapping) else []


def prompt_session(argv) -> PromptSession:
def get_content(source: str) -> str:
"""Return content of the given source or empty string."""

content = ''

if source.startswith(('http://', 'https://')):
try:
resp = httpx.get(source, follow_redirects=True)
if 'text/html' == resp.headers['content-type']:
content = html2text(resp.text, baseurl=source)
else:
content = resp.text
except httpx.ConnectError as err:
print(f'Enter an existing URL.\n{err}\n')
else:
try:
content = Path(source).read_text()
except (FileNotFoundError, PermissionError) as err:
print(f'Enter name of an existing file.\n{err}\n')

return content


def prompt_session(argv: argparse.Namespace) -> PromptSession:
"""Create and return a PromptSession object."""

session: PromptSession = PromptSession(message=t_prompt_ml if argv.multiline else t_prompt,
Expand Down Expand Up @@ -103,7 +126,7 @@ def run(argv: argparse.Namespace) -> None:
if system_prompt:
print_fmt('Using system prompt:', HTML(f'<ansigreen>{argv.system_prompt.name}</ansigreen>'), '\n')

open_filename = ''
open_source = ''

while True:
try:
Expand All @@ -112,38 +135,19 @@ def run(argv: argparse.Namespace) -> None:

output.append(f'{session.message}{user_input}\n')

if open_filename:
file_content = ''

if open_filename.startswith(('http://', 'https://')):
try:
resp = httpx.get(open_filename, follow_redirects=True)
if 'text/html' == resp.headers['content-type']:
file_content = html2text(resp.text, baseurl=open_filename)
else:
file_content = resp.text
except httpx.ConnectError as err:
print(f'Enter an existing URL.\n{err}\n')
else:
try:
file_content = Path(open_filename).read_text()
except (FileNotFoundError, PermissionError) as err:
print(f'Enter name of an existing file.\n{err}\n')

open_filename = ''

if file_content:
user_input = user_input.strip() + '\n\n' + file_content
else:
continue

if session.message == t_open:
open_filename = user_input.strip()
session.bottom_toolbar = t_open_toolbar + open_filename
open_source = user_input.strip()
session.bottom_toolbar = t_open_toolbar + open_source
session.message = t_prompt_ml if session.multiline else t_prompt
session.completer = None
continue

if open_source:
if content := get_content(open_source):
user_input = user_input.strip() + '\n\n' + content
else:
continue

print(f'\n{t_response}\n')
context = generate(argv.model, user_input, context, output, system=system_prompt)
print('\n')
Expand Down

0 comments on commit ade33e9

Please sign in to comment.