Skip to content

Commit 4432a58

Browse files
committed
Make ChatGPT workable using access token
1 parent adad8d7 commit 4432a58

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed
Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
11
import os
22
from typing import Optional
33

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
67

78
from .query_client import QueryClient
89

910

1011
class ChatGPTClient(QueryClient):
12+
access_key: str
1113

12-
def __init__(self, api_key: Optional[str] = None):
14+
def __init__(
15+
self,
16+
chatgpt_access_key: Optional[str] = None,
17+
):
1318
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')
1827

1928
def _construct_prompt(self, text: str) -> str:
2029
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:
2332

2433
def query(self, prompt: str) -> str:
2534
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

Comments
 (0)