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