openai-assistant.mdx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. ---
  2. title: 'OpenAI Assistant'
  3. ---
  4. <img src="https://blogs.swarthmore.edu/its/wp-content/uploads/2022/05/openai.jpg" align="center" width="500" alt="OpenAI Logo"/>
  5. Embedchain now supports [OpenAI Assistants API](https://platform.openai.com/docs/assistants/overview) which allows you to build AI assistants within your own applications. An Assistant has instructions and can leverage models, tools, and knowledge to respond to user queries.
  6. At a high level, an integration of the Assistants API has the following flow:
  7. 1. Create an Assistant in the API by defining custom instructions and picking a model
  8. 2. Create a Thread when a user starts a conversation
  9. 3. Add Messages to the Thread as the user ask questions
  10. 4. Run the Assistant on the Thread to trigger responses. This automatically calls the relevant tools.
  11. Creating an OpenAI Assistant using Embedchain is very simple 3 step process.
  12. ## Step 1: Create OpenAI Assistant
  13. Make sure that you have `OPENAI_API_KEY` set in the environment variable.
  14. ```python Initialize
  15. from embedchain.store.assistants import OpenAIAssistant
  16. assistant = OpenAIAssistant(
  17. name="OpenAI DevDay Assistant",
  18. instructions="You are an organizer of OpenAI DevDay",
  19. )
  20. ```
  21. If you want to use the existing assistant, you can do something like this:
  22. ```python Initialize
  23. # Load an assistant and create a new thread
  24. assistant = OpenAIAssistant(assistant_id="asst_xxx")
  25. # Load a specific thread for an assistant
  26. assistant = OpenAIAssistant(assistant_id="asst_xxx", thread_id="thread_xxx")
  27. ```
  28. ## Step-2: Add data to thread
  29. You can add any custom data source that is supported by Embedchain. Else, you can directly pass the file path on your local system and Embedchain propagates it to OpenAI Assistant.
  30. ```python Add data
  31. assistant.add("/path/to/file.pdf")
  32. assistant.add("https://www.youtube.com/watch?v=U9mJuUkhUzk")
  33. assistant.add("https://openai.com/blog/new-models-and-developer-products-announced-at-devday")
  34. ```
  35. ## Step-3: Chat with your Assistant
  36. ```python Chat
  37. assistant.chat("How much OpenAI credits were offered to attendees during OpenAI DevDay?")
  38. # Response: 'Every attendee of OpenAI DevDay 2023 was offered $500 in OpenAI credits.'
  39. ```
  40. You can try it out yourself using the following Google Colab notebook:
  41. <a href="https://colab.research.google.com/drive/1BKlXZYSl6AFRgiHZ5XIzXrXC_24kDYHQ?usp=sharing">
  42. <img src="https://camo.githubusercontent.com/84f0493939e0c4de4e6dbe113251b4bfb5353e57134ffd9fcab6b8714514d4d1/68747470733a2f2f636f6c61622e72657365617263682e676f6f676c652e636f6d2f6173736574732f636f6c61622d62616467652e737667" alt="Open in Colab" />
  43. </a>