Antonio Cheong commited on
Commit
9853364
·
1 Parent(s): 6404fd6

better prompting

Browse files
Files changed (1) hide show
  1. src/EdgeGPT.py +19 -17
src/EdgeGPT.py CHANGED
@@ -19,10 +19,10 @@ from typing import Union
19
  import certifi
20
  import httpx
21
  import websockets.client as websockets
22
- from prompt_toolkit import prompt
23
  from prompt_toolkit import PromptSession
24
  from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
25
  from prompt_toolkit.completion import WordCompleter
 
26
  from rich.live import Live
27
  from rich.markdown import Markdown
28
 
@@ -350,35 +350,37 @@ class Chatbot:
350
  self.chat_hub = ChatHub(Conversation(self.cookiePath, self.cookies))
351
 
352
 
353
- async def get_input(
354
  session: PromptSession = None,
355
  completer: WordCompleter = None,
356
  ) -> str:
357
  """
358
  Multiline input function.
359
  """
360
- return (
361
- await session.prompt_async(
362
- completer=completer,
363
- multiline=True,
364
- auto_suggest=AutoSuggestFromHistory(),
365
- )
366
- if session
367
- else prompt(multiline=True)
368
  )
369
 
370
 
 
 
 
 
371
  async def main():
372
  """
373
  Main function
374
  """
375
  print("Initializing...")
376
  bot = Chatbot()
 
377
  while True:
378
- prompt = get_input("\nYou:\n")
379
- if prompt == "!exit":
 
380
  break
381
- elif prompt == "!help":
382
  print(
383
  """
384
  !help - Show this help message
@@ -387,13 +389,13 @@ async def main():
387
  """,
388
  )
389
  continue
390
- elif prompt == "!reset":
391
  await bot.reset()
392
  continue
393
  print("Bot:")
394
  if args.no_stream:
395
  print(
396
- (await bot.ask(prompt=prompt, conversation_style=args.style))["item"][
397
  "messages"
398
  ][1]["adaptiveCards"][0]["body"][0]["text"],
399
  )
@@ -403,7 +405,7 @@ async def main():
403
  md = Markdown("")
404
  with Live(md, auto_refresh=False) as live:
405
  async for final, response in bot.ask_stream(
406
- prompt=prompt,
407
  conversation_style=args.style,
408
  ):
409
  if not final:
@@ -416,7 +418,7 @@ async def main():
416
  else:
417
  wrote = 0
418
  async for final, response in bot.ask_stream(
419
- prompt=prompt,
420
  conversation_style=args.style,
421
  ):
422
  if not final:
 
19
  import certifi
20
  import httpx
21
  import websockets.client as websockets
 
22
  from prompt_toolkit import PromptSession
23
  from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
24
  from prompt_toolkit.completion import WordCompleter
25
+ from prompt_toolkit.history import InMemoryHistory
26
  from rich.live import Live
27
  from rich.markdown import Markdown
28
 
 
350
  self.chat_hub = ChatHub(Conversation(self.cookiePath, self.cookies))
351
 
352
 
353
+ async def get_input_async(
354
  session: PromptSession = None,
355
  completer: WordCompleter = None,
356
  ) -> str:
357
  """
358
  Multiline input function.
359
  """
360
+ return await session.prompt_async(
361
+ completer=completer,
362
+ multiline=True,
363
+ auto_suggest=AutoSuggestFromHistory(),
 
 
 
 
364
  )
365
 
366
 
367
+ def create_session() -> PromptSession:
368
+ return PromptSession(history=InMemoryHistory())
369
+
370
+
371
  async def main():
372
  """
373
  Main function
374
  """
375
  print("Initializing...")
376
  bot = Chatbot()
377
+ session = create_session()
378
  while True:
379
+ print("\nYou:\n")
380
+ question = await get_input_async(session=session)
381
+ if question == "!exit":
382
  break
383
+ elif question == "!help":
384
  print(
385
  """
386
  !help - Show this help message
 
389
  """,
390
  )
391
  continue
392
+ elif question == "!reset":
393
  await bot.reset()
394
  continue
395
  print("Bot:")
396
  if args.no_stream:
397
  print(
398
+ (await bot.ask(prompt=question, conversation_style=args.style))["item"][
399
  "messages"
400
  ][1]["adaptiveCards"][0]["body"][0]["text"],
401
  )
 
405
  md = Markdown("")
406
  with Live(md, auto_refresh=False) as live:
407
  async for final, response in bot.ask_stream(
408
+ prompt=question,
409
  conversation_style=args.style,
410
  ):
411
  if not final:
 
418
  else:
419
  wrote = 0
420
  async for final, response in bot.ask_stream(
421
+ prompt=question,
422
  conversation_style=args.style,
423
  ):
424
  if not final: