Skip to content

Commit

Permalink
feat: use chat completions api
Browse files Browse the repository at this point in the history
Use the OpenAI chat completions API with the appropriate
prompts and allow the user to the specify server URL.

Turn comments into code or vice versa when pressing
Ctrl+X.
  • Loading branch information
Realiserad committed Jan 6, 2024
1 parent bc8ef57 commit e38eed5
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 149 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2022 Tom Dörr
Copyright (c) 2022 Tom Dörr and Bastian Fredriksson

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
75 changes: 24 additions & 51 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,65 +1,38 @@
<h1 align="center">⌨️ 🦾 codex.fish 🐟</h1>
# About

<p align="center">
AI in the command line.
</p>
`fish-ai` adds AI functionality to [Fish shell](https://fishshell.com).

<p align="center">
<a href="https://github.com/tom-doerr/codex.fish/stargazers"
><img
src="https://img.shields.io/github/stars/tom-doerr/codex.fish?colorA=2c2837&colorB=c9cbff&style=for-the-badge&logo=starship style=flat-square"
alt="Repository's starts"
/></a>
<a href="https://github.com/tom-doerr/codex.fish/issues"
><img
src="https://img.shields.io/github/issues-raw/tom-doerr/codex.fish?colorA=2c2837&colorB=f2cdcd&style=for-the-badge&logo=starship style=flat-square"
alt="Issues"
/></a>
<a href="https://github.com/tom-doerr/codex.fish/blob/main/LICENSE"
><img
src="https://img.shields.io/github/license/tom-doerr/codex.fish?colorA=2c2837&colorB=b5e8e0&style=for-the-badge&logo=starship style=flat-square"
alt="License"
/><br />
<a href="https://github.com/tom-doerr/codex.fish/commits/main"
><img
src="https://img.shields.io/github/last-commit/tom-doerr/codex.fish/main?colorA=2c2837&colorB=ddb6f2&style=for-the-badge&logo=starship style=flat-square"
alt="Latest commit"
/></a>
<a href="https://github.com/tom-doerr/codex.fish"
><img
src="https://img.shields.io/github/repo-size/tom-doerr/codex.fish?colorA=2c2837&colorB=89DCEB&style=for-the-badge&logo=starship style=flat-square"
alt="GitHub repository size"
/></a>
</p>
This is a fork of [Tom Dörr's `fish.codex` repository](https://github.com/tom-doerr/codex.fish).
It uses the [chat completions API endpoint](https://platform.openai.com/docs/api-reference/chat/create)
and is powered by a self-hosted LLM. It has been tested against
[Llama GPT](https://github.com/getumbrel/llama-gpt) but should work with other models as well.

## What is it?
## How to install

This is a fish plugin that enables you to use OpenAI's powerful Codex AI in the command line. OpenAI Codex is the AI that also powers GitHub Copilot.
To use this plugin you need to get access to OpenAI's [Codex API](https://openai.com/blog/openai-codex/).
1. Create a configuration file called `.config/fish-ai.ini`. It should look something like this:


## How do I install it?
1. Install the OpenAI package.
```
pip3 install openai
```ini
[fish-ai]
server = https://your-server/v1
api_key = sk-XXXXXXXXXXXX
model = code-llama-13b-chat.gguf
```

2. Install the plugin itself.
You can install it using [fisher](https://github.com/jorgebucaran/fisher):
```
fisher install tom-doerr/codex.fish
2. Install the OpenAI package.

```shell
pip3 install openai
```

3. Create a file called `openaiapirc` in `~/.config` with your ORGANIZATION_ID and SECRET_KEY.
3. Install the plugin itself. You can install it using [fisher](https://github.com/jorgebucaran/fisher).

```
[openai]
organization_id = ...
secret_key = ...
```shell
fisher install realiserad/fish-ai
```

4. Run `fish`, start typing and complete it using `^X`!
## How to use

---
Type a comment, and press `^X` to turn it into shell command!

[ZSH version](https://github.com/tom-doerr/zsh_codex)
You can also run it in reverse. Type a command and press `^X` to turn it into a comment explaining what the
command does.
1 change: 1 addition & 0 deletions conf.d/ai.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bind \cx codify
1 change: 0 additions & 1 deletion conf.d/create_completion.fish

This file was deleted.

8 changes: 8 additions & 0 deletions functions/codify.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env fish

function codify --description "Turn a comment into a command and vice versa using AI."
set dir (dirname (status -f))
set buffer (commandline -b)
set command (echo -n "$buffer" | "$dir/codify.py")
commandline -r "$command"
end
73 changes: 73 additions & 0 deletions functions/codify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/usr/bin/env python3

from sys import stdin
import engine as ai

buffer = stdin.read()

system_prompt = {
'role': 'system',
'content': '''
You are a programming assistant called Fish AI helping users inside a Fish shell.
If the user is asking how they can update Fish AI, please respond with the
following command:
fisher install realiserad/fish-ai
For all other questions, you may consult Stack Overflow for answers.
'''
}
user_prompt_comment2code = {
'role': 'user',
'content': '''
RESPOND WITH A COMMAND WHICH DOES THE FOLLOWING:
{}
Only respond with command. Only respond with a single line. Do not explain or add
any additional text to the response. Never use markdown backticks or any other
formatting. Do not use shebangs. The command must be compatible with Fish shell.
Examples:
[User] List all disks on the system.
[Fish AI] df -h
[User] Pull the Alpine 3 container from DockerHub.
[Fish AI] docker pull alpine:3
[User] Substitute all occurrences of the string 'foo' with the string 'bar' in the file 'docker-compose.yml'
[Fish AI] sed -i 's/foo/bar/g' docker-compose.yml
'''.format(buffer[2:])
}
user_prompt_code2comment = {
'role': 'user',
'content': '''
Respond with a single sentence which explains the following command:
{}
The sentence must begin with a verb.
Examples:
[User] df -h
[Fish AI] List all disks on the system.
[User] docker pull alpine:3
[Fish AI] Pull the Alpine 3 container from DockerHub.
[User] sed -i 's/foo/bar/g' docker-compose.yml
[Fish AI] Substitute all occurrences of the string 'foo' with the string 'bar' in the file 'docker-compose.yml'
'''.format(buffer)
}

if (buffer.startswith('# ')):
# Turn a comment into a shell command
response = ai.get_response(messages = [system_prompt, user_prompt_comment2code])
print(response.strip())
else:
# Turn a shell command into a comment
response = ai.get_response(messages = [system_prompt, user_prompt_code2comment])
print('# ' + response.strip())
10 changes: 0 additions & 10 deletions functions/create_completion.fish

This file was deleted.

86 changes: 0 additions & 86 deletions functions/create_completion.py

This file was deleted.

23 changes: 23 additions & 0 deletions functions/engine.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from openai import OpenAI
from configparser import ConfigParser
from os import path

def get_config():
config = ConfigParser()
config.read(path.expanduser('~/.config/fish-ai.ini'))

def get_response(messages):
config = get_config()
client = OpenAI(
base_url = config.get('fish-ai', 'server'),
api_key = config.get('fish-ai', 'api_key'),
)
completions = client.chat.completions.create(
model = config.get('fish-ai', 'model'),
max_tokens = 1024,
messages = messages,
stream = False,
temperature = 0.2,
n = 1,
)
return completions.choices[0].message.content

0 comments on commit e38eed5

Please sign in to comment.