Skip to content

Commit

Permalink
fix: improve file reading when codifying
Browse files Browse the repository at this point in the history
Do not attempt to read binary files and read max 3072 chars.

Append the file contents to the user message instead of adding another message (only the last user message is sent when using the huggingface provider, so the actual commandline was not sent before if a file was mentioned, ooops!).
  • Loading branch information
Realiserad committed Jul 22, 2024
1 parent 2e367d9 commit 04ecabe
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
1 change: 1 addition & 0 deletions .devcontainer/requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ pyfakefs
iterfzf
hugchat
mistralai
binaryornot
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ dependencies = [
"iterfzf==1.4.0.51.0",
"hugchat==0.4.8",
"mistralai==0.4.2",
"binaryornot==0.4.4",
]

[project.urls]
Expand Down
14 changes: 8 additions & 6 deletions src/fish_ai/codify.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from re import match
from fish_ai import engine
import textwrap
from binaryornot.check import is_binary


def get_instructions(commandline):
Expand Down Expand Up @@ -47,13 +48,12 @@ def get_instructions(commandline):
]
(filename, file_contents) = get_file_info(commandline)
if filename:
instructions.append({
'role': 'user',
'content': textwrap.dedent('''\
instructions[-1]['content'] = instructions[-1]['content'] + \
textwrap.dedent('''\
The content of the file {} is'
{}''').format(filename, '\n'.join(file_contents))
})
{}''').format(filename, file_contents)
return instructions


Expand All @@ -70,9 +70,11 @@ def get_file_info(commandline):
continue
if not access(filename, R_OK):
continue
if is_binary(filename):
continue
with open(filename, 'r') as file:
engine.get_logger().debug('Loading file: ' + filename)
return filename, file.readlines(3072)
return filename, file.read(3072)
return None, None


Expand Down

0 comments on commit 04ecabe

Please sign in to comment.