llms.mdx 12 KB

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