embedding-models.mdx 9.5 KB

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