Skip to content

Commit 2656ace

Browse files
authored
Merge pull request #4 from code-yeongyu/feature/split
2 parents a9346b3 + 6289051 commit 2656ace

File tree

10 files changed

+120
-5
lines changed

10 files changed

+120
-5
lines changed

aishell/models/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .open_ai_response_model import OpenAIResponseModel as OpenAIResponseModel
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from typing import List, Optional
2+
3+
from pydantic import BaseModel
4+
5+
6+
class OpenAIResponseModel(BaseModel):
7+
8+
class Choice(BaseModel):
9+
finish_reason: str
10+
index: int
11+
logprobs: Optional[None]
12+
text: Optional[str]
13+
14+
class Usage(BaseModel):
15+
completion_tokens: int
16+
prompt_tokens: int
17+
total_tokens: int
18+
19+
choices: Optional[List[Choice]]
20+
created: int
21+
id: str
22+
model: str
23+
object: str
24+
usage: Usage

aishell/py.typed

Whitespace-only changes.

aishell/query_clients/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .chatgpt_client import ChatGPTClient as ChatGPTClient
2+
from .gpt3_client import GPT3Client as GPT3Client
3+
from .query_client import QueryClient as QueryClient
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import os
2+
from typing import Optional
3+
4+
from revChatGPT.Official import Chatbot
5+
from utils import make_executable_command
6+
7+
from .query_client import QueryClient
8+
9+
10+
class ChatGPTClient(QueryClient):
11+
12+
def __init__(self, api_key: Optional[str] = None):
13+
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
18+
19+
def _construct_prompt(self, text: str) -> str:
20+
return f'''You are now a translater from human language to {os.uname()[0]} shell command.
21+
No explanation required, respond with only the raw shell command.
22+
What should I type to shell for: {text}, in one line.'''
23+
24+
def query(self, prompt: str) -> str:
25+
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)
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 cast
3+
4+
import openai
5+
from models import OpenAIResponseModel
6+
from utils import make_executable_command
7+
8+
from .query_client import QueryClient
9+
10+
11+
class GPT3Client(QueryClient):
12+
13+
def _construct_prompt(self, text: str) -> str:
14+
return f'''User: You are now a translater from human language to {os.uname()[0]} shell command.
15+
No explanation required, respond with only the raw shell command.
16+
What should I type to shell for: {text}, in one line.
17+
18+
You: '''
19+
20+
def query(self, prompt: str) -> str:
21+
prompt = self._construct_prompt(prompt)
22+
23+
completion: OpenAIResponseModel = cast(
24+
OpenAIResponseModel,
25+
openai.Completion.create(
26+
engine='text-davinci-003',
27+
prompt=prompt,
28+
temperature=0.5,
29+
max_tokens=4000 - len(prompt),
30+
stop=['\n\n\n'],
31+
),
32+
)
33+
if not completion.choices or len(completion.choices) == 0 or not completion.choices[0].text:
34+
raise RuntimeError('No response from OpenAI')
35+
response_text: str = completion.choices[0].text
36+
return make_executable_command(response_text)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from abc import ABCMeta, abstractmethod
2+
3+
4+
class QueryClient(metaclass=ABCMeta):
5+
6+
@abstractmethod
7+
def query(self, prompt: str) -> str:
8+
pass

aishell/utils/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .make_executable_command import make_executable_command as make_executable_command
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
def make_executable_command(command: str) -> str:
2+
# starting '\n' or trailing '\n' should be replaced as ''
3+
# starting ' ' or trailing ' ' should be replaced as ''
4+
if command.startswith('\n'):
5+
command = command[1:]
6+
if command.endswith('\n'):
7+
command = command[:-1]
8+
if command.startswith('`'):
9+
command = command[1:]
10+
if command.endswith('`'):
11+
command = command[:-1]
12+
command = command.strip()
13+
command = command.split('User: ')[0]
14+
return command

pyproject.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[build-system]
2-
requires = [ "poetry-core",]
2+
requires = ["poetry-core"]
33
build-backend = "poetry.core.masonry.api"
44

55
[tool.poetry]
@@ -39,8 +39,8 @@ typeCheckingMode = "strict"
3939
stubPath = "./.type_stubs"
4040
venvPath = "./.venv"
4141
pythonPlatform = "All"
42-
include = [ "./*",]
43-
exclude = [ "**/node_modules", "**/__pycache__",]
42+
include = ["./*"]
43+
exclude = ["**/node_modules", "**/__pycache__"]
4444
useLibraryCodeForTypes = true
4545
verboseOutput = false
4646
reportMissingTypeStubs = false
@@ -55,7 +55,7 @@ allow_split_before_dict_value = false
5555

5656
[tool.ruff]
5757
line-length = 119
58-
select = [ "PLC", "PLE", "PLR", "PLW", "E", "W", "F", "I", "Q", "C", "B",]
58+
select = ["PLE", "PLR", "PLW", "E", "W", "F", "I", "Q", "C", "B"]
5959

6060
[tool.autoflake]
6161
check = false
@@ -71,7 +71,7 @@ remove-unused-variables = true
7171
python = "^3.8"
7272
poetry = "^1.3.1"
7373
revchatgpt = "^1.0.7"
74-
typer = {extras = ["all"], version = "^0.7.0"}
74+
typer = { extras = ["all"], version = "^0.7.0" }
7575
openai = "^0.26.5"
7676
pydantic = "^1.10.4"
7777

0 commit comments

Comments
 (0)