Skip to content

Commit

Permalink
chat cli to add model param
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjpxie committed May 14, 2024
1 parent 7e103e6 commit 1e6e802
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
13 changes: 6 additions & 7 deletions pxutil/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,15 @@ def chat_main():
description="ChatGPT cli, type q to exit. Set env variable OPENAI_API_KEY first."
)
parser.add_argument(
"--v4", action="store_true", help="use GPT-4-turbo, default is GPT-3.5-turbo"
"-m",
"--model",
default="gpt-3.5-turbo",
help="OpenAI chatGPT model, gpt-3.5-turbo (default), gpt-4o etc.",
)
args = parser.parse_args()

register_signal_ctrl_c()
if args.v4:
model = "gpt-4-turbo"
else:
model = "gpt-3.5-turbo"
chat = ChatAPI(model=model)
chat = ChatAPI(model=args.model.strip())
while True:
question = input("> ")
if question in ("q", "quit"):
Expand Down Expand Up @@ -128,4 +127,4 @@ def listmod_main():
# sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))

# loop_main()
listmod_main()
chat_main()
12 changes: 6 additions & 6 deletions pxutil/pxutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ def grep(pattern, string=None, filename=None):
return result


def replace_in_file(files, old, new, backup=None):
def replace_in_file(files, old, new, backup=""):
"""
Replace in place directly on a file.
Expand Down Expand Up @@ -830,15 +830,15 @@ def chat(self, question: str):
if isinstance(resp, Exception):
return Exception("Chat API request failed with error: %s." % resp)

if len(resp["choices"]) >= 1:
for choice in resp["choices"]:
if choice["index"] == 0 and choice["finish_reason"] in ("stop", None):
answer = choice["message"]["content"]
if len(resp["choices"]) >= 1: # type: ignore
for choice in resp["choices"]: # type: ignore
if choice["index"] == 0 and choice["finish_reason"] in ("stop", None): # type: ignore
answer = choice["message"]["content"] # type: ignore
answer = answer.strip("\n").strip()
# record chat history
if self.remember_chat_history:
self.chat_history.append({"role": "user", "content": question})
self.chat_history.append(choice["message"])
self.chat_history.append(choice["message"]) # type: ignore
return answer

return Exception(
Expand Down

0 comments on commit 1e6e802

Please sign in to comment.