Skip to content

Commit

Permalink
Merge pull request #32 from Ace-Radom/main
Browse files Browse the repository at this point in the history
增加/rand命令以更改temperature
  • Loading branch information
xiaoxx970 authored May 5, 2023
2 parents dda167a + bc14bad commit e014424
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 6 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@ Uses the [gpt-3.5-turbo](https://platform.openai.com/docs/guides/chat/chat-compl

## Changelog

### 2023-04-23
Released `gpt-term` on [Pypi](https://pypi.org/project/gpt-term/), started version control. No need to clone the project locally anymore, simply use the `pip` command to install gpt-term.
### 2023-05-05

- Add `/rand` command to set temperature parameter

<details>
<summary>More Change log</summary>

### 2023-04-23
Released `gpt-term` on [Pypi](https://pypi.org/project/gpt-term/), started version control. No need to clone the project locally anymore, simply use the `pip` command to install gpt-term.

### 2023-04-15

- Added the ability to create a line break in single-line mode using `Esc` + `Enter`
Expand Down Expand Up @@ -216,6 +220,8 @@ LOG_LEVEL=INFO
- `/system [new_prompt]`: Modify the system prompt

- `/rand [randomness]`: Set Model sampling temperature (0~2), higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.

- `/title [new_title]`: Set terminal title for this chat

> If new_title is not provided, a new title will be generated based on first question
Expand Down
10 changes: 8 additions & 2 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@

## 更新记录

### 2023-04-23
### 2023-05-05

- 发布 `gpt-term`[Pypi](https://pypi.org/project/gpt-term/),开始版本管理,现在不需要克隆项目到本地,直接使用 `pip` 命令就可以安装 `gpt-term`
- 添加`/rand`命令设置temperature参数

<details>
<summary>更多 Change log</summary>

### 2023-04-23

- 发布 `gpt-term`[Pypi](https://pypi.org/project/gpt-term/),开始版本管理,现在不需要克隆项目到本地,直接使用 `pip` 命令就可以安装 `gpt-term`

### 2023-04-15

- 新增在单行模式下换行功能,现在可以使用 `Esc` + `Enter` 在单行模式下换行
Expand Down Expand Up @@ -219,6 +223,8 @@ LOG_LEVEL=INFO
- `/system [new_prompt]`:修改系统提示语

- `/rand [randomness]`: 设置对话随机程度(0~2),较高的值如 0.8 将使输出更随机,而较低的值如 0.2 将使回答更集中和确定

- `/title [new_title]`: 为这个聊天终端设置标题

> 如果没有提供 new_title,将根据第一个提问生成新标题
Expand Down
48 changes: 46 additions & 2 deletions gpt_term/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ def __init__(self, api_key: str, timeout: float):
self.tokens_limit = 4096
# as default: gpt-3.5-turbo has a tokens limit as 4096
# when model changes, tokens will also be changed
self.temperature = 1
self.total_tokens_spent = 0
self.current_tokens = count_token(self.messages)
self.timeout = timeout
Expand Down Expand Up @@ -233,7 +234,8 @@ def handle(self, message: str):
data = {
"model": self.model,
"messages": self.messages,
"stream": ChatMode.stream_mode
"stream": ChatMode.stream_mode,
"temperature": self.temperature
}
response = self.send_request(data)
if response is None:
Expand Down Expand Up @@ -501,10 +503,22 @@ def set_timeout(self, timeout):
return
console.print(f"[dim]API timeout set to [green]{timeout}s[/].")

def set_temperature(self, temperature):
try:
new_temperature = float(temperature)
except ValueError:
console.print("[red]Input must be a number between 0 and 2")
return
if new_temperature > 2 or new_temperature < 0:
console.print("[red]Input must be a number between 0 and 2")
return
self.temperature = new_temperature
console.print(f"[dim]Randomness set to [green]{temperature}[/].")


class CustomCompleter(Completer):
commands = [
'/raw', '/multi', '/stream', '/tokens', '/usage', '/last', '/copy', '/model', '/save', '/system', '/title', '/timeout', '/undo', '/delete', '/version', '/help', '/exit'
'/raw', '/multi', '/stream', '/tokens', '/usage', '/last', '/copy', '/model', '/save', '/system', '/rand', '/title', '/timeout', '/undo', '/delete', '/version', '/help', '/exit'
]

copy_actions = [
Expand Down Expand Up @@ -570,6 +584,23 @@ def validate(self, document):
raise ValidationError(message="Please input an Integer!",
cursor_position=len(text))

class FloatRangeValidator(Validator):
def __init__(self, min_value=None, max_value=None):
self.min_value = min_value
self.max_value = max_value

def validate(self, document):
try:
value = float(document.text)
except ValueError:
raise ValidationError(message='Input must be a number')

if self.min_value is not None and value < self.min_value:
raise ValidationError(message=f'Input must be at least {self.min_value}')
if self.max_value is not None and value > self.max_value:
raise ValidationError(message=f'Input must be at most {self.max_value}')

temperature_validator = FloatRangeValidator(min_value=0.0, max_value=2.0)

def print_message(message: Dict[str, str]):
'''打印单条来自 ChatGPT 或用户的消息'''
Expand Down Expand Up @@ -730,6 +761,18 @@ def handle_command(command: str, chat_gpt: ChatGPT, key_bindings: KeyBindings, c
else:
console.print("[dim]No change.")

elif command.startswith('/rand'):
args = command.split()
if len(args) > 1:
new_temperature = args[1]
else:
new_temperature = prompt(
"New Randomness: ", default=str(chat_gpt.temperature), style=style, validator=temperature_validator)
if new_temperature != str(chat_gpt.temperature):
chat_gpt.set_temperature(new_temperature)
else:
console.print("[dim]No change.")

elif command.startswith('/title'):
args = command.split()
if len(args) > 1:
Expand Down Expand Up @@ -809,6 +852,7 @@ def handle_command(command: str, chat_gpt: ChatGPT, key_bindings: KeyBindings, c
/save \[filename_or_path] - Save the chat history to a file, suggest title if filename_or_path not provided
/model \[model_name] - Change AI model
/system \[new_prompt] - Modify the system prompt
/rand \[randomness] - Set Model sampling temperature (0~2)
/title \[new_title] - Set title for this chat, if new_title is not provided, a new title will be generated
/timeout \[new_timeout] - Modify the api timeout
/undo - Undo the last question and remove its answer
Expand Down

0 comments on commit e014424

Please sign in to comment.