OpenAI Implementation for Generating Shell Commands from Descriptions
·
2 min read
Recently researched the feasibility of using OpenAI to implement this requirement, here’s a summary
Competitive Product Research
- iTerm2 recently launched this feature in 3.5.0 beta, but being a beta version, the experience is quite limited. The general implementation approach is to construct context based on user input descriptions and make requests to OpenAI. iTerm2 uses the
text-davinci-003
model (GPT model). After obtaining multiple results, it displays all of them rather than just selecting the first one. - Fig also introduced AI functionality, but the AI component is not directly open-sourced, so specific details are uncertain. However, through discussions in the open-source community, we know it still uses OpenAI Codex behind the scenes. In terms of user experience, after submitting a description, it only displays one result.
- zsh_codex for zsh uses OpenAI and selects the
code-davinci-002
model (Codex model). Taking zsh_codex implementation as an example, after obtaining multiple results, it only displays the first one.
From the above, we can see that all use OpenAI, with the main differences being the selected algorithm model configurations and how returned results are handled, which affects the user experience.
Validation
Demo can be found here
Actual testing shows that using GPT/Codex to generate shell commands works as expected.
However, there are differences in model/configuration between the two models. Here are some points to note:
- Model-Prompt
GPT and Codex differ in how context is constructed. Codex requires explicit language environment specification, while GPT requires clear specific requirements.
- Codex
#!/bin/bash\n\n# ${text}.\n
- GPT
I want to write a command, the requirement is ${text}.\n
- Include periods - as shown above, periods and line breaks are necessary
- The temperature value (0-1) makes a significant difference - 0 means completely accurate, 1 means maximum possible answers (fuzzy). In the research section above, we found that iTerm2 configures it to 0, while zsh configures it to 0.5. The specific value should be determined based on the selected model and use case, this is just for reference.
Final Thoughts
- In summary, with OpenAI, generating commands from descriptions is relatively easy to implement. In practical experience, this functionality can only serve as an assistant, as it’s difficult to accurately generate a command from a single natural language description. Therefore, providing command references to users while ensuring a smooth experience is acceptable.