llms.mdx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  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="vLLM" href="#vllm"></Card>
  15. <Card title="GPT4All" href="#gpt4all"></Card>
  16. <Card title="JinaChat" href="#jinachat"></Card>
  17. <Card title="Hugging Face" href="#hugging-face"></Card>
  18. <Card title="Llama2" href="#llama2"></Card>
  19. <Card title="Vertex AI" href="#vertex-ai"></Card>
  20. <Card title="Mistral AI" href="#mistral-ai"></Card>
  21. <Card title="AWS Bedrock" href="#aws-bedrock"></Card>
  22. </CardGroup>
  23. ## OpenAI
  24. 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).
  25. Once you have obtained the key, you can use it like this:
  26. ```python
  27. import os
  28. from embedchain import App
  29. os.environ['OPENAI_API_KEY'] = 'xxx'
  30. app = App()
  31. app.add("https://en.wikipedia.org/wiki/OpenAI")
  32. app.query("What is OpenAI?")
  33. ```
  34. 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.
  35. <CodeGroup>
  36. ```python main.py
  37. import os
  38. from embedchain import App
  39. os.environ['OPENAI_API_KEY'] = 'xxx'
  40. # load llm configuration from config.yaml file
  41. app = App.from_config(config_path="config.yaml")
  42. ```
  43. ```yaml config.yaml
  44. llm:
  45. provider: openai
  46. config:
  47. model: 'gpt-3.5-turbo'
  48. temperature: 0.5
  49. max_tokens: 1000
  50. top_p: 1
  51. stream: false
  52. ```
  53. </CodeGroup>
  54. ### Function Calling
  55. 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:
  56. Examples:
  57. <Accordion title="Using Pydantic Models">
  58. ```python
  59. import os
  60. from embedchain import App
  61. from embedchain.llm.openai import OpenAILlm
  62. import requests
  63. from pydantic import BaseModel, Field, ValidationError, field_validator
  64. os.environ["OPENAI_API_KEY"] = "sk-xxx"
  65. class QA(BaseModel):
  66. """
  67. A question and answer pair.
  68. """
  69. question: str = Field(
  70. ..., description="The question.", example="What is a mountain?"
  71. )
  72. answer: str = Field(
  73. ..., description="The answer.", example="A mountain is a hill."
  74. )
  75. person_who_is_asking: str = Field(
  76. ..., description="The person who is asking the question.", example="John"
  77. )
  78. @field_validator("question")
  79. def question_must_end_with_a_question_mark(cls, v):
  80. """
  81. Validate that the question ends with a question mark.
  82. """
  83. if not v.endswith("?"):
  84. raise ValueError("question must end with a question mark")
  85. return v
  86. @field_validator("answer")
  87. def answer_must_end_with_a_period(cls, v):
  88. """
  89. Validate that the answer ends with a period.
  90. """
  91. if not v.endswith("."):
  92. raise ValueError("answer must end with a period")
  93. return v
  94. llm = OpenAILlm(config=None,functions=[QA])
  95. app = App(llm=llm)
  96. result = app.query("Hey I am Sid. What is a mountain? A mountain is a hill.")
  97. print(result)
  98. ```
  99. </Accordion>
  100. <Accordion title="Using OpenAI JSON schema">
  101. ```python
  102. import os
  103. from embedchain import App
  104. from embedchain.llm.openai import OpenAILlm
  105. import requests
  106. from pydantic import BaseModel, Field, ValidationError, field_validator
  107. os.environ["OPENAI_API_KEY"] = "sk-xxx"
  108. json_schema = {
  109. "name": "get_qa",
  110. "description": "A question and answer pair and the user who is asking the question.",
  111. "parameters": {
  112. "type": "object",
  113. "properties": {
  114. "question": {"type": "string", "description": "The question."},
  115. "answer": {"type": "string", "description": "The answer."},
  116. "person_who_is_asking": {
  117. "type": "string",
  118. "description": "The person who is asking the question.",
  119. }
  120. },
  121. "required": ["question", "answer", "person_who_is_asking"],
  122. },
  123. }
  124. llm = OpenAILlm(config=None,functions=[json_schema])
  125. app = App(llm=llm)
  126. result = app.query("Hey I am Sid. What is a mountain? A mountain is a hill.")
  127. print(result)
  128. ```
  129. </Accordion>
  130. <Accordion title="Using actual python functions">
  131. ```python
  132. import os
  133. from embedchain import App
  134. from embedchain.llm.openai import OpenAILlm
  135. import requests
  136. from pydantic import BaseModel, Field, ValidationError, field_validator
  137. os.environ["OPENAI_API_KEY"] = "sk-xxx"
  138. def find_info_of_pokemon(pokemon: str):
  139. """
  140. Find the information of the given pokemon.
  141. Args:
  142. pokemon: The pokemon.
  143. """
  144. req = requests.get(f"https://pokeapi.co/api/v2/pokemon/{pokemon}")
  145. if req.status_code == 404:
  146. raise ValueError("pokemon not found")
  147. return req.json()
  148. llm = OpenAILlm(config=None,functions=[find_info_of_pokemon])
  149. app = App(llm=llm)
  150. result = app.query("Tell me more about the pokemon pikachu.")
  151. print(result)
  152. ```
  153. </Accordion>
  154. ## Google AI
  155. 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)
  156. <CodeGroup>
  157. ```python main.py
  158. import os
  159. from embedchain import App
  160. os.environ["GOOGLE_API_KEY"] = "xxx"
  161. app = App.from_config(config_path="config.yaml")
  162. app.add("https://www.forbes.com/profile/elon-musk")
  163. response = app.query("What is the net worth of Elon Musk?")
  164. if app.llm.config.stream: # if stream is enabled, response is a generator
  165. for chunk in response:
  166. print(chunk)
  167. else:
  168. print(response)
  169. ```
  170. ```yaml config.yaml
  171. llm:
  172. provider: google
  173. config:
  174. model: gemini-pro
  175. max_tokens: 1000
  176. temperature: 0.5
  177. top_p: 1
  178. stream: false
  179. embedder:
  180. provider: google
  181. config:
  182. model: 'models/embedding-001'
  183. task_type: "retrieval_document"
  184. title: "Embeddings for Embedchain"
  185. ```
  186. </CodeGroup>
  187. ## Azure OpenAI
  188. To use Azure OpenAI model, you have to set some of the azure openai related environment variables as given in the code block below:
  189. <CodeGroup>
  190. ```python main.py
  191. import os
  192. from embedchain import App
  193. os.environ["OPENAI_API_TYPE"] = "azure"
  194. os.environ["OPENAI_API_BASE"] = "https://xxx.openai.azure.com/"
  195. os.environ["OPENAI_API_KEY"] = "xxx"
  196. os.environ["OPENAI_API_VERSION"] = "xxx"
  197. app = App.from_config(config_path="config.yaml")
  198. ```
  199. ```yaml config.yaml
  200. llm:
  201. provider: azure_openai
  202. config:
  203. model: gpt-3.5-turbo
  204. deployment_name: your_llm_deployment_name
  205. temperature: 0.5
  206. max_tokens: 1000
  207. top_p: 1
  208. stream: false
  209. embedder:
  210. provider: azure_openai
  211. config:
  212. model: text-embedding-ada-002
  213. deployment_name: you_embedding_model_deployment_name
  214. ```
  215. </CodeGroup>
  216. You can find the list of models and deployment name on the [Azure OpenAI Platform](https://oai.azure.com/portal).
  217. ## Anthropic
  218. 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).
  219. <CodeGroup>
  220. ```python main.py
  221. import os
  222. from embedchain import App
  223. os.environ["ANTHROPIC_API_KEY"] = "xxx"
  224. # load llm configuration from config.yaml file
  225. app = App.from_config(config_path="config.yaml")
  226. ```
  227. ```yaml config.yaml
  228. llm:
  229. provider: anthropic
  230. config:
  231. model: 'claude-instant-1'
  232. temperature: 0.5
  233. max_tokens: 1000
  234. top_p: 1
  235. stream: false
  236. ```
  237. </CodeGroup>
  238. ## Cohere
  239. Install related dependencies using the following command:
  240. ```bash
  241. pip install --upgrade 'embedchain[cohere]'
  242. ```
  243. Set the `COHERE_API_KEY` as environment variable which you can find on their [Account settings page](https://dashboard.cohere.com/api-keys).
  244. Once you have the API key, you are all set to use it with Embedchain.
  245. <CodeGroup>
  246. ```python main.py
  247. import os
  248. from embedchain import App
  249. os.environ["COHERE_API_KEY"] = "xxx"
  250. # load llm configuration from config.yaml file
  251. app = App.from_config(config_path="config.yaml")
  252. ```
  253. ```yaml config.yaml
  254. llm:
  255. provider: cohere
  256. config:
  257. model: large
  258. temperature: 0.5
  259. max_tokens: 1000
  260. top_p: 1
  261. ```
  262. </CodeGroup>
  263. ## Together
  264. Install related dependencies using the following command:
  265. ```bash
  266. pip install --upgrade 'embedchain[together]'
  267. ```
  268. Set the `TOGETHER_API_KEY` as environment variable which you can find on their [Account settings page](https://api.together.xyz/settings/api-keys).
  269. Once you have the API key, you are all set to use it with Embedchain.
  270. <CodeGroup>
  271. ```python main.py
  272. import os
  273. from embedchain import App
  274. os.environ["TOGETHER_API_KEY"] = "xxx"
  275. # load llm configuration from config.yaml file
  276. app = App.from_config(config_path="config.yaml")
  277. ```
  278. ```yaml config.yaml
  279. llm:
  280. provider: together
  281. config:
  282. model: togethercomputer/RedPajama-INCITE-7B-Base
  283. temperature: 0.5
  284. max_tokens: 1000
  285. top_p: 1
  286. ```
  287. </CodeGroup>
  288. ## Ollama
  289. Setup Ollama using https://github.com/jmorganca/ollama
  290. <CodeGroup>
  291. ```python main.py
  292. import os
  293. from embedchain import App
  294. # load llm configuration from config.yaml file
  295. app = App.from_config(config_path="config.yaml")
  296. ```
  297. ```yaml config.yaml
  298. llm:
  299. provider: ollama
  300. config:
  301. model: 'llama2'
  302. temperature: 0.5
  303. top_p: 1
  304. stream: true
  305. ```
  306. </CodeGroup>
  307. ## vLLM
  308. Setup vLLM by following instructions given in [their docs](https://docs.vllm.ai/en/latest/getting_started/installation.html).
  309. <CodeGroup>
  310. ```python main.py
  311. import os
  312. from embedchain import 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: vllm
  319. config:
  320. model: 'meta-llama/Llama-2-70b-hf'
  321. temperature: 0.5
  322. top_p: 1
  323. top_k: 10
  324. stream: true
  325. trust_remote_code: true
  326. ```
  327. </CodeGroup>
  328. ## GPT4ALL
  329. Install related dependencies using the following command:
  330. ```bash
  331. pip install --upgrade 'embedchain[opensource]'
  332. ```
  333. 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:
  334. <CodeGroup>
  335. ```python main.py
  336. from embedchain import App
  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: gpt4all
  343. config:
  344. model: 'orca-mini-3b-gguf2-q4_0.gguf'
  345. temperature: 0.5
  346. max_tokens: 1000
  347. top_p: 1
  348. stream: false
  349. embedder:
  350. provider: gpt4all
  351. ```
  352. </CodeGroup>
  353. ## JinaChat
  354. First, set `JINACHAT_API_KEY` in environment variable which you can obtain from [their platform](https://chat.jina.ai/api).
  355. Once you have the key, load the app using the config yaml file:
  356. <CodeGroup>
  357. ```python main.py
  358. import os
  359. from embedchain import App
  360. os.environ["JINACHAT_API_KEY"] = "xxx"
  361. # load llm configuration from config.yaml file
  362. app = App.from_config(config_path="config.yaml")
  363. ```
  364. ```yaml config.yaml
  365. llm:
  366. provider: jina
  367. config:
  368. temperature: 0.5
  369. max_tokens: 1000
  370. top_p: 1
  371. stream: false
  372. ```
  373. </CodeGroup>
  374. ## Hugging Face
  375. Install related dependencies using the following command:
  376. ```bash
  377. pip install --upgrade 'embedchain[huggingface-hub]'
  378. ```
  379. First, set `HUGGINGFACE_ACCESS_TOKEN` in environment variable which you can obtain from [their platform](https://huggingface.co/settings/tokens).
  380. Once you have the token, load the app using the config yaml file:
  381. <CodeGroup>
  382. ```python main.py
  383. import os
  384. from embedchain import App
  385. os.environ["HUGGINGFACE_ACCESS_TOKEN"] = "xxx"
  386. # load llm configuration from config.yaml file
  387. app = App.from_config(config_path="config.yaml")
  388. ```
  389. ```yaml config.yaml
  390. llm:
  391. provider: huggingface
  392. config:
  393. model: 'google/flan-t5-xxl'
  394. temperature: 0.5
  395. max_tokens: 1000
  396. top_p: 0.5
  397. stream: false
  398. ```
  399. </CodeGroup>
  400. ### Custom Endpoints
  401. You can also use [Hugging Face Inference Endpoints](https://huggingface.co/docs/inference-endpoints/index#-inference-endpoints) to access custom endpoints. First, set the `HUGGINGFACE_ACCESS_TOKEN` as above.
  402. Then, load the app using the config yaml file:
  403. <CodeGroup>
  404. ```python main.py
  405. import os
  406. from embedchain import App
  407. os.environ["HUGGINGFACE_ACCESS_TOKEN"] = "xxx"
  408. # load llm configuration from config.yaml file
  409. app = App.from_config(config_path="config.yaml")
  410. ```
  411. ```yaml config.yaml
  412. llm:
  413. provider: huggingface
  414. config:
  415. endpoint: https://api-inference.huggingface.co/models/gpt2 # replace with your personal endpoint
  416. ```
  417. </CodeGroup>
  418. If your endpoint requires additional parameters, you can pass them in the `model_kwargs` field:
  419. ```
  420. llm:
  421. provider: huggingface
  422. config:
  423. endpoint: <YOUR_ENDPOINT_URL_HERE>
  424. model_kwargs:
  425. max_new_tokens: 100
  426. temperature: 0.5
  427. ```
  428. Currently only supports `text-generation` and `text2text-generation` for now [[ref](https://api.python.langchain.com/en/latest/llms/langchain_community.llms.huggingface_endpoint.HuggingFaceEndpoint.html?highlight=huggingfaceendpoint#)].
  429. See langchain's [hugging face endpoint](https://python.langchain.com/docs/integrations/chat/huggingface#huggingfaceendpoint) for more information.
  430. ## Llama2
  431. 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).
  432. Once you have the token, load the app using the config yaml file:
  433. <CodeGroup>
  434. ```python main.py
  435. import os
  436. from embedchain import App
  437. os.environ["REPLICATE_API_TOKEN"] = "xxx"
  438. # load llm configuration from config.yaml file
  439. app = App.from_config(config_path="config.yaml")
  440. ```
  441. ```yaml config.yaml
  442. llm:
  443. provider: llama2
  444. config:
  445. model: 'a16z-infra/llama13b-v2-chat:df7690f1994d94e96ad9d568eac121aecf50684a0b0963b25a41cc40061269e5'
  446. temperature: 0.5
  447. max_tokens: 1000
  448. top_p: 0.5
  449. stream: false
  450. ```
  451. </CodeGroup>
  452. ## Vertex AI
  453. 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:
  454. <CodeGroup>
  455. ```python main.py
  456. from embedchain import App
  457. # load llm configuration from config.yaml file
  458. app = App.from_config(config_path="config.yaml")
  459. ```
  460. ```yaml config.yaml
  461. llm:
  462. provider: vertexai
  463. config:
  464. model: 'chat-bison'
  465. temperature: 0.5
  466. top_p: 0.5
  467. ```
  468. </CodeGroup>
  469. ## Mistral AI
  470. Obtain the Mistral AI api key from their [console](https://console.mistral.ai/).
  471. <CodeGroup>
  472. ```python main.py
  473. os.environ["MISTRAL_API_KEY"] = "xxx"
  474. app = App.from_config(config_path="config.yaml")
  475. app.add("https://www.forbes.com/profile/elon-musk")
  476. response = app.query("what is the net worth of Elon Musk?")
  477. # As of January 16, 2024, Elon Musk's net worth is $225.4 billion.
  478. response = app.chat("which companies does elon own?")
  479. # Elon Musk owns Tesla, SpaceX, Boring Company, Twitter, and X.
  480. response = app.chat("what question did I ask you already?")
  481. # You have asked me several times already which companies Elon Musk owns, specifically Tesla, SpaceX, Boring Company, Twitter, and X.
  482. ```
  483. ```yaml config.yaml
  484. llm:
  485. provider: mistralai
  486. config:
  487. model: mistral-tiny
  488. temperature: 0.5
  489. max_tokens: 1000
  490. top_p: 1
  491. embedder:
  492. provider: mistralai
  493. config:
  494. model: mistral-embed
  495. ```
  496. </CodeGroup>
  497. ## AWS Bedrock
  498. ### Setup
  499. - Before using the AWS Bedrock LLM, make sure you have the appropriate model access from [Bedrock Console](https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/modelaccess).
  500. - You will also need `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` to authenticate the API with AWS. You can find these in your [AWS Console](https://us-east-1.console.aws.amazon.com/iam/home?region=us-east-1#/users).
  501. ### Usage
  502. <CodeGroup>
  503. ```python main.py
  504. import os
  505. from embedchain import App
  506. os.environ["AWS_ACCESS_KEY_ID"] = "xxx"
  507. os.environ["AWS_SECRET_ACCESS_KEY"] = "xxx"
  508. app = App.from_config(config_path="config.yaml")
  509. ```
  510. ```yaml config.yaml
  511. llm:
  512. provider: aws_bedrock
  513. config:
  514. model: amazon.titan-text-express-v1
  515. # check notes below for model_kwargs
  516. model_kwargs:
  517. temperature: 0.5
  518. topP: 1
  519. maxTokenCount: 1000
  520. ```
  521. </CodeGroup>
  522. <br />
  523. <Note>
  524. The model arguments are different for each providers. Please refer to the [AWS Bedrock Documentation](https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers) to find the appropriate arguments for your model.
  525. </Note>
  526. <br/ >
  527. <Snippet file="missing-llm-tip.mdx" />