llms.mdx 12 KB

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