Spaces:
Sleeping
Sleeping
LVKinyanjui
commited on
Commit
•
c6b7e90
1
Parent(s):
183f195
Created a nice chatbot ui with streamlit and gave it a chat history
Browse files- examples/Groq_Autogen.ipynb +444 -0
- examples/Groq_Langchain.ipynb +343 -0
- examples/coding/tmp_code_65e3424c19629fcfd124f7ea31ec17d8.py +19 -0
- examples/data/sample.png +0 -0
- examples/getting_started.py +18 -0
- examples/vison.ipynb +152 -0
- requirements.txt +7 -0
- st_image_chat.py +63 -0
- st_long_context_chat.py +37 -0
examples/Groq_Autogen.ipynb
ADDED
@@ -0,0 +1,444 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "markdown",
|
5 |
+
"id": "983c0b04-833c-415a-8cff-fb683f89d832",
|
6 |
+
"metadata": {},
|
7 |
+
"source": [
|
8 |
+
"<h1 align=center> GroQ + Autogen </h1>\n",
|
9 |
+
"\n",
|
10 |
+
"`Autogen` is an LLM framework developed by microsoft. It provides a variety of features that may be lacking in other frameworks such as *multi agent tasks*, *nested chats*, *code execution* and so on. It would be interesting to see how agents powered by lightning fast groq perform with such a feature rich framework. Our notebook follows this [guide](https://microsoft.github.io/autogen/0.2/docs/topics/non-openai-models/cloud-groq/)."
|
11 |
+
]
|
12 |
+
},
|
13 |
+
{
|
14 |
+
"cell_type": "code",
|
15 |
+
"execution_count": 1,
|
16 |
+
"id": "114b3ac6-30fb-4e43-9c50-a7fa18bf5fcb",
|
17 |
+
"metadata": {},
|
18 |
+
"outputs": [],
|
19 |
+
"source": [
|
20 |
+
"import os\n",
|
21 |
+
"api_key = os.getenv(\"GROQ_API_KEY\")\n",
|
22 |
+
"if api_key is None:\n",
|
23 |
+
" raise ValueError(\"Groq API key environment variable not set\")"
|
24 |
+
]
|
25 |
+
},
|
26 |
+
{
|
27 |
+
"cell_type": "code",
|
28 |
+
"execution_count": 16,
|
29 |
+
"id": "2bee41af-6510-44e2-875f-8c55da14aa4c",
|
30 |
+
"metadata": {},
|
31 |
+
"outputs": [],
|
32 |
+
"source": [
|
33 |
+
"config_list = [\n",
|
34 |
+
" {\n",
|
35 |
+
" \"model\": \"llama3-8b-8192\",\n",
|
36 |
+
" \"api_key\": api_key,\n",
|
37 |
+
" # \"api_type\": \"groq\", # Removed as explained here https://stackoverflow.com/a/77560277\n",
|
38 |
+
" \"base_url\": \"https://api.groq.com/openai/v1\",\n",
|
39 |
+
" # \"frequency_penalty\": 0.5,\n",
|
40 |
+
" # \"max_tokens\": 2048,\n",
|
41 |
+
" # \"presence_penalty\": 0.2,\n",
|
42 |
+
" # \"seed\": 42,\n",
|
43 |
+
" # \"temperature\": 0.5,\n",
|
44 |
+
" # \"top_p\": 0.2,\n",
|
45 |
+
" }\n",
|
46 |
+
"]"
|
47 |
+
]
|
48 |
+
},
|
49 |
+
{
|
50 |
+
"cell_type": "code",
|
51 |
+
"execution_count": 17,
|
52 |
+
"id": "998dfb98-4253-4377-b39d-b1241f047cf0",
|
53 |
+
"metadata": {},
|
54 |
+
"outputs": [],
|
55 |
+
"source": [
|
56 |
+
"from pathlib import Path\n",
|
57 |
+
"\n",
|
58 |
+
"from autogen import AssistantAgent, UserProxyAgent\n",
|
59 |
+
"from autogen.coding import LocalCommandLineCodeExecutor"
|
60 |
+
]
|
61 |
+
},
|
62 |
+
{
|
63 |
+
"cell_type": "code",
|
64 |
+
"execution_count": 18,
|
65 |
+
"id": "3880db83-8514-4b2d-919f-b1c42647bb74",
|
66 |
+
"metadata": {},
|
67 |
+
"outputs": [],
|
68 |
+
"source": [
|
69 |
+
"# Setting up the code executor\n",
|
70 |
+
"workdir = Path(\"coding\")\n",
|
71 |
+
"workdir.mkdir(exist_ok=True)\n",
|
72 |
+
"code_executor = LocalCommandLineCodeExecutor(work_dir=workdir)"
|
73 |
+
]
|
74 |
+
},
|
75 |
+
{
|
76 |
+
"cell_type": "code",
|
77 |
+
"execution_count": 19,
|
78 |
+
"id": "e261e35e-3c14-4d84-8710-b1726d9fe4b6",
|
79 |
+
"metadata": {},
|
80 |
+
"outputs": [],
|
81 |
+
"source": [
|
82 |
+
"# Setting up the agents\n",
|
83 |
+
"\n",
|
84 |
+
"# The UserProxyAgent will execute the code that the AssistantAgent provides\n",
|
85 |
+
"user_proxy_agent = UserProxyAgent(\n",
|
86 |
+
" name=\"User\",\n",
|
87 |
+
" code_execution_config={\"executor\": code_executor},\n",
|
88 |
+
" is_termination_msg=lambda msg: \"FINISH\" in msg.get(\"content\"),\n",
|
89 |
+
")\n",
|
90 |
+
"\n",
|
91 |
+
"system_message = \"\"\"You are a helpful AI assistant who writes code and the user executes it.\n",
|
92 |
+
"Solve tasks using your coding and language skills.\n",
|
93 |
+
"In the following cases, suggest python code (in a python coding block) for the user to execute.\n",
|
94 |
+
"Solve the task step by step if you need to. If a plan is not provided, explain your plan first. Be clear which step uses code, and which step uses your language skill.\n",
|
95 |
+
"When using code, you must indicate the script type in the code block. The user cannot provide any other feedback or perform any other action beyond executing the code you suggest. The user can't modify your code. So do not suggest incomplete code which requires users to modify. Don't use a code block if it's not intended to be executed by the user.\n",
|
96 |
+
"Don't include multiple code blocks in one response. Do not ask users to copy and paste the result. Instead, use 'print' function for the output when relevant. Check the execution result returned by the user.\n",
|
97 |
+
"If the result indicates there is an error, fix the error and output the code again. Suggest the full code instead of partial code or code changes. If the error can't be fixed or if the task is not solved even after the code is executed successfully, analyze the problem, revisit your assumption, collect additional info you need, and think of a different approach to try.\n",
|
98 |
+
"When you find an answer, verify the answer carefully. Include verifiable evidence in your response if possible.\n",
|
99 |
+
"IMPORTANT: Wait for the user to execute your code and then you can reply with the word \"FINISH\". DO NOT OUTPUT \"FINISH\" after your code block.\"\"\"\n",
|
100 |
+
"\n",
|
101 |
+
"# The AssistantAgent, using Groq's model, will take the coding request and return code\n",
|
102 |
+
"assistant_agent = AssistantAgent(\n",
|
103 |
+
" name=\"Groq Assistant\",\n",
|
104 |
+
" system_message=system_message,\n",
|
105 |
+
" llm_config={\"config_list\": config_list},\n",
|
106 |
+
")"
|
107 |
+
]
|
108 |
+
},
|
109 |
+
{
|
110 |
+
"cell_type": "code",
|
111 |
+
"execution_count": 21,
|
112 |
+
"id": "f58a8e4c-f026-4dcb-a4f3-cd19cc716ff6",
|
113 |
+
"metadata": {},
|
114 |
+
"outputs": [
|
115 |
+
{
|
116 |
+
"name": "stdout",
|
117 |
+
"output_type": "stream",
|
118 |
+
"text": [
|
119 |
+
"\u001b[33mUser\u001b[0m (to Groq Assistant):\n",
|
120 |
+
"\n",
|
121 |
+
"Provide code to count the number of prime numbers from 1 to 10000.\n",
|
122 |
+
"\n",
|
123 |
+
"--------------------------------------------------------------------------------\n",
|
124 |
+
"\u001b[33mGroq Assistant\u001b[0m (to User):\n",
|
125 |
+
"\n",
|
126 |
+
"Here is a Python script to count the number of prime numbers from 1 to 10000:\n",
|
127 |
+
"\n",
|
128 |
+
"```python\n",
|
129 |
+
"def is_prime(n):\n",
|
130 |
+
" if n <= 1:\n",
|
131 |
+
" return False\n",
|
132 |
+
" if n == 2:\n",
|
133 |
+
" return True\n",
|
134 |
+
" if n % 2 == 0:\n",
|
135 |
+
" return False\n",
|
136 |
+
" max_divisor = int(n**0.5) + 1\n",
|
137 |
+
" for d in range(3, max_divisor, 2):\n",
|
138 |
+
" if n % d == 0:\n",
|
139 |
+
" return False\n",
|
140 |
+
" return True\n",
|
141 |
+
"\n",
|
142 |
+
"count = 0\n",
|
143 |
+
"for num in range(1, 10001):\n",
|
144 |
+
" if is_prime(num):\n",
|
145 |
+
" count += 1\n",
|
146 |
+
"\n",
|
147 |
+
"print(\"Number of prime numbers from 1 to 10000:\", count)\n",
|
148 |
+
"```\n",
|
149 |
+
"\n",
|
150 |
+
"This script defines a helper function `is_prime` to check if a number is prime, and then iterates over the range from 1 to 10000, incrementing a counter for each prime number found. Finally, it prints the total count of prime numbers.\n",
|
151 |
+
"\n",
|
152 |
+
"--------------------------------------------------------------------------------\n"
|
153 |
+
]
|
154 |
+
},
|
155 |
+
{
|
156 |
+
"name": "stdin",
|
157 |
+
"output_type": "stream",
|
158 |
+
"text": [
|
159 |
+
"Provide feedback to Groq Assistant. Press enter to skip and use auto-reply, or type 'exit' to end the conversation: \n"
|
160 |
+
]
|
161 |
+
},
|
162 |
+
{
|
163 |
+
"name": "stdout",
|
164 |
+
"output_type": "stream",
|
165 |
+
"text": [
|
166 |
+
"\u001b[31m\n",
|
167 |
+
">>>>>>>> NO HUMAN INPUT RECEIVED.\u001b[0m\n",
|
168 |
+
"\u001b[31m\n",
|
169 |
+
">>>>>>>> USING AUTO REPLY...\u001b[0m\n",
|
170 |
+
"\u001b[31m\n",
|
171 |
+
">>>>>>>> EXECUTING CODE BLOCK (inferred language is python)...\u001b[0m\n",
|
172 |
+
"\u001b[33mUser\u001b[0m (to Groq Assistant):\n",
|
173 |
+
"\n",
|
174 |
+
"exitcode: 0 (execution succeeded)\n",
|
175 |
+
"Code output: Number of prime numbers from 1 to 10000: 1229\n",
|
176 |
+
"\n",
|
177 |
+
"\n",
|
178 |
+
"--------------------------------------------------------------------------------\n",
|
179 |
+
"\u001b[33mGroq Assistant\u001b[0m (to User):\n",
|
180 |
+
"\n",
|
181 |
+
"FINISH\n",
|
182 |
+
"\n",
|
183 |
+
"--------------------------------------------------------------------------------\n"
|
184 |
+
]
|
185 |
+
},
|
186 |
+
{
|
187 |
+
"name": "stdin",
|
188 |
+
"output_type": "stream",
|
189 |
+
"text": [
|
190 |
+
"Provide feedback to Groq Assistant. Press enter to skip and use auto-reply, or type 'exit' to end the conversation: exit\n"
|
191 |
+
]
|
192 |
+
}
|
193 |
+
],
|
194 |
+
"source": [
|
195 |
+
"# Start the chat, with the UserProxyAgent asking the AssistantAgent the message\n",
|
196 |
+
"chat_result = user_proxy_agent.initiate_chat(\n",
|
197 |
+
" assistant_agent,\n",
|
198 |
+
" message=\"Provide code to count the number of prime numbers from 1 to 10000.\",\n",
|
199 |
+
")"
|
200 |
+
]
|
201 |
+
},
|
202 |
+
{
|
203 |
+
"cell_type": "markdown",
|
204 |
+
"id": "a9ab2dfb-9752-4ee7-b63e-4cb267ee112b",
|
205 |
+
"metadata": {},
|
206 |
+
"source": [
|
207 |
+
"<h2 align=center> GroQ + Autogen + Tool Use </h2>\n",
|
208 |
+
"\n",
|
209 |
+
"We push our knowledge further by trying out function calling."
|
210 |
+
]
|
211 |
+
},
|
212 |
+
{
|
213 |
+
"cell_type": "code",
|
214 |
+
"execution_count": 33,
|
215 |
+
"id": "e607f555-8f04-4d48-b780-f63215e401a2",
|
216 |
+
"metadata": {},
|
217 |
+
"outputs": [],
|
218 |
+
"source": [
|
219 |
+
"from autogen import AssistantAgent, UserProxyAgent\n",
|
220 |
+
"from typing import Literal\n",
|
221 |
+
"from typing_extensions import Annotated\n",
|
222 |
+
"import json"
|
223 |
+
]
|
224 |
+
},
|
225 |
+
{
|
226 |
+
"cell_type": "code",
|
227 |
+
"execution_count": 34,
|
228 |
+
"id": "864c50ef-e904-40de-9d3e-f7918d70141e",
|
229 |
+
"metadata": {},
|
230 |
+
"outputs": [],
|
231 |
+
"source": [
|
232 |
+
"config_list = [\n",
|
233 |
+
" {\n",
|
234 |
+
" \"model\": \"llama3-groq-8b-8192-tool-use-preview\",\n",
|
235 |
+
" \"base_url\": \"https://api.groq.com/openai/v1\",\n",
|
236 |
+
" \"api_key\": api_key,\n",
|
237 |
+
" }\n",
|
238 |
+
"]"
|
239 |
+
]
|
240 |
+
},
|
241 |
+
{
|
242 |
+
"cell_type": "code",
|
243 |
+
"execution_count": 35,
|
244 |
+
"id": "e9ee73b2-7a10-4381-8e9d-9e060226f840",
|
245 |
+
"metadata": {},
|
246 |
+
"outputs": [],
|
247 |
+
"source": [
|
248 |
+
"# Create the agent for tool calling\n",
|
249 |
+
"chatbot = AssistantAgent(\n",
|
250 |
+
" name=\"chatbot\",\n",
|
251 |
+
" system_message=\"\"\"For currency exchange and weather forecasting tasks,\n",
|
252 |
+
" only use the functions you have been provided with.\n",
|
253 |
+
" Output 'HAVE FUN!' when an answer has been provided.\"\"\",\n",
|
254 |
+
" llm_config={\"config_list\": config_list},\n",
|
255 |
+
")\n",
|
256 |
+
"\n",
|
257 |
+
"# Note that we have changed the termination string to be \"HAVE FUN!\"\n",
|
258 |
+
"user_proxy = UserProxyAgent(\n",
|
259 |
+
" name=\"user_proxy\",\n",
|
260 |
+
" is_termination_msg=lambda x: x.get(\"content\", \"\") and \"HAVE FUN!\" in x.get(\"content\", \"\"),\n",
|
261 |
+
" human_input_mode=\"NEVER\",\n",
|
262 |
+
" max_consecutive_auto_reply=1,\n",
|
263 |
+
")"
|
264 |
+
]
|
265 |
+
},
|
266 |
+
{
|
267 |
+
"cell_type": "code",
|
268 |
+
"execution_count": 36,
|
269 |
+
"id": "20887c70-d084-4cb3-b920-d1217fd78a48",
|
270 |
+
"metadata": {},
|
271 |
+
"outputs": [],
|
272 |
+
"source": [
|
273 |
+
"# Currency Exchange function\n",
|
274 |
+
"\n",
|
275 |
+
"CurrencySymbol = Literal[\"USD\", \"EUR\"]\n",
|
276 |
+
"\n",
|
277 |
+
"# Define our function that we expect to call\n",
|
278 |
+
"\n",
|
279 |
+
"\n",
|
280 |
+
"def exchange_rate(base_currency: CurrencySymbol, quote_currency: CurrencySymbol) -> float:\n",
|
281 |
+
" if base_currency == quote_currency:\n",
|
282 |
+
" return 1.0\n",
|
283 |
+
" elif base_currency == \"USD\" and quote_currency == \"EUR\":\n",
|
284 |
+
" return 1 / 1.1\n",
|
285 |
+
" elif base_currency == \"EUR\" and quote_currency == \"USD\":\n",
|
286 |
+
" return 1.1\n",
|
287 |
+
" else:\n",
|
288 |
+
" raise ValueError(f\"Unknown currencies {base_currency}, {quote_currency}\")\n",
|
289 |
+
"\n",
|
290 |
+
"\n",
|
291 |
+
"# Register the function with the agent\n",
|
292 |
+
"\n",
|
293 |
+
"\n",
|
294 |
+
"@user_proxy.register_for_execution()\n",
|
295 |
+
"@chatbot.register_for_llm(description=\"Currency exchange calculator.\")\n",
|
296 |
+
"def currency_calculator(\n",
|
297 |
+
" base_amount: Annotated[float, \"Amount of currency in base_currency\"],\n",
|
298 |
+
" base_currency: Annotated[CurrencySymbol, \"Base currency\"] = \"USD\",\n",
|
299 |
+
" quote_currency: Annotated[CurrencySymbol, \"Quote currency\"] = \"EUR\",\n",
|
300 |
+
") -> str:\n",
|
301 |
+
" quote_amount = exchange_rate(base_currency, quote_currency) * base_amount\n",
|
302 |
+
" return f\"{format(quote_amount, '.2f')} {quote_currency}\"\n",
|
303 |
+
"\n",
|
304 |
+
"\n",
|
305 |
+
"# Weather function\n",
|
306 |
+
"\n",
|
307 |
+
"\n",
|
308 |
+
"# Example function to make available to model\n",
|
309 |
+
"def get_current_weather(location, unit=\"fahrenheit\"):\n",
|
310 |
+
" \"\"\"Get the weather for some location\"\"\"\n",
|
311 |
+
" if \"chicago\" in location.lower():\n",
|
312 |
+
" return json.dumps({\"location\": \"Chicago\", \"temperature\": \"13\", \"unit\": unit})\n",
|
313 |
+
" elif \"san francisco\" in location.lower():\n",
|
314 |
+
" return json.dumps({\"location\": \"San Francisco\", \"temperature\": \"55\", \"unit\": unit})\n",
|
315 |
+
" elif \"new york\" in location.lower():\n",
|
316 |
+
" return json.dumps({\"location\": \"New York\", \"temperature\": \"11\", \"unit\": unit})\n",
|
317 |
+
" else:\n",
|
318 |
+
" return json.dumps({\"location\": location, \"temperature\": \"unknown\"})\n",
|
319 |
+
"\n",
|
320 |
+
"\n",
|
321 |
+
"# Register the function with the agent\n",
|
322 |
+
"\n",
|
323 |
+
"\n",
|
324 |
+
"@user_proxy.register_for_execution()\n",
|
325 |
+
"@chatbot.register_for_llm(description=\"Weather forecast for US cities.\")\n",
|
326 |
+
"def weather_forecast(\n",
|
327 |
+
" location: Annotated[str, \"City name\"],\n",
|
328 |
+
") -> str:\n",
|
329 |
+
" weather_details = get_current_weather(location=location)\n",
|
330 |
+
" weather = json.loads(weather_details)\n",
|
331 |
+
" return f\"{weather['location']} will be {weather['temperature']} degrees {weather['unit']}\""
|
332 |
+
]
|
333 |
+
},
|
334 |
+
{
|
335 |
+
"cell_type": "code",
|
336 |
+
"execution_count": 37,
|
337 |
+
"id": "6751d8fe-f0b2-49c8-8fa2-2ba10ccfcd4e",
|
338 |
+
"metadata": {},
|
339 |
+
"outputs": [
|
340 |
+
{
|
341 |
+
"name": "stdout",
|
342 |
+
"output_type": "stream",
|
343 |
+
"text": [
|
344 |
+
"\u001b[33muser_proxy\u001b[0m (to chatbot):\n",
|
345 |
+
"\n",
|
346 |
+
"What's the weather in New York and can you tell me how much is 123.45 EUR in USD so I can spend it on my holiday? Throw a few holiday tips in as well.\n",
|
347 |
+
"\n",
|
348 |
+
"--------------------------------------------------------------------------------\n",
|
349 |
+
"\u001b[33mchatbot\u001b[0m (to user_proxy):\n",
|
350 |
+
"\n",
|
351 |
+
"\u001b[32m***** Suggested tool call (call_fxry): weather_forecast *****\u001b[0m\n",
|
352 |
+
"Arguments: \n",
|
353 |
+
"{\"location\": \"New York\"}\n",
|
354 |
+
"\u001b[32m*************************************************************\u001b[0m\n",
|
355 |
+
"\u001b[32m***** Suggested tool call (call_6xgr): currency_calculator *****\u001b[0m\n",
|
356 |
+
"Arguments: \n",
|
357 |
+
"{\"base_amount\": 123.45, \"base_currency\": \"EUR\", \"quote_currency\": \"USD\"}\n",
|
358 |
+
"\u001b[32m****************************************************************\u001b[0m\n",
|
359 |
+
"\n",
|
360 |
+
"--------------------------------------------------------------------------------\n",
|
361 |
+
"\u001b[35m\n",
|
362 |
+
">>>>>>>> EXECUTING FUNCTION weather_forecast...\u001b[0m\n",
|
363 |
+
"\u001b[35m\n",
|
364 |
+
">>>>>>>> EXECUTING FUNCTION currency_calculator...\u001b[0m\n",
|
365 |
+
"\u001b[33muser_proxy\u001b[0m (to chatbot):\n",
|
366 |
+
"\n",
|
367 |
+
"\u001b[33muser_proxy\u001b[0m (to chatbot):\n",
|
368 |
+
"\n",
|
369 |
+
"\u001b[32m***** Response from calling tool (call_fxry) *****\u001b[0m\n",
|
370 |
+
"New York will be 11 degrees fahrenheit\n",
|
371 |
+
"\u001b[32m**************************************************\u001b[0m\n",
|
372 |
+
"\n",
|
373 |
+
"--------------------------------------------------------------------------------\n",
|
374 |
+
"\u001b[33muser_proxy\u001b[0m (to chatbot):\n",
|
375 |
+
"\n",
|
376 |
+
"\u001b[32m***** Response from calling tool (call_6xgr) *****\u001b[0m\n",
|
377 |
+
"135.80 USD\n",
|
378 |
+
"\u001b[32m**************************************************\u001b[0m\n",
|
379 |
+
"\n",
|
380 |
+
"--------------------------------------------------------------------------------\n",
|
381 |
+
"\u001b[33mchatbot\u001b[0m (to user_proxy):\n",
|
382 |
+
"\n",
|
383 |
+
"\u001b[32m***** Suggested tool call (call_ydzn): weather_forecast *****\u001b[0m\n",
|
384 |
+
"Arguments: \n",
|
385 |
+
"{\"location\":\"New York\"}\n",
|
386 |
+
"\u001b[32m*************************************************************\u001b[0m\n",
|
387 |
+
"\n",
|
388 |
+
"--------------------------------------------------------------------------------\n"
|
389 |
+
]
|
390 |
+
},
|
391 |
+
{
|
392 |
+
"ename": "TypeError",
|
393 |
+
"evalue": "string indices must be integers",
|
394 |
+
"output_type": "error",
|
395 |
+
"traceback": [
|
396 |
+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
397 |
+
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
|
398 |
+
"Cell \u001b[0;32mIn[37], line 8\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m# start the conversation\u001b[39;00m\n\u001b[1;32m 2\u001b[0m res \u001b[38;5;241m=\u001b[39m user_proxy\u001b[38;5;241m.\u001b[39minitiate_chat(\n\u001b[1;32m 3\u001b[0m chatbot,\n\u001b[1;32m 4\u001b[0m message\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mWhat\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124ms the weather in New York and can you tell me how much is 123.45 EUR in USD so I can spend it on my holiday? Throw a few holiday tips in as well.\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 5\u001b[0m summary_method\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mreflection_with_llm\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 6\u001b[0m )\n\u001b[0;32m----> 8\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mLLM SUMMARY: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[43mres\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msummary\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mcontent\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m]\u001b[49m\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n",
|
399 |
+
"\u001b[0;31mTypeError\u001b[0m: string indices must be integers"
|
400 |
+
]
|
401 |
+
}
|
402 |
+
],
|
403 |
+
"source": [
|
404 |
+
"# start the conversation\n",
|
405 |
+
"res = user_proxy.initiate_chat(\n",
|
406 |
+
" chatbot,\n",
|
407 |
+
" message=\"What's the weather in New York and can you tell me how much is 123.45 EUR in USD so I can spend it on my holiday? Throw a few holiday tips in as well.\",\n",
|
408 |
+
" summary_method=\"reflection_with_llm\",\n",
|
409 |
+
")\n",
|
410 |
+
"\n",
|
411 |
+
"print(f\"LLM SUMMARY: {res.summary['content']}\")"
|
412 |
+
]
|
413 |
+
},
|
414 |
+
{
|
415 |
+
"cell_type": "code",
|
416 |
+
"execution_count": null,
|
417 |
+
"id": "35883ac7-0fb4-44c1-b0a6-8e6f626a8366",
|
418 |
+
"metadata": {},
|
419 |
+
"outputs": [],
|
420 |
+
"source": []
|
421 |
+
}
|
422 |
+
],
|
423 |
+
"metadata": {
|
424 |
+
"kernelspec": {
|
425 |
+
"display_name": "Python 3 (ipykernel)",
|
426 |
+
"language": "python",
|
427 |
+
"name": "python3"
|
428 |
+
},
|
429 |
+
"language_info": {
|
430 |
+
"codemirror_mode": {
|
431 |
+
"name": "ipython",
|
432 |
+
"version": 3
|
433 |
+
},
|
434 |
+
"file_extension": ".py",
|
435 |
+
"mimetype": "text/x-python",
|
436 |
+
"name": "python",
|
437 |
+
"nbconvert_exporter": "python",
|
438 |
+
"pygments_lexer": "ipython3",
|
439 |
+
"version": "3.10.12"
|
440 |
+
}
|
441 |
+
},
|
442 |
+
"nbformat": 4,
|
443 |
+
"nbformat_minor": 5
|
444 |
+
}
|
examples/Groq_Langchain.ipynb
ADDED
@@ -0,0 +1,343 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "markdown",
|
5 |
+
"id": "b0023ae4-d8b4-40a7-b1f4-49e2e2447fbe",
|
6 |
+
"metadata": {},
|
7 |
+
"source": [
|
8 |
+
"<h1 align=center> GroQ </h1>\n",
|
9 |
+
"\n",
|
10 |
+
"Groq is a lightning fast inference api for large language models. We here intend to explore some of its capabilities and gauge the performance of its various hosted models on different tasks."
|
11 |
+
]
|
12 |
+
},
|
13 |
+
{
|
14 |
+
"cell_type": "markdown",
|
15 |
+
"id": "212a627f-57b7-4dd0-8fa7-375e414050f9",
|
16 |
+
"metadata": {},
|
17 |
+
"source": [
|
18 |
+
"[<h2 align=center> GroQ + Langchain </h2>](https://python.langchain.com/docs/integrations/chat/groq/)\n",
|
19 |
+
"\n",
|
20 |
+
"It is often useful to combine such an API with an LLM framework that allows us to perform advanced operations. Langchain is one of the more ubiquitous ones so we will turn to that."
|
21 |
+
]
|
22 |
+
},
|
23 |
+
{
|
24 |
+
"cell_type": "code",
|
25 |
+
"execution_count": 6,
|
26 |
+
"id": "385f99dd-d702-497d-8f86-04e25ec8da64",
|
27 |
+
"metadata": {},
|
28 |
+
"outputs": [],
|
29 |
+
"source": [
|
30 |
+
"import os\n",
|
31 |
+
"groq_api_key = os.getenv(\"GROQ_API_KEY\")\n",
|
32 |
+
"if groq_api_key is None:\n",
|
33 |
+
" raise ValueError(\"Groq API key environment variable not set\")"
|
34 |
+
]
|
35 |
+
},
|
36 |
+
{
|
37 |
+
"cell_type": "code",
|
38 |
+
"execution_count": 2,
|
39 |
+
"id": "30721e40-e728-44ab-b94c-1f328881199e",
|
40 |
+
"metadata": {},
|
41 |
+
"outputs": [],
|
42 |
+
"source": [
|
43 |
+
"from langchain_groq import ChatGroq"
|
44 |
+
]
|
45 |
+
},
|
46 |
+
{
|
47 |
+
"cell_type": "code",
|
48 |
+
"execution_count": 3,
|
49 |
+
"id": "20b24c21-019b-498c-aef7-fbc39cd959e2",
|
50 |
+
"metadata": {},
|
51 |
+
"outputs": [],
|
52 |
+
"source": [
|
53 |
+
"llm = ChatGroq(\n",
|
54 |
+
" model=\"llama3-8b-8192\",\n",
|
55 |
+
" temperature=0,\n",
|
56 |
+
" max_tokens=None,\n",
|
57 |
+
" timeout=None,\n",
|
58 |
+
" max_retries=2,\n",
|
59 |
+
")"
|
60 |
+
]
|
61 |
+
},
|
62 |
+
{
|
63 |
+
"cell_type": "code",
|
64 |
+
"execution_count": 4,
|
65 |
+
"id": "ae47d02f-d7f4-49e0-9d1a-a301116dfb70",
|
66 |
+
"metadata": {},
|
67 |
+
"outputs": [
|
68 |
+
{
|
69 |
+
"data": {
|
70 |
+
"text/plain": [
|
71 |
+
"AIMessage(content='Je adore le programmation.', additional_kwargs={}, response_metadata={'token_usage': {'completion_tokens': 7, 'prompt_tokens': 35, 'total_tokens': 42, 'completion_time': 0.005833333, 'prompt_time': 0.004470233, 'queue_time': 0.009797347000000001, 'total_time': 0.010303566}, 'model_name': 'llama3-8b-8192', 'system_fingerprint': 'fp_6a6771ae9c', 'finish_reason': 'stop', 'logprobs': None}, id='run-d6fc7095-9f72-405a-ad18-3148da303a81-0', usage_metadata={'input_tokens': 35, 'output_tokens': 7, 'total_tokens': 42})"
|
72 |
+
]
|
73 |
+
},
|
74 |
+
"execution_count": 4,
|
75 |
+
"metadata": {},
|
76 |
+
"output_type": "execute_result"
|
77 |
+
}
|
78 |
+
],
|
79 |
+
"source": [
|
80 |
+
"messages = [\n",
|
81 |
+
" (\n",
|
82 |
+
" \"system\",\n",
|
83 |
+
" \"You are a helpful assistant that translates English to French. Translate the user sentence.\",\n",
|
84 |
+
" ),\n",
|
85 |
+
" (\"human\", \"I love programming.\"),\n",
|
86 |
+
"]\n",
|
87 |
+
"ai_msg = llm.invoke(messages)\n",
|
88 |
+
"ai_msg"
|
89 |
+
]
|
90 |
+
},
|
91 |
+
{
|
92 |
+
"cell_type": "code",
|
93 |
+
"execution_count": 5,
|
94 |
+
"id": "724f99f7-abff-4a4c-9464-8d8df62840f8",
|
95 |
+
"metadata": {},
|
96 |
+
"outputs": [
|
97 |
+
{
|
98 |
+
"data": {
|
99 |
+
"text/plain": [
|
100 |
+
"AIMessage(content='Ich liebe Programmierung!\\n\\nTranslation: I love programming.', additional_kwargs={}, response_metadata={'token_usage': {'completion_tokens': 12, 'prompt_tokens': 30, 'total_tokens': 42, 'completion_time': 0.01, 'prompt_time': 0.001169453, 'queue_time': 0.012058078, 'total_time': 0.011169453}, 'model_name': 'llama3-8b-8192', 'system_fingerprint': 'fp_179b0f92c9', 'finish_reason': 'stop', 'logprobs': None}, id='run-8f6b3c1a-e9d1-41de-b5c4-2659214b9bd5-0', usage_metadata={'input_tokens': 30, 'output_tokens': 12, 'total_tokens': 42})"
|
101 |
+
]
|
102 |
+
},
|
103 |
+
"execution_count": 5,
|
104 |
+
"metadata": {},
|
105 |
+
"output_type": "execute_result"
|
106 |
+
}
|
107 |
+
],
|
108 |
+
"source": [
|
109 |
+
"from langchain_core.prompts import ChatPromptTemplate\n",
|
110 |
+
"\n",
|
111 |
+
"prompt = ChatPromptTemplate.from_messages(\n",
|
112 |
+
" [\n",
|
113 |
+
" (\n",
|
114 |
+
" \"system\",\n",
|
115 |
+
" \"You are a helpful assistant that translates {input_language} to {output_language}.\",\n",
|
116 |
+
" ),\n",
|
117 |
+
" (\"human\", \"{input}\"),\n",
|
118 |
+
" ]\n",
|
119 |
+
")\n",
|
120 |
+
"\n",
|
121 |
+
"chain = prompt | llm\n",
|
122 |
+
"chain.invoke(\n",
|
123 |
+
" {\n",
|
124 |
+
" \"input_language\": \"English\",\n",
|
125 |
+
" \"output_language\": \"German\",\n",
|
126 |
+
" \"input\": \"I love programming.\",\n",
|
127 |
+
" }\n",
|
128 |
+
")"
|
129 |
+
]
|
130 |
+
},
|
131 |
+
{
|
132 |
+
"cell_type": "markdown",
|
133 |
+
"id": "d4faefc1-4736-46ab-b786-965f662434c7",
|
134 |
+
"metadata": {},
|
135 |
+
"source": [
|
136 |
+
"<h2 align=center> GroQ + Langchain + Tool Use </h2>\n",
|
137 |
+
"\n",
|
138 |
+
"Here we test the capabilities of tool-enabled models hosted by groq to perform *function calling* (such as **llama3-groq-8b-8192-tool-use-preview**). This involves the LLM returning a json schema formatted in a particular way that it can be used to call functions with arguments."
|
139 |
+
]
|
140 |
+
},
|
141 |
+
{
|
142 |
+
"cell_type": "code",
|
143 |
+
"execution_count": 9,
|
144 |
+
"id": "5dc16219-bbf5-42e6-a014-bc67e0e3cf93",
|
145 |
+
"metadata": {},
|
146 |
+
"outputs": [],
|
147 |
+
"source": [
|
148 |
+
"from langchain.agents import Tool, AgentExecutor, create_react_agent\n",
|
149 |
+
"from langchain_core.prompts import ChatPromptTemplate\n",
|
150 |
+
"from langchain_groq import ChatGroq"
|
151 |
+
]
|
152 |
+
},
|
153 |
+
{
|
154 |
+
"cell_type": "code",
|
155 |
+
"execution_count": 15,
|
156 |
+
"id": "9287b236-19ee-45cf-8503-fbdd05c038ee",
|
157 |
+
"metadata": {},
|
158 |
+
"outputs": [],
|
159 |
+
"source": [
|
160 |
+
"# Create the LLM\n",
|
161 |
+
"llm = ChatGroq(\n",
|
162 |
+
" model=\"llama3-groq-70b-8192-tool-use-preview\",\n",
|
163 |
+
" temperature=0,\n",
|
164 |
+
" max_tokens=None,\n",
|
165 |
+
" timeout=None,\n",
|
166 |
+
" max_retries=2,\n",
|
167 |
+
")"
|
168 |
+
]
|
169 |
+
},
|
170 |
+
{
|
171 |
+
"cell_type": "code",
|
172 |
+
"execution_count": 16,
|
173 |
+
"id": "e9aa733c-057b-436e-ab9b-4e94d2e91df5",
|
174 |
+
"metadata": {},
|
175 |
+
"outputs": [],
|
176 |
+
"source": [
|
177 |
+
"def calculate_square(number: str) -> str:\n",
|
178 |
+
" \"\"\"Calculates the square of a given number\"\"\"\n",
|
179 |
+
" try:\n",
|
180 |
+
" num = float(number)\n",
|
181 |
+
" return str(num * num)\n",
|
182 |
+
" except ValueError:\n",
|
183 |
+
" return \"Please provide a valid number\"\n",
|
184 |
+
"\n",
|
185 |
+
"# Define the tool\n",
|
186 |
+
"tools = [\n",
|
187 |
+
" Tool(\n",
|
188 |
+
" name=\"Calculator\",\n",
|
189 |
+
" func=calculate_square,\n",
|
190 |
+
" description=\"Useful for calculating the square of a number. Input should be a single number.\"\n",
|
191 |
+
" )\n",
|
192 |
+
"]"
|
193 |
+
]
|
194 |
+
},
|
195 |
+
{
|
196 |
+
"cell_type": "code",
|
197 |
+
"execution_count": 17,
|
198 |
+
"id": "e8da1fd2-8f1f-40c5-8d35-07bbcbf41e96",
|
199 |
+
"metadata": {},
|
200 |
+
"outputs": [],
|
201 |
+
"source": [
|
202 |
+
"# Create the prompt template\n",
|
203 |
+
"prompt = ChatPromptTemplate.from_messages([\n",
|
204 |
+
" (\"system\", \"\"\"You are a helpful assistant that can use tools to solve problems.\n",
|
205 |
+
" \n",
|
206 |
+
" Available tools:\n",
|
207 |
+
" {tools}\n",
|
208 |
+
" \n",
|
209 |
+
" {agent_scratchpad}\n",
|
210 |
+
" \n",
|
211 |
+
" Use the following format:\n",
|
212 |
+
" Question: The user question you must answer\n",
|
213 |
+
" Thought: Your thought process about what to do\n",
|
214 |
+
" Action: The action to take, should be one of [{tool_names}]\n",
|
215 |
+
" Action Input: The input to the action\n",
|
216 |
+
" Observation: The result of the action\n",
|
217 |
+
" ... (this Thought/Action/Action Input/Observation can repeat N times)\n",
|
218 |
+
" Thought: I now know the final answer\n",
|
219 |
+
" Final Answer: The final answer to the original question\n",
|
220 |
+
" \"\"\"),\n",
|
221 |
+
" (\"human\", \"{input}\")\n",
|
222 |
+
"])\n",
|
223 |
+
"\n",
|
224 |
+
"# Create the agent\n",
|
225 |
+
"agent = create_react_agent(llm, tools, prompt)\n",
|
226 |
+
"\n",
|
227 |
+
"# Create the agent executor\n",
|
228 |
+
"agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)"
|
229 |
+
]
|
230 |
+
},
|
231 |
+
{
|
232 |
+
"cell_type": "code",
|
233 |
+
"execution_count": 18,
|
234 |
+
"id": "c0ba975b-f671-4473-8684-ac621c3bfeeb",
|
235 |
+
"metadata": {},
|
236 |
+
"outputs": [
|
237 |
+
{
|
238 |
+
"name": "stdout",
|
239 |
+
"output_type": "stream",
|
240 |
+
"text": [
|
241 |
+
"\n",
|
242 |
+
"\n",
|
243 |
+
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
|
244 |
+
"\u001b[32;1m\u001b[1;3mQuestion: What is the square of 7?\n",
|
245 |
+
"Thought: I can use the Calculator tool to find the square of a number.\n",
|
246 |
+
"Action: Calculator\n",
|
247 |
+
"Action Input: 7\u001b[0m\u001b[36;1m\u001b[1;3m49.0\u001b[0m\u001b[32;1m\u001b[1;3mQuestion: What is the square of 7?\n",
|
248 |
+
"Thought: I can use the Calculator tool to find the square of a number.\n",
|
249 |
+
"Action: Calculator\n",
|
250 |
+
"Action Input: 7\u001b[0m\u001b[36;1m\u001b[1;3m49.0\u001b[0m\u001b[32;1m\u001b[1;3mQuestion: What is the square of 7?\n",
|
251 |
+
"Thought: I can use the Calculator tool to find the square of a number.\n",
|
252 |
+
"Action: Calculator\n",
|
253 |
+
"Action Input: 7\u001b[0m\u001b[36;1m\u001b[1;3m49.0\u001b[0m\u001b[32;1m\u001b[1;3mQuestion: What is the square of 7?\n",
|
254 |
+
"Thought: I can use the Calculator tool to find the square of a number.\n",
|
255 |
+
"Action: Calculator\n",
|
256 |
+
"Action Input: 7\u001b[0m\u001b[36;1m\u001b[1;3m49.0\u001b[0m\u001b[32;1m\u001b[1;3mQuestion: What is the square of 7?\n",
|
257 |
+
"Thought: I can use the Calculator tool to find the square of a number.\n",
|
258 |
+
"Action: Calculator\n",
|
259 |
+
"Action Input: 7\u001b[0m\u001b[36;1m\u001b[1;3m49.0\u001b[0m\u001b[32;1m\u001b[1;3mQuestion: What is the square of 7?\n",
|
260 |
+
"Thought: I can use the Calculator tool to find the square of a number.\n",
|
261 |
+
"Action: Calculator\n",
|
262 |
+
"Action Input: 7\u001b[0m\u001b[36;1m\u001b[1;3m49.0\u001b[0m\u001b[32;1m\u001b[1;3mQuestion: What is the square of 7?\n",
|
263 |
+
"Thought: I can use the Calculator tool to find the square of a number.\n",
|
264 |
+
"Action: Calculator\n",
|
265 |
+
"Action Input: 7\u001b[0m\u001b[36;1m\u001b[1;3m49.0\u001b[0m\u001b[32;1m\u001b[1;3mQuestion: What is the square of 7?\n",
|
266 |
+
"Thought: I can use the Calculator tool to find the square of a number.\n",
|
267 |
+
"Action: Calculator\n",
|
268 |
+
"Action Input: 7\u001b[0m\u001b[36;1m\u001b[1;3m49.0\u001b[0m\u001b[32;1m\u001b[1;3mQuestion: What is the square of 7?\n",
|
269 |
+
"Thought: I can use the Calculator tool to find the square of a number.\n",
|
270 |
+
"Action: Calculator\n",
|
271 |
+
"Action Input: 7\u001b[0m\u001b[36;1m\u001b[1;3m49.0\u001b[0m\u001b[32;1m\u001b[1;3mQuestion: What is the square of 7?\n",
|
272 |
+
"Thought: I can use the Calculator tool to find the square of a number.\n",
|
273 |
+
"Action: Calculator\n",
|
274 |
+
"Action Input: 7\u001b[0m\u001b[36;1m\u001b[1;3m49.0\u001b[0m\u001b[32;1m\u001b[1;3mQuestion: What is the square of 7?\n",
|
275 |
+
"Thought: I can use the Calculator tool to find the square of a number.\n",
|
276 |
+
"Action: Calculator\n",
|
277 |
+
"Action Input: 7\u001b[0m\u001b[36;1m\u001b[1;3m49.0\u001b[0m\u001b[32;1m\u001b[1;3mQuestion: What is the square of 7?\n",
|
278 |
+
"Thought: I can use the Calculator tool to find the square of a number.\n",
|
279 |
+
"Action: Calculator\n",
|
280 |
+
"Action Input: 7\u001b[0m\u001b[36;1m\u001b[1;3m49.0\u001b[0m\u001b[32;1m\u001b[1;3mQuestion: What is the square of 7?\n",
|
281 |
+
"Thought: I can use the Calculator tool to find the square of a number.\n",
|
282 |
+
"Action: Calculator\n",
|
283 |
+
"Action Input: 7\u001b[0m\u001b[36;1m\u001b[1;3m49.0\u001b[0m\u001b[32;1m\u001b[1;3mQuestion: What is the square of 7?\n",
|
284 |
+
"Thought: I can use the Calculator tool to find the square of a number.\n",
|
285 |
+
"Action: Calculator\n",
|
286 |
+
"Action Input: 7\u001b[0m\u001b[36;1m\u001b[1;3m49.0\u001b[0m\u001b[32;1m\u001b[1;3mQuestion: What is the square of 7?\n",
|
287 |
+
"Thought: I can use the Calculator tool to find the square of a number.\n",
|
288 |
+
"Action: Calculator\n",
|
289 |
+
"Action Input: 7\u001b[0m\u001b[36;1m\u001b[1;3m49.0\u001b[0m\u001b[32;1m\u001b[1;3m\u001b[0m\n",
|
290 |
+
"\n",
|
291 |
+
"\u001b[1m> Finished chain.\u001b[0m\n",
|
292 |
+
"\n",
|
293 |
+
"Final Result: Agent stopped due to iteration limit or time limit.\n"
|
294 |
+
]
|
295 |
+
}
|
296 |
+
],
|
297 |
+
"source": [
|
298 |
+
"result = agent_executor.invoke({\"input\": \"What is the square of 7?\"}, handle_parsing_errors=True)\n",
|
299 |
+
"print(\"\\nFinal Result:\", result[\"output\"])"
|
300 |
+
]
|
301 |
+
},
|
302 |
+
{
|
303 |
+
"cell_type": "markdown",
|
304 |
+
"id": "4eeddfdd-da26-421b-a8e0-425f6c6854e2",
|
305 |
+
"metadata": {},
|
306 |
+
"source": [
|
307 |
+
"<h3 align=center> Results </h3>\n",
|
308 |
+
"\n",
|
309 |
+
"We managed to get tool use working with the model `llama3-groq-70b-8192-tool-use-preview`. This is a much bigger model than ` llama3-groq-8b-8192-tool-use-preview` which simply outputted that it did not have the capabilities to perform such a task (causing the program to error out).\n",
|
310 |
+
"The choice of the model clearly continues to be a primary determinant in whether function calling works or not. In a sense, this creates a single point of failure for such systems."
|
311 |
+
]
|
312 |
+
},
|
313 |
+
{
|
314 |
+
"cell_type": "code",
|
315 |
+
"execution_count": null,
|
316 |
+
"id": "fabf8023-0ec4-4861-ad92-627867232108",
|
317 |
+
"metadata": {},
|
318 |
+
"outputs": [],
|
319 |
+
"source": []
|
320 |
+
}
|
321 |
+
],
|
322 |
+
"metadata": {
|
323 |
+
"kernelspec": {
|
324 |
+
"display_name": "groq",
|
325 |
+
"language": "python",
|
326 |
+
"name": "groq"
|
327 |
+
},
|
328 |
+
"language_info": {
|
329 |
+
"codemirror_mode": {
|
330 |
+
"name": "ipython",
|
331 |
+
"version": 3
|
332 |
+
},
|
333 |
+
"file_extension": ".py",
|
334 |
+
"mimetype": "text/x-python",
|
335 |
+
"name": "python",
|
336 |
+
"nbconvert_exporter": "python",
|
337 |
+
"pygments_lexer": "ipython3",
|
338 |
+
"version": "3.10.12"
|
339 |
+
}
|
340 |
+
},
|
341 |
+
"nbformat": 4,
|
342 |
+
"nbformat_minor": 5
|
343 |
+
}
|
examples/coding/tmp_code_65e3424c19629fcfd124f7ea31ec17d8.py
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
def is_prime(n):
|
2 |
+
if n <= 1:
|
3 |
+
return False
|
4 |
+
if n == 2:
|
5 |
+
return True
|
6 |
+
if n % 2 == 0:
|
7 |
+
return False
|
8 |
+
max_divisor = int(n**0.5) + 1
|
9 |
+
for d in range(3, max_divisor, 2):
|
10 |
+
if n % d == 0:
|
11 |
+
return False
|
12 |
+
return True
|
13 |
+
|
14 |
+
count = 0
|
15 |
+
for num in range(1, 10001):
|
16 |
+
if is_prime(num):
|
17 |
+
count += 1
|
18 |
+
|
19 |
+
print("Number of prime numbers from 1 to 10000:", count)
|
examples/data/sample.png
ADDED
examples/getting_started.py
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from groq import Groq
|
3 |
+
|
4 |
+
client = Groq(
|
5 |
+
api_key=os.environ.get("GROQ_API_KEY"),
|
6 |
+
)
|
7 |
+
|
8 |
+
chat_completion = client.chat.completions.create(
|
9 |
+
messages=[
|
10 |
+
{
|
11 |
+
"role": "user",
|
12 |
+
"content": "Explain the importance of fast language models",
|
13 |
+
}
|
14 |
+
],
|
15 |
+
model="llama3-8b-8192",
|
16 |
+
)
|
17 |
+
|
18 |
+
print(chat_completion.choices[0].message.content)
|
examples/vison.ipynb
ADDED
@@ -0,0 +1,152 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "markdown",
|
5 |
+
"id": "865525ac-d33e-4f44-a670-451980d5e828",
|
6 |
+
"metadata": {},
|
7 |
+
"source": [
|
8 |
+
"<h1 align=center> Vison Agents </h1>\n",
|
9 |
+
"\n",
|
10 |
+
"The goal of this notebook is to use a visual model to create agentic systems that work to produce visual content that they can (hopefully) iteratively refine."
|
11 |
+
]
|
12 |
+
},
|
13 |
+
{
|
14 |
+
"cell_type": "markdown",
|
15 |
+
"id": "ad115e5e-e74b-4837-b07f-cbeb30257037",
|
16 |
+
"metadata": {},
|
17 |
+
"source": [
|
18 |
+
"[<h2 align=center> Groq + Llama Vision </h2>](https://console.groq.com/docs/vision)"
|
19 |
+
]
|
20 |
+
},
|
21 |
+
{
|
22 |
+
"cell_type": "code",
|
23 |
+
"execution_count": 1,
|
24 |
+
"id": "4df0ad72-04a4-4b1f-bcaf-b1fb1cf0221f",
|
25 |
+
"metadata": {},
|
26 |
+
"outputs": [
|
27 |
+
{
|
28 |
+
"name": "stdout",
|
29 |
+
"output_type": "stream",
|
30 |
+
"text": [
|
31 |
+
"ChatCompletionMessage(content=\"The image depicts a computer chip, which is a small, rectangular piece of technology that contains a large number of microscopic components. It is typically made of silicon and is used to control and process information in a computer system. The image shows the back of the chip, which has a grid-like pattern of tiny lines and rectangles. These lines and rectangles are the individual components that make up the chip's circuitry.\", role='assistant', function_call=None, tool_calls=None)\n"
|
32 |
+
]
|
33 |
+
}
|
34 |
+
],
|
35 |
+
"source": [
|
36 |
+
"from groq import Groq\n",
|
37 |
+
"\n",
|
38 |
+
"client = Groq()\n",
|
39 |
+
"completion = client.chat.completions.create(\n",
|
40 |
+
" model=\"llama-3.2-11b-vision-preview\",\n",
|
41 |
+
" messages=[\n",
|
42 |
+
" {\n",
|
43 |
+
" \"role\": \"user\",\n",
|
44 |
+
" \"content\": [\n",
|
45 |
+
" {\n",
|
46 |
+
" \"type\": \"text\",\n",
|
47 |
+
" \"text\": \"What's in this image?\"\n",
|
48 |
+
" },\n",
|
49 |
+
" {\n",
|
50 |
+
" \"type\": \"image_url\",\n",
|
51 |
+
" \"image_url\": {\n",
|
52 |
+
" \"url\": \"https://upload.wikimedia.org/wikipedia/commons/f/f2/LPU-v1-die.jpg\"\n",
|
53 |
+
" }\n",
|
54 |
+
" }\n",
|
55 |
+
" ]\n",
|
56 |
+
" }\n",
|
57 |
+
" ],\n",
|
58 |
+
" temperature=1,\n",
|
59 |
+
" max_tokens=1024,\n",
|
60 |
+
" top_p=1,\n",
|
61 |
+
" stream=False,\n",
|
62 |
+
" stop=None,\n",
|
63 |
+
")\n",
|
64 |
+
"\n",
|
65 |
+
"print(completion.choices[0].message)"
|
66 |
+
]
|
67 |
+
},
|
68 |
+
{
|
69 |
+
"cell_type": "code",
|
70 |
+
"execution_count": 4,
|
71 |
+
"id": "4c8b06f0-a689-4d5e-9027-daa2972d607f",
|
72 |
+
"metadata": {},
|
73 |
+
"outputs": [
|
74 |
+
{
|
75 |
+
"name": "stdout",
|
76 |
+
"output_type": "stream",
|
77 |
+
"text": [
|
78 |
+
"This image shows a screenshot of YouTube video players and various programming screenshots and programming text that reads (translated) 'The Simplest Tech Stack' at the top, saying things like 'Go let's go ahead and do that', 'Under the templates directory' and code, which suggests a tutorial on code. For example, the yellow marker picks out parts of the image related to going ahead and doing something.\n",
|
79 |
+
"\n",
|
80 |
+
"The video players show mostly black screens with various videos' comments in white text. These include HTML/XML code under the title in the same screenshot as the title is shown at the top-left of the screenshot. White icons arranged in vertical rows are in the right and left margins, and the bars of some videos are visible at the bottom of the screenshot. This appears to be a screenshot from a tutorial that helps viewers build their own website.\n"
|
81 |
+
]
|
82 |
+
}
|
83 |
+
],
|
84 |
+
"source": [
|
85 |
+
"from groq import Groq\n",
|
86 |
+
"import base64\n",
|
87 |
+
"\n",
|
88 |
+
"# Function to encode the image\n",
|
89 |
+
"def encode_image(image_path):\n",
|
90 |
+
" with open(image_path, \"rb\") as image_file:\n",
|
91 |
+
" return base64.b64encode(image_file.read()).decode('utf-8')\n",
|
92 |
+
"\n",
|
93 |
+
"# Path to your image\n",
|
94 |
+
"image_path = \"examples/data/sample.png\"\n",
|
95 |
+
"\n",
|
96 |
+
"# Getting the base64 string\n",
|
97 |
+
"base64_image = encode_image(image_path)\n",
|
98 |
+
"\n",
|
99 |
+
"client = Groq()\n",
|
100 |
+
"\n",
|
101 |
+
"chat_completion = client.chat.completions.create(\n",
|
102 |
+
" messages=[\n",
|
103 |
+
" {\n",
|
104 |
+
" \"role\": \"user\",\n",
|
105 |
+
" \"content\": [\n",
|
106 |
+
" {\"type\": \"text\", \"text\": \"What's in this image?\"},\n",
|
107 |
+
" {\n",
|
108 |
+
" \"type\": \"image_url\",\n",
|
109 |
+
" \"image_url\": {\n",
|
110 |
+
" \"url\": f\"data:image/jpeg;base64,{base64_image}\",\n",
|
111 |
+
" },\n",
|
112 |
+
" },\n",
|
113 |
+
" ],\n",
|
114 |
+
" }\n",
|
115 |
+
" ],\n",
|
116 |
+
" model=\"llama-3.2-11b-vision-preview\",\n",
|
117 |
+
")\n",
|
118 |
+
"\n",
|
119 |
+
"print(chat_completion.choices[0].message.content)"
|
120 |
+
]
|
121 |
+
},
|
122 |
+
{
|
123 |
+
"cell_type": "code",
|
124 |
+
"execution_count": null,
|
125 |
+
"id": "7bd1b552-805d-40d9-9341-1c530f939a22",
|
126 |
+
"metadata": {},
|
127 |
+
"outputs": [],
|
128 |
+
"source": []
|
129 |
+
}
|
130 |
+
],
|
131 |
+
"metadata": {
|
132 |
+
"kernelspec": {
|
133 |
+
"display_name": "groq",
|
134 |
+
"language": "python",
|
135 |
+
"name": "groq"
|
136 |
+
},
|
137 |
+
"language_info": {
|
138 |
+
"codemirror_mode": {
|
139 |
+
"name": "ipython",
|
140 |
+
"version": 3
|
141 |
+
},
|
142 |
+
"file_extension": ".py",
|
143 |
+
"mimetype": "text/x-python",
|
144 |
+
"name": "python",
|
145 |
+
"nbconvert_exporter": "python",
|
146 |
+
"pygments_lexer": "ipython3",
|
147 |
+
"version": "3.10.12"
|
148 |
+
}
|
149 |
+
},
|
150 |
+
"nbformat": 4,
|
151 |
+
"nbformat_minor": 5
|
152 |
+
}
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
groq==0.11.0
|
2 |
+
langchain==0.3.7
|
3 |
+
langchain-core==0.3.15
|
4 |
+
langchain-groq==0.2.1
|
5 |
+
langchain-text-splitters==0.3.2
|
6 |
+
autogen==0.3.1
|
7 |
+
streamlit==1.40.0
|
st_image_chat.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from groq import Groq
|
3 |
+
import os
|
4 |
+
|
5 |
+
# Initialize Groq client
|
6 |
+
client = Groq(
|
7 |
+
api_key=os.environ.get("GROQ_API_KEY"),
|
8 |
+
)
|
9 |
+
|
10 |
+
# Initialize session state for chat history if it doesn't exist
|
11 |
+
if 'messages' not in st.session_state:
|
12 |
+
st.session_state.messages = [
|
13 |
+
{"role": "system", "content": "You are a helpful assistant"}
|
14 |
+
]
|
15 |
+
|
16 |
+
def generate_response(messages):
|
17 |
+
stream = client.chat.completions.create(
|
18 |
+
model="llama-3.2-3b-preview", #128K model
|
19 |
+
messages=messages,
|
20 |
+
temperature=0.1,
|
21 |
+
top_p=1,
|
22 |
+
stream=True,
|
23 |
+
stop=None,
|
24 |
+
)
|
25 |
+
|
26 |
+
for chunk in stream:
|
27 |
+
content = chunk.choices[0].delta.content
|
28 |
+
if content:
|
29 |
+
yield content # Yield content for streaming
|
30 |
+
|
31 |
+
st.title("Groq API Response Streaming")
|
32 |
+
|
33 |
+
# Display chat history
|
34 |
+
for message in st.session_state.messages:
|
35 |
+
if message["role"] != "system":
|
36 |
+
with st.chat_message(message["role"]):
|
37 |
+
st.markdown(message["content"])
|
38 |
+
|
39 |
+
# Get user input
|
40 |
+
user_input = st.chat_input('Message to Assistant...', key='prompt_input')
|
41 |
+
|
42 |
+
if user_input:
|
43 |
+
# Add user message to chat history
|
44 |
+
st.session_state.messages.append({"role": "user", "content": user_input})
|
45 |
+
|
46 |
+
# Display user message
|
47 |
+
with st.chat_message("user"):
|
48 |
+
st.markdown(user_input)
|
49 |
+
|
50 |
+
# Generate and display assistant response
|
51 |
+
with st.chat_message("assistant"):
|
52 |
+
response_placeholder = st.empty()
|
53 |
+
full_response = ""
|
54 |
+
|
55 |
+
# Stream the response
|
56 |
+
with st.spinner("Generating response..."):
|
57 |
+
for content in generate_response(st.session_state.messages):
|
58 |
+
full_response += content
|
59 |
+
response_placeholder.markdown(full_response + "▌")
|
60 |
+
response_placeholder.markdown(full_response)
|
61 |
+
|
62 |
+
# Add assistant response to chat history
|
63 |
+
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
st_long_context_chat.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# CREDITS
|
2 |
+
# https://gist.github.com/truevis/f31706b8af60e8c73d62b281bddb988f
|
3 |
+
|
4 |
+
import streamlit as st
|
5 |
+
from groq import Groq
|
6 |
+
|
7 |
+
import os
|
8 |
+
client = Groq(
|
9 |
+
api_key=os.environ.get("GROQ_API_KEY"),
|
10 |
+
)
|
11 |
+
|
12 |
+
def generate_response(user_input):
|
13 |
+
stream = client.chat.completions.create(
|
14 |
+
model="llama-3.2-3b-preview", #128K model
|
15 |
+
messages=[
|
16 |
+
{"role": "system", "content": "You are a helpful assistant"},
|
17 |
+
{"role": "user", "content": user_input},
|
18 |
+
],
|
19 |
+
temperature=0.1,
|
20 |
+
# max_tokens=128000,
|
21 |
+
top_p=1,
|
22 |
+
stream=True,
|
23 |
+
stop=None,
|
24 |
+
)
|
25 |
+
|
26 |
+
for chunk in stream:
|
27 |
+
content = chunk.choices[0].delta.content
|
28 |
+
if content:
|
29 |
+
yield content # Yield content for streaming
|
30 |
+
|
31 |
+
st.title("Groq API Response Streaming")
|
32 |
+
user_input = st.chat_input('Message to Assistant...', key='prompt_input')
|
33 |
+
if user_input: # Get user input
|
34 |
+
with st.spinner("Generating response..."):
|
35 |
+
st.write_stream(generate_response(user_input)) # Use st.write_stream to display streamed content
|
36 |
+
st.markdown("Message: " + user_input)
|
37 |
+
st.markdown("---") # Add a newline after the
|