main.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/env python3
  2. from typing import List, Tuple
  3. from langchain.chains import ConversationChain
  4. from langchain.chat_models import ChatOpenAI
  5. from nicegui import Client, ui
  6. from log_callback_handler import NiceGuiLogElementCallbackHandler
  7. OPENAI_API_KEY = "not-set" # TODO: set your OpenAI API key here
  8. llm = ConversationChain(
  9. llm=ChatOpenAI(model_name="gpt-3.5-turbo", openai_api_key=OPENAI_API_KEY),
  10. )
  11. messages: List[Tuple[str, str]] = []
  12. thinking: bool = False
  13. @ui.refreshable
  14. async def chat_messages() -> None:
  15. for name, text in messages:
  16. ui.chat_message(text=text, name=name, sent=name == "You")
  17. if thinking:
  18. ui.spinner(size="3rem").classes("self-center")
  19. await ui.run_javascript(
  20. "window.scrollTo(0, document.body.scrollHeight)", respond=False
  21. )
  22. @ui.page("/")
  23. async def main(client: Client):
  24. with ui.tabs().classes("w-full") as tabs:
  25. chat_panel = ui.tab("Chat")
  26. logs_panel = ui.tab("Logs")
  27. with ui.tab_panels(tabs).classes("w-full"):
  28. with ui.tab_panel(logs_panel):
  29. log = ui.log().classes("w-full h-full")
  30. async def send() -> None:
  31. global thinking
  32. message = text.value
  33. messages.append(("You", text.value))
  34. thinking = True
  35. text.value = ""
  36. chat_messages.refresh()
  37. response = await llm.arun(
  38. message, callbacks=[NiceGuiLogElementCallbackHandler(log)]
  39. )
  40. messages.append(("Bot", response))
  41. thinking = False
  42. chat_messages.refresh()
  43. anchor_style = r"a:link, a:visited {color: inherit !important; text-decoration: none; font-weight: 500}"
  44. ui.add_head_html(f"<style>{anchor_style}</style>")
  45. await client.connected()
  46. with ui.tab_panels(tabs, value=chat_panel).classes("w-full"):
  47. with ui.tab_panel(chat_panel):
  48. with ui.column().classes("w-full max-w-2xl mx-auto items-stretch"):
  49. await chat_messages()
  50. with ui.footer().classes("bg-white"), ui.column().classes(
  51. "w-full max-w-3xl mx-auto my-6"
  52. ):
  53. with ui.row().classes("w-full no-wrap items-center"):
  54. placeholder = (
  55. "message"
  56. if OPENAI_API_KEY != "not-set"
  57. else "Please provide your OPENAI key in the Python script first!"
  58. )
  59. text = (
  60. ui.input(placeholder=placeholder)
  61. .props("rounded outlined input-class=mx-3")
  62. .classes("w-full self-center")
  63. .on("keydown.enter", send)
  64. )
  65. ui.markdown("simple chat app built with [NiceGUI](https://nicegui.io)").classes(
  66. "text-xs self-end mr-8 m-[-1em] text-primary"
  67. )
  68. ui.run(title="Chat with GPT-3 (example)")