Skip to content

Commit 373ee4c

Browse files
authored
Merge pull request #60 from xiaoxx970/xiaoxx970/issue58
参数提问在terminal中直接返回答案
2 parents ca8ba3e + 0d5b834 commit 373ee4c

File tree

7 files changed

+81
-12
lines changed

7 files changed

+81
-12
lines changed

README.md

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,31 @@ Uses the [gpt-3.5-turbo](https://platform.openai.com/docs/guides/chat/chat-compl
2727

2828
## Changelog
2929

30-
### 2023-05-20
30+
### 2023-06-21
3131

32-
- 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
32+
- 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
3333

34-
### 2023-05-18
34+
```sh
35+
gpt-term "What's the weather like today?"
36+
```
3537

36-
- Added multi-language support: English, Chinese, Japanese, German, follow the system language by default, now you can use `/lang` to switch languages
38+
The answer will be printed directly, or piped into a variable
39+
40+
```sh
41+
gpt-term "What's the weather like today" | read answer
42+
echo $answer
43+
```
3744

3845
<details>
3946
<summary>More Change log</summary>
4047

48+
### 2023-05-20
49+
50+
- 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
51+
52+
### 2023-05-18
53+
54+
- Added multi-language support: English, Chinese, Japanese, German, follow the system language by default, now you can use `/lang` to switch languages
4155
### 2023-05-11
4256

4357
- Find the command the user most likely intended to enter when typing an unrecognized command
@@ -49,6 +63,7 @@ Uses the [gpt-3.5-turbo](https://platform.openai.com/docs/guides/chat/chat-compl
4963
- 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
5064

5165
### 2023-04-23
66+
5267
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.
5368

5469
### 2023-04-15
@@ -143,6 +158,12 @@ Or:
143158
python3 -m gpt_term
144159
```
145160

161+
Quick query:
162+
163+
```sh
164+
gpt-term "What's the weather like today?"
165+
```
166+
146167
When entering a question in single-line mode, use `Esc` + `Enter` to start a new line, and use `Enter` to submit the question.
147168

148169
Here are some common shortcut keys (also shortcut keys for the shell):

README.zh-CN.md

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,24 @@
2929

3030
## 更新记录
3131

32+
### 2023-06-21
33+
34+
- 新增直接询问模式,现在你可以运行 `gpt-term` 加上要提问的内容作为参数来进行单次提问和回答
35+
36+
```sh
37+
gpt-term 今天天气怎么样
38+
```
39+
40+
回答将直接被打印,或者使用管道存放于变量中
41+
42+
```sh
43+
gpt-term 今天天气怎么样 | read answer
44+
echo $answer
45+
```
46+
47+
<details>
48+
<summary>更多 Change log</summary>
49+
3250
### 2023-05-20
3351

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

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

40-
<details>
41-
<summary>更多 Change log</summary>
42-
4358
### 2023-05-11
4459

4560
- 在输入未被识别的命令时查找用户最可能想输入的命令
@@ -146,6 +161,12 @@ gpt-term
146161
python3 -m gpt_term
147162
```
148163

164+
单次提问:
165+
166+
```sh
167+
gpt-term 今天天气怎么样
168+
```
169+
149170
在默认的单行模式下输入提问时,使用 `Esc` + `Enter` 换行,`Enter` 提交
150171

151172
以下是一些常见的快捷键(同时也是shell的快捷键):

gpt_term/locale/gpt_term.de.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ de:
125125
help_set_lang: "Sprache einstellen"
126126
help_set_saveperfix: "Speicherperfix für die Chatverlaufsdatei einstellen"
127127
help_set_loglevel: "Log-Stufe einstellen:"
128+
help_direct_query: "Frag GPT direkt an"
128129
#
129130
help_use_help: "Verwenden `[deep_sky_blue3]/help[/]`, um alle verfügbaren Slash-Befehle zu sehen"
130131
help_uncommand: "Unerkannter Slash-Befehl `[bold red]%{command}[/]`"
@@ -151,4 +152,4 @@ de:
151152
/version - Zeigen der Lokal- und der Fernersion von gpt-term an
152153
/lang \[new_language] - Sprache umschalten
153154
/help - Zeigen dieser Hilfemeldung an
154-
/exit - Beenden der Anwendung
155+
/exit - Beenden der Anwendung

gpt_term/locale/gpt_term.en.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ en:
125125
help_set_lang: "Set language"
126126
help_set_saveperfix: "Set chat history file's save perfix"
127127
help_set_loglevel: "Set log level:"
128+
help_direct_query: "The direct query to GPT"
128129
#
129130
help_use_help: "Use `[deep_sky_blue3]/help[/]` to see all available slash commands"
130131
help_uncommand: "Unrecognized Slash Command `[bold red]%{command}[/]`"

gpt_term/locale/gpt_term.jp.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ jp:
125125
help_set_lang: "言語を設定する"
126126
help_set_saveperfix: "チャット履歴ファイルの保存プレフィックスを設定する"
127127
help_set_loglevel: "ログレベルを設定する:"
128+
help_direct_query: "GPT への直接クエリ"
128129
#
129130
help_use_help: "`[deep_sky_blue3]/help[/]`を使用して利用可能なスラッシュコマンドをすべて表示します"
130131
help_uncommand: "未知のスラッシュコマンド `[bold red]%{command}[/]`"

gpt_term/locale/gpt_term.zh_CN.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ zh_CN:
125125
help_set_lang: "设置语言"
126126
help_set_saveperfix: "设置聊天历史记录文件的保存前缀"
127127
help_set_loglevel: "设置日志级别: "
128+
help_direct_query: "直接向GPT提问的内容"
128129
#
129130
help_use_help: "使用 `[deep_sky_blue3]/help[/]` 查看所有可用命令"
130131
help_uncommand: "无法识别命令 `[bold red]%{command}[/]`"

gpt_term/main.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,6 @@ def send_request(self, data):
160160

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

244+
def handle_simple(self, message: str):
245+
self.messages.append({"role": "user", "content": message})
246+
data = {
247+
"model": self.model,
248+
"messages": self.messages,
249+
"temperature": self.temperature
250+
}
251+
response = self.send_request_silent(data)
252+
if response:
253+
response_json = response.json()
254+
log.debug(f"Response: {response_json}")
255+
print(response_json["choices"][0]["message"]["content"])
256+
246257
def handle(self, message: str):
247258
try:
248259
self.messages.append({"role": "user", "content": message})
@@ -1070,6 +1081,8 @@ def main():
10701081
parser.add_argument('--set-lang', type=str, choices=['en', 'zh_CN', 'jp', 'de'], help=_("gpt_term.help_set_lang"))
10711082
parser.add_argument('--set-saveperfix', metavar='PERFIX', type=str, help=_("gpt_term.help_set_saveperfix"))
10721083
parser.add_argument('--set-loglevel', metavar='LEVEL', type=str, help=_("gpt_term.help_set_loglevel")+'DEBUG, INFO, WARNING, ERROR, CRITICAL')
1084+
# Query without parameter
1085+
parser.add_argument("query", nargs="*", help=_("gpt_term.help_direct_query"))
10731086
# setting args
10741087
args = parser.parse_args()
10751088

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

1118-
console.print(_("gpt_term.welcome"))
1119-
11201131
api_timeout = config.getfloat("OPENAI_API_TIMEOUT", 30)
11211132
log.debug(f"API Timeout set to {api_timeout}")
11221133

@@ -1160,6 +1171,18 @@ def main():
11601171
log.info(f"Chat history successfully loaded from: {args.load}")
11611172
console.print(
11621173
_("gpt_term.load_chat_history",load=args.load), highlight=False)
1174+
1175+
if args.query:
1176+
query_text = " ".join(args.query)
1177+
log.info(f"> {query_text}")
1178+
is_stdout_tty = os.isatty(sys.stdout.fileno())
1179+
if is_stdout_tty:
1180+
chat_gpt.handle(query_text)
1181+
else: # Running in pipe/stream mode
1182+
chat_gpt.handle_simple(query_text)
1183+
return
1184+
else:
1185+
console.print(_("gpt_term.welcome"))
11631186

11641187
session = PromptSession()
11651188

0 commit comments

Comments
 (0)