embedding-models.mdx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. model_kwargs:
  148. trust_remote_code: True # Only use if you trust your embedder
  149. ```
  150. </CodeGroup>
  151. ## Vertex AI
  152. 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.
  153. <CodeGroup>
  154. ```python main.py
  155. from embedchain import App
  156. # load embedding model configuration from config.yaml file
  157. app = App.from_config(config_path="config.yaml")
  158. ```
  159. ```yaml config.yaml
  160. llm:
  161. provider: vertexai
  162. config:
  163. model: 'chat-bison'
  164. temperature: 0.5
  165. top_p: 0.5
  166. embedder:
  167. provider: vertexai
  168. config:
  169. model: 'textembedding-gecko'
  170. ```
  171. </CodeGroup>
  172. ## NVIDIA AI
  173. [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.
  174. ### Usage
  175. In order to use embedding models and LLMs from NVIDIA AI, create an account on [NVIDIA NGC Service](https://catalog.ngc.nvidia.com/).
  176. 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-`.
  177. Below is an example of how to use LLM model and embedding model from NVIDIA AI:
  178. <CodeGroup>
  179. ```python main.py
  180. import os
  181. from embedchain import App
  182. os.environ['NVIDIA_API_KEY'] = 'nvapi-xxxx'
  183. config = {
  184. "app": {
  185. "config": {
  186. "id": "my-app",
  187. },
  188. },
  189. "llm": {
  190. "provider": "nvidia",
  191. "config": {
  192. "model": "nemotron_steerlm_8b",
  193. },
  194. },
  195. "embedder": {
  196. "provider": "nvidia",
  197. "config": {
  198. "model": "nvolveqa_40k",
  199. "vector_dimension": 1024,
  200. },
  201. },
  202. }
  203. app = App.from_config(config=config)
  204. app.add("https://www.forbes.com/profile/elon-musk")
  205. answer = app.query("What is the net worth of Elon Musk today?")
  206. # Answer: The net worth of Elon Musk is subject to fluctuations based on the market value of his holdings in various companies.
  207. # 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.
  208. # Additionally, his net worth may include other assets such as real estate and art, which are not reflected in his stock portfolio.
  209. ```
  210. </CodeGroup>
  211. ## Cohere
  212. To use embedding models and LLMs from COHERE, create an account on [COHERE](https://dashboard.cohere.com/welcome/login?redirect_uri=%2Fapi-keys).
  213. Generate an API key from their dashboard. Set the API key as `COHERE_API_KEY` environment variable.
  214. Once you have obtained the key, you can use it like this:
  215. <CodeGroup>
  216. ```python main.py
  217. import os
  218. from embedchain import App
  219. os.environ['COHERE_API_KEY'] = 'xxx'
  220. # load embedding model configuration from config.yaml file
  221. app = App.from_config(config_path="config.yaml")
  222. ```
  223. ```yaml config.yaml
  224. embedder:
  225. provider: cohere
  226. config:
  227. model: 'embed-english-light-v3.0'
  228. ```
  229. </CodeGroup>
  230. * 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:
  231. <CodeGroup>
  232. ```yaml embed-english-v3.0.yaml
  233. embedder:
  234. provider: cohere
  235. config:
  236. model: 'embed-english-v3.0'
  237. vector_dimension: 1024
  238. ```
  239. ```yaml embed-multilingual-v3.0.yaml
  240. embedder:
  241. provider: cohere
  242. config:
  243. model: 'embed-multilingual-v3.0'
  244. vector_dimension: 1024
  245. ```
  246. ```yaml embed-multilingual-light-v3.0.yaml
  247. embedder:
  248. provider: cohere
  249. config:
  250. model: 'embed-multilingual-light-v3.0'
  251. vector_dimension: 384
  252. ```
  253. ```yaml embed-english-v2.0.yaml
  254. embedder:
  255. provider: cohere
  256. config:
  257. model: 'embed-english-v2.0'
  258. vector_dimension: 4096
  259. ```
  260. ```yaml embed-english-light-v2.0.yaml
  261. embedder:
  262. provider: cohere
  263. config:
  264. model: 'embed-english-light-v2.0'
  265. vector_dimension: 1024
  266. ```
  267. ```yaml embed-multilingual-v2.0.yaml
  268. embedder:
  269. provider: cohere
  270. config:
  271. model: 'embed-multilingual-v2.0'
  272. vector_dimension: 768
  273. ```
  274. </CodeGroup>
  275. ## Ollama
  276. 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.
  277. You can find the list of models at [Ollama Embedding Models](https://ollama.com/blog/embedding-models).
  278. Below is an example of how to use embedding model Ollama:
  279. <CodeGroup>
  280. ```python main.py
  281. import os
  282. from embedchain import App
  283. # load embedding model configuration from config.yaml file
  284. app = App.from_config(config_path="config.yaml")
  285. ```
  286. ```yaml config.yaml
  287. embedder:
  288. provider: ollama
  289. config:
  290. model: 'all-minilm:latest'
  291. ```
  292. </CodeGroup>
  293. ## Clarifai
  294. Install related dependencies using the following command:
  295. ```bash
  296. pip install --upgrade 'embedchain[clarifai]'
  297. ```
  298. 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.
  299. Now you are all set with exploring Embedchain.
  300. <CodeGroup>
  301. ```python main.py
  302. import os
  303. from embedchain import App
  304. os.environ["CLARIFAI_PAT"] = "XXX"
  305. # load llm and embedder configuration from config.yaml file
  306. app = App.from_config(config_path="config.yaml")
  307. #Now let's add some data.
  308. app.add("https://www.forbes.com/profile/elon-musk")
  309. #Query the app
  310. response = app.query("what college degrees does elon musk have?")
  311. ```
  312. 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.
  313. 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.
  314. ```yaml config.yaml
  315. llm:
  316. provider: clarifai
  317. config:
  318. model: "https://clarifai.com/mistralai/completion/models/mistral-7B-Instruct"
  319. model_kwargs:
  320. temperature: 0.5
  321. max_tokens: 1000
  322. embedder:
  323. provider: clarifai
  324. config:
  325. model: "https://clarifai.com/clarifai/main/models/BAAI-bge-base-en-v15"
  326. ```
  327. </CodeGroup>