Skip to content

Commit d9c4b23

Browse files
committed
Add OfficialChatGPTClient
1 parent 7ab4f12 commit d9c4b23

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

aishell/query_clients/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
from .gpt3_client import GPT3Client as GPT3Client
2+
from .official_chatgpt_client import OfficialChatGPTClient as OfficialChatGPTClient
23
from .query_client import QueryClient as QueryClient
34
from .reverse_engineered_chatgpt_client import ReverseEngineeredChatGPTClient as ReverseEngineeredChatGPTClient
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import os
2+
from typing import Final, Optional
3+
4+
from revChatGPT.V3 import Chatbot
5+
6+
from aishell.utils import make_executable_command
7+
8+
from .query_client import QueryClient
9+
10+
11+
class OfficialChatGPTClient(QueryClient):
12+
openai_api_key: str
13+
14+
def __init__(
15+
self,
16+
openai_api_key: Optional[str] = None,
17+
):
18+
super().__init__()
19+
OPENAI_API_KEY: Final[Optional[str]] = os.environ.get('OPENAI_API_KEY', openai_api_key)
20+
if OPENAI_API_KEY is None:
21+
raise Exception('OPENAI_API_KEY should not be none')
22+
23+
self.openai_api_key = OPENAI_API_KEY
24+
25+
def _construct_prompt(self, text: str) -> str:
26+
return f'''You are now a translater from human language to {os.uname()[0]} shell command.
27+
No explanation required, respond with only the raw shell command.
28+
What should I type to shell for: {text}, in one line.'''
29+
30+
def query(self, prompt: str) -> str:
31+
prompt = self._construct_prompt(prompt)
32+
33+
chatbot = Chatbot(api_key=self.openai_api_key)
34+
response_text = chatbot.ask(prompt)
35+
36+
return make_executable_command(response_text)

0 commit comments

Comments
 (0)