embedding-models.mdx 11 KB

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