embedding-models.mdx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. ---
  2. title: 🧩 Embedding models
  3. ---
  4. ## Overview
  5. Embedchain supports several embedding models from the following providers:
  6. <CardGroup cols={4}>
  7. <Card title="OpenAI" href="#openai"></Card>
  8. <Card title="GoogleAI" href="#google-ai"></Card>
  9. <Card title="Azure OpenAI" href="#azure-openai"></Card>
  10. <Card title="GPT4All" href="#gpt4all"></Card>
  11. <Card title="Hugging Face" href="#hugging-face"></Card>
  12. <Card title="Vertex AI" href="#vertex-ai"></Card>
  13. <Card title="NVIDIA AI" href="#nvidia-ai"></Card>
  14. </CardGroup>
  15. ## OpenAI
  16. To use OpenAI embedding function, you have to set the `OPENAI_API_KEY` environment variable. You can obtain the OpenAI API key from the [OpenAI Platform](https://platform.openai.com/account/api-keys).
  17. Once you have obtained the key, you can use it like this:
  18. <CodeGroup>
  19. ```python main.py
  20. import os
  21. from embedchain import App
  22. os.environ['OPENAI_API_KEY'] = 'xxx'
  23. # load embedding model configuration from config.yaml file
  24. app = App.from_config(config_path="config.yaml")
  25. app.add("https://en.wikipedia.org/wiki/OpenAI")
  26. app.query("What is OpenAI?")
  27. ```
  28. ```yaml config.yaml
  29. embedder:
  30. provider: openai
  31. config:
  32. model: 'text-embedding-3-small'
  33. ```
  34. </CodeGroup>
  35. * OpenAI announced two new embedding models: `text-embedding-3-small` and `text-embedding-3-large`. Embedchain supports both these models. Below you can find YAML config for both:
  36. <CodeGroup>
  37. ```yaml text-embedding-3-small.yaml
  38. embedder:
  39. provider: openai
  40. config:
  41. model: 'text-embedding-3-small'
  42. ```
  43. ```yaml text-embedding-3-large.yaml
  44. embedder:
  45. provider: openai
  46. config:
  47. model: 'text-embedding-3-large'
  48. ```
  49. </CodeGroup>
  50. ## Google AI
  51. To use Google AI embedding function, you have to set the `GOOGLE_API_KEY` environment variable. You can obtain the Google API key from the [Google Maker Suite](https://makersuite.google.com/app/apikey)
  52. <CodeGroup>
  53. ```python main.py
  54. import os
  55. from embedchain import App
  56. os.environ["GOOGLE_API_KEY"] = "xxx"
  57. app = App.from_config(config_path="config.yaml")
  58. ```
  59. ```yaml config.yaml
  60. embedder:
  61. provider: google
  62. config:
  63. model: 'models/embedding-001'
  64. task_type: "retrieval_document"
  65. title: "Embeddings for Embedchain"
  66. ```
  67. </CodeGroup>
  68. <br/>
  69. <Note>
  70. For more details regarding the Google AI embedding model, please refer to the [Google AI documentation](https://ai.google.dev/tutorials/python_quickstart#use_embeddings).
  71. </Note>
  72. ## Azure OpenAI
  73. To use Azure OpenAI embedding model, you have to set some of the azure openai related environment variables as given in the code block below:
  74. <CodeGroup>
  75. ```python main.py
  76. import os
  77. from embedchain import App
  78. os.environ["OPENAI_API_TYPE"] = "azure"
  79. os.environ["AZURE_OPENAI_ENDPOINT"] = "https://xxx.openai.azure.com/"
  80. os.environ["AZURE_OPENAI_API_KEY"] = "xxx"
  81. os.environ["OPENAI_API_VERSION"] = "xxx"
  82. app = App.from_config(config_path="config.yaml")
  83. ```
  84. ```yaml config.yaml
  85. llm:
  86. provider: azure_openai
  87. config:
  88. model: gpt-35-turbo
  89. deployment_name: your_llm_deployment_name
  90. temperature: 0.5
  91. max_tokens: 1000
  92. top_p: 1
  93. stream: false
  94. embedder:
  95. provider: azure_openai
  96. config:
  97. model: text-embedding-ada-002
  98. deployment_name: you_embedding_model_deployment_name
  99. ```
  100. </CodeGroup>
  101. You can find the list of models and deployment name on the [Azure OpenAI Platform](https://oai.azure.com/portal).
  102. ## GPT4ALL
  103. GPT4All supports generating high quality embeddings of arbitrary length documents of text using a CPU optimized contrastively trained Sentence Transformer.
  104. <CodeGroup>
  105. ```python main.py
  106. from embedchain import App
  107. # load embedding model configuration from config.yaml file
  108. app = App.from_config(config_path="config.yaml")
  109. ```
  110. ```yaml config.yaml
  111. llm:
  112. provider: gpt4all
  113. config:
  114. model: 'orca-mini-3b-gguf2-q4_0.gguf'
  115. temperature: 0.5
  116. max_tokens: 1000
  117. top_p: 1
  118. stream: false
  119. embedder:
  120. provider: gpt4all
  121. ```
  122. </CodeGroup>
  123. ## Hugging Face
  124. Hugging Face supports generating embeddings of arbitrary length documents of text using Sentence Transformer library. Example of how to generate embeddings using hugging face is given below:
  125. <CodeGroup>
  126. ```python main.py
  127. from embedchain import App
  128. # load embedding model configuration from config.yaml file
  129. app = App.from_config(config_path="config.yaml")
  130. ```
  131. ```yaml config.yaml
  132. llm:
  133. provider: huggingface
  134. config:
  135. model: 'google/flan-t5-xxl'
  136. temperature: 0.5
  137. max_tokens: 1000
  138. top_p: 0.5
  139. stream: false
  140. embedder:
  141. provider: huggingface
  142. config:
  143. model: 'sentence-transformers/all-mpnet-base-v2'
  144. ```
  145. </CodeGroup>
  146. ## Vertex AI
  147. Embedchain supports Google's VertexAI embeddings model through a simple interface. You just have to pass the `model_name` in the config yaml and it would work out of the box.
  148. <CodeGroup>
  149. ```python main.py
  150. from embedchain import App
  151. # load embedding model configuration from config.yaml file
  152. app = App.from_config(config_path="config.yaml")
  153. ```
  154. ```yaml config.yaml
  155. llm:
  156. provider: vertexai
  157. config:
  158. model: 'chat-bison'
  159. temperature: 0.5
  160. top_p: 0.5
  161. embedder:
  162. provider: vertexai
  163. config:
  164. model: 'textembedding-gecko'
  165. ```
  166. </CodeGroup>
  167. ## NVIDIA AI
  168. [NVIDIA AI Foundation Endpoints](https://www.nvidia.com/en-us/ai-data-science/foundation-models/) let you quickly use NVIDIA's AI models, such as Mixtral 8x7B, Llama 2 etc, through our API. These models are available in the [NVIDIA NGC catalog](https://catalog.ngc.nvidia.com/ai-foundation-models), fully optimized and ready to use on NVIDIA's AI platform. They are designed for high speed and easy customization, ensuring smooth performance on any accelerated setup.
  169. ### Usage
  170. In order to use embedding models and LLMs from NVIDIA AI, create an account on [NVIDIA NGC Service](https://catalog.ngc.nvidia.com/).
  171. Generate an API key from their dashboard. Set the API key as `NVIDIA_API_KEY` environment variable. Note that the `NVIDIA_API_KEY` will start with `nvapi-`.
  172. Below is an example of how to use LLM model and embedding model from NVIDIA AI:
  173. <CodeGroup>
  174. ```python main.py
  175. import os
  176. from embedchain import App
  177. os.environ['NVIDIA_API_KEY'] = 'nvapi-xxxx'
  178. config = {
  179. "app": {
  180. "config": {
  181. "id": "my-app",
  182. },
  183. },
  184. "llm": {
  185. "provider": "nvidia",
  186. "config": {
  187. "model": "nemotron_steerlm_8b",
  188. },
  189. },
  190. "embedder": {
  191. "provider": "nvidia",
  192. "config": {
  193. "model": "nvolveqa_40k",
  194. "vector_dimension": 1024,
  195. },
  196. },
  197. }
  198. app = App.from_config(config=config)
  199. app.add("https://www.forbes.com/profile/elon-musk")
  200. answer = app.query("What is the net worth of Elon Musk today?")
  201. # Answer: The net worth of Elon Musk is subject to fluctuations based on the market value of his holdings in various companies.
  202. # As of March 1, 2024, his net worth is estimated to be approximately $210 billion. However, this figure can change rapidly due to stock market fluctuations and other factors.
  203. # Additionally, his net worth may include other assets such as real estate and art, which are not reflected in his stock portfolio.
  204. ```
  205. </CodeGroup>