openai-assistant.mdx 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. ### Arguments
  29. <ResponseField name="name" type="string">
  30. Name for your AI assistant
  31. </ResponseField>
  32. <ResponseField name="instructions" type="string">
  33. how the Assistant and model should behave or respond
  34. </ResponseField>
  35. <ResponseField name="assistant_id" type="string">
  36. Load existing OpenAI Assistant. If you pass this, you don't have to pass other arguments.
  37. </ResponseField>
  38. <ResponseField name="thread_id" type="string">
  39. Existing OpenAI thread id if exists
  40. </ResponseField>
  41. <ResponseField name="model" type="str" default="gpt-4-1106-preview">
  42. OpenAI model to use
  43. </ResponseField>
  44. <ResponseField name="tools" type="list">
  45. OpenAI tools to use. Default set to `[{"type": "retrieval"}]`
  46. </ResponseField>
  47. <ResponseField name="data_sources" type="list" default="[]">
  48. Add data sources to your assistant. You can add in the following format: `[{"source": "https://example.com", "data_type": "web_page"}]`
  49. </ResponseField>
  50. <ResponseField name="telemetry" type="boolean" default="True">
  51. Anonymous telemetry (doesn't collect any user information or user's files). Used to improve the Embedchain package utilization. Default is `True`.
  52. </ResponseField>
  53. ## Step-2: Add data to thread
  54. 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.
  55. ```python Add data
  56. assistant.add("/path/to/file.pdf")
  57. assistant.add("https://www.youtube.com/watch?v=U9mJuUkhUzk")
  58. assistant.add("https://openai.com/blog/new-models-and-developer-products-announced-at-devday")
  59. ```
  60. ## Step-3: Chat with your Assistant
  61. ```python Chat
  62. assistant.chat("How much OpenAI credits were offered to attendees during OpenAI DevDay?")
  63. # Response: 'Every attendee of OpenAI DevDay 2023 was offered $500 in OpenAI credits.'
  64. ```
  65. You can try it out yourself using the following Google Colab notebook:
  66. <a href="https://colab.research.google.com/drive/1BKlXZYSl6AFRgiHZ5XIzXrXC_24kDYHQ?usp=sharing">
  67. <img src="https://camo.githubusercontent.com/84f0493939e0c4de4e6dbe113251b4bfb5353e57134ffd9fcab6b8714514d4d1/68747470733a2f2f636f6c61622e72657365617263682e676f6f676c652e636f6d2f6173736574732f636f6c61622d62616467652e737667" alt="Open in Colab" />
  68. </a>