Skip to content

Commit 96eca64

Browse files
committed
Using groq API to Build a Chatbot
1 parent 52118e1 commit 96eca64

7 files changed

Lines changed: 641 additions & 32 deletions

File tree

.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
langchain-3.13.2

3. LLM Appication Using LCEL/1. simpleLLMwithLCEL.ipynb

Lines changed: 168 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
},
4343
{
4444
"cell_type": "code",
45-
"execution_count": 3,
45+
"execution_count": 2,
4646
"id": "dee14503",
4747
"metadata": {},
4848
"outputs": [],
@@ -61,20 +61,19 @@
6161
},
6262
{
6363
"cell_type": "code",
64-
"execution_count": 4,
64+
"execution_count": 3,
6565
"id": "351d1b3b",
6666
"metadata": {},
6767
"outputs": [
6868
{
69-
"ename": "ModuleNotFoundError",
70-
"evalue": "No module named 'langchain_openai'",
71-
"output_type": "error",
72-
"traceback": [
73-
"\u001b[31m---------------------------------------------------------------------------\u001b[39m",
74-
"\u001b[31mModuleNotFoundError\u001b[39m Traceback (most recent call last)",
75-
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[4]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mlangchain_openai\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m ChatOpenAI\n\u001b[32m 2\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mlangchain_groq\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m ChatGroq\n\u001b[32m 4\u001b[39m model = ChatGroq(model =\u001b[33m\"\u001b[39m\u001b[33mgemma2-9b-it\u001b[39m\u001b[33m\"\u001b[39m, groq_api_key=groq_api_key)\n",
76-
"\u001b[31mModuleNotFoundError\u001b[39m: No module named 'langchain_openai'"
77-
]
69+
"data": {
70+
"text/plain": [
71+
"ChatGroq(client=<groq.resources.chat.completions.Completions object at 0x7f16f0bde270>, async_client=<groq.resources.chat.completions.AsyncCompletions object at 0x7f16f0bfc6e0>, model_name='gemma2-9b-it', model_kwargs={}, groq_api_key=SecretStr('**********'))"
72+
]
73+
},
74+
"execution_count": 3,
75+
"metadata": {},
76+
"output_type": "execute_result"
7877
}
7978
],
8079
"source": [
@@ -87,7 +86,7 @@
8786
},
8887
{
8988
"cell_type": "code",
90-
"execution_count": 32,
89+
"execution_count": 4,
9190
"metadata": {},
9291
"outputs": [],
9392
"source": [
@@ -105,17 +104,17 @@
105104
},
106105
{
107106
"cell_type": "code",
108-
"execution_count": 33,
107+
"execution_count": 5,
109108
"id": "cf9414ea",
110109
"metadata": {},
111110
"outputs": [
112111
{
113112
"data": {
114113
"text/plain": [
115-
"AIMessage(content='Bonjour, comment allez-vous ? \\n\\n\\nThis is the most common and formal way to say \"Hello, how are you?\" in French. \\n', additional_kwargs={}, response_metadata={'token_usage': {'completion_tokens': 33, 'prompt_tokens': 22, 'total_tokens': 55, 'completion_time': 0.06, 'prompt_time': 0.002229266, 'queue_time': 0.233778333, 'total_time': 0.062229266}, 'model_name': 'gemma2-9b-it', 'system_fingerprint': 'fp_10c08bf97d', 'finish_reason': 'stop', 'logprobs': None}, id='run-c34d75fe-6f05-4223-bf37-1f6b44b13bab-0', usage_metadata={'input_tokens': 22, 'output_tokens': 33, 'total_tokens': 55})"
114+
"AIMessage(content='Bonjour, comment allez-vous ? \\n', additional_kwargs={}, response_metadata={'token_usage': {'completion_tokens': 11, 'prompt_tokens': 22, 'total_tokens': 33, 'completion_time': 0.02, 'prompt_time': 0.002139595, 'queue_time': 0.232384914, 'total_time': 0.022139595}, 'model_name': 'gemma2-9b-it', 'system_fingerprint': 'fp_10c08bf97d', 'finish_reason': 'stop', 'logprobs': None}, id='run-0ad88787-a5fa-46da-8316-fd78854e13bf-0', usage_metadata={'input_tokens': 22, 'output_tokens': 11, 'total_tokens': 33})"
116115
]
117116
},
118-
"execution_count": 33,
117+
"execution_count": 5,
119118
"metadata": {},
120119
"output_type": "execute_result"
121120
}
@@ -126,17 +125,17 @@
126125
},
127126
{
128127
"cell_type": "code",
129-
"execution_count": 34,
128+
"execution_count": 6,
130129
"id": "c5f7fc94",
131130
"metadata": {},
132131
"outputs": [
133132
{
134133
"data": {
135134
"text/plain": [
136-
"'Bonjour, comment allez-vous ? \\n\\n\\nThis is the most common and formal way to say \"Hello, how are you?\" in French. \\n'"
135+
"'Bonjour, comment allez-vous ? \\n'"
137136
]
138137
},
139-
"execution_count": 34,
138+
"execution_count": 6,
140139
"metadata": {},
141140
"output_type": "execute_result"
142141
}
@@ -149,17 +148,17 @@
149148
},
150149
{
151150
"cell_type": "code",
152-
"execution_count": 35,
151+
"execution_count": 7,
153152
"id": "46472e65",
154153
"metadata": {},
155154
"outputs": [
156155
{
157156
"data": {
158157
"text/plain": [
159-
"'Bonjour, comment allez-vous ? \\n'"
158+
"'Bonjour, comment allez-vous ? \\n\\n\\nYou can also say:\\n\\n* **Salut, ça va ?** (Informal)\\n* **Coucou, comment vas-tu ?** (Very informal, for someone you know well)\\n'"
160159
]
161160
},
162-
"execution_count": 35,
161+
"execution_count": 7,
163162
"metadata": {},
164163
"output_type": "execute_result"
165164
}
@@ -172,7 +171,7 @@
172171
},
173172
{
174173
"cell_type": "code",
175-
"execution_count": 37,
174+
"execution_count": 8,
176175
"id": "5e3de0b7",
177176
"metadata": {},
178177
"outputs": [],
@@ -189,7 +188,7 @@
189188
},
190189
{
191190
"cell_type": "code",
192-
"execution_count": 41,
191+
"execution_count": 9,
193192
"id": "c99b32de",
194193
"metadata": {},
195194
"outputs": [],
@@ -199,7 +198,7 @@
199198
},
200199
{
201200
"cell_type": "code",
202-
"execution_count": 42,
201+
"execution_count": 10,
203202
"id": "2efdbe24",
204203
"metadata": {},
205204
"outputs": [
@@ -210,7 +209,7 @@
210209
" HumanMessage(content='Hello?', additional_kwargs={}, response_metadata={})]"
211210
]
212211
},
213-
"execution_count": 42,
212+
"execution_count": 10,
214213
"metadata": {},
215214
"output_type": "execute_result"
216215
}
@@ -221,17 +220,17 @@
221220
},
222221
{
223222
"cell_type": "code",
224-
"execution_count": null,
223+
"execution_count": 11,
225224
"id": "1e98497e",
226225
"metadata": {},
227226
"outputs": [
228227
{
229228
"data": {
230229
"text/plain": [
231-
"\"Bonjour ? \\n\\n\\nLet me know if you have any other phrases you'd like translated!\\n\""
230+
"'Bonjour ? \\n'"
232231
]
233232
},
234-
"execution_count": 43,
233+
"execution_count": 11,
235234
"metadata": {},
236235
"output_type": "execute_result"
237236
}
@@ -244,16 +243,155 @@
244243
},
245244
{
246245
"cell_type": "code",
247-
"execution_count": null,
246+
"execution_count": 6,
248247
"id": "695cfdd2",
249248
"metadata": {},
249+
"outputs": [
250+
{
251+
"name": "stdout",
252+
"output_type": "stream",
253+
"text": [
254+
"0.3.1\n"
255+
]
256+
}
257+
],
258+
"source": [
259+
"import langserve\n",
260+
"print(langserve.__version__)"
261+
]
262+
},
263+
{
264+
"cell_type": "code",
265+
"execution_count": 9,
266+
"id": "4859ec2a",
267+
"metadata": {},
268+
"outputs": [
269+
{
270+
"name": "stdout",
271+
"output_type": "stream",
272+
"text": [
273+
"Found existing installation: langserve 0.0.27\n",
274+
"Uninstalling langserve-0.0.27:\n",
275+
" Successfully uninstalled langserve-0.0.27\n",
276+
"Found existing installation: pydantic 1.10.8\n",
277+
"Uninstalling pydantic-1.10.8:\n",
278+
" Successfully uninstalled pydantic-1.10.8\n",
279+
"Found existing installation: fastapi 0.99.1\n",
280+
"Uninstalling fastapi-0.99.1:\n",
281+
" Successfully uninstalled fastapi-0.99.1\n"
282+
]
283+
}
284+
],
285+
"source": [
286+
"!pip uninstall langserve -y\n",
287+
"!pip uninstall pydantic -y\n",
288+
"!pip uninstall fastapi -y"
289+
]
290+
},
291+
{
292+
"cell_type": "code",
293+
"execution_count": 11,
294+
"id": "22b1455b",
295+
"metadata": {},
296+
"outputs": [
297+
{
298+
"name": "stdout",
299+
"output_type": "stream",
300+
"text": [
301+
"Requirement already satisfied: langserve in /home/ghost/.pyenv/versions/3.13.2/lib/python3.13/site-packages (0.0.27)\n",
302+
"Collecting langserve\n",
303+
" Using cached langserve-0.3.1-py3-none-any.whl.metadata (40 kB)\n",
304+
"Requirement already satisfied: httpx<1.0,>=0.23.0 in /home/ghost/.pyenv/versions/3.13.2/lib/python3.13/site-packages (from langserve) (0.28.1)\n",
305+
"Requirement already satisfied: langchain-core<0.4,>=0.3 in /home/ghost/.pyenv/versions/3.13.2/lib/python3.13/site-packages (from langserve) (0.3.51)\n",
306+
"Requirement already satisfied: orjson<4,>=2 in /home/ghost/.pyenv/versions/3.13.2/lib/python3.13/site-packages (from langserve) (3.10.16)\n",
307+
"Collecting pydantic<3.0,>=2.7 (from langserve)\n",
308+
" Using cached pydantic-2.11.3-py3-none-any.whl.metadata (65 kB)\n",
309+
"Requirement already satisfied: anyio in /home/ghost/.pyenv/versions/3.13.2/lib/python3.13/site-packages (from httpx<1.0,>=0.23.0->langserve) (4.9.0)\n",
310+
"Requirement already satisfied: certifi in /home/ghost/.pyenv/versions/3.13.2/lib/python3.13/site-packages (from httpx<1.0,>=0.23.0->langserve) (2025.1.31)\n",
311+
"Requirement already satisfied: httpcore==1.* in /home/ghost/.pyenv/versions/3.13.2/lib/python3.13/site-packages (from httpx<1.0,>=0.23.0->langserve) (1.0.7)\n",
312+
"Requirement already satisfied: idna in /home/ghost/.pyenv/versions/3.13.2/lib/python3.13/site-packages (from httpx<1.0,>=0.23.0->langserve) (3.10)\n",
313+
"Requirement already satisfied: h11<0.15,>=0.13 in /home/ghost/.pyenv/versions/3.13.2/lib/python3.13/site-packages (from httpcore==1.*->httpx<1.0,>=0.23.0->langserve) (0.14.0)\n",
314+
"Requirement already satisfied: langsmith<0.4,>=0.1.125 in /home/ghost/.pyenv/versions/3.13.2/lib/python3.13/site-packages (from langchain-core<0.4,>=0.3->langserve) (0.3.27)\n",
315+
"Requirement already satisfied: tenacity!=8.4.0,<10.0.0,>=8.1.0 in /home/ghost/.pyenv/versions/3.13.2/lib/python3.13/site-packages (from langchain-core<0.4,>=0.3->langserve) (9.1.2)\n",
316+
"Requirement already satisfied: jsonpatch<2.0,>=1.33 in /home/ghost/.pyenv/versions/3.13.2/lib/python3.13/site-packages (from langchain-core<0.4,>=0.3->langserve) (1.33)\n",
317+
"Requirement already satisfied: PyYAML>=5.3 in /home/ghost/.pyenv/versions/3.13.2/lib/python3.13/site-packages (from langchain-core<0.4,>=0.3->langserve) (6.0.2)\n",
318+
"Requirement already satisfied: packaging<25,>=23.2 in /home/ghost/.pyenv/versions/3.13.2/lib/python3.13/site-packages (from langchain-core<0.4,>=0.3->langserve) (24.2)\n",
319+
"Requirement already satisfied: typing-extensions>=4.7 in /home/ghost/.pyenv/versions/3.13.2/lib/python3.13/site-packages (from langchain-core<0.4,>=0.3->langserve) (4.13.1)\n",
320+
"Requirement already satisfied: annotated-types>=0.6.0 in /home/ghost/.pyenv/versions/3.13.2/lib/python3.13/site-packages (from pydantic<3.0,>=2.7->langserve) (0.7.0)\n",
321+
"Requirement already satisfied: pydantic-core==2.33.1 in /home/ghost/.pyenv/versions/3.13.2/lib/python3.13/site-packages (from pydantic<3.0,>=2.7->langserve) (2.33.1)\n",
322+
"Requirement already satisfied: typing-inspection>=0.4.0 in /home/ghost/.pyenv/versions/3.13.2/lib/python3.13/site-packages (from pydantic<3.0,>=2.7->langserve) (0.4.0)\n",
323+
"Requirement already satisfied: jsonpointer>=1.9 in /home/ghost/.pyenv/versions/3.13.2/lib/python3.13/site-packages (from jsonpatch<2.0,>=1.33->langchain-core<0.4,>=0.3->langserve) (3.0.0)\n",
324+
"Requirement already satisfied: requests<3,>=2 in /home/ghost/.pyenv/versions/3.13.2/lib/python3.13/site-packages (from langsmith<0.4,>=0.1.125->langchain-core<0.4,>=0.3->langserve) (2.32.3)\n",
325+
"Requirement already satisfied: requests-toolbelt<2.0.0,>=1.0.0 in /home/ghost/.pyenv/versions/3.13.2/lib/python3.13/site-packages (from langsmith<0.4,>=0.1.125->langchain-core<0.4,>=0.3->langserve) (1.0.0)\n",
326+
"Requirement already satisfied: zstandard<0.24.0,>=0.23.0 in /home/ghost/.pyenv/versions/3.13.2/lib/python3.13/site-packages (from langsmith<0.4,>=0.1.125->langchain-core<0.4,>=0.3->langserve) (0.23.0)\n",
327+
"Requirement already satisfied: sniffio>=1.1 in /home/ghost/.pyenv/versions/3.13.2/lib/python3.13/site-packages (from anyio->httpx<1.0,>=0.23.0->langserve) (1.3.1)\n",
328+
"Requirement already satisfied: charset-normalizer<4,>=2 in /home/ghost/.pyenv/versions/3.13.2/lib/python3.13/site-packages (from requests<3,>=2->langsmith<0.4,>=0.1.125->langchain-core<0.4,>=0.3->langserve) (3.4.1)\n",
329+
"Requirement already satisfied: urllib3<3,>=1.21.1 in /home/ghost/.pyenv/versions/3.13.2/lib/python3.13/site-packages (from requests<3,>=2->langsmith<0.4,>=0.1.125->langchain-core<0.4,>=0.3->langserve) (2.3.0)\n",
330+
"Using cached langserve-0.3.1-py3-none-any.whl (1.2 MB)\n",
331+
"Using cached pydantic-2.11.3-py3-none-any.whl (443 kB)\n",
332+
"Installing collected packages: pydantic, langserve\n",
333+
" Attempting uninstall: pydantic\n",
334+
" Found existing installation: pydantic 1.10.8\n",
335+
" Uninstalling pydantic-1.10.8:\n",
336+
" Successfully uninstalled pydantic-1.10.8\n",
337+
" Attempting uninstall: langserve\n",
338+
" Found existing installation: langserve 0.0.27\n",
339+
" Uninstalling langserve-0.0.27:\n",
340+
" Successfully uninstalled langserve-0.0.27\n",
341+
"\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\n",
342+
"fastapi 0.99.1 requires pydantic!=1.8,!=1.8.1,<2.0.0,>=1.7.4, but you have pydantic 2.11.3 which is incompatible.\u001b[0m\u001b[31m\n",
343+
"\u001b[0mSuccessfully installed langserve-0.3.1 pydantic-2.11.3\n",
344+
"Requirement already satisfied: pydantic in /home/ghost/.pyenv/versions/3.13.2/lib/python3.13/site-packages (2.11.3)\n",
345+
"Requirement already satisfied: annotated-types>=0.6.0 in /home/ghost/.pyenv/versions/3.13.2/lib/python3.13/site-packages (from pydantic) (0.7.0)\n",
346+
"Requirement already satisfied: pydantic-core==2.33.1 in /home/ghost/.pyenv/versions/3.13.2/lib/python3.13/site-packages (from pydantic) (2.33.1)\n",
347+
"Requirement already satisfied: typing-extensions>=4.12.2 in /home/ghost/.pyenv/versions/3.13.2/lib/python3.13/site-packages (from pydantic) (4.13.1)\n",
348+
"Requirement already satisfied: typing-inspection>=0.4.0 in /home/ghost/.pyenv/versions/3.13.2/lib/python3.13/site-packages (from pydantic) (0.4.0)\n",
349+
"Requirement already satisfied: fastapi in /home/ghost/.pyenv/versions/3.13.2/lib/python3.13/site-packages (0.99.1)\n",
350+
"Collecting fastapi\n",
351+
" Using cached fastapi-0.115.12-py3-none-any.whl.metadata (27 kB)\n",
352+
"Collecting starlette<0.47.0,>=0.40.0 (from fastapi)\n",
353+
" Using cached starlette-0.46.1-py3-none-any.whl.metadata (6.2 kB)\n",
354+
"Requirement already satisfied: pydantic!=1.8,!=1.8.1,!=2.0.0,!=2.0.1,!=2.1.0,<3.0.0,>=1.7.4 in /home/ghost/.pyenv/versions/3.13.2/lib/python3.13/site-packages (from fastapi) (2.11.3)\n",
355+
"Requirement already satisfied: typing-extensions>=4.8.0 in /home/ghost/.pyenv/versions/3.13.2/lib/python3.13/site-packages (from fastapi) (4.13.1)\n",
356+
"Requirement already satisfied: annotated-types>=0.6.0 in /home/ghost/.pyenv/versions/3.13.2/lib/python3.13/site-packages (from pydantic!=1.8,!=1.8.1,!=2.0.0,!=2.0.1,!=2.1.0,<3.0.0,>=1.7.4->fastapi) (0.7.0)\n",
357+
"Requirement already satisfied: pydantic-core==2.33.1 in /home/ghost/.pyenv/versions/3.13.2/lib/python3.13/site-packages (from pydantic!=1.8,!=1.8.1,!=2.0.0,!=2.0.1,!=2.1.0,<3.0.0,>=1.7.4->fastapi) (2.33.1)\n",
358+
"Requirement already satisfied: typing-inspection>=0.4.0 in /home/ghost/.pyenv/versions/3.13.2/lib/python3.13/site-packages (from pydantic!=1.8,!=1.8.1,!=2.0.0,!=2.0.1,!=2.1.0,<3.0.0,>=1.7.4->fastapi) (0.4.0)\n",
359+
"Requirement already satisfied: anyio<5,>=3.6.2 in /home/ghost/.pyenv/versions/3.13.2/lib/python3.13/site-packages (from starlette<0.47.0,>=0.40.0->fastapi) (4.9.0)\n",
360+
"Requirement already satisfied: idna>=2.8 in /home/ghost/.pyenv/versions/3.13.2/lib/python3.13/site-packages (from anyio<5,>=3.6.2->starlette<0.47.0,>=0.40.0->fastapi) (3.10)\n",
361+
"Requirement already satisfied: sniffio>=1.1 in /home/ghost/.pyenv/versions/3.13.2/lib/python3.13/site-packages (from anyio<5,>=3.6.2->starlette<0.47.0,>=0.40.0->fastapi) (1.3.1)\n",
362+
"Downloading fastapi-0.115.12-py3-none-any.whl (95 kB)\n",
363+
"Downloading starlette-0.46.1-py3-none-any.whl (71 kB)\n",
364+
"Installing collected packages: starlette, fastapi\n",
365+
" Attempting uninstall: starlette\n",
366+
" Found existing installation: starlette 0.27.0\n",
367+
" Uninstalling starlette-0.27.0:\n",
368+
" Successfully uninstalled starlette-0.27.0\n",
369+
" Attempting uninstall: fastapi\n",
370+
" Found existing installation: fastapi 0.99.1\n",
371+
" Uninstalling fastapi-0.99.1:\n",
372+
" Successfully uninstalled fastapi-0.99.1\n",
373+
"Successfully installed fastapi-0.115.12 starlette-0.46.1\n"
374+
]
375+
}
376+
],
377+
"source": [
378+
"!pip install --upgrade langserve\n",
379+
"!pip install --upgrade pydantic\n",
380+
"!pip install --upgrade fastapi\n"
381+
]
382+
},
383+
{
384+
"cell_type": "code",
385+
"execution_count": null,
386+
"id": "e7f8c5b9",
387+
"metadata": {},
250388
"outputs": [],
251389
"source": []
252390
}
253391
],
254392
"metadata": {
255393
"kernelspec": {
256-
"display_name": "langchain_env",
394+
"display_name": "Python 3",
257395
"language": "python",
258396
"name": "python3"
259397
},

3. LLM Appication Using LCEL/server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
chain=prompt_template|model|parser
2424

2525

26+
2627
## App definition
2728
app=FastAPI(title="Langchain Server",
2829
version="1.0",
@@ -37,5 +38,4 @@
3738

3839
if __name__=="__main__":
3940
import uvicorn
40-
uvicorn.run(app,host="127.0.0.1",port=8000)
41-
41+
uvicorn.run(app,host="127.0.0.1",port=8000)

0 commit comments

Comments
 (0)