llms.mdx 15 KB

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