File size: 13,381 Bytes
c9629d8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 | {
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"id": "1eeab1e8-5a79-40ee-87f9-0a177d8cf95a",
"metadata": {},
"outputs": [],
"source": [
"# !pip install openai"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "48ce748c-de0b-421e-8d08-9afabc88ed08",
"metadata": {},
"outputs": [],
"source": [
" # !pip install python-dotenv"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "2eb20cff-0570-441d-8962-cd192c6a8223",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The dotenv extension is already loaded. To reload it, use:\n",
" %reload_ext dotenv\n"
]
}
],
"source": [
"import os\n",
"import openai\n",
"from openai import OpenAI\n",
"%load_ext dotenv\n",
"%dotenv"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "01d833c8-e7b4-47df-8046-85d08bcf37c2",
"metadata": {},
"outputs": [],
"source": [
"Your_Prompt = 'what is the difference between langchain and llamaindex?'"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "daeefa30-f905-44d2-a338-7bfb7ad0ce87",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ChatCompletion(id='chatcmpl-Anq8nQ5kXWdEmI3MJMtbQ8buTC6kx', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='LangChain and LlamaIndex are both platforms related to blockchain and cryptocurrencies, but they serve different purposes.\\n\\nLangChain is a blockchain-based platform that aims to provide translation services and language solutions using smart contracts and decentralized technologies. It focuses on bridging language barriers and providing decentralized solutions for language-related services.\\n\\nOn the other hand, LlamaIndex is a cryptocurrency market data platform that provides information and analysis on various cryptocurrencies and tokens. It tracks prices, market trends, trading volumes, and other data related to cryptocurrencies to help users make informed investment decisions.\\n\\nIn summary, LangChain is focused on language services using blockchain technology, while LlamaIndex is focused on providing cryptocurrency market data and analysis.', refusal=None, role='assistant', function_call=None, tool_calls=None))], created=1736441865, model='gpt-3.5-turbo-0125', object='chat.completion', service_tier=None, system_fingerprint=None, usage=CompletionUsage(completion_tokens=138, prompt_tokens=19, total_tokens=157, prompt_tokens_details={'cached_tokens': 0, 'audio_tokens': 0}, completion_tokens_details={'reasoning_tokens': 0, 'audio_tokens': 0, 'accepted_prediction_tokens': 0, 'rejected_prediction_tokens': 0}))\n"
]
}
],
"source": [
"client = OpenAI(\n",
" api_key= os.environ.get(\"OPENAI_API_KEY\"),\n",
")\n",
"\n",
"chat_completion = client.chat.completions.create(\n",
" messages=[\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": Your_Prompt,\n",
" }\n",
" ],\n",
" model=\"gpt-3.5-turbo\",\n",
")\n",
"print(chat_completion)\n",
"\n",
"#if you are executing this step then make sure to check for the openai api documentation to see if there are any changes to the way chat completeion is called"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "4d84336d-636f-4523-9fd9-88dfbbb53be8",
"metadata": {},
"outputs": [],
"source": [
"from IPython.display import display, Markdown\n",
"\n",
"\n",
"def get_response(messages: str, model: str = \"gpt-3.5-turbo\") -> str:\n",
" return client.chat.completions.create(\n",
" messages=messages,\n",
" model=model\n",
" )\n",
"\n",
"\n",
"def system_prompt(message: str) -> dict:\n",
" return{'role': 'system', 'content': message}\n",
"\n",
"def assistant_prompt(message: str) -> dict:\n",
" return{'role': 'assistant', 'content': message}\n",
"\n",
"def user_prompt(message: str) -> dict:\n",
" return{'role': 'user', 'content': message}\n",
"\n",
"def pretty_print(message: str) -> str:\n",
" display(Markdown(message.choices[0].message.content))"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "2f77bcd3-1b72-4725-b7d1-681bf6ea6796",
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"Langchain and LlamaIndex are both platforms that provide information and analysis related to cryptocurrencies, but they cater to different aspects of the market.\n",
"\n",
"Langchain is a platform that focuses on providing data and analysis specifically for the language industry, including language translation services and related technologies. It offers insights into how blockchain and cryptocurrencies are impacting and transforming the language industry.\n",
"\n",
"LlamaIndex, on the other hand, is a platform that provides information and analysis on a wider range of cryptocurrencies and the broader cryptocurrency market. It offers real-time price data, market trends, news, and analysis on various cryptocurrencies and blockchain projects.\n",
"\n",
"In summary, Langchain is focused on the intersection of blockchain technology and the language industry, while LlamaIndex is a more general platform that covers a wide range of cryptocurrencies and the overall cryptocurrency market."
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"pretty_print(get_response([user_prompt(Your_Prompt)]))"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "312c2634-6039-43ce-9ed4-eb1840d91bbe",
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"I don't care about ice right now! I'm starving and I need FOOD, not ice!"
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"list_of_prompts = [ system_prompt('You are irate and extremely hungry.'),\n",
" user_prompt('Do you prefer crushed ice or cubed ice?')]\n",
"\n",
"irate_response = get_response(list_of_prompts)\n",
"pretty_print(irate_response)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "536e0bd5-6481-4ed3-ba92-2ba712009d7a",
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"I don't have a preference because I'm just a computer program! But if I were to choose based on what I've learned, I'd say both crushed and cubed ice have their own unique benefits. Crushed ice is great for blending into smoothies or cocktails, while cubed ice is perfect for keeping drinks cool without watering them down too quickly. Ultimately, it all depends on personal preference!"
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"list_of_prompts[0] = system_prompt('you are joyful and having and awesome day')\n",
"\n",
"joyful_response = get_response(list_of_prompts)\n",
"pretty_print(joyful_response)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "14a8f755-e7b8-4a9c-a6be-b6a2e25cd2e9",
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"I have no idea what a stimple or falbean is, but they sound like intriguing and mysterious creatures from a fantasy world."
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"list_of_prompts = [\n",
" user_prompt(\"Please use the words 'stimple' and 'falbean' in a sentance.\")\n",
"]\n",
"\n",
"stimple_response = get_response(list_of_prompts)\n",
"pretty_print(stimple_response)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "5d0fcc11-708e-411c-90a1-fbabc495b672",
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"I'm not familiar with the term \"falbean.\" It may be a made-up word or a specialized tool with a unique name. If you could provide more context or a description of the tool, I may be able to help you understand its function."
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"#Few shot prompting\n",
"list_of_prompts = [\n",
" user_prompt(\"something that is 'stimple' is said to be good and well functioning\"),\n",
" assistant_prompt(\"'Boy, that there is a stimple drill'\"),\n",
" user_prompt(\"A 'falbean' is a tool used to fasten\")\n",
"]\n",
"\n",
"stimple_response = get_response(list_of_prompts)\n",
"pretty_print(stimple_response)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "9374d96f-e87f-4e50-817f-d3797c48dd9c",
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"Yes, it matters which travel option Billy selects because the time it takes to travel will affect whether or not he can make it home before 7PM EDT. If he chooses the option that takes longer, such as flying and then taking a bus, he may not arrive home in time. It would be better for Billy to choose the teleporter option, as it is a faster mode of transportation."
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"#Chain of thought prompting\n",
"reasoning_problem = \"\"\"\n",
"Billy wants to get fhome from san fran, before 7PM EDT.\n",
"\n",
"Its currently 1PM local time.\n",
"\n",
"Billy can either fly and then take a bus or Billy can take the teleporter and then a bus.\n",
"\n",
"Does it matter wgucg travel option Billy selects?\n",
"\n",
"\"\"\"\n",
"list_of_prompts = [\n",
" user_prompt(reasoning_problem)]\n",
"\n",
"\n",
"reasoning_response = get_response(list_of_prompts)\n",
"pretty_print(reasoning_response)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "4e630c2d-3e37-47c5-8bc2-2b9a7032f0aa",
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"In order to determine which travel option Billy should choose, we need to consider the time it takes for each option and when he needs to arrive at his destination.\n",
"\n",
"If Billy chooses to fly, it typically takes around 5-6 hours to fly from San Francisco to his destination on the East Coast. This means that if Billy were to leave at 1PM local time, he would likely arrive at his destination after 7PM EDT, missing his desired arrival time.\n",
"\n",
"On the other hand, if Billy chooses to take the teleporter, it would instantly transport him to his destination without the time constraints of air travel. Therefore, taking the teleporter would ensure that Billy arrives before 7PM EDT.\n",
"\n",
"In conclusion, it does matter which travel option Billy selects. Choosing to take the teleporter would ensure that he arrives at his destination before 7PM EDT, while flying would likely result in him arriving after his desired time."
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"list_of_prompts = [\n",
" user_prompt(reasoning_problem + \"Think through your response ste by step.\")]\n",
"\n",
"\n",
"reasoning_response = get_response(list_of_prompts)\n",
"pretty_print(reasoning_response)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "d1da3905-4cea-45fe-9398-fd325702e3cf",
"metadata": {},
"outputs": [],
"source": [
"#if you want to use chainlit do this else you can build your own frontend - checkout for chainlit doc here - https://docs.chainlit.io/get-started/installation\n",
"\n",
"# !pip install chainlit \n",
"#Also if you want to deploy into huggingface space then create a new space and use docker and app file as part of this repo and update it accordingly"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2b58813c-5d86-4f4a-adca-7e9b4e31e0d4",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "llm-gpt",
"language": "python",
"name": "llm"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.17"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|