main.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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(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. with ui.tabs().classes('w-full') as tabs:
  21. chat_panel = ui.tab('Chat')
  22. logs_panel = ui.tab('Logs')
  23. with ui.tab_panels(tabs).classes('w-full'):
  24. with ui.tab_panel(logs_panel):
  25. log = ui.log().classes('w-full').style('height: 50vh;')
  26. async def send() -> None:
  27. global thinking
  28. message = text.value
  29. messages.append(('You', text.value))
  30. thinking = True
  31. text.value = ''
  32. chat_messages.refresh()
  33. response = await llm.arun(message, callbacks=[NiceGuiLogElementCallbackHandler(log)])
  34. messages.append(('Bot', response))
  35. thinking = False
  36. chat_messages.refresh()
  37. anchor_style = r'a:link, a:visited {color: inherit !important; text-decoration: none; font-weight: 500}'
  38. ui.add_head_html(f'<style>{anchor_style}</style>')
  39. await client.connected()
  40. with ui.tab_panels(tabs, value=chat_panel).classes('w-full'):
  41. with ui.tab_panel(chat_panel):
  42. with ui.column().classes('w-full max-w-2xl mx-auto items-stretch'):
  43. await chat_messages()
  44. with ui.footer().classes('bg-white'), ui.column().classes('w-full max-w-3xl mx-auto my-6'):
  45. with ui.row().classes('w-full no-wrap items-center'):
  46. placeholder = 'message' if OPENAI_API_KEY != 'not-set' else \
  47. 'Please provide your OPENAI key in the Python script first!'
  48. text = ui.input(placeholder=placeholder).props('rounded outlined input-class=mx-3') \
  49. .classes('w-full self-center').on('keydown.enter', send)
  50. ui.markdown('simple chat app built with [NiceGUI](https://nicegui.io)') \
  51. .classes('text-xs self-end mr-8 m-[-1em] text-primary')
  52. ui.run(title='Chat with GPT-3 (example)')