main.py 2.7 KB

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