1
1
import os
2
- from typing import Optional , cast
2
+ from typing import Optional , Union , cast
3
3
4
4
from revChatGPT .V1 import Chatbot
5
5
6
6
from aishell .exceptions import UnauthorizedAccessError
7
+ from aishell .models import RevChatGPTChatbotConfigModel
7
8
from aishell .utils import make_executable_command
8
9
9
10
from .query_client import QueryClient
10
11
11
12
12
13
class ReverseEngineeredChatGPTClient (QueryClient ):
13
- config : dict [str , str ] = {}
14
+ _config : RevChatGPTChatbotConfigModel
15
+
16
+ @property
17
+ def revchatgpt_config (self ) -> dict [str , Union [str , bool ]]:
18
+ return self ._config .dict (exclude_none = True )
14
19
15
20
def __init__ (
16
21
self ,
@@ -19,21 +24,17 @@ def __init__(
19
24
):
20
25
CHATGPT_ACCESS_TOKEN = os .environ .get ('CHATGPT_ACCESS_TOKEN' , access_token )
21
26
CHATGPT_SESSION_TOKEN = os .environ .get ('CHATGPT_SESSION_TOKEN' , session_token )
22
- if CHATGPT_ACCESS_TOKEN is not None :
23
- self .config [ 'access_token' ] = CHATGPT_ACCESS_TOKEN
24
- elif CHATGPT_SESSION_TOKEN is not None :
25
- self .config [ 'session_token' ] = CHATGPT_SESSION_TOKEN
27
+ if CHATGPT_ACCESS_TOKEN :
28
+ self ._config = RevChatGPTChatbotConfigModel ( access_token = CHATGPT_ACCESS_TOKEN )
29
+ elif CHATGPT_SESSION_TOKEN :
30
+ self ._config = RevChatGPTChatbotConfigModel ( session_token = CHATGPT_SESSION_TOKEN )
26
31
else :
27
32
raise UnauthorizedAccessError ('No access token or session token provided.' )
28
33
29
- def _construct_prompt (self , text : str ) -> str :
30
- return f'''You are now a translater from human language to { os .uname ()[0 ]} shell command.
31
- No explanation required, respond with only the raw shell command.
32
- What should I type to shell for: { text } , in one line.'''
33
-
34
34
def query (self , prompt : str ) -> str :
35
35
prompt = self ._construct_prompt (prompt )
36
- chatbot = Chatbot (config = self .config )
36
+ chatbot = Chatbot (config = self .revchatgpt_config ) # pyright: ignore [reportGeneralTypeIssues]
37
+ # ignore for wrong type hint of revchatgpt
37
38
38
39
response_text = ''
39
40
for data in chatbot .ask (prompt ):
@@ -42,3 +43,8 @@ def query(self, prompt: str) -> str:
42
43
response_text = make_executable_command (cast (str , response_text ))
43
44
44
45
return response_text
46
+
47
+ def _construct_prompt (self , text : str ) -> str :
48
+ return f'''You are now a translater from human language to { os .uname ()[0 ]} shell command.
49
+ No explanation required, respond with only the raw shell command.
50
+ What should I type to shell for: { text } , in one line.'''
0 commit comments