embedding-models.mdx 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. <Card title="Cohere" href="#cohere"></Card>
  15. </CardGroup>
  16. ## OpenAI
  17. 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).
  18. Once you have obtained the key, you can use it like this:
  19. <CodeGroup>
  20. ```python main.py
  21. import os
  22. from embedchain import App
  23. os.environ['OPENAI_API_KEY'] = 'xxx'
  24. # load embedding model configuration from config.yaml file
  25. app = App.from_config(config_path="config.yaml")
  26. app.add("https://en.wikipedia.org/wiki/OpenAI")
  27. app.query("What is OpenAI?")
  28. ```
  29. ```yaml config.yaml
  30. embedder:
  31. provider: openai
  32. config:
  33. model: 'text-embedding-3-small'
  34. ```
  35. </CodeGroup>
  36. * 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:
  37. <CodeGroup>
  38. ```yaml text-embedding-3-small.yaml
  39. embedder:
  40. provider: openai
  41. config:
  42. model: 'text-embedding-3-small'
  43. ```
  44. ```yaml text-embedding-3-large.yaml
  45. embedder:
  46. provider: openai
  47. config:
  48. model: 'text-embedding-3-large'
  49. ```
  50. </CodeGroup>
  51. ## Google AI
  52. 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)
  53. <CodeGroup>
  54. ```python main.py
  55. import os
  56. from embedchain import App
  57. os.environ["GOOGLE_API_KEY"] = "xxx"
  58. app = App.from_config(config_path="config.yaml")
  59. ```
  60. ```yaml config.yaml
  61. embedder:
  62. provider: google
  63. config:
  64. model: 'models/embedding-001'
  65. task_type: "retrieval_document"
  66. title: "Embeddings for Embedchain"
  67. ```
  68. </CodeGroup>
  69. <br/>
  70. <Note>
  71. 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).
  72. </Note>
  73. ## Azure OpenAI
  74. 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:
  75. <CodeGroup>
  76. ```python main.py
  77. import os
  78. from embedchain import App
  79. os.environ["OPENAI_API_TYPE"] = "azure"
  80. os.environ["AZURE_OPENAI_ENDPOINT"] = "https://xxx.openai.azure.com/"
  81. os.environ["AZURE_OPENAI_API_KEY"] = "xxx"
  82. os.environ["OPENAI_API_VERSION"] = "xxx"
  83. app = App.from_config(config_path="config.yaml")
  84. ```
  85. ```yaml config.yaml
  86. llm:
  87. provider: azure_openai
  88. config:
  89. model: gpt-35-turbo
  90. deployment_name: your_llm_deployment_name
  91. temperature: 0.5
  92. max_tokens: 1000
  93. top_p: 1
  94. stream: false
  95. embedder:
  96. provider: azure_openai
  97. config:
  98. model: text-embedding-ada-002
  99. deployment_name: you_embedding_model_deployment_name
  100. ```
  101. </CodeGroup>
  102. You can find the list of models and deployment name on the [Azure OpenAI Platform](https://oai.azure.com/portal).
  103. ## GPT4ALL
  104. GPT4All supports generating high quality embeddings of arbitrary length documents of text using a CPU optimized contrastively trained Sentence Transformer.
  105. <CodeGroup>
  106. ```python main.py
  107. from embedchain import App
  108. # load embedding model configuration from config.yaml file
  109. app = App.from_config(config_path="config.yaml")
  110. ```
  111. ```yaml config.yaml
  112. llm:
  113. provider: gpt4all
  114. config:
  115. model: 'orca-mini-3b-gguf2-q4_0.gguf'
  116. temperature: 0.5
  117. max_tokens: 1000
  118. top_p: 1
  119. stream: false
  120. embedder:
  121. provider: gpt4all
  122. ```
  123. </CodeGroup>
  124. ## Hugging Face
  125. 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:
  126. <CodeGroup>
  127. ```python main.py
  128. from embedchain import App
  129. # load embedding model configuration from config.yaml file
  130. app = App.from_config(config_path="config.yaml")
  131. ```
  132. ```yaml config.yaml
  133. llm:
  134. provider: huggingface
  135. config:
  136. model: 'google/flan-t5-xxl'
  137. temperature: 0.5
  138. max_tokens: 1000
  139. top_p: 0.5
  140. stream: false
  141. embedder:
  142. provider: huggingface
  143. config:
  144. model: 'sentence-transformers/all-mpnet-base-v2'
  145. ```
  146. </CodeGroup>
  147. ## Vertex AI
  148. 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.
  149. <CodeGroup>
  150. ```python main.py
  151. from embedchain import App
  152. # load embedding model configuration from config.yaml file
  153. app = App.from_config(config_path="config.yaml")
  154. ```
  155. ```yaml config.yaml
  156. llm:
  157. provider: vertexai
  158. config:
  159. model: 'chat-bison'
  160. temperature: 0.5
  161. top_p: 0.5
  162. embedder:
  163. provider: vertexai
  164. config:
  165. model: 'textembedding-gecko'
  166. ```
  167. </CodeGroup>
  168. ## NVIDIA AI
  169. [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.
  170. ### Usage
  171. In order to use embedding models and LLMs from NVIDIA AI, create an account on [NVIDIA NGC Service](https://catalog.ngc.nvidia.com/).
  172. 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-`.
  173. Below is an example of how to use LLM model and embedding model from NVIDIA AI:
  174. <CodeGroup>
  175. ```python main.py
  176. import os
  177. from embedchain import App
  178. os.environ['NVIDIA_API_KEY'] = 'nvapi-xxxx'
  179. config = {
  180. "app": {
  181. "config": {
  182. "id": "my-app",
  183. },
  184. },
  185. "llm": {
  186. "provider": "nvidia",
  187. "config": {
  188. "model": "nemotron_steerlm_8b",
  189. },
  190. },
  191. "embedder": {
  192. "provider": "nvidia",
  193. "config": {
  194. "model": "nvolveqa_40k",
  195. "vector_dimension": 1024,
  196. },
  197. },
  198. }
  199. app = App.from_config(config=config)
  200. app.add("https://www.forbes.com/profile/elon-musk")
  201. answer = app.query("What is the net worth of Elon Musk today?")
  202. # Answer: The net worth of Elon Musk is subject to fluctuations based on the market value of his holdings in various companies.
  203. # 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.
  204. # Additionally, his net worth may include other assets such as real estate and art, which are not reflected in his stock portfolio.
  205. ```
  206. </CodeGroup>
  207. ## Cohere
  208. To use embedding models and LLMs from COHERE, create an account on [COHERE](https://dashboard.cohere.com/welcome/login?redirect_uri=%2Fapi-keys).
  209. Generate an API key from their dashboard. Set the API key as `COHERE_API_KEY` environment variable.
  210. Once you have obtained the key, you can use it like this:
  211. <CodeGroup>
  212. ```python main.py
  213. import os
  214. from embedchain import App
  215. os.environ['COHERE_API_KEY'] = 'xxx'
  216. # load embedding model configuration from config.yaml file
  217. app = App.from_config(config_path="config.yaml")
  218. ```
  219. ```yaml config.yaml
  220. embedder:
  221. provider: cohere
  222. config:
  223. model: 'embed-english-light-v3.0'
  224. ```
  225. </CodeGroup>
  226. * Cohere has few embedding models: `embed-english-v3.0`, `embed-multilingual-v3.0`, `embed-multilingual-light-v3.0`, `embed-english-v2.0`, `embed-english-light-v2.0` and `embed-multilingual-v2.0`. Embedchain supports all these models. Below you can find YAML config for all:
  227. <CodeGroup>
  228. ```yaml embed-english-v3.0.yaml
  229. embedder:
  230. provider: cohere
  231. config:
  232. model: 'embed-english-v3.0'
  233. vector_dimension: 1024
  234. ```
  235. ```yaml embed-multilingual-v3.0.yaml
  236. embedder:
  237. provider: cohere
  238. config:
  239. model: 'embed-multilingual-v3.0'
  240. vector_dimension: 1024
  241. ```
  242. ```yaml embed-multilingual-light-v3.0.yaml
  243. embedder:
  244. provider: cohere
  245. config:
  246. model: 'embed-multilingual-light-v3.0'
  247. vector_dimension: 384
  248. ```
  249. ```yaml embed-english-v2.0.yaml
  250. embedder:
  251. provider: cohere
  252. config:
  253. model: 'embed-english-v2.0'
  254. vector_dimension: 4096
  255. ```
  256. ```yaml embed-english-light-v2.0.yaml
  257. embedder:
  258. provider: cohere
  259. config:
  260. model: 'embed-english-light-v2.0'
  261. vector_dimension: 1024
  262. ```
  263. ```yaml embed-multilingual-v2.0.yaml
  264. embedder:
  265. provider: cohere
  266. config:
  267. model: 'embed-multilingual-v2.0'
  268. vector_dimension: 768
  269. ```
  270. </CodeGroup>