{ "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": [ "" ] }, "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": [ "" ] }, "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": [ "" ] }, "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": [ "" ] }, "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": [ "" ] }, "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": [ "" ] }, "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": [ "" ] }, "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 }