1
1
import os
2
2
from typing import Optional
3
3
4
- from revChatGPT .Official import Chatbot
5
- from utils import make_executable_command
4
+ from revChatGPT .V1 import Chatbot
5
+
6
+ from aishell .utils import make_executable_command
6
7
7
8
from .query_client import QueryClient
8
9
9
10
10
11
class ChatGPTClient (QueryClient ):
12
+ access_key : str
11
13
12
- def __init__ (self , api_key : Optional [str ] = None ):
14
+ def __init__ (
15
+ self ,
16
+ chatgpt_access_key : Optional [str ] = None ,
17
+ ):
13
18
super ().__init__ ()
14
- OPEN_API_KEY = os .environ .get ('OPENAI_API_KEY' )
15
- if api_key is None or OPEN_API_KEY is None :
16
- raise Exception ('api_key should not be none' )
17
- self .API_KEY = api_key or OPEN_API_KEY
19
+ CHATGPT_ACCESS_KEY = os .environ .get ('CHATGPT_ACCESS_KEY' )
20
+
21
+ if chatgpt_access_key is not None :
22
+ self .access_key = chatgpt_access_key
23
+ elif CHATGPT_ACCESS_KEY is not None :
24
+ self .access_key = CHATGPT_ACCESS_KEY
25
+ else :
26
+ raise Exception ('access_key should not be none' )
18
27
19
28
def _construct_prompt (self , text : str ) -> str :
20
29
return f'''You are now a translater from human language to { os .uname ()[0 ]} shell command.
@@ -23,6 +32,12 @@ def _construct_prompt(self, text: str) -> str:
23
32
24
33
def query (self , prompt : str ) -> str :
25
34
prompt = self ._construct_prompt (prompt )
26
- chatbot = Chatbot (api_key = self .API_KEY )
27
- response_text : str = chatbot .ask (prompt )['choices' ][0 ]['text' ]
28
- return make_executable_command (response_text )
35
+ chatbot = Chatbot (config = {'access_token' : self .access_key })
36
+
37
+ response_text = ''
38
+ for data in chatbot .ask (prompt ):
39
+ responsed_text = data ['message' ]
40
+
41
+ responsed_text = make_executable_command (responsed_text )
42
+
43
+ return response_text
0 commit comments