llms.mdx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. ---
  2. title: 🤖 Large language models (LLMs)
  3. ---
  4. ## Overview
  5. Embedchain comes with built-in support for various popular large language models. We handle the complexity of integrating these models for you, allowing you to easily customize your language model interactions through a user-friendly interface.
  6. <CardGroup cols={4}>
  7. <Card title="OpenAI" href="#openai"></Card>
  8. <Card title="Google AI" href="#google-ai"></Card>
  9. <Card title="Azure OpenAI" href="#azure-openai"></Card>
  10. <Card title="Anthropic" href="#anthropic"></Card>
  11. <Card title="Cohere" href="#cohere"></Card>
  12. <Card title="Together" href="#together"></Card>
  13. <Card title="Ollama" href="#ollama"></Card>
  14. <Card title="GPT4All" href="#gpt4all"></Card>
  15. <Card title="JinaChat" href="#jinachat"></Card>
  16. <Card title="Hugging Face" href="#hugging-face"></Card>
  17. <Card title="Llama2" href="#llama2"></Card>
  18. <Card title="Vertex AI" href="#vertex-ai"></Card>
  19. </CardGroup>
  20. ## OpenAI
  21. To use OpenAI LLM models, 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).
  22. Once you have obtained the key, you can use it like this:
  23. ```python
  24. import os
  25. from embedchain import Pipeline as App
  26. os.environ['OPENAI_API_KEY'] = 'xxx'
  27. app = App()
  28. app.add("https://en.wikipedia.org/wiki/OpenAI")
  29. app.query("What is OpenAI?")
  30. ```
  31. If you are looking to configure the different parameters of the LLM, you can do so by loading the app using a [yaml config](https://github.com/embedchain/embedchain/blob/main/configs/chroma.yaml) file.
  32. <CodeGroup>
  33. ```python main.py
  34. import os
  35. from embedchain import Pipeline as App
  36. os.environ['OPENAI_API_KEY'] = 'xxx'
  37. # load llm configuration from config.yaml file
  38. app = App.from_config(config_path="config.yaml")
  39. ```
  40. ```yaml config.yaml
  41. llm:
  42. provider: openai
  43. config:
  44. model: 'gpt-3.5-turbo'
  45. temperature: 0.5
  46. max_tokens: 1000
  47. top_p: 1
  48. stream: false
  49. ```
  50. </CodeGroup>
  51. ### Function Calling
  52. To enable [function calling](https://platform.openai.com/docs/guides/function-calling) in your application using embedchain and OpenAI, you need to pass functions into `OpenAILlm` class as an array of functions. Here are several ways in which you can achieve that:
  53. Examples:
  54. <Accordion title="Using Pydantic Models">
  55. ```python
  56. import os
  57. from embedchain import Pipeline as App
  58. from embedchain.llm.openai import OpenAILlm
  59. import requests
  60. from pydantic import BaseModel, Field, ValidationError, field_validator
  61. os.environ["OPENAI_API_KEY"] = "sk-xxx"
  62. class QA(BaseModel):
  63. """
  64. A question and answer pair.
  65. """
  66. question: str = Field(
  67. ..., description="The question.", example="What is a mountain?"
  68. )
  69. answer: str = Field(
  70. ..., description="The answer.", example="A mountain is a hill."
  71. )
  72. person_who_is_asking: str = Field(
  73. ..., description="The person who is asking the question.", example="John"
  74. )
  75. @field_validator("question")
  76. def question_must_end_with_a_question_mark(cls, v):
  77. """
  78. Validate that the question ends with a question mark.
  79. """
  80. if not v.endswith("?"):
  81. raise ValueError("question must end with a question mark")
  82. return v
  83. @field_validator("answer")
  84. def answer_must_end_with_a_period(cls, v):
  85. """
  86. Validate that the answer ends with a period.
  87. """
  88. if not v.endswith("."):
  89. raise ValueError("answer must end with a period")
  90. return v
  91. llm = OpenAILlm(config=None,functions=[QA])
  92. app = App(llm=llm)
  93. result = app.query("Hey I am Sid. What is a mountain? A mountain is a hill.")
  94. print(result)
  95. ```
  96. </Accordion>
  97. <Accordion title="Using OpenAI JSON schema">
  98. ```python
  99. import os
  100. from embedchain import Pipeline as App
  101. from embedchain.llm.openai import OpenAILlm
  102. import requests
  103. from pydantic import BaseModel, Field, ValidationError, field_validator
  104. os.environ["OPENAI_API_KEY"] = "sk-xxx"
  105. json_schema = {
  106. "name": "get_qa",
  107. "description": "A question and answer pair and the user who is asking the question.",
  108. "parameters": {
  109. "type": "object",
  110. "properties": {
  111. "question": {"type": "string", "description": "The question."},
  112. "answer": {"type": "string", "description": "The answer."},
  113. "person_who_is_asking": {
  114. "type": "string",
  115. "description": "The person who is asking the question.",
  116. }
  117. },
  118. "required": ["question", "answer", "person_who_is_asking"],
  119. },
  120. }
  121. llm = OpenAILlm(config=None,functions=[json_schema])
  122. app = App(llm=llm)
  123. result = app.query("Hey I am Sid. What is a mountain? A mountain is a hill.")
  124. print(result)
  125. ```
  126. </Accordion>
  127. <Accordion title="Using actual python functions">
  128. ```python
  129. import os
  130. from embedchain import Pipeline as App
  131. from embedchain.llm.openai import OpenAILlm
  132. import requests
  133. from pydantic import BaseModel, Field, ValidationError, field_validator
  134. os.environ["OPENAI_API_KEY"] = "sk-xxx"
  135. def find_info_of_pokemon(pokemon: str):
  136. """
  137. Find the information of the given pokemon.
  138. Args:
  139. pokemon: The pokemon.
  140. """
  141. req = requests.get(f"https://pokeapi.co/api/v2/pokemon/{pokemon}")
  142. if req.status_code == 404:
  143. raise ValueError("pokemon not found")
  144. return req.json()
  145. llm = OpenAILlm(config=None,functions=[find_info_of_pokemon])
  146. app = App(llm=llm)
  147. result = app.query("Tell me more about the pokemon pikachu.")
  148. print(result)
  149. ```
  150. </Accordion>
  151. ## Google AI
  152. To use Google AI model, 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)
  153. <CodeGroup>
  154. ```python main.py
  155. import os
  156. from embedchain import Pipeline as App
  157. os.environ["GOOGLE_API_KEY"] = "xxx"
  158. app = App.from_config(config_path="config.yaml")
  159. app.add("https://www.forbes.com/profile/elon-musk")
  160. response = app.query("What is the net worth of Elon Musk?")
  161. if app.llm.config.stream: # if stream is enabled, response is a generator
  162. for chunk in response:
  163. print(chunk)
  164. else:
  165. print(response)
  166. ```
  167. ```yaml config.yaml
  168. llm:
  169. provider: google
  170. config:
  171. model: gemini-pro
  172. max_tokens: 1000
  173. temperature: 0.5
  174. top_p: 1
  175. stream: false
  176. embedder:
  177. provider: google
  178. config:
  179. model: 'models/embedding-001'
  180. task_type: "retrieval_document"
  181. title: "Embeddings for Embedchain"
  182. ```
  183. </CodeGroup>
  184. ## Azure OpenAI
  185. To use Azure OpenAI model, you have to set some of the azure openai related environment variables as given in the code block below:
  186. <CodeGroup>
  187. ```python main.py
  188. import os
  189. from embedchain import Pipeline as App
  190. os.environ["OPENAI_API_TYPE"] = "azure"
  191. os.environ["OPENAI_API_BASE"] = "https://xxx.openai.azure.com/"
  192. os.environ["OPENAI_API_KEY"] = "xxx"
  193. os.environ["OPENAI_API_VERSION"] = "xxx"
  194. app = App.from_config(config_path="config.yaml")
  195. ```
  196. ```yaml config.yaml
  197. llm:
  198. provider: azure_openai
  199. config:
  200. model: gpt-35-turbo
  201. deployment_name: your_llm_deployment_name
  202. temperature: 0.5
  203. max_tokens: 1000
  204. top_p: 1
  205. stream: false
  206. embedder:
  207. provider: azure_openai
  208. config:
  209. model: text-embedding-ada-002
  210. deployment_name: you_embedding_model_deployment_name
  211. ```
  212. </CodeGroup>
  213. You can find the list of models and deployment name on the [Azure OpenAI Platform](https://oai.azure.com/portal).
  214. ## Anthropic
  215. To use anthropic's model, please set the `ANTHROPIC_API_KEY` which you find on their [Account Settings Page](https://console.anthropic.com/account/keys).
  216. <CodeGroup>
  217. ```python main.py
  218. import os
  219. from embedchain import Pipeline as App
  220. os.environ["ANTHROPIC_API_KEY"] = "xxx"
  221. # load llm configuration from config.yaml file
  222. app = App.from_config(config_path="config.yaml")
  223. ```
  224. ```yaml config.yaml
  225. llm:
  226. provider: anthropic
  227. config:
  228. model: 'claude-instant-1'
  229. temperature: 0.5
  230. max_tokens: 1000
  231. top_p: 1
  232. stream: false
  233. ```
  234. </CodeGroup>
  235. ## Cohere
  236. Install related dependencies using the following command:
  237. ```bash
  238. pip install --upgrade 'embedchain[cohere]'
  239. ```
  240. Set the `COHERE_API_KEY` as environment variable which you can find on their [Account settings page](https://dashboard.cohere.com/api-keys).
  241. Once you have the API key, you are all set to use it with Embedchain.
  242. <CodeGroup>
  243. ```python main.py
  244. import os
  245. from embedchain import Pipeline as App
  246. os.environ["COHERE_API_KEY"] = "xxx"
  247. # load llm configuration from config.yaml file
  248. app = App.from_config(config_path="config.yaml")
  249. ```
  250. ```yaml config.yaml
  251. llm:
  252. provider: cohere
  253. config:
  254. model: large
  255. temperature: 0.5
  256. max_tokens: 1000
  257. top_p: 1
  258. ```
  259. </CodeGroup>
  260. ## Together
  261. Install related dependencies using the following command:
  262. ```bash
  263. pip install --upgrade 'embedchain[together]'
  264. ```
  265. Set the `TOGETHER_API_KEY` as environment variable which you can find on their [Account settings page](https://api.together.xyz/settings/api-keys).
  266. Once you have the API key, you are all set to use it with Embedchain.
  267. <CodeGroup>
  268. ```python main.py
  269. import os
  270. from embedchain import Pipeline as App
  271. os.environ["TOGETHER_API_KEY"] = "xxx"
  272. # load llm configuration from config.yaml file
  273. app = App.from_config(config_path="config.yaml")
  274. ```
  275. ```yaml config.yaml
  276. llm:
  277. provider: together
  278. config:
  279. model: togethercomputer/RedPajama-INCITE-7B-Base
  280. temperature: 0.5
  281. max_tokens: 1000
  282. top_p: 1
  283. ```
  284. </CodeGroup>
  285. ## Ollama
  286. Setup Ollama using https://github.com/jmorganca/ollama
  287. <CodeGroup>
  288. ```python main.py
  289. import os
  290. from embedchain import Pipeline as App
  291. # load llm configuration from config.yaml file
  292. app = App.from_config(config_path="config.yaml")
  293. ```
  294. ```yaml config.yaml
  295. llm:
  296. provider: ollama
  297. config:
  298. model: 'llama2'
  299. temperature: 0.5
  300. top_p: 1
  301. stream: true
  302. ```
  303. </CodeGroup>
  304. ## GPT4ALL
  305. Install related dependencies using the following command:
  306. ```bash
  307. pip install --upgrade 'embedchain[opensource]'
  308. ```
  309. GPT4all is a free-to-use, locally running, privacy-aware chatbot. No GPU or internet required. You can use this with Embedchain using the following code:
  310. <CodeGroup>
  311. ```python main.py
  312. from embedchain import Pipeline as App
  313. # load llm configuration from config.yaml file
  314. app = App.from_config(config_path="config.yaml")
  315. ```
  316. ```yaml config.yaml
  317. llm:
  318. provider: gpt4all
  319. config:
  320. model: 'orca-mini-3b-gguf2-q4_0.gguf'
  321. temperature: 0.5
  322. max_tokens: 1000
  323. top_p: 1
  324. stream: false
  325. embedder:
  326. provider: gpt4all
  327. ```
  328. </CodeGroup>
  329. ## JinaChat
  330. First, set `JINACHAT_API_KEY` in environment variable which you can obtain from [their platform](https://chat.jina.ai/api).
  331. Once you have the key, load the app using the config yaml file:
  332. <CodeGroup>
  333. ```python main.py
  334. import os
  335. from embedchain import Pipeline as App
  336. os.environ["JINACHAT_API_KEY"] = "xxx"
  337. # load llm configuration from config.yaml file
  338. app = App.from_config(config_path="config.yaml")
  339. ```
  340. ```yaml config.yaml
  341. llm:
  342. provider: jina
  343. config:
  344. temperature: 0.5
  345. max_tokens: 1000
  346. top_p: 1
  347. stream: false
  348. ```
  349. </CodeGroup>
  350. ## Hugging Face
  351. Install related dependencies using the following command:
  352. ```bash
  353. pip install --upgrade 'embedchain[huggingface-hub]'
  354. ```
  355. First, set `HUGGINGFACE_ACCESS_TOKEN` in environment variable which you can obtain from [their platform](https://huggingface.co/settings/tokens).
  356. Once you have the token, load the app using the config yaml file:
  357. <CodeGroup>
  358. ```python main.py
  359. import os
  360. from embedchain import Pipeline as App
  361. os.environ["HUGGINGFACE_ACCESS_TOKEN"] = "xxx"
  362. # load llm configuration from config.yaml file
  363. app = App.from_config(config_path="config.yaml")
  364. ```
  365. ```yaml config.yaml
  366. llm:
  367. provider: huggingface
  368. config:
  369. model: 'google/flan-t5-xxl'
  370. temperature: 0.5
  371. max_tokens: 1000
  372. top_p: 0.5
  373. stream: false
  374. ```
  375. </CodeGroup>
  376. ## Llama2
  377. Llama2 is integrated through [Replicate](https://replicate.com/). Set `REPLICATE_API_TOKEN` in environment variable which you can obtain from [their platform](https://replicate.com/account/api-tokens).
  378. Once you have the token, load the app using the config yaml file:
  379. <CodeGroup>
  380. ```python main.py
  381. import os
  382. from embedchain import Pipeline as App
  383. os.environ["REPLICATE_API_TOKEN"] = "xxx"
  384. # load llm configuration from config.yaml file
  385. app = App.from_config(config_path="config.yaml")
  386. ```
  387. ```yaml config.yaml
  388. llm:
  389. provider: llama2
  390. config:
  391. model: 'a16z-infra/llama13b-v2-chat:df7690f1994d94e96ad9d568eac121aecf50684a0b0963b25a41cc40061269e5'
  392. temperature: 0.5
  393. max_tokens: 1000
  394. top_p: 0.5
  395. stream: false
  396. ```
  397. </CodeGroup>
  398. ## Vertex AI
  399. Setup Google Cloud Platform application credentials by following the instruction on [GCP](https://cloud.google.com/docs/authentication/external/set-up-adc). Once setup is done, use the following code to create an app using VertexAI as provider:
  400. <CodeGroup>
  401. ```python main.py
  402. from embedchain import Pipeline as App
  403. # load llm configuration from config.yaml file
  404. app = App.from_config(config_path="config.yaml")
  405. ```
  406. ```yaml config.yaml
  407. llm:
  408. provider: vertexai
  409. config:
  410. model: 'chat-bison'
  411. temperature: 0.5
  412. top_p: 0.5
  413. ```
  414. </CodeGroup>
  415. <br/ >
  416. <Snippet file="missing-llm-tip.mdx" />