chainlit.mdx 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. ---
  2. title: '⛓️ Chainlit'
  3. description: 'Integrate with Chainlit to create LLM chat apps'
  4. ---
  5. In this example, we will learn how to use Chainlit and Embedchain together.
  6. ![chainlit-demo](https://github.com/embedchain/embedchain/assets/73601258/d6635624-5cdb-485b-bfbd-3b7c8f18bfff)
  7. ## Setup
  8. First, install the required packages:
  9. ```bash
  10. pip install embedchain chainlit
  11. ```
  12. ## Create a Chainlit app
  13. Create a new file called `app.py` and add the following code:
  14. ```python
  15. import chainlit as cl
  16. from embedchain import App
  17. import os
  18. os.environ["OPENAI_API_KEY"] = "sk-xxx"
  19. @cl.on_chat_start
  20. async def on_chat_start():
  21. app = App.from_config(config={
  22. 'app': {
  23. 'config': {
  24. 'name': 'chainlit-app'
  25. }
  26. },
  27. 'llm': {
  28. 'config': {
  29. 'stream': True,
  30. }
  31. }
  32. })
  33. # import your data here
  34. app.add("https://www.forbes.com/profile/elon-musk/")
  35. app.collect_metrics = False
  36. cl.user_session.set("app", app)
  37. @cl.on_message
  38. async def on_message(message: cl.Message):
  39. app = cl.user_session.get("app")
  40. msg = cl.Message(content="")
  41. for chunk in await cl.make_async(app.chat)(message.content):
  42. await msg.stream_token(chunk)
  43. await msg.send()
  44. ```
  45. ## Run the app
  46. ```
  47. chainlit run app.py
  48. ```
  49. ## Try it out
  50. Open the app in your browser and start chatting with it!