main.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/usr/bin/env python3
  2. from typing import List, Tuple
  3. from langchain.llms import OpenAI
  4. from nicegui import Client, ui
  5. OPENAI_API_KEY = 'not-set' # TODO: set your OpenAI API key here
  6. llm = OpenAI(model_name='gpt-3.5-turbo', openai_api_key=OPENAI_API_KEY)
  7. messages: List[Tuple[str, str, str]] = []
  8. thinking: bool = False
  9. @ui.refreshable
  10. async def chat_messages() -> None:
  11. for name, text in messages:
  12. ui.chat_message(text=text, name=name, sent=name == 'You')
  13. if thinking:
  14. ui.spinner(size='3rem').classes('self-center')
  15. await ui.run_javascript('window.scrollTo(0, document.body.scrollHeight)', respond=False)
  16. @ui.page('/')
  17. async def main(client: Client):
  18. async def send() -> None:
  19. global thinking
  20. message = text.value
  21. messages.append(('You', text.value))
  22. thinking = True
  23. text.value = ''
  24. chat_messages.refresh()
  25. response = await llm.agenerate([message])
  26. messages.append(('Bot', response.generations[0][0].text))
  27. thinking = False
  28. chat_messages.refresh()
  29. anchor_style = r'a:link, a:visited {color: inherit !important; text-decoration: none; font-weight: 500}'
  30. ui.add_head_html(f'<style>{anchor_style}</style>')
  31. await client.connected()
  32. with ui.column().classes('w-full max-w-2xl mx-auto items-stretch'):
  33. await chat_messages()
  34. with ui.footer().classes('bg-white'), ui.column().classes('w-full max-w-3xl mx-auto my-6'):
  35. with ui.row().classes('w-full no-wrap items-center'):
  36. placeholder = 'message' if OPENAI_API_KEY != 'not-set' else \
  37. 'Please provide your OPENAI key in the Python script first!'
  38. text = ui.input(placeholder=placeholder).props('rounded outlined input-class=mx-3') \
  39. .classes('w-full self-center').on('keydown.enter', send)
  40. ui.markdown('simple chat app built with [NiceGUI](https://nicegui.io)') \
  41. .classes('text-xs self-end mr-8 m-[-1em] text-primary')
  42. ui.run(title='Chat with GPT-3 (example)')