Skip to content

Commit f9d3380

Browse files
committed
Add basic personas support
1 parent b37fb90 commit f9d3380

File tree

12 files changed

+85
-34
lines changed

12 files changed

+85
-34
lines changed

CONTRIBUTING.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
The following topics are missing for a 1.0.0 release:
44

55
```
6-
* Allow adding "personas" to the config ([see here](docs/personas.md))
7-
* Rewrite with tests as soon as the basic prototype is useful
6+
* Rewrite/Refactor with tests as soon as the basic prototype is useful
87
* Add GitHub Actions for PRs
98
* Build a landing page
109
```
1110

1211
Please use `clai` and contribute here on GitHub. I'm open to all suggestions and improvements.
1312

14-
0.x.x versions don't need tests or extensive documentation. Adding a small description will help me understand your change though :)
13+
PRs for the 0.x.x versions don't need tests or extensive documentation. Adding a small description will help me understand your change though :)
1514

1615
Please open a PR with a short description of your use-case and I will review and merge.
1716

docs/chat.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ Send a single prompt to the completions api.
44

55
Some examples:
66

7-
87
```
98
clai chat "Tell me a funny joke"
9+
10+
# Write ruby code
11+
clai chat "Count to 10" --persona ruby
1012
```

docs/intro.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# Introduction
22

33
You can think of `clai` as ChatGPT for the terminal.
4-
It's very basic at the moment. You can think of it as ChatGPT for the terminal. I'm currently trying to incorporate clai in my daily work, so I'm still experimenting with the API.
4+
It's very basic at the moment. I'm currently trying to incorporate clai in my daily work, so I'm still experimenting with the API.
55

66
`clai` currently supports OpenAI and needs a paid API key to work.
77

88
My main use case currently is to have a terminal opened with a clai session and ask questions instead of googling.
99

10-
0.x.0 will be unstable and untested, I'm currently aiming to release a tested 1.0.0 soon. Please see [here](https://github.com/codergeek121/clai/CONTRIBUTING.MD) on how to contribute at this stage of the project.
10+
0.x.0 will be unstable and untested, I'm currently aiming to release a tested 1.0.0 soon. Please see [here](https://github.com/codergeek121/clai/CONTRIBUTING.MD) on how to contribute at the current stage of the project.

docs/personas.md

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,36 @@
11
# Personas
22

3-
TODO: This feature is still under development!
3+
Clai allows you to configure different personas. Whenever you're calling clai, it's possible to pass a specified persona with `--persona`. This will pass system prompts to ChatGPT. It works similar to "Custom instructions" in ChatGPT.
44

5-
A clai setup should allow preconfiguring multiple personas. Each command should support a `--persona` option to allow setting some default prompts to the api.
6-
7-
An example could look like this:
5+
The base clai setup comes with the following default personas config:
86

97
```yaml
108
personas:
11-
work:
12-
- Use professional language
13-
- Keep your answer below 100 words
14-
bavarian:
15-
- Use a bavarian accent
16-
17-
# Starting a session with a preselected persona
18-
$ clai session --persona bavarian
19-
Your prompt: <Your prompt><Enter>
20-
Some bavarian answer.
9+
default:
10+
- You are a helpful assistant
11+
- You answers are formatted in markdown
12+
ruby:
13+
- You are a ruby programmer
14+
- You do not comment your code
15+
```
16+
17+
Compare the outputs when using different personas.
18+
19+
```
20+
$ clai chat "Count to 10" --persona ruby
21+
Sure, here's how you can count to 10 in Ruby:
22+
23+
(1..10).each do |num|
24+
puts num
25+
end
26+
27+
// Without a persona, fallbacks to the "default" persona
28+
$ clai chat "Count to 10"
29+
30+
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
31+
32+
// Start a ruby persona session
33+
$ clai session --persona ruby
2134
```
2235

23-
The default setup should have useful personas as a default.
36+
The persona under the key `default:` is used whenever you're not specifying a explicit persona.

docs/session.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11
# Session
22

3-
TODO
3+
Start a REPL like chat session.
4+
5+
Some examples:
6+
7+
```
8+
clai session
9+
10+
# Start a Ruby session
11+
clai session --persona ruby
12+
```

docs/setup.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
# Setup
22

3-
TODO
3+
`clai setup` will create a config file with a base configuration.
4+
5+
`clai setup --force` will overwrite the basic config with the default configuration. It's mainly useful in development of the gem.

lib/clai/cli.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,34 @@ module CLAI
22
class CLI < Thor
33
default_task :session
44

5+
option :persona, type: :string
56
desc "session", "Start an interactive chat session"
67
long_desc <<~DESC
78
Starts a REPL like chat process
89
910
> $ clai session
11+
12+
Use a preconfigured persona with --persona
13+
14+
> $ clai chat "Count to 10" --persona ruby
1015
DESC
1116
def session
12-
Commands::Session.new(config).start_session
17+
Commands::Session.new(config).start_session(persona: options['persona'])
1318
end
1419

20+
option :persona, type: :string
1521
desc "chat", "Prompt on the terminal"
1622
long_desc <<~DESC
1723
Send a single prompt to AI
1824
1925
> $ clai chat "Tell me a short story!"
26+
27+
Use a preconfigured persona with --persona
28+
29+
> $ clai chat "Count to 10" --persona ruby
2030
DESC
2131
def chat(prompt)
22-
Commands::Chat.new(config).chat(prompt)
32+
Commands::Chat.new(config).chat(prompt, persona: options['persona'])
2333
end
2434

2535
option :force, type: :boolean, default: false

lib/clai/commands/chat.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ def initialize(config)
55
@config = config
66
end
77

8-
def chat(prompt)
9-
HTTPClient.new(@config).chat(prompt)
8+
def chat(prompt, persona: 'default')
9+
system_prompts = @config.personas.fetch(persona, [])
10+
HTTPClient.new(@config).chat(prompt, system_prompts: system_prompts)
1011
end
1112
end
1213
end

lib/clai/commands/session.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ def initialize(config)
55
@config = config
66
end
77

8-
def start_session
8+
def start_session(persona: 'default')
9+
system_prompts = @config.personas.fetch(persona, [])
910
loop do
1011
print S.(:prompt)
1112
question = $stdin.gets
1213
break unless question
13-
HTTPClient.new(@config).chat(question.strip)
14+
HTTPClient.new(@config).chat(question.strip, system_prompts: system_prompts)
1415
puts
1516
end
1617
end

lib/clai/commands/setup.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ def sample_config
4242
# Read more about the clai.yml config file at https://github.com/codergeek121/clai/docs/setup.md
4343
api_key: #{@api_key}
4444
model: gpt-3.5-turbo
45+
personas:
46+
default:
47+
- You are a helpful assistant
48+
- You answers are formatted in markdown
49+
ruby:
50+
- You are a ruby programmer
51+
- You do not comment your code
4552
YML
4653
end
4754
end

0 commit comments

Comments
 (0)