Skip to content

Commit

Permalink
feat: include manpage in request
Browse files Browse the repository at this point in the history
Include the manpage in the system prompt to help the LLM
explain a command.

The manpage is retrieved using 'man', or if no manpage
is available on the system, by running the command with
the --help flag.

This can improve the explanation of uncommon commands.
  • Loading branch information
Realiserad committed May 24, 2024
1 parent b4c63cc commit 6e0faed
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,17 +152,19 @@ configuration = foo
temperature = 0.5
```

### Switch between contexts
## 🎭 Switch between contexts

You can switch between different sections in the configuration using the
`fish_ai_switch_context` command.

## 🐾 Data privacy

When using the plugin, `fish-ai` submits the name of your OS and the
commandline buffer to the LLM. When you codify a command, it also
sends the contents of any files you mention (as long as the file is
readable).
commandline buffer to the LLM.

When you codify a command, it also sends the contents of any files you
mention (as long as the file is readable), and when you explain a command,
the manpage of the current command is provided to the LLM for reference.

Finally, to fix the previous command, the previous commandline buffer,
along with any terminal output and the corresponding exit code is sent
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "fish_ai"
version = "0.2.1"
version = "0.3.0"
authors = [
{ name="Bastian Fredriksson", email="realiserad@gmail.com" },
]
Expand Down
26 changes: 21 additions & 5 deletions src/fish_ai/explain.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,25 @@

from sys import argv
from fish_ai import engine
import subprocess


def get_instructions(commandline):
return [
{
'role': 'system',
'content': '''
Respond with a single sentence which explains the fish shell
command given by the user.
Respond with a maximum of three sentences which explain the fish
shell command given by the user.
The sentence must begin with a verb. The sentence should be
The response must begin with a verb. The sentences should be
written in {language}.
'''.format(language=engine.get_config('language') or 'English')
You may use the following manpage to help explain the command:
{manpage}
'''.format(language=engine.get_config('language') or 'English',
manpage=get_manpage(commandline.split()[0]))
},
{
'role': 'user',
Expand Down Expand Up @@ -48,6 +54,16 @@ def get_instructions(commandline):
]


def get_manpage(command):
manpage = subprocess.run(['man', command], stdout=subprocess.PIPE)
if manpage.returncode == 0:
return manpage.stdout.decode('utf-8')
helppage = subprocess.run([command, '--help'], stdout=subprocess.PIPE)
if helppage.returncode == 0:
return helppage.stdout.decode('utf-8')
return 'No manpage available.'


def get_messages(commandline):
return [engine.get_system_prompt()] + get_instructions(commandline)

Expand All @@ -58,7 +74,7 @@ def explain():
try:
engine.get_logger().debug('Explaining commandline: ' + commandline)
response = engine.get_response(messages=get_messages(commandline))
print('# ' + response)
print('# ' + response.replace('\n', ' '))
except KeyboardInterrupt:
pass
except Exception as e:
Expand Down

0 comments on commit 6e0faed

Please sign in to comment.