Skip to content

Commit

Permalink
Merge pull request #60 from xiaoxx970/xiaoxx970/issue58
Browse files Browse the repository at this point in the history
参数提问在terminal中直接返回答案
  • Loading branch information
xiaoxx970 authored Jun 21, 2023
2 parents ca8ba3e + 0d5b834 commit 373ee4c
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 12 deletions.
29 changes: 25 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,31 @@ Uses the [gpt-3.5-turbo](https://platform.openai.com/docs/guides/chat/chat-compl

## Changelog

### 2023-05-20
### 2023-06-21

- Added host configuration support, which is very useful when using self-built API reverse proxy server ([#49](https://github.com/xiaoxx970/chatgpt-in-terminal/issues/49)), you can now use `gpt-term --set-host HOST` to configure host, the default is https://api.openai.com
- Added direct query mode, now you can run `gpt-term` with the content of the question as a parameter to conduct a single question and answer

### 2023-05-18
```sh
gpt-term "What's the weather like today?"
```

- Added multi-language support: English, Chinese, Japanese, German, follow the system language by default, now you can use `/lang` to switch languages
The answer will be printed directly, or piped into a variable

```sh
gpt-term "What's the weather like today" | read answer
echo $answer
```

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

### 2023-05-20

- Added host configuration support, which is very useful when using self-built API reverse proxy server ([#49](https://github.com/xiaoxx970/chatgpt-in-terminal/issues/49)), you can now use `gpt-term --set-host HOST` to configure host, the default is https://api.openai.com

### 2023-05-18

- Added multi-language support: English, Chinese, Japanese, German, follow the system language by default, now you can use `/lang` to switch languages
### 2023-05-11

- Find the command the user most likely intended to enter when typing an unrecognized command
Expand All @@ -49,6 +63,7 @@ Uses the [gpt-3.5-turbo](https://platform.openai.com/docs/guides/chat/chat-compl
- Add overflow mode switch for `/stream` command, now you can run command `/stream visible` to switch to always visible mode. In this mode, the content that exceeds the screen will be scrolled up, and the new content will be output until it is completed

### 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
Expand Down Expand Up @@ -143,6 +158,12 @@ Or:
python3 -m gpt_term
```
Quick query:
```sh
gpt-term "What's the weather like today?"
```
When entering a question in single-line mode, use `Esc` + `Enter` to start a new line, and use `Enter` to submit the question.
Here are some common shortcut keys (also shortcut keys for the shell):
Expand Down
27 changes: 24 additions & 3 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,24 @@

## 更新记录

### 2023-06-21

- 新增直接询问模式,现在你可以运行 `gpt-term` 加上要提问的内容作为参数来进行单次提问和回答

```sh
gpt-term 今天天气怎么样
```

回答将直接被打印,或者使用管道存放于变量中

```sh
gpt-term 今天天气怎么样 | read answer
echo $answer
```

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

### 2023-05-20

- 新增 host 配置项支持,在使用自建 API 反向代理服务器的时候很有用([#49](https://github.com/xiaoxx970/chatgpt-in-terminal/issues/49)),你现在可以使用 `gpt-term --set-host HOST` 来配置 host,默认为 https://api.openai.com。
Expand All @@ -37,9 +55,6 @@

- 新增多语言支持:英语、中文、日语、德语,默认跟随系统语言,现在可以使用 `/lang` 来切换语言

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

### 2023-05-11

- 在输入未被识别的命令时查找用户最可能想输入的命令
Expand Down Expand Up @@ -146,6 +161,12 @@ gpt-term
python3 -m gpt_term
```
单次提问:
```sh
gpt-term 今天天气怎么样
```
在默认的单行模式下输入提问时,使用 `Esc` + `Enter` 换行,`Enter` 提交
以下是一些常见的快捷键(同时也是shell的快捷键):
Expand Down
3 changes: 2 additions & 1 deletion gpt_term/locale/gpt_term.de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ de:
help_set_lang: "Sprache einstellen"
help_set_saveperfix: "Speicherperfix für die Chatverlaufsdatei einstellen"
help_set_loglevel: "Log-Stufe einstellen:"
help_direct_query: "Frag GPT direkt an"
#
help_use_help: "Verwenden `[deep_sky_blue3]/help[/]`, um alle verfügbaren Slash-Befehle zu sehen"
help_uncommand: "Unerkannter Slash-Befehl `[bold red]%{command}[/]`"
Expand All @@ -151,4 +152,4 @@ de:
/version - Zeigen der Lokal- und der Fernersion von gpt-term an
/lang \[new_language] - Sprache umschalten
/help - Zeigen dieser Hilfemeldung an
/exit - Beenden der Anwendung
/exit - Beenden der Anwendung
1 change: 1 addition & 0 deletions gpt_term/locale/gpt_term.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ en:
help_set_lang: "Set language"
help_set_saveperfix: "Set chat history file's save perfix"
help_set_loglevel: "Set log level:"
help_direct_query: "The direct query to GPT"
#
help_use_help: "Use `[deep_sky_blue3]/help[/]` to see all available slash commands"
help_uncommand: "Unrecognized Slash Command `[bold red]%{command}[/]`"
Expand Down
1 change: 1 addition & 0 deletions gpt_term/locale/gpt_term.jp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ jp:
help_set_lang: "言語を設定する"
help_set_saveperfix: "チャット履歴ファイルの保存プレフィックスを設定する"
help_set_loglevel: "ログレベルを設定する:"
help_direct_query: "GPT への直接クエリ"
#
help_use_help: "`[deep_sky_blue3]/help[/]`を使用して利用可能なスラッシュコマンドをすべて表示します"
help_uncommand: "未知のスラッシュコマンド `[bold red]%{command}[/]`"
Expand Down
1 change: 1 addition & 0 deletions gpt_term/locale/gpt_term.zh_CN.yml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ zh_CN:
help_set_lang: "设置语言"
help_set_saveperfix: "设置聊天历史记录文件的保存前缀"
help_set_loglevel: "设置日志级别: "
help_direct_query: "直接向GPT提问的内容"
#
help_use_help: "使用 `[deep_sky_blue3]/help[/]` 查看所有可用命令"
help_uncommand: "无法识别命令 `[bold red]%{command}[/]`"
Expand Down
31 changes: 27 additions & 4 deletions gpt_term/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,6 @@ def send_request(self, data):

def send_request_silent(self, data):
# this is a silent sub function, for sending request without outputs (silently)
# it SHOULD NOT be triggered or used by not-silent functions
# it is only used by gen_title_silent now
try:
response = requests.post(
self.endpoint, headers=self.headers, data=json.dumps(data), timeout=self.timeout)
Expand Down Expand Up @@ -243,6 +241,19 @@ def delete_all_conversation(self):
os.system('cls' if os.name == 'nt' else 'clear')
console.print(_('gpt_term.delete_all'))

def handle_simple(self, message: str):
self.messages.append({"role": "user", "content": message})
data = {
"model": self.model,
"messages": self.messages,
"temperature": self.temperature
}
response = self.send_request_silent(data)
if response:
response_json = response.json()
log.debug(f"Response: {response_json}")
print(response_json["choices"][0]["message"]["content"])

def handle(self, message: str):
try:
self.messages.append({"role": "user", "content": message})
Expand Down Expand Up @@ -1070,6 +1081,8 @@ def main():
parser.add_argument('--set-lang', type=str, choices=['en', 'zh_CN', 'jp', 'de'], help=_("gpt_term.help_set_lang"))
parser.add_argument('--set-saveperfix', metavar='PERFIX', type=str, help=_("gpt_term.help_set_saveperfix"))
parser.add_argument('--set-loglevel', metavar='LEVEL', type=str, help=_("gpt_term.help_set_loglevel")+'DEBUG, INFO, WARNING, ERROR, CRITICAL')
# Query without parameter
parser.add_argument("query", nargs="*", help=_("gpt_term.help_direct_query"))
# setting args
args = parser.parse_args()

Expand Down Expand Up @@ -1115,8 +1128,6 @@ def main():
api_key_log = api_key[:3] + '*' * (len(api_key) - 7) + api_key[-4:]
log.debug(f"Loaded API Key: {api_key_log}")

console.print(_("gpt_term.welcome"))

api_timeout = config.getfloat("OPENAI_API_TIMEOUT", 30)
log.debug(f"API Timeout set to {api_timeout}")

Expand Down Expand Up @@ -1160,6 +1171,18 @@ def main():
log.info(f"Chat history successfully loaded from: {args.load}")
console.print(
_("gpt_term.load_chat_history",load=args.load), highlight=False)

if args.query:
query_text = " ".join(args.query)
log.info(f"> {query_text}")
is_stdout_tty = os.isatty(sys.stdout.fileno())
if is_stdout_tty:
chat_gpt.handle(query_text)
else: # Running in pipe/stream mode
chat_gpt.handle_simple(query_text)
return
else:
console.print(_("gpt_term.welcome"))

session = PromptSession()

Expand Down

0 comments on commit 373ee4c

Please sign in to comment.