Skip to content

Commit ef1db33

Browse files
authored
Merge pull request #25 from code-yeongyu/feature/chatgpt-official
Add support for Official ChatGPT API 🚀
2 parents 3a2b8f0 + ce01957 commit ef1db33

File tree

11 files changed

+131
-37
lines changed

11 files changed

+131
-37
lines changed

README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,13 @@ aishell --help
4343
1. Set the API key as an environment variable `CHATGPT_ACCESS_KEY`
4444
1. Enjoy AiShell
4545

46-
### For those who want to use `GPT-3`
46+
### For those who want to use `Official ChatGPT(GPT3.5-turbo)` or `GPT-3`
4747

4848
1. Create account on OpenAI
4949
1. Go to <https://platform.openai.com/account/api-keys>, Copy API key
5050
1. Set the API key as an environment variable `OPENAI_API_KEY`
5151
1. Enjoy AiShell
5252

53-
### For those who want to use Official ChatGPT API `gpt-3.5-turbo`
54-
55-
- Currently not supported, but soon will be supported!
56-
5753
## Contributions 💬
5854

5955
Feel free to contribute to AiShell by adding more functionality or fixing bugs.

aishell/cli.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,23 @@
33
import typer
44
from rich.console import Console
55

6-
from aishell.query_clients import ChatGPTClient, GPT3Client, QueryClient
6+
from aishell.models.language_model import LanguageModel
7+
from aishell.query_clients import GPT3Client, OfficialChatGPTClient, QueryClient, ReverseEngineeredChatGPTClient
78

89
cli_app = typer.Typer()
910

1011

1112
@cli_app.command()
12-
def ask(question: str, use_chatgpt: bool = False):
13+
def ask(question: str, language_model: LanguageModel = LanguageModel.OFFICIAL_CHATGPT):
1314
query_client: QueryClient
14-
if use_chatgpt:
15-
query_client = ChatGPTClient()
16-
else:
15+
if language_model == LanguageModel.GPT3:
1716
query_client = GPT3Client()
17+
elif language_model == LanguageModel.OFFICIAL_CHATGPT:
18+
query_client = OfficialChatGPTClient()
19+
elif language_model == LanguageModel.REVERSE_ENGINEERED_CHATGPT:
20+
query_client = ReverseEngineeredChatGPTClient()
21+
22+
query_client.query(question)
1823

1924
console = Console()
2025
with console.status(

aishell/models/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
from .language_model import LanguageModel as LanguageModel
12
from .open_ai_response_model import OpenAIResponseModel as OpenAIResponseModel

aishell/models/language_model.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from enum import auto
2+
3+
from aishell.utils import StrEnum
4+
5+
6+
class LanguageModel(StrEnum):
7+
OFFICIAL_CHATGPT = auto()
8+
REVERSE_ENGINEERED_CHATGPT = auto()
9+
GPT3 = auto()

aishell/query_clients/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
from .chatgpt_client import ChatGPTClient as ChatGPTClient
21
from .gpt3_client import GPT3Client as GPT3Client
2+
from .official_chatgpt_client import OfficialChatGPTClient as OfficialChatGPTClient
33
from .query_client import QueryClient as QueryClient
4+
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)

aishell/query_clients/chatgpt_client.py renamed to aishell/query_clients/reverse_engineered_chatgpt_client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import os
2-
from typing import Optional
2+
from typing import Optional, cast
33

44
from revChatGPT.V1 import Chatbot
55

@@ -8,7 +8,7 @@
88
from .query_client import QueryClient
99

1010

11-
class ChatGPTClient(QueryClient):
11+
class ReverseEngineeredChatGPTClient(QueryClient):
1212
access_key: str
1313

1414
def __init__(
@@ -38,6 +38,6 @@ def query(self, prompt: str) -> str:
3838
for data in chatbot.ask(prompt):
3939
response_text = data['message']
4040

41-
response_text = make_executable_command(response_text)
41+
response_text = make_executable_command(cast(str, response_text))
4242

4343
return response_text

aishell/utils/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
from .make_executable_command import make_executable_command as make_executable_command
2+
from .str_enum import StrEnum as StrEnum

aishell/utils/str_enum.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from enum import Enum
2+
from typing import Any
3+
4+
5+
class StrEnum(str, Enum):
6+
7+
def _generate_next_value_(name: str, start: int, count: int, last_values: list[Any]): # type: ignore
8+
return name.lower()
9+
10+
def __repr__(self):
11+
return self.name
12+
13+
def __str__(self):
14+
return self.name

poetry.lock

Lines changed: 53 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)