File tree Expand file tree Collapse file tree 4 files changed +75
-0
lines changed Expand file tree Collapse file tree 4 files changed +75
-0
lines changed Original file line number Diff line number Diff line change 1+ from .chatgpt_client import ChatGPTClient as ChatGPTClient
2+ from .gpt3_client import GPT3Client as GPT3Client
3+ from .query_client import QueryClient as QueryClient
Original file line number Diff line number Diff line change 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 )
Original file line number Diff line number Diff line change 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 )
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments