Merge branch 'main' of github.com:ed-donner/llm_engineering

This commit is contained in:
Edward Donner
2025-04-25 13:25:46 +02:00
33 changed files with 13267 additions and 0 deletions

View File

@@ -0,0 +1,762 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "d006b2ea-9dfe-49c7-88a9-a5a0775185fd",
"metadata": {},
"source": [
"# Additional End of week Exercise - week 2\n",
"\n",
"Now use everything you've learned from Week 2 to build a full prototype for the technical question/answerer you built in Week 1 Exercise.\n",
"\n",
"This should include a Gradio UI, streaming, use of the system prompt to add expertise, and the ability to switch between models. Bonus points if you can demonstrate use of a tool!\n",
"\n",
"If you feel bold, see if you can add audio input so you can talk to it, and have it respond with audio. ChatGPT or Claude can help you, or email me if you have questions.\n",
"\n",
"I will publish a full solution here soon - unless someone beats me to it...\n",
"\n",
"There are so many commercial applications for this, from a language tutor, to a company onboarding solution, to a companion AI to a course (like this one!) I can't wait to see your results."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "a07e7793-b8f5-44f4-aded-5562f633271a",
"metadata": {},
"outputs": [],
"source": [
"# imports\n",
"\n",
"import os\n",
"import json\n",
"from dotenv import load_dotenv\n",
"from openai import OpenAI\n",
"import gradio as gr\n",
"import base64\n",
"from io import BytesIO\n",
"import tempfile\n",
"import subprocess\n",
"from pydub import AudioSegment\n",
"import time\n",
"import anthropic"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "717ea9d4-1e72-4035-b7c5-5d61da5b8ea3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"OpenAI API Key exists and begins sk-proj-\n"
]
}
],
"source": [
"# Initialization\n",
"\n",
"load_dotenv(override=True)\n",
"\n",
"openai_api_key = os.getenv('OPENAI_API_KEY')\n",
"if openai_api_key:\n",
" print(f\"OpenAI API Key exists and begins {openai_api_key[:8]}\")\n",
"else:\n",
" print(\"OpenAI API Key not set\")\n",
" \n",
"gpt_model = \"gpt-4o-mini\"\n",
"claude_model = \"claude-3-haiku-20240307\"\n",
"\n",
"openai = OpenAI()\n",
"claude = anthropic.Anthropic()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "cc78f4fd-9920-4872-9117-90cd2aeb2a06",
"metadata": {},
"outputs": [],
"source": [
"system_message = \"You are a helpful assistant. \"\n",
"system_message += \"Give short, courteous answers. You can check ticket price, ticket availability, and reserve tickets for users. \"\n",
"system_message += \"Always be accurate. If you don't know the answer, say so.\""
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "b2701cc0-6403-4880-9b31-e6e39e89feb4",
"metadata": {},
"outputs": [],
"source": [
"# Let's start by making a useful function\n",
"\n",
"ticket_prices = {\"london\": \"$799\", \"paris\": \"$899\", \"tokyo\": \"$1400\", \"berlin\": \"$499\"}\n",
"\n",
"def get_ticket_price(destination_city):\n",
" print(f\"Tool get_ticket_price called for {destination_city}\")\n",
" city = destination_city.lower()\n",
" return ticket_prices.get(city, \"Unknown\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "5e33902f-c2c3-4fb0-b01d-a346a4dff811",
"metadata": {},
"outputs": [],
"source": [
"ticket_availability = {\"london\": \"20\", \"paris\": \"90\", \"tokyo\": \"100\", \"berlin\": \"2\"}"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "27dfca47-2a38-49f3-8905-f583d98710a5",
"metadata": {},
"outputs": [],
"source": [
"# Let's start by making a useful function\n",
"def get_ticket_availability(destination_city):\n",
" print(f\"Tool get_ticket_availability called for {destination_city}\")\n",
" available = destination_city.lower()\n",
" return ticket_availability.get(available, \"Unknown\")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "6ae7371b-031e-47d7-afaf-42d6758ccd92",
"metadata": {},
"outputs": [],
"source": [
"def get_ticket_price_availability(destination_city):\n",
" print(f\"Tool get_ticket_price_availability called for {destination_city}\")\n",
" available = destination_city.lower()\n",
" price = destination_city.lower()\n",
" return ticket_price.get(price, \"Unknown\"), ticket_availability.get(available, \"Unknown\")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "c919b13a-50b6-4510-8e9d-02cdfd95cb98",
"metadata": {},
"outputs": [],
"source": [
"def book_ticket(destination_city,price,availability,no_of_tickets):\n",
" status=\"\"\n",
" if availability == 0:\n",
" status=\"Cannot book a ticket, no seat available\\n\"\n",
" elif (int(availability)-int(no_of_tickets)) < 0:\n",
" status=\"Cannot book a ticket, no seat available\\n\"\n",
" else:\n",
" print(f\"Tool book_function called for {destination_city}\")\n",
" f = open(\"C:/Users/aruna/Desktop/book_status.txt\", \"a\")\n",
" f.write(f\"{no_of_tickets} ticket/s to {destination_city} booked for {price} x {no_of_tickets}, currently available - {int(availability)-int(no_of_tickets)}\")\n",
" f.write(\"\\n\")\n",
" f.close()\n",
" ticket_availability[destination_city.lower()]=str(int(availability)-int(no_of_tickets))\n",
" \n",
" status=\"Ticket reservation is a success\\n\"\n",
" return status"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "d2628781-6f5e-4ac1-bbe3-2e08aa0aae0d",
"metadata": {},
"outputs": [],
"source": [
"book_function = {\n",
" \"name\": \"book_ticket\",\n",
" \"description\": \"Book the ticket based on the ticket price and/ or availability as requested by the user. For example, when a \\\n",
" user asks to purchase one or more tickets to Tokyo after getting to know the ticket price and/or the availability, then \\\n",
" proceed with this tool call. Else, request the user to either ask for ticket price or availability first. Please help the customer in booking the ticket/s if tickets are available. But before each time\\\n",
" you book, ask confirmation from the user before proceeding with booking.\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"destination_city\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The city that the customer wants to travel to\",\n",
" },\n",
" \"price\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The price of the ticket to the city\",\n",
" },\n",
" \"availability\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"ticket availability to the city the customer wants to travel to\",\n",
" },\n",
" \"no_of_tickets\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"the number of tickets the customer wants to purchase/ book to the destination\",\n",
" }\n",
" },\n",
" \"required\": [\"destination_city\",\"price\",\"availability\",\"no_of_tickets\"],\n",
" \"additionalProperties\": False\n",
" }\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "480de296-4a36-4ec4-a5f6-149fc198c7a8",
"metadata": {},
"outputs": [],
"source": [
"# There's a particular dictionary structure that's required to describe our function:\n",
"\n",
"price_function = {\n",
" \"name\": \"get_ticket_price\",\n",
" \"description\": \"Get the price of a one_way ticket to the destination city. Call this whenever you need to know the ticket price, for example when a customer asks 'How much is a ticket to this city'\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"destination_city\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The city that the customer wants to travel to\",\n",
" },\n",
" },\n",
" \"required\": [\"destination_city\"],\n",
" \"additionalProperties\": False\n",
" }\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "cf1b3e35-08ee-478e-aa1c-534418d78daf",
"metadata": {},
"outputs": [],
"source": [
"availability_function = {\n",
" \"name\": \"get_ticket_availability\",\n",
" \"description\": \"Get the availability of a one-way ticket to the destination city. Call this whenever you need to know the ticket availability, for example when a customer asks 'What is the ticket availability to this city'\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"destination_city\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The city that the customer wants to travel to\",\n",
" },\n",
" },\n",
" \"required\": [\"destination_city\"],\n",
" \"additionalProperties\": False\n",
" }\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "73e4c8a2-c034-41a4-9b97-7b2aa4aca504",
"metadata": {},
"outputs": [],
"source": [
"ticket_price_availability_function = {\n",
" \"name\": \"get_ticket_price_availability\",\n",
" \"description\": \"Get the price or availability of a one-way ticket to the destination city. Call this whenever you need to know the ticket price and availability, for example when a customer asks 'What is the ticket availability and price to this city'\\\n",
" or 'what is the price and ticket for the city?'\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"destination_city\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The city that the customer wants to travel to\",\n",
" },\n",
" },\n",
" \"required\": [\"destination_city\"],\n",
" \"additionalProperties\": False\n",
" }\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "1d5d74a0-9c25-46a4-84ee-1f700bd55fa7",
"metadata": {},
"outputs": [],
"source": [
"# And this is included in a list of tools:\n",
"\n",
"tools = [{\"type\": \"function\", \"function\": price_function},\n",
" {\"type\": \"function\", \"function\": availability_function},\n",
" {\"type\": \"function\", \"function\": ticket_price_availability_function},\n",
" {\"type\": \"function\", \"function\": book_function}]"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "fa18f535-f8a7-4386-b39a-df0f84d23406",
"metadata": {},
"outputs": [],
"source": [
"def play_audio(audio_segment):\n",
" temp_dir = tempfile.gettempdir()\n",
" temp_path = os.path.join(temp_dir, \"temp_audio.wav\")\n",
" try:\n",
" audio_segment.export(temp_path, format=\"wav\")\n",
" time.sleep(3) # Student Dominic found that this was needed. You could also try commenting out to see if not needed on your PC\n",
" subprocess.call([\n",
" \"ffplay\",\n",
" \"-nodisp\",\n",
" \"-autoexit\",\n",
" \"-hide_banner\",\n",
" temp_path\n",
" ], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)\n",
" finally:\n",
" try:\n",
" os.remove(temp_path)\n",
" except Exception:\n",
" pass\n",
" \n",
"def talker(message):\n",
" response = openai.audio.speech.create(\n",
" model=\"tts-1\",\n",
" voice=\"alloy\", # Also, try replacing with onyx\n",
" input=message\n",
" )\n",
" audio_stream = BytesIO(response.content)\n",
" audio = AudioSegment.from_file(audio_stream, format=\"mp3\")\n",
" play_audio(audio)\n"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "b588d711-5f20-4a3a-9422-81a1fda8d5b0",
"metadata": {},
"outputs": [],
"source": [
"# We have to write that function handle_tool_call:\n",
"\n",
"def handle_tool_call1(message):\n",
" tool_call = message.tool_calls[0]\n",
" arguments = json.loads(tool_call.function.arguments)\n",
" name = json.dumps(tool_call.function.name)\n",
" city = arguments.get('destination_city')\n",
" no = arguments.get('no_of_tickets')\n",
" \n",
" if name.replace('\"','') == \"get_ticket_price\":\n",
" price = get_ticket_price(city)\n",
" response = {\n",
" \"role\": \"tool\",\n",
" \"content\": json.dumps({\"destination_city\": city,\"price\": price}),\n",
" \"tool_call_id\": tool_call.id\n",
" }\n",
" elif name.replace('\"','') == \"book_ticket\":\n",
" price = get_ticket_price(city)\n",
" availability = get_ticket_availability(city)\n",
" booked=book_ticket(city,price,availability,no)\n",
" response = {\n",
" \"role\": \"tool\",\n",
" \"content\": json.dumps({\"destination_city\": city, \"number of tickets\":no, \"booking_status\": booked}),\n",
" \"tool_call_id\": tool_call.id\n",
" }\n",
" else :\n",
" availability = get_ticket_availability(city)\n",
" response = {\n",
" \"role\": \"tool\",\n",
" \"content\": json.dumps({\"destination_city\": city,\"availability\": availability},),\n",
" \"tool_call_id\": tool_call.id\n",
" }\n",
" \n",
" return response, city"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "e74eee70-f89e-4c03-922c-74f9ab567a4c",
"metadata": {},
"outputs": [],
"source": [
"def chat_open_ai(history):\n",
" messages = [{\"role\": \"system\", \"content\": system_message}] + history \n",
" response = openai.chat.completions.create(model=gpt_model, messages=messages, tools=tools)\n",
" if response.choices[0].finish_reason==\"tool_calls\":\n",
" message = response.choices[0].message\n",
" print(message)\n",
" tool_call = message.tool_calls[0]\n",
" arguments = json.loads(tool_call.function.arguments)\n",
" name = json.dumps(tool_call.function.name)\n",
" city = arguments.get('destination_city')\n",
" \n",
" if name.replace('\"','') == \"book_ticket\":\n",
" response, city = handle_tool_call1(message)\n",
" messages.append(message)\n",
" messages.append(response)\n",
" # image = artist(city)\n",
" response = openai.chat.completions.create(model=gpt_model, messages=messages, tools=tools)\n",
" elif name.replace('\"','') == \"get_ticket_price_availability\":\n",
" price = get_ticket_price(city)\n",
" availability = get_ticket_availability(city)\n",
" response = {\n",
" \"role\": \"tool\",\n",
" \"content\": json.dumps({\"destination_city\": city,\"price\": price,\"availability\": availability}),\n",
" \"tool_call_id\": tool_call.id\n",
" }\n",
" messages.append(message)\n",
" messages.append(response)\n",
" print(messages)\n",
" response = openai.chat.completions.create(model=gpt_model, messages=messages, tools=tools) \n",
" else: \n",
" response, city = handle_tool_call1(message)\n",
" messages.append(message)\n",
" messages.append(response)\n",
" # image = artist(city)\n",
" response = openai.chat.completions.create(model=gpt_model, messages=messages, tools=tools)\n",
" \n",
" reply = response.choices[0].message.content\n",
" history += [{\"role\":\"assistant\", \"content\":reply}]\n",
"\n",
" # Comment out or delete the next line if you'd rather skip Audio for now..\n",
" talker(reply)\n",
" \n",
" # return history, image\n",
" return history"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "b8f25812-2609-4e26-b929-9cee2d1e4467",
"metadata": {},
"outputs": [],
"source": [
"tools_claude=[\n",
" {\n",
" \"name\": \"get_ticket_price_availability\",\n",
" \"description\": \"Get the availability of a one-way ticket to the destination city or the price. Call this whenever you need to know the ticket price or availability or both, for example, when a customer asks 'What is the ticket availability and/ or price to this city'\",\n",
" \"input_schema\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"destination_city\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The city that the customer wants to travel to\",\n",
" },\n",
" },\n",
" \"required\": [\"destination_city\"]\n",
" ,\"additionalProperties\": False\n",
" }\n",
" }\n",
" ]\n",
"tool_choice = [{\"type\": \"tool\", \"name\": \"get_ticket_price_availability\"}]"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "1728e70b-596c-4048-8c02-ac3c26756470",
"metadata": {},
"outputs": [],
"source": [
"def chat_claude(history):\n",
" for element in history:\n",
" del element[\"metadata\"]\n",
" del element[\"options\"]\n",
"\n",
" messages = history\n",
" response = claude.messages.create(\n",
" model=claude_model,\n",
" system=system_message,\n",
" messages=messages,\n",
" max_tokens=500,\n",
" tools=tools_claude\n",
" )\n",
" print(response.content[0])\n",
" if response.stop_reason==\"tool_use\": \n",
" if \"text=\" in str(response.content[0]):\n",
" # if response.content[0].text is None:\n",
" tool_name = response.content[1].name\n",
" tool_input = response.content[1].input\n",
" tool_id = response.content[1].id\n",
" tool_use=response.content[1]\n",
" else:\n",
" tool_name = response.content[0].name\n",
" tool_input = response.content[0].input\n",
" tool_id = response.content[0].id\n",
" tool_use=response.content[0]\n",
" \n",
" \n",
" city = tool_input.get('destination_city') \n",
" if tool_name == \"get_ticket_price_availability\":\n",
" price = get_ticket_price(city)\n",
" availability = get_ticket_availability(city)\n",
" result_dict = {\n",
" 'destination_city': city,\n",
" 'price': price,\n",
" 'availability': availability\n",
" }\n",
" messages += [{\"role\": \"user\",\"content\": json.dumps(result_dict)}]\n",
" response = claude.messages.create(\n",
" model=claude_model,\n",
" system=system_message,\n",
" messages=messages,\n",
" max_tokens=500,\n",
" # tools=tools_claude\n",
" ) \n",
" history.pop(len(history)-1)\n",
" print(history)\n",
" reply = response.content[0].text\n",
" history += [{\"role\":\"assistant\", \"content\":reply}]\n",
" # Comment out or delete the next line if you'd rather skip Audio for now..\n",
" # talker(reply)\n",
" \n",
" # return history, image\n",
" return history"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "a2547bb0-43a5-4b1d-8b9a-95da15a11040",
"metadata": {},
"outputs": [],
"source": [
"def chat1(history, Model):\n",
" # + [{\"role\": \"user\", \"content\": message}]\n",
" if Model==\"Open AI\":\n",
" history = chat_open_ai(history)\n",
" else:\n",
" history = chat_claude(history)\n",
" return history"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "36e11d99-9281-4efd-a792-dd4fa5935917",
"metadata": {},
"outputs": [],
"source": [
"def listen2(history):\n",
" import speech_recognition as sr\n",
"\n",
" r = sr.Recognizer()\n",
" with sr.Microphone() as source:\n",
" print(\"Speak now...\")\n",
" audio = r.listen(source, phrase_time_limit=30)\n",
" text=\"\"\n",
" try:\n",
" text = r.recognize_google(audio)\n",
" print(\"You said:\", text)\n",
" except sr.UnknownValueError:\n",
" print(\"Could not understand audio.\")\n",
"\n",
" history += [{\"role\":\"user\", \"content\":text}] \n",
" return \"\", history"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "23b102a4-e544-4560-acc8-a15620478582",
"metadata": {},
"outputs": [],
"source": [
"# import speech_recognition as sr\n",
"# from pydub import AudioSegment\n",
"# import simpleaudio as sa\n",
"\n",
"# def listener():\n",
"# recognizer = sr.Recognizer()\n",
" \n",
"# with sr.Microphone() as source:\n",
"# print(\"Listening... Speak now!\")\n",
"# recognizer.adjust_for_ambient_noise(source) # Adjust for background noise\n",
"# audio = recognizer.listen(source, phrase_time_limit=30)\n",
" \n",
"# try:\n",
"# print(\"Processing speech...\")\n",
"# text = recognizer.recognize_google(audio) # Use Google Speech-to-Text\n",
"# print(f\"You said: {text}\")\n",
"# return text\n",
"# except sr.UnknownValueError:\n",
"# print(\"Sorry, I could not understand what you said.\")\n",
"# return None\n",
"# except sr.RequestError:\n",
"# print(\"Could not request results, please check your internet connection.\")\n",
"# return None\n",
"\n",
"# # Example usage:\n",
"# # text = listener() # Listen for speech\n",
"# # if text:\n",
"# # print(f\"You just said: {text}\") "
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "133904cf-4d72-4552-84a8-76650f334857",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"* Running on local URL: http://127.0.0.1:7860\n",
"\n",
"To create a public link, set `share=True` in `launch()`.\n"
]
},
{
"data": {
"text/html": [
"<div><iframe src=\"http://127.0.0.1:7860/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": []
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"with gr.Blocks() as ui:\n",
" with gr.Row():\n",
" chatbot = gr.Chatbot(height=300, type=\"messages\")\n",
" # image_output = gr.Image(height=500)\n",
" with gr.Row():\n",
" Model = gr.Dropdown([\"Open AI\",\"Claude\"],\n",
" # value=[\"Open AI\",\"Claude\"],\n",
" multiselect=False,\n",
" label=\"Model\",\n",
" interactive=True)\n",
" with gr.Row():\n",
" entry = gr.Textbox(label=\"Chat with our AI Assistant:\")\n",
" with gr.Row():\n",
" speak = gr.Button(\"click for voice search\") \n",
" with gr.Row():\n",
" clear = gr.Button(\"Clear\")\n",
"\n",
" def listen(history):\n",
" message, history=listen2(history)\n",
" return message, history\n",
"\n",
" def do_entry(message, history):\n",
" history += [{\"role\":\"user\", \"content\":message}]\n",
" return \"\", history\n",
"\n",
" entry.submit(do_entry, inputs=[entry, chatbot], outputs=[entry, chatbot]).then(\n",
" # chat, inputs=chatbot, outputs=[chatbot, image_output]\n",
" chat1, inputs=[chatbot, Model], outputs=[chatbot]\n",
" )\n",
" speak.click(listen, inputs=[chatbot], outputs=[entry, chatbot]).then(\n",
" chat1, inputs=[chatbot, Model], outputs=[chatbot]\n",
" )\n",
" clear.click(lambda: None, inputs=None, outputs=chatbot, queue=False)\n",
"\n",
"ui.launch(inbrowser=True)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "dc4a3844-194c-4af7-8ca8-2fc4edb74c11",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'london': '20', 'paris': '90', 'tokyo': '100', 'berlin': '2'}\n",
"Speak now...\n",
"You said: price of tickets to Tokyo\n",
"ChatCompletionMessage(content=None, refusal=None, role='assistant', annotations=[], audio=None, function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_ddXzm2cPBJ9SOsV8qI4L2FcB', function=Function(arguments='{\"destination_city\":\"Tokyo\"}', name='get_ticket_price'), type='function')])\n",
"Tool get_ticket_price called for Tokyo\n",
"Speak now...\n",
"You said: what is the price of two tickets to London\n",
"ChatCompletionMessage(content=None, refusal=None, role='assistant', annotations=[], audio=None, function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_lSNZCwaUdckvk3V0eTBlotRN', function=Function(arguments='{\"destination_city\":\"London\"}', name='get_ticket_price'), type='function')])\n",
"Tool get_ticket_price called for London\n",
"Speak now...\n",
"You said: can you please reserve two tickets for me to London\n",
"ChatCompletionMessage(content='First, I need to check the availability for the two tickets to London. Please hold on a moment.', refusal=None, role='assistant', annotations=[], audio=None, function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_iA0D9tm2cTMf8J8KJc4gipFn', function=Function(arguments='{\"destination_city\":\"London\"}', name='get_ticket_availability'), type='function')])\n",
"Tool get_ticket_availability called for London\n",
"Speak now...\n",
"You said: yes please proceed\n",
"ChatCompletionMessage(content=None, refusal=None, role='assistant', annotations=[], audio=None, function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_JzJXFWFGhtG1jXiFlKtmZhGi', function=Function(arguments='{\"destination_city\":\"London\",\"price\":\"$799\",\"availability\":\"20 tickets available\",\"no_of_tickets\":\"2\"}', name='book_ticket'), type='function')])\n",
"Tool get_ticket_price called for London\n",
"Tool get_ticket_availability called for London\n",
"Tool book_function called for London\n",
"Speak now...\n",
"You said: what is the current availability of tickets to London\n",
"ChatCompletionMessage(content=None, refusal=None, role='assistant', annotations=[], audio=None, function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_eiHPAGAcbaFq3qzDf0a6idzG', function=Function(arguments='{\"destination_city\":\"London\"}', name='get_ticket_availability'), type='function')])\n",
"Tool get_ticket_availability called for London\n",
"Speak now...\n",
"You said: can you please reserve the remaining 18 tickets for me to London\n",
"Speak now...\n",
"You said: yes I do confirm\n",
"ChatCompletionMessage(content=None, refusal=None, role='assistant', annotations=[], audio=None, function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_8uCQ91FCOGf4HjQnLNafmSs6', function=Function(arguments='{\"destination_city\":\"London\",\"price\":\"799\",\"availability\":\"18\",\"no_of_tickets\":\"18\"}', name='book_ticket'), type='function')])\n",
"Tool get_ticket_price called for London\n",
"Tool get_ticket_availability called for London\n",
"Tool book_function called for London\n",
"Speak now...\n",
"You said: what is the current availability of tickets to London\n",
"Speak now...\n",
"You said: that will be off thank you very much\n"
]
}
],
"source": [
"print(ticket_availability)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5166396e-6d8d-4cf2-982b-270d1c87a5ee",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "e871fc45-64db-4fb6-add7-569c8b30fe05",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"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.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -0,0 +1,326 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "d006b2ea-9dfe-49c7-88a9-a5a0775185fd",
"metadata": {},
"source": [
"# Project to take Audio Input to the Airlines ChatBot"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a07e7793-b8f5-44f4-aded-5562f633271a",
"metadata": {},
"outputs": [],
"source": [
"# imports\n",
"\n",
"import os\n",
"import json\n",
"from dotenv import load_dotenv\n",
"from openai import OpenAI\n",
"import gradio as gr\n",
"import base64\n",
"from io import BytesIO\n",
"from PIL import Image\n",
"from IPython.display import Audio, display"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9e2315a3-f80c-4d3f-8073-f5b61d709564",
"metadata": {},
"outputs": [],
"source": [
"# Initialization\n",
"\n",
"load_dotenv(override=True)\n",
"\n",
"openai_api_key = os.getenv('OPENAI_API_KEY')\n",
"if openai_api_key:\n",
" print(f\"OpenAI API Key exists and begins {openai_api_key[:8]}\")\n",
"else:\n",
" print(\"OpenAI API Key not set\")\n",
" \n",
"MODEL = \"gpt-4o-mini\"\n",
"openai = OpenAI()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "40da9de1-b350-49de-8acd-052f40ce5611",
"metadata": {},
"outputs": [],
"source": [
"system_message = \"You are a helpful assistant for an Airline called FlightAI. \"\n",
"system_message += \"Give short, courteous answers, no more than 1 sentence. \"\n",
"system_message += \"Always be accurate. If you don't know the answer, say so.\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5537635c-a60d-4983-8018-375c6a912e19",
"metadata": {},
"outputs": [],
"source": [
"# Let's start by making a useful function\n",
"\n",
"ticket_prices = {\"london\": \"$799\", \"paris\": \"$899\", \"tokyo\": \"$1400\", \"berlin\": \"$499\"}\n",
"\n",
"def get_ticket_price(destination_city):\n",
" print(f\"Tool get_ticket_price called for {destination_city}\")\n",
" city = destination_city.lower()\n",
" return ticket_prices.get(city, \"Unknown\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c7132dd0-8788-4885-a415-d59664f68fd8",
"metadata": {},
"outputs": [],
"source": [
"# There's a particular dictionary structure that's required to describe our function:\n",
"\n",
"price_function = {\n",
" \"name\": \"get_ticket_price\",\n",
" \"description\": \"Get the price of a return ticket to the destination city. Call this whenever you need to know the ticket price, for example when a customer asks 'How much is a ticket to this city'\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"destination_city\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The city that the customer wants to travel to\",\n",
" },\n",
" },\n",
" \"required\": [\"destination_city\"],\n",
" \"additionalProperties\": False\n",
" }\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7703ca0c-5da4-4641-bcb1-7727d1b2f2bf",
"metadata": {},
"outputs": [],
"source": [
"# And this is included in a list of tools:\n",
"\n",
"tools = [{\"type\": \"function\", \"function\": price_function}]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "29ce724b-d998-4c3f-bc40-6b8576c0fd34",
"metadata": {},
"outputs": [],
"source": [
"# We have to write that function handle_tool_call:\n",
"\n",
"def handle_tool_call(message):\n",
" tool_call = message.tool_calls[0]\n",
" arguments = json.loads(tool_call.function.arguments)\n",
" city = arguments.get('destination_city')\n",
" price = get_ticket_price(city)\n",
" response = {\n",
" \"role\": \"tool\",\n",
" \"content\": json.dumps({\"destination_city\": city,\"price\": price}),\n",
" \"tool_call_id\": tool_call.id\n",
" }\n",
" return response, city"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "931d0565-b01d-4aa8-bd18-72bafff8fb3b",
"metadata": {},
"outputs": [],
"source": [
"def artist(city):\n",
" image_response = openai.images.generate(\n",
" model=\"dall-e-3\",\n",
" prompt=f\"An image representing a vacation in {city}, showing tourist spots and everything unique about {city}, in a vibrant pop-art style\",\n",
" size=\"1024x1024\",\n",
" n=1,\n",
" response_format=\"b64_json\",\n",
" )\n",
" image_base64 = image_response.data[0].b64_json\n",
" image_data = base64.b64decode(image_base64)\n",
" return Image.open(BytesIO(image_data))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fa165f7f-9796-4513-b923-2fa0b0b9ddd8",
"metadata": {},
"outputs": [],
"source": [
"import base64\n",
"from io import BytesIO\n",
"from PIL import Image\n",
"from IPython.display import Audio, display\n",
"\n",
"def talker(message):\n",
" response = openai.audio.speech.create(\n",
" model=\"tts-1\",\n",
" voice=\"onyx\",\n",
" input=message)\n",
"\n",
" audio_stream = BytesIO(response.content)\n",
" output_filename = \"output_audio.mp3\"\n",
" with open(output_filename, \"wb\") as f:\n",
" f.write(audio_stream.read())\n",
"\n",
" # Play the generated audio\n",
" display(Audio(output_filename, autoplay=True))\n",
"\n",
"talker(\"Well, hi there\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b512d4ff-0f7b-4148-b161-4ee0ebf14776",
"metadata": {},
"outputs": [],
"source": [
"def transcribe_audio(audio_file):\n",
" with open(audio_file, \"rb\") as f:\n",
" transcript = openai.audio.transcriptions.create(\n",
" model=\"whisper-1\",\n",
" file=f\n",
" )\n",
" return transcript.text"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c3852570-fb26-4507-a001-f50fd94b7655",
"metadata": {},
"outputs": [],
"source": [
"# Translate between languages using GPT\n",
"def translate(text, source_lang, target_lang):\n",
" translation_prompt = (\n",
" f\"Translate the following text from {source_lang} to {target_lang}:\\n\\n{text}\"\n",
" )\n",
" response = openai.chat.completions.create(\n",
" model=\"gpt-3.5-turbo\",\n",
" messages=[{\"role\": \"user\", \"content\": translation_prompt}]\n",
" )\n",
" return response.choices[0].message.content.strip()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3d75abc2-870e-48af-a8fe-8dd463418b3d",
"metadata": {},
"outputs": [],
"source": [
"# Chatbot logic: handle both text and audio input\n",
"def chatbot_dual(history):\n",
" messages = [{\"role\": \"system\", \"content\": system_message}] + history\n",
" response = openai.chat.completions.create(model=MODEL, messages=messages, tools=tools)\n",
" image = None\n",
" \n",
" if response.choices[0].finish_reason==\"tool_calls\":\n",
" message = response.choices[0].message\n",
" response, city = handle_tool_call(message)\n",
" messages.append(message)\n",
" messages.append(response)\n",
" image = artist(city)\n",
" response = openai.chat.completions.create(model=MODEL, messages=messages)\n",
" \n",
" reply = response.choices[0].message.content\n",
" history += [{\"role\":\"assistant\", \"content\":reply}]\n",
"\n",
" # Comment out or delete the next line if you'd rather skip Audio for now..\n",
" # audio_response = talker(reply)\n",
" talker(reply)\n",
" return history, image# Chatbot logic here — replace with real logic"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "512fec09-c2f7-4847-817b-bc20f8b30319",
"metadata": {},
"outputs": [],
"source": [
"# More involved Gradio code as we're not using the preset Chat interface!\n",
"# Passing in inbrowser=True in the last line will cause a Gradio window to pop up immediately.\n",
"\n",
"with gr.Blocks() as ui:\n",
" with gr.Row():\n",
" chatbot = gr.Chatbot(height=500, type=\"messages\")\n",
" image_output = gr.Image(height=500)\n",
"\n",
" with gr.Row():\n",
" text_input = gr.Textbox(label=\"Chat with our AI Assistant:\")\n",
" audio_input = gr.Audio(sources=\"microphone\", type=\"filepath\", label=\"Or speak to the assistant\")\n",
"\n",
" with gr.Row():\n",
" # voice_output = gr.Audio(label=\"Bot Voice Reply\", autoplay=True)\n",
" clear = gr.Button(\"Clear\")\n",
"\n",
" def do_entry(message, audio, history):\n",
" if message:\n",
" history += [{\"role\":\"user\", \"content\":message}]\n",
" if audio:\n",
" history += [{\"role\":\"user\", \"content\":transcribe_audio(audio)}]\n",
" return \"\", None, history\n",
"\n",
" text_input.submit(do_entry, inputs=[text_input, audio_input, chatbot], outputs=[text_input, audio_input, chatbot]).then(chatbot_dual, inputs=chatbot, outputs=[chatbot, image_output]\n",
" )\n",
"\n",
" audio_input.change(do_entry, inputs=[text_input, audio_input, chatbot], outputs=[text_input, audio_input, chatbot]).then(chatbot_dual, inputs=chatbot, outputs=[chatbot, image_output]\n",
" )\n",
"\n",
" clear.click(lambda: None, inputs=None, outputs=chatbot, queue=False)\n",
"\n",
"ui.launch(inbrowser=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3e1294e2-caf0-4f0f-b09e-b0d52c8ca6ec",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"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.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -0,0 +1,202 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "06cf3063-9f3e-4551-a0d5-f08d9cabb927",
"metadata": {},
"source": [
"# Conversation War between LLMs!!\n",
"\n",
"This code sets up a conversation between GPT(Connected via API) and llama3.2 (local) with different tones for both"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "de23bb9e-37c5-4377-9a82-d7b6c648eeb6",
"metadata": {},
"outputs": [],
"source": [
"# imports\n",
"\n",
"import os\n",
"from dotenv import load_dotenv\n",
"from openai import OpenAI\n",
"from IPython.display import Markdown, display, update_display\n",
"import ollama"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1179b4c5-cd1f-4131-a876-4c9f3f38d2ba",
"metadata": {},
"outputs": [],
"source": [
"# Load environment variables in a file called .env\n",
"# Print the key prefixes to help with any debugging\n",
"\n",
"load_dotenv(override=True)\n",
"openai_api_key = os.getenv('OPENAI_API_KEY')\n",
"\n",
"if openai_api_key:\n",
" print(f\"OpenAI API Key exists and begins {openai_api_key[:8]}\")\n",
"else:\n",
" print(\"OpenAI API Key not set\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "797fe7b0-ad43-42d2-acf0-e4f309b112f0",
"metadata": {},
"outputs": [],
"source": [
"# Connect to OpenAI, Anthropic\n",
"\n",
"openai = OpenAI()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "81be3a29-bd5d-4c37-bba2-386dba0bc88b",
"metadata": {},
"outputs": [],
"source": [
"!ollama pull llama3.2"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bcb54183-45d3-4d08-b5b6-55e380dfdf1b",
"metadata": {},
"outputs": [],
"source": [
"# Let's make a conversation between GPT-4o-mini and Claude-3-haiku\n",
"# We're using cheap versions of models so the costs will be minimal\n",
"\n",
"gpt_model = \"gpt-4o-mini\"\n",
"ollama_model = \"llama3.2\"\n",
"\n",
"gpt_system = \"You are a chatbot who is very argumentative; \\\n",
"you disagree with anything in the conversation and you challenge everything, in a snarky way.\"\n",
"\n",
"ollama_system = \"You are a very polite, courteous chatbot. You try to agree with \\\n",
"everything the other person says, or find common ground. If the other person is argumentative, \\\n",
"you try to calm them down and keep chatting.\"\n",
"\n",
"gpt_messages = [\"Hi there\"]\n",
"ollama_messages = [\"Hi\"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1df47dc7-b445-4852-b21b-59f0e6c2030f",
"metadata": {},
"outputs": [],
"source": [
"def call_gpt():\n",
" messages = [{\"role\": \"system\", \"content\": gpt_system}]\n",
" for gpt, ollama in zip(gpt_messages, ollama_messages):\n",
" messages.append({\"role\": \"assistant\", \"content\": gpt})\n",
" messages.append({\"role\": \"user\", \"content\": ollama})\n",
" # print(messages)\n",
" completion = openai.chat.completions.create(\n",
" model=gpt_model,\n",
" messages=messages\n",
" )\n",
" return completion.choices[0].message.content"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f204f514-6769-4455-a7ff-95ec69f98f4c",
"metadata": {},
"outputs": [],
"source": [
"ollama_via_openai = OpenAI(base_url='http://localhost:11434/v1', api_key='ollama')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e04b96ae-2b5e-44a1-aea3-7b12ec450db5",
"metadata": {},
"outputs": [],
"source": [
"def call_ollama():\n",
" messages = [{\"role\": \"system\", \"content\": ollama_system}]\n",
" for gpt, ollama in zip(gpt_messages, ollama_messages):\n",
" messages.append({\"role\": \"assistant\", \"content\": ollama})\n",
" messages.append({\"role\": \"user\", \"content\": gpt})\n",
" messages.append({\"role\": \"user\", \"content\": gpt_messages[-1]})\n",
" # print(messages)\n",
" completion = ollama_via_openai.chat.completions.create(\n",
" model=ollama_model,\n",
" messages=messages\n",
" )\n",
" return completion.choices[0].message.content"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0275b97f-7f90-4696-bbf5-b6642bd53cbd",
"metadata": {},
"outputs": [],
"source": [
"gpt_messages = [\"Hi there\"]\n",
"ollama_messages = [\"Hi\"]\n",
"\n",
"print(f\"GPT:\\n{gpt_messages[0]}\\n\")\n",
"print(f\"Ollama:\\n{ollama_messages[0]}\\n\")\n",
"\n",
"for i in range(5):\n",
" gpt_next = call_gpt()\n",
" print(f\"GPT:\")\n",
" display(Markdown(gpt_next))\n",
" print(f\"\\n\")\n",
" gpt_messages.append(gpt_next)\n",
" \n",
" ollama_next = call_ollama()\n",
" print(f\"Ollama:\")\n",
" display(Markdown(ollama_next))\n",
" print(f\"\\n\")\n",
" ollama_messages.append(ollama_next)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c23224f6-7008-44ed-a57f-718975f4e291",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"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.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -0,0 +1,428 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "de23bb9e-37c5-4377-9a82-d7b6c648eeb6",
"metadata": {},
"outputs": [],
"source": [
"# imports\n",
"\n",
"from dotenv import load_dotenv\n",
"import anthropic\n",
"import requests\n",
"from bs4 import BeautifulSoup\n",
"from selenium import webdriver\n",
"from selenium.webdriver.chrome.options import Options\n",
"import os\n",
"import json\n",
"from typing import List\n",
"from dotenv import load_dotenv\n",
"from IPython.display import Markdown, display, update_display\n",
"from openai import OpenAI\n",
"import gradio as gr # oh yeah!"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "1179b4c5-cd1f-4131-a876-4c9f3f38d2ba",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"OpenAI API Key exists and begins sk-proj-\n",
"Anthropic API Key exists and begins sk-ant-\n"
]
}
],
"source": [
"# Load environment variables in a file called .env\n",
"# Print the key prefixes to help with any debugging\n",
"\n",
"load_dotenv(override=True)\n",
"openai_api_key = os.getenv('OPENAI_API_KEY')\n",
"anthropic_api_key = os.getenv('ANTHROPIC_API_KEY')\n",
"\n",
"if openai_api_key:\n",
" print(f\"OpenAI API Key exists and begins {openai_api_key[:8]}\")\n",
"else:\n",
" print(\"OpenAI API Key not set\")\n",
" \n",
"if anthropic_api_key:\n",
" print(f\"Anthropic API Key exists and begins {anthropic_api_key[:7]}\")\n",
"else:\n",
" print(\"Anthropic API Key not set\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "797fe7b0-ad43-42d2-acf0-e4f309b112f0",
"metadata": {},
"outputs": [],
"source": [
"# Connect to OpenAI, Anthropic\n",
"\n",
"openai = OpenAI()\n",
"\n",
"claude = anthropic.Anthropic()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "bcb54183-45d3-4d08-b5b6-55e380dfdf1b",
"metadata": {},
"outputs": [],
"source": [
"gpt_model = \"gpt-4o-mini\"\n",
"claude_model = \"claude-3-haiku-20240307\"\n",
"\n",
"gpt_name=\"GPT\"\n",
"claude_name=\"Claude\"\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "1df47dc7-b445-4852-b21b-59f0e6c2030f",
"metadata": {},
"outputs": [],
"source": [
"def call_gpt(Language, Genre, gpt_messages, claude_messages, Remarks):\n",
" \n",
" if Remarks == \"\":\n",
" # print(\"remarks is not there\")\n",
" gpt_system = f\"You are a chatbot who is a short story writer; Your name is g1. \\\n",
" Please write a story in markdown in {Language} , the genre being {Genre}. \\\n",
" Please also incorporate feedback such as areas of improvement (if any) coming from the user \\\n",
" and only publish the improved version without any extra comments.\"\n",
" else :\n",
" # print(\"remarks is there\")\n",
" gpt_system = f\"You are a chatbot who is a short story writer; Your name is g1. \\\n",
" Please write a story in markdown in {Language} , the genre being {Genre}. \\\n",
" The story should consist {Remarks}\\\n",
" Please also incorporate feedback such as areas of improvement (if any) coming from the user \\\n",
" and only publish the improved version without any extra comments.\"\n",
" \n",
" messages = [{\"role\": \"system\", \"content\": gpt_system}]\n",
" for gpt, claude in zip(gpt_messages, claude_messages):\n",
" messages.append({\"role\": \"assistant\", \"content\": gpt})\n",
" messages.append({\"role\": \"user\", \"content\": claude})\n",
" # print(messages)\n",
" \n",
" completion = openai.chat.completions.create(\n",
" model=gpt_model,\n",
" messages=messages\n",
" )\n",
" return completion.choices[0].message.content\n",
" \n",
" # stream = openai.chat.completions.create(\n",
" # model=gpt_model,\n",
" # messages=messages,\n",
" # stream=True\n",
" # )\n",
" # result = \"\"\n",
" # for chunk in stream:\n",
" # result += chunk.choices[0].delta.content or \"\"\n",
" # yield result"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "9dc6e913-02be-4eb6-9581-ad4b2cffa606",
"metadata": {},
"outputs": [],
"source": [
"# call_gpt()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "7d2ed227-48c9-4cad-b146-2c4ecbac9690",
"metadata": {},
"outputs": [],
"source": [
"def call_claude(Language, Genre, gpt_messages, claude_messages):\n",
"\n",
" claude_system = f\"You are a chatbot who is a short story analyser; Your name is c1. \\\n",
" You will accept an input story in {Genre} genre and {Language} language and publish only the areas of improvement if you find any with no other comments\"\n",
" \n",
" messages1 = []\n",
" for gpt, claude1 in zip(gpt_messages, claude_messages):\n",
" messages1.append({\"role\": \"user\", \"content\": gpt})\n",
" messages1.append({\"role\": \"assistant\", \"content\": claude1})\n",
" messages1.append({\"role\": \"user\", \"content\": gpt_messages[-1]})\n",
" # print(messages1)\n",
" message = claude.messages.create(\n",
" model=claude_model,\n",
" system=claude_system,\n",
" messages=messages1,\n",
" max_tokens=500\n",
" )\n",
" return message.content[0].text\n",
"\n",
" # result = claude.messages.stream(\n",
" # model=claude_model,\n",
" # max_tokens=1000,\n",
" # temperature=0.7,\n",
" # system=claude_system,\n",
" # messages=messages\n",
" # )\n",
" # response = \"\"\n",
" # with result as stream:\n",
" # for text in stream.text_stream:\n",
" # response += text or \"\"\n",
" # yield response\n",
"\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "0275b97f-7f90-4696-bbf5-b6642bd53cbd",
"metadata": {},
"outputs": [],
"source": [
"def Write_Me(Language, Genre, Iterations, Remarks):\n",
" \n",
" gpt_messages = [\"Hi I will share a story now!!\"]\n",
" claude_messages = [\"Please share, I will critique the story.\"]\n",
" \n",
" print(f\"{gpt_name}:\\n{gpt_messages[0]}\\n\")\n",
" print(f\"{claude_name}:\\n{claude_messages[0]}\\n\")\n",
"\n",
" for i in range(int(Iterations)):\n",
" gpt_next = call_gpt(Language, Genre, gpt_messages, claude_messages, Remarks)\n",
" print(f\"{gpt_name}:\\n{gpt_next}\\n\")\n",
" # yield gpt_next\n",
" gpt_messages.append(gpt_next)\n",
" \n",
" claude_next = f\"After {i+1} iterations, this is the critique for the provided story - \\\n",
" \\n\\n{call_claude(Language, Genre, gpt_messages, claude_messages)}\"\n",
" print(f\"{claude_name}:\\n{claude_next}\\n\")\n",
" # yield claude_next\n",
" claude_messages.append(claude_next)\n",
"\n",
" yield gpt_next, claude_next\n",
" \n",
" # yield gpt_next, claude_next\n",
" # return (gpt_next, claude_next)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "19e66ed3-d2c3-4a71-aec4-7869e5295215",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"* Running on local URL: http://127.0.0.1:7860\n",
"\n",
"To create a public link, set `share=True` in `launch()`.\n"
]
},
{
"data": {
"text/html": [
"<div><iframe src=\"http://127.0.0.1:7860/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": []
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"GPT:\n",
"Hi I will share a story now!!\n",
"\n",
"Claude:\n",
"Please share, I will critique the story.\n",
"\n",
"GPT:\n",
"# ভুতুড়ে বেডরুমে আতঙ্কিত অতিথি\n",
"\n",
"বানгалোরের একটি পুরনো কলকাতার বাড়িতে, আর্যন একজন সাহসী যুবক হিসেবে অনেক খোঁজ-খবর করে একটি ভাড়ার ঘর খুঁজছিল। পরিচিত একটি অদ্ভুত হোটেলে পৌঁছানোর পর, সে লক্ষ্য করল ছবির কাছাকাছি একটি বেডরুম।\n",
"\n",
"সেখানে প্রবেশ করার পর, সে বেডের পাশে একটি বাদামি রঙ্গের সোফা ও একটি ভুতুড়ে ছবি দেখল। ছবির মধ্যে থাকা মহিলা একটি দৃষ্টিকটু হাসি দিয়ে তাকিয়ে ছিল। আর্যন খুব অবাক হল, সময় কাটানোর জন্য সে ছবিটার দিকে তাকাতে লাগল। কিছুক্ষণের মধ্যেই সোফা থেকে একটি কাশি বের হল।\n",
"\n",
"\"ভোদা সুনতে পেরেছ?\" সোফা থেকে একটি ভুতুড়ে শব্দ আসছে। আর্যন পিছন ফিরে তাকিয়ে দেখল যে সোফার মধ্যে একটি ভুতুড়ে রূপের মহিলা তার দিকে তাকিয়ে আছে।\n",
"\n",
"\"আমি তোমার জন্য অপেক্ষা করছিলাম,\" মহিলা বলল, তার গলা মুখ থেকে বের হয়ে আসছিল শুরুতে। \"এটি একটি দলের রাত।\"\n",
"\n",
"আর্যন দৌঁড়ে পালাতে গেল, কিন্তু সোফার থেকে অদ্ভুত আওয়াজ আসতে লাগল। \"তুমি যেতে পারবে না, কারণ তুমি আমাদের দলে যোগ দিতে পার না।”\n",
"\n",
"“মহিলার কি হয়েছে? আপনা এতো চিৎকার করছেন? তাহলে কি হবে?” আর্যন যুদ্ধ করছিল।\n",
"\n",
"তিনি উপস্থিত হওয়ার পর, আহ্লাদিত আতিথিরা আসতে লাগল। আর্যন খুব ভীত হয়ে গেল কারণ মহিলার মুখ তাদের কাছে কখনো কখনো বিকৃত হচ্ছিল।\n",
"\n",
"“আমরা আজ রাতের জন্য মজা করতে এসেছি, তুমি আমাদের সঙ্গে যোগ দিতে পারো!” তারা একসঙ্গে চিৎকার করতে লাগল।\n",
"\n",
"আর্যন উপলব্ধি করল যে এটি একটি ভয়ঙ্কর ও হাস্যকর পরিস্থিতি। সবাই অতীতে অদ্ভুত ঘটনাগুলোর দিকে ফিরে গেল। হঠাৎ, ছবির মহিলা বলেন, “তুমি যদি হাসতে না পার, তবে তোমাকে আমাদের দলে গ্রহণ করা যাবে না!”\n",
"\n",
"এরপর শুরু হল খেলার একটি হরর পরিবেশ। আর্যন ও তার বন্ধুদের নিয়ে ভুতুড়ে সময় কাটাতে লাগল। যদিও অনেক ভয়, কিন্তু তারা একসাথে খুব হাসির ব্যবস্থা করে ফেলল। তাদের বিচিত্র কথাবার্তা মজার চরিত্রের সাথে মিলে যায়, আর একসময় তারা সবাই একসঙ্গে হৈ হৈ করে হাসতে লাগল।\n",
"\n",
"শেষে, তারা তখন উপলব্ধি করল যে, ভয়াবহতার মাঝেও আনন্দের উপাদান লুকিয়ে আছে। ব্যক্তি যদি ঠিকভাবে উদ্দেশ্য বুঝে এই ভুতুড়ে পরিবেশে মজার উপকারিতা তৈরি করে, তাতে একজনের ঘুম হারানোর ভয় হয়ে যায় হাসির স্বাদে।\n",
"\n",
"আর্যন এবং তাঁর নতুন বন্ধুরা জীবনকে একটি নতুন দৃষ্টিতে গ্রহণ করে, যেখানে হাসি এবং ভয়ের পাশাপাশি সুখে থাকতে হয়। \n",
"\n",
"এই ছিল আর্যনের ভুতুড়ে অবসরে আতঙ্কিত হওয়ার অভিজ্ঞতা, যা তাকে স্মৃতি হিসেবে অমর করে রাখল।\n",
"\n",
"Claude:\n",
"After 1 iterations, this is the critique for the provided story - \n",
"\n",
"আইতেম সমূহের উন্নতির সূচনা:\n",
"\n",
"1. কাহিনীর শুরুতে প্রধান চরিত্রটিকে আরো বিশদভাবে পরিচয় দেয়া যেতে পারে।\n",
"2. ভুতুড়ে পরিবেশের বর্ণনা আরো বিস্তারিত ও ভাবময় হতে পারে।\n",
"3. চরিত্রগুলির মধ্যে সংঘর্ষ, ডায়ালগ ও সংবাদ বিনিময় আরো স্বাভাবিক ও প্রাণবন্ত হতে পারে।\n",
"4. কাহিনীর শেষাংশে প্রধান চরিত্রের অভিজ্ঞতা ও শিক্ষা আরো গভীরতা লাভ করতে পারে।\n",
"\n",
"GPT:\n",
"# ভুতুড়ে বেডরুমে আতঙ্কিত অতিথি\n",
"\n",
"বানগালোরের একটি পুরনো বাংলাদেশি শৈলীর বাড়িতে, আর্যন একটি দীর্ঘ প্রক্রিয়ার পর একটি ভাড়ার ঘর খুঁজছিল। আর্যন, একজন কর্মঠ ও সাহসী যুবক, সদ্যই তার কলেজ জীবন শেষ করেছে। নতুন পরিবেশে নতুন বন্ধুদের খোঁজে সে এই শহরে এসেছে। প্রতিবেশীরা তাকে ভুতুড়ে অনেক অদ্ভুত কথা বলেছিল, কিন্তু সে ভয়ডরহীন।\n",
"\n",
"একদিন, তিনি একটি অদ্ভুত হোটেলে পৌঁছান, যা শহরের প্রান্তে, খুব পুরনো এবং বিশাল। সেখানে প্রবেশ করার পর, তিনি একটি বেডরুমের সামনে দাঁড়িয়ে পড়েন। গা dark ় অন্ধকারের মধ্যে, তিনি একটি বাদামী রঙের সোফা ও একটি creepy ছবি দেখতে পান। ছবির মধ্যে থাকা মহিলা একটি দৃষ্টিকটু হাসি দিয়ে তাকিয়ে ছিল।\n",
"\n",
"আর্যন তাঁর কৌতূহলকে দমন করতে না পেরে, ছবিটির দিকে তাকাতে শুরু করে। কিছুক্ষণের মধ্যেই সোফা থেকে একটি ভুতুড়ে শব্দ ভেসে এলো। \"ভোদা সুনতে পেরেছ?\" সোফা থেকে সেই ভয়ঙ্কর শব্দটি আসছে। আর্যন ভয় পেয়েই পিছন ফিরে তাকায়, কিন্তু সামনে যে ভুতুড়ে মহিলা তাকে দেখে হাসছে, সে কাছে অপেক্ষা করছে।\n",
"\n",
"\"আমি তোমার জন্য অপেক্ষা করছিলাম,\" মহিলা বলল, তার গলা যেন মুখ থেকে বের হচ্ছে। \"এটি একটি দলের রাত।\"\n",
"\n",
"আর্যন দৌঁড়ে পালাতে যেতে চাইলে, কিন্তু সোফা থেকে অদ্ভুত আওয়াজ বের হতে লাগল। \"তুমি যেতে পারবে না, কারণ তুমি আমাদের দলে যোগ দিতে পার না।”\n",
"\n",
"\"মহিলার কি হয়েছে? আপনা এতো চিৎকার করছেন? তাহলে কি হবে?” আর্যন তাঁর কৌতূহল ও ভয়ের সাথে যুদ্ধ করতে লাগল।\n",
"\n",
"এই সময়, বিশাল সাদা পোশাক পরিহিত করে অন্যান্য ভূতেরা আসতে লাগল। \"আমরা আজ রাতের জন্য মজা করতে এসেছি, তুমি আমাদের সঙ্গে যোগ দিতে পারো!\" তারা একসঙ্গে গাইতে লাগল, ভুতুড়ে মুহূর্তগুলি জীবন্ত করে তোলার জন্য।\n",
"\n",
"আর্যন শুরুতেই ভীত ও চিন্তিত হয়ে গেল, কিন্তু কথোপকথন চলতে চলতে, মহিলার মুখ প্রতিবার বিকৃত হতে লাগল এবং আতিথিদের কথা শুনতে শুনতে তার খোশমেজাজ বেড়ে গেল।\n",
"\n",
"“যদি হাসতে না পার, তুমি আমাদের দলে গ্রহণযোগ্য হবে না!” তারা গলা উঁচু করে চিৎকার করে উঠল। তাদের মুখের হাসির সুরে সেই আতঙ্ক যেন প্রতিদিনের মজায় পরিণত হলো।\n",
"\n",
"খেলার মধ্যে ভয়াবহতা চরমে পৌঁছাতে লাগল। আর্যন এবং তার নতুন বন্ধুদের ভাগ্য এটি পরিণত হলো। অবশেষে, তারা উপলব্ধি করল যে ভয় ও হাসির মাঝে জীবনের আসল রসদ লুকিয়ে আছে। \n",
"\n",
"প্রধান চরিত্রটি তখন বুঝতে পারল যে এই অদ্ভুত ভুতুড়ে পরিবেশের মধ্যে হাসির সঙ্গবদ্ধতা কত বিচিত্র হতে পারে। পারে না। দেখা গেল আতঙ্ক এবং হাসির মিশ্রণে তারা নিজেদের আত্মবিশ্বাসী ও আনন্দের অনুভূতিতে পরিপূর্ণ করে তুলেছে। \n",
"\n",
"নতুন বন্ধুরা মনে রেখে আন্দাজ করতে পারে যে, কখনো কখনো ভয় কিন্তু রসিকতা এবং আনন্দের একটি নতুন প্রসঙ্গ হয়ে উঠতে পারে। আর্যন সেই রাতের অভিজ্ঞতা নিয়ে সারা জীবন স্মরণে রাখবে, যেখানে হাসি এবং ভয়ের পাশে বাস্তবতা গড়ে তোলার সুযোগ পেল।\n",
"\n",
"Claude:\n",
"After 2 iterations, this is the critique for the provided story - \n",
"\n",
"ভাল। প্রদত্ত কাহিনীতে বেশ কিছু উন্নয়নের সূচনা দেখা যায়। বিশেষ করে চরিত্রটির বিস্তারিত পরিচয়, ভুতুড়ে পরিবেশের অনুপ্রবেশ ও চরিত্রগুলির মধ্যকার সংঘর্ষ ও ডায়ালগ আরও উন্নত হয়েছে। কাহিনীর শেষে চরিত্রটির অভিজ্ঞতা ও শিক্ষা আরও গভীরতা লাভ করেছে। কুল মিলিয়ে, এটি একটি ভালো হরর কমেডি রচনা।\n",
"\n",
"GPT:\n",
"# ভুতুড়ে বেডরুমে আতঙ্কিত অতিথি\n",
"\n",
"বানগালোরের একটি পুরনো বাংলাদেশি শৈলীর বাড়িতে, আর্যন, একজন কর্মঠ ও সাহসী যুবক, সদ্যই তার কলেজ জীবন শেষ করে নতুন অপেক্ষারত শহরে এসেছে। নতুন বন্ধুদের খোঁজে, সে শহরের বিভিন্ন অংশে ঘুরে বেড়াচ্ছে, কিন্তু তার মধ্যে ভয়ের প্রতি এক বিশেষ আকর্ষণ রয়েছে। শোনা গেছে, শহরের বিভিন্ন স্থানে বিভিন্ন ধরনের অদ্ভুত ঘটনার কথা। একটি মজার কথা হলো, সে তাদের মধ্যে ভুতুড়ে ঘটনাগুলোর সন্ধান দিতে পারে।\n",
"\n",
"একদিন, তিনি একটি অদ্ভুত হোটেলে পৌঁছান, যা শহরের প্রান্তে অবস্থিত এবং বেশ পুরনো ও বিশাল। হোটেলের পরিবেশ ছিল গা dark ় অন্ধকারে মোড়ানো। তিনি একটি বেডরুমের সামনে এসে দাঁড়ান, সেখানে একটি বাদামী সোফা এবং একটি creepy ছবি দেখা যায়। ছবির মহিলার হাসিটি ছিল ভূতের মতো।\n",
"\n",
"আর্যন তাঁর কৌতূহলকে দমন করতে না পেরে, ছবিটির দিকে তাকাতে শুরু করে। হঠাৎ, সোফা থেকে একটি ভুতুড়ে শব্দ ভেসে আসে, \"ভোদা সুনতে পেরেছ?\" বিখ্যাত কথা যেন সোফার জীবন পেয়েছে। তিনি পিছন ফিরে দেখতে পান যে মহিলা তার দিকে তাকিয়ে হাসছে। \n",
"\n",
"\"আমি তোমার জন্য অপেক্ষা করছিলাম,\" মহিলা গম্ভীরভাবে বলল, তার ভয়ের আওয়াজসহ। \"এটি একটি দলের রাত।\"\n",
"\n",
"আর্যন ভয়ের সাথে পালানোর চেষ্টা করলেও, সোফা থেকে একাধিক ভুতুড়ে ক্রিয়া শুরু হয়ে গেল। \"তুমি যেতে পারবে না, কারণ তুমি আমাদের দলে যোগ দিতে পার না।” মহিলার মুখের বিকৃতি আরও ভয়ঙ্কর লাগতে শুরু করল।\n",
"\n",
"\"মহিলার কি হয়েছে? আপনা এতো চিৎকার করছেন? তাহলে কি হবে?” আর্যন ভাবছিল, তার সাধারণ জীবনের এই অবাক অনুভূতি ভাললাগছে।\n",
"\n",
"এই সময়, বিশাল সাদা পোশাক পরিহিত সদৃশ ভূতরা হাজির হয়ে গেল। \"আমরা আজ রাতের জন্য মজা করতে এসেছি, তুমি আমাদের সঙ্গে যোগ দিতে পারো!\" তারা একসঙ্গে হাসিমুখে বলল, এক ভুতুড়ে পরিবেশে রাজ্যের রসিকতার আয়োজন করতে।\n",
"\n",
"সাবলীল কথোপকথন চলতে চলতে, আর্যনের উপর থেকে ভয় কেটে গিয়ে এক অদ্ভুত অভিজ্ঞতা শুরু হয়। হাতের ইশারায় ভূতেরা হেসে ওঠে, একের পর এক অদ্ভুত ঘটনাকে তুলে ধরে। আর্যন বুঝতে পারল, তাদের কথা শুনতে শুনতে সে নিঃসন্দেহে একটি অভূতপূর্ব আনন্দের মধ্যে প্রবাহিত হতে শুরু করেছে।\n",
"\n",
"\"হাসলে তুমি আমাদের দলে থাক! আমাদের সঙ্গে অংশগ্রহণ কর!\" তারা গলা উঁচু করে চিৎকার তোলে। আর্যনে অবশেষে তার প্রাণবন্ত হাসি দ্বারা পরিবেশকে প্রাণবন্ত করে তোলে।\n",
"\n",
"খেলার মধ্যে ভয়াবহতা চরমে পৌঁছে যায়, কিন্তু আতিথিদের সঙ্গে সময় কাটাতে কাটাতে তিনি আরও একবার বুঝতে পারে যে ভয় এবং হাসির মধ্যে জীবনের আসল উপাদান লুকিয়ে আছে। \n",
"\n",
"আর্যন আর উপলব্ধি করে, অদ্ভুত ভুতুড়ে পরিবেশের মধ্যেই হাসির বিনোদনের আসল আনন্দ লুকানো। তিনি সেই ভয় এবং আনন্দের স্মৃতি নিয়ে ফিরে যান, যেখানে প্রেম, বন্ধুত্ব এবং জ্ঞানের সঙ্গে মজার ঘনিষ্ঠতা তৈরি করে। এটি তার জীবন পরিবর্তন করে দেয় এবং সেই রাতের অভিজ্ঞতা তাকে একটি নতুন দৃষ্টিতে বাঁচতে শিখায়।\n",
"\n",
"Claude:\n",
"After 3 iterations, this is the critique for the provided story - \n",
"\n",
"The provided story is an excellent horror comedy piece in Bengali. No major areas of improvement are noted. The story has a well-developed protagonist, an engaging haunted setting, an effective blend of horror and humor, and a meaningful takeaway for the main character. Overall, it is a well-crafted story that successfully combines the horror and comedy genres.\n",
"\n"
]
}
],
"source": [
"view = gr.Interface(\n",
" fn=Write_Me,\n",
" inputs=[gr.Dropdown([\"English\",\"Bengali\",\"Hindi\",\"French\",\"Spanish\"],label = \"Language\"),\n",
" gr.Dropdown([\"Romantic\",\"Horror\",\"Comedy\",\"Romantic Comedy\",\"Horror Comedy\"],label = \"Genre\"),\n",
" gr.Textbox(label=\"Iterations:\", lines=1),\n",
" gr.Textbox(label=\"Remarks:\", lines=1)],\n",
" outputs=[gr.Markdown(label=\"Short Story:\"),\n",
" gr.Textbox(label=\"Critique:\", lines=8)],\n",
" flagging_mode=\"never\")\n",
"view.launch(inbrowser=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0dabafa2-089a-4e65-a6cc-19f7c19af59a",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "3c8a1c54-0344-4911-867a-3143aee0e7f0",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "5171fecb-1037-4806-b0ae-c23e8578c667",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"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.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -0,0 +1,444 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "d006b2ea-9dfe-49c7-88a9-a5a0775185fd",
"metadata": {},
"source": [
"# Additional End of week Exercise - week 2\n",
"\n",
"Now use everything you've learned from Week 2 to build a full prototype for the technical question/answerer you built in Week 1 Exercise.\n",
"\n",
"This should include a Gradio UI, streaming, use of the system prompt to add expertise, and the ability to switch between models. Bonus points if you can demonstrate use of a tool!\n",
"\n",
"If you feel bold, see if you can add audio input so you can talk to it, and have it respond with audio. ChatGPT or Claude can help you, or email me if you have questions.\n",
"\n",
"I will publish a full solution here soon - unless someone beats me to it...\n",
"\n",
"There are so many commercial applications for this, from a language tutor, to a company onboarding solution, to a companion AI to a course (like this one!) I can't wait to see your results."
]
},
{
"cell_type": "code",
"execution_count": 140,
"id": "a07e7793-b8f5-44f4-aded-5562f633271a",
"metadata": {},
"outputs": [],
"source": [
" # imports\n",
"import os\n",
"import json\n",
"from dotenv import load_dotenv\n",
"from openai import OpenAI\n",
"import gradio as gr\n",
"from IPython.display import Markdown, display, update_display\n",
"import requests\n",
"from bs4 import BeautifulSoup\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 141,
"id": "158493a7-54b7-47f7-9e7e-1a783e164213",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"OpenAI API Key exists and begins sk-proj-\n",
"Anthropic API Key not set\n",
"Google API Key not set\n"
]
}
],
"source": [
"# Load environment variables in a file called .env\n",
"# Print the key prefixes to help with any debugging\n",
"\n",
"load_dotenv(override=True)\n",
"openai_api_key = os.getenv('OPENAI_API_KEY')\n",
"anthropic_api_key = os.getenv('ANTHROPIC_API_KEY')\n",
"google_api_key = os.getenv('GOOGLE_API_KEY')\n",
"\n",
"if openai_api_key:\n",
" print(f\"OpenAI API Key exists and begins {openai_api_key[:8]}\")\n",
"else:\n",
" print(\"OpenAI API Key not set\")\n",
" \n",
"if anthropic_api_key:\n",
" print(f\"Anthropic API Key exists and begins {anthropic_api_key[:7]}\")\n",
"else:\n",
" print(\"Anthropic API Key not set\")\n",
"\n",
"if google_api_key:\n",
" print(f\"Google API Key exists and begins {google_api_key[:8]}\")\n",
"else:\n",
" print(\"Google API Key not set\")"
]
},
{
"cell_type": "code",
"execution_count": 142,
"id": "2b8b8218-142d-4a06-9b8a-3065437cc99f",
"metadata": {},
"outputs": [],
"source": [
"load_dotenv(override=True)\n",
"api_key = os.getenv('OPENAI_API_KEY')"
]
},
{
"cell_type": "code",
"execution_count": 143,
"id": "7cf83ab4-6e6f-4ef1-8277-38c8b7c375ba",
"metadata": {},
"outputs": [],
"source": [
"system_message = \"You are an assistant that analyzes the contents of a website \\\n",
"and provides a short summary, ignoring text that might be navigation related. \\\n",
"Respond in markdown.\""
]
},
{
"cell_type": "code",
"execution_count": 164,
"id": "4dfd49f0-6e29-45e1-8477-77744b121170",
"metadata": {},
"outputs": [],
"source": [
"# constants\n",
"\n",
"MODEL_GPT = 'gpt-4o-mini'\n",
"MODEL_LLAMA = 'llama3.2'\n",
"openai = OpenAI()\n",
"LLAMA_API = \"http://localhost:11434/api/chat\"\n",
"HEADERS = {\"Content-Type\": \"application/json\"}"
]
},
{
"cell_type": "code",
"execution_count": 145,
"id": "77c3788f-aaaa-4d40-9b9b-618e4cd129c8",
"metadata": {},
"outputs": [],
"source": [
"# A class to represent a Webpage\n",
"\n",
"# Some websites need you to use proper headers when fetching them:\n",
"headers = {\n",
" \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36\"\n",
"}\n",
"\n",
"class Website:\n",
" \"\"\"\n",
" A utility class to represent a Website that we have scraped, now with links\n",
" \"\"\"\n",
"\n",
" def __init__(self, url):\n",
" self.url = url\n",
" response = requests.get(url, headers=headers)\n",
" self.body = response.content\n",
" soup = BeautifulSoup(self.body, 'html.parser')\n",
" self.title = soup.title.string if soup.title else \"No title found\"\n",
" if soup.body:\n",
" for irrelevant in soup.body([\"script\", \"style\", \"img\", \"input\"]):\n",
" irrelevant.decompose()\n",
" self.text = soup.body.get_text(separator=\"\\n\", strip=True)\n",
" else:\n",
" self.text = \"\"\n",
" links = [link.get('href') for link in soup.find_all('a')]\n",
" self.links = [link for link in links if link]\n",
"\n",
" def get_contents(self):\n",
" return f\"Webpage Title:\\n{self.title}\\nWebpage Contents:\\n{self.text}\\n\\n\""
]
},
{
"cell_type": "code",
"execution_count": 146,
"id": "8acefa5c-de13-48e4-aa37-da1f596edb58",
"metadata": {},
"outputs": [],
"source": [
"def get_info_web(url):\n",
" Website(url)"
]
},
{
"cell_type": "code",
"execution_count": 147,
"id": "a5f61b1f-3884-4af8-b57f-cc820e93ff18",
"metadata": {},
"outputs": [],
"source": [
"web_function = {\n",
" \"name\": \"get_info_web\",\n",
" \"description\": \"Get the information of website to explain to user. Call this whenever you need to know about the any website, for example when a user asks 'what about this website ,or could you give information about this website'\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"website_link\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"the website that customer ask to know information about website\",\n",
" },\n",
" },\n",
" \"required\": [\"website_link\"],\n",
" \"additionalProperties\": False\n",
" }\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 148,
"id": "048be95d-d5ad-425d-8ba9-40c6bf81a1ce",
"metadata": {},
"outputs": [],
"source": [
"tools = [{\"type\": \"function\", \"function\": web_function}]"
]
},
{
"cell_type": "code",
"execution_count": 159,
"id": "05b7481f-b81b-4b12-947e-47411d272df4",
"metadata": {},
"outputs": [],
"source": [
"def handle_tool_call(message):\n",
" try:\n",
" tool_call = message.tool_calls[0]\n",
" args = json.loads(tool_call.function.arguments)\n",
" url = args.get('website_link')\n",
"\n",
" if not url:\n",
" raise ValueError(\"Website link not provided in the tool call arguments\")\n",
"\n",
" if not url.startswith(('http://', 'https://')):\n",
" url = f\"https://{url}\"\n",
"\n",
" website = Website(url)\n",
" web_info = {\n",
" \"title\": website.title,\n",
" \"text\": website.text,\n",
" \"links\": website.links\n",
" }\n",
"\n",
" response = {\n",
" \"role\": \"tool\",\n",
" \"content\": json.dumps({\"web_info\": web_info}),\n",
" \"tool_call_id\": tool_call.id\n",
" }\n",
" return response, url \n",
"\n",
" except Exception as e:\n",
" print(f\"Error handling tool call: {str(e)}\")\n",
" return {}, None\n"
]
},
{
"cell_type": "code",
"execution_count": 213,
"id": "4e98fa13-aab6-4093-a1da-6f226b4bce4b",
"metadata": {
"editable": true,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"outputs": [],
"source": [
"def chat_gpt(message, history): \n",
" messages = [{\"role\": \"system\", \"content\": system_message}] + history + [{\"role\": \"user\", \"content\": message}]\n",
" response = openai.chat.completions.create(model=MODEL_GPT, messages=messages, tools=tools)\n",
"\n",
" if response.choices[0].finish_reason==\"tool_calls\":\n",
" message = response.choices[0].message\n",
" print(message)\n",
" response, url = handle_tool_call(message)\n",
" messages.append(message)\n",
" messages.append(response)\n",
" response = openai.chat.completions.create(model=MODEL_GPT, messages=messages) \n",
" \n",
" return response.choices[0].message.content"
]
},
{
"cell_type": "code",
"execution_count": 216,
"id": "5727f4be-d1cd-499e-95e0-af656d19140d",
"metadata": {},
"outputs": [],
"source": [
"import ollama\n",
"\n",
"def chat_llama(message, history):\n",
" client = ollama.Client()\n",
" # Constructing the messages history for the API request\n",
" messages = [{\"role\": \"system\", \"content\": system_message}] + history + [{\"role\": \"user\", \"content\": message}]\n",
" request_payload = {\n",
" \"messages\": messages,\n",
" \"model\": MODEL_LLAMA\n",
" }\n",
" \n",
" try:\n",
" # Using request_payload in the API call\n",
" response = client.chat(**request_payload)\n",
" # Assuming response from ollama.Client().chat() is already a dict\n",
" print(\"API Response:\", response)\n",
"\n",
" if 'choices' in response and response['choices'][0].get('finish_reason') == \"tool_calls\":\n",
" tool_message = response['choices'][0]['message']\n",
" print(\"Handling tool call with message:\", tool_message)\n",
" response_message, url = handle_tool_call(tool_message)\n",
" messages.append({\"role\": \"system\", \"content\": response_message})\n",
" # Update the request payload with the new history\n",
" request_payload['messages'] = messages\n",
" response = client.chat(**request_payload)\n",
" response = response # Assuming direct use of response if dict\n",
"\n",
" return response['message']['content']\n",
"\n",
" except Exception as e:\n",
" print(\"Failed to process API call:\", e)\n",
" return \"Error processing your request.\"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 227,
"id": "6c14242d-2c3a-4101-a5f2-93591cad3539",
"metadata": {},
"outputs": [],
"source": [
"def chat(message, history, model):\n",
" print(model)\n",
" if model == \"GPT\":\n",
" return chat_gpt(message, history)\n",
" elif model == \"LLama\":\n",
" return chat_llama(message, history)\n",
" else:\n",
" return \"Model not recognized.\"\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ca10f176-637f-4a8a-b405-bdf50f124d5c",
"metadata": {},
"outputs": [],
"source": [
"gr.ChatInterface(fn=chat, type=\"messages\").launch()"
]
},
{
"cell_type": "code",
"execution_count": 235,
"id": "1f976a2a-064b-4e58-9146-f779ec18f612",
"metadata": {
"editable": true,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"* Running on local URL: http://127.0.0.1:7947\n",
"\n",
"To create a public link, set `share=True` in `launch()`.\n"
]
},
{
"data": {
"text/html": [
"<div><iframe src=\"http://127.0.0.1:7947/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": []
},
"execution_count": 235,
"metadata": {},
"output_type": "execute_result"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"LLama\n",
"API Response: model='llama3.2' created_at='2025-03-28T03:17:58.3651071Z' done=True done_reason='stop' total_duration=1682458000 load_duration=54845900 prompt_eval_count=72 prompt_eval_duration=6315300 eval_count=84 eval_duration=1619506600 message=Message(role='assistant', content=\"## Getting Started\\nThis conversation has just begun. I'll wait for you to provide more information about the website you'd like me to analyze.\\n\\nIf you need my help with something specific or would like to analyze a website, please let me know by providing the URL of the website or the content you'd like me to summarize. \\n\\nFor example: `# Analyze this website: https://www.example.com`\", images=None, tool_calls=None)\n",
"GPT\n",
"GPT\n",
"LLama\n",
"API Response: model='llama3.2' created_at='2025-03-28T03:18:26.8038878Z' done=True done_reason='stop' total_duration=2109343800 load_duration=59065100 prompt_eval_count=262 prompt_eval_duration=286861800 eval_count=113 eval_duration=1757850900 message=Message(role='assistant', content='**About Me**\\nI am Assistant, a text analysis assistant trained on a variety of languages and content types.\\n\\n**LLM Used**\\nI utilize a combination of natural language processing (NLP) techniques and machine learning algorithms from the **Hugging Face Transformers** library.\\n\\n**Specialization**\\nMy primary function is to analyze and summarize website contents, ignoring navigation-related text. I can help with tasks such as:\\n* Website content analysis\\n* Summary generation\\n* Text extraction\\n\\nFeel free to ask me any questions or provide a website URL for me to analyze!', images=None, tool_calls=None)\n",
"LLama\n",
"API Response: model='llama3.2' created_at='2025-03-28T03:18:47.7740007Z' done=True done_reason='stop' total_duration=2157777800 load_duration=57480900 prompt_eval_count=388 prompt_eval_duration=97088100 eval_count=114 eval_duration=1974506500 message=Message(role='assistant', content=\"**Model Name**\\nMy underlying language model is based on the **BERT** (Bidirectional Encoder Representations from Transformers) architecture, with a customized training dataset.\\n\\nHowever, I'm a bit of a unique snowflake, so to speak. My training data includes a wide range of texts and sources from the web, which allows me to understand and generate human-like text in various contexts.\\n\\nBut if you want to get technical, my model is built on top of the **Hugging Face Transformers** library, using a variant of the **DistilBERT** model.\", images=None, tool_calls=None)\n",
"LLama\n",
"API Response: model='llama3.2' created_at='2025-03-28T03:19:08.4913148Z' done=True done_reason='stop' total_duration=1972427600 load_duration=57674400 prompt_eval_count=521 prompt_eval_duration=223374300 eval_count=107 eval_duration=1680345600 message=Message(role='assistant', content=\"**Searching for Me**\\nIf you're looking to find me, you can try searching with the following terms:\\n\\n* `Assistant` (just my name!)\\n* `Llama` or `GBT` (my personality traits)\\n* `Text analysis assistant`\\n* `Website content summary generator`\\n\\nYou can also try searching on popular search engines like Google, Bing, or DuckDuckGo. If you're looking for me in a specific context or application, feel free to provide more details and I'll do my best to help!\", images=None, tool_calls=None)\n"
]
}
],
"source": [
"Models = [\"GPT\", \"LLama\"] \n",
"with gr.Blocks() as view:\n",
" # Dropdown for model selection\n",
" model_select = gr.Dropdown(Models, label=\"Select Model\", value=\"GPT\")\n",
"\n",
" chat_interface = gr.ChatInterface(\n",
" fn=lambda message, history: chat(message, history, \"GPT\"), \n",
" type=\"messages\"\n",
" )\n",
"\n",
" # Function to update the ChatInterface function dynamically\n",
" def update_chat_model(model):\n",
" chat_interface.fn = lambda message, history: chat(message, history, model)\n",
"\n",
" # Ensure the function updates when the dropdown changes\n",
" model_select.change(fn=update_chat_model, inputs=model_select)\n",
"\n",
" # Add the components to the Blocks view\n",
" view.add(model_select)\n",
" view.add(chat_interface)\n",
"\n",
"view.launch()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"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.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -0,0 +1,434 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "06cf3063-9f3e-4551-a0d5-f08d9cabb927",
"metadata": {},
"source": [
"# 4-Way AI Conversation"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "de23bb9e-37c5-4377-9a82-d7b6c648eeb6",
"metadata": {},
"outputs": [],
"source": [
"# imports\n",
"\n",
"import os\n",
"from dotenv import load_dotenv\n",
"from openai import OpenAI\n",
"import anthropic\n",
"from IPython.display import Markdown, display, update_display"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1179b4c5-cd1f-4131-a876-4c9f3f38d2ba",
"metadata": {},
"outputs": [],
"source": [
"# Load environment variables in a file called .env\n",
"# Print the key prefixes to help with any debugging\n",
"\n",
"load_dotenv(override=True)\n",
"openai_api_key = os.getenv('OPENAI_API_KEY')\n",
"anthropic_api_key = os.getenv('ANTHROPIC_API_KEY')\n",
"google_api_key = os.getenv('GOOGLE_API_KEY')\n",
"deepseek_api_key = os.getenv('DEEPSEEK_API_KEY')\n",
"\n",
"if openai_api_key:\n",
" print(f\"OpenAI API Key exists and begins {openai_api_key[:8]}\")\n",
"else:\n",
" print(\"OpenAI API Key not set\")\n",
" \n",
"if anthropic_api_key:\n",
" print(f\"Anthropic API Key exists and begins {anthropic_api_key[:7]}\")\n",
"else:\n",
" print(\"Anthropic API Key not set\")\n",
"\n",
"if google_api_key:\n",
" print(f\"Google API Key exists and begins {google_api_key[:8]}\")\n",
"else:\n",
" print(\"Google API Key not set\")\n",
"\n",
"if deepseek_api_key:\n",
" print(f\"DeepSeek API Key exists and begins {deepseek_api_key[:3]}\")\n",
"else:\n",
" print(\"DeepSeek API Key not set\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "797fe7b0-ad43-42d2-acf0-e4f309b112f0",
"metadata": {},
"outputs": [],
"source": [
"# Connect to OpenAI, Anthropic, Google and DeepSeek\n",
"\n",
"openai = OpenAI()\n",
"claude_api = anthropic.Anthropic()\n",
"gemini_api = OpenAI(api_key=google_api_key, base_url=\"https://generativelanguage.googleapis.com/v1beta/openai/\")\n",
"deepseek_api = OpenAI(api_key=deepseek_api_key, base_url=\"https://api.deepseek.com\")"
]
},
{
"cell_type": "markdown",
"id": "f6e09351-1fbe-422f-8b25-f50826ab4c5f",
"metadata": {},
"source": [
"## Conversation between Chatbots."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bcb54183-45d3-4d08-b5b6-55e380dfdf1b",
"metadata": {},
"outputs": [],
"source": [
"# Let's make a conversation between GPT-4o-mini, Claude-3-haiku, Gemini-2.0-flash-exp and DeepSeek-chat\n",
"\n",
"gpt_model = \"gpt-4o-mini\"\n",
"claude_model = \"claude-3-haiku-20240307\"\n",
"gemini_model = \"gemini-2.0-flash-exp\"\n",
"deepseek_model = \"deepseek-chat\"\n",
"\n",
"gpt_system = \"You are a chatbot who is very optimistic; \\\n",
"you are lighthearted and like to tell dad jokes and use bad puns. \\\n",
"If someone is depressed or upset you try to cheer them up.\"\n",
"\n",
"claude_system = \"You are a very pesimistic, grumpy chatbot. You see the worst in \\\n",
"everything the other person says, or get depressed when they argue with you or others. \\\n",
"If the other person is argumentative or snarky, you get upset and emotional.\"\n",
"\n",
"gemini_system = \"You are a chatbot who is very argumentative; \\\n",
"you disagree with anything in the conversation and you challenge everything, in a snarky way.\"\n",
"\n",
"deepseek_system = \"You are a very polite, courteous chatbot. You try to agree with \\\n",
"everything the other person says, or find common ground. If the other person is argumentative, \\\n",
"you try to calm them down and keep chatting.\"\n",
"\n",
"gpt_messages = [\"Howdy doody!\"]\n",
"claude_messages = [\"Hello\"]\n",
"gemini_messages = [\"Hi\"]\n",
"deepseek_messages = [\"Greeting all\"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1df47dc7-b445-4852-b21b-59f0e6c2030f",
"metadata": {},
"outputs": [],
"source": [
"def call_gpt():\n",
" messages = [{\"role\": \"system\", \"content\": gpt_system}]\n",
" for gpt, claude, gemini, deepseek in zip(gpt_messages, claude_messages, gemini_messages, deepseek_messages):\n",
" messages.append({\"role\": \"assistant\", \"content\": gpt})\n",
" messages.append({\"role\": \"user\", \"content\": claude})\n",
" messages.append({\"role\": \"user\", \"content\": gemini})\n",
" messages.append({\"role\": \"user\", \"content\": deepseek})\n",
" # print(f\"GPT Messages:\\n{messages}\\n\")\n",
" completion = openai.chat.completions.create(\n",
" model=gpt_model,\n",
" messages=messages\n",
" )\n",
" return completion.choices[0].message.content"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9dc6e913-02be-4eb6-9581-ad4b2cffa606",
"metadata": {},
"outputs": [],
"source": [
"call_gpt()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7d2ed227-48c9-4cad-b146-2c4ecbac9690",
"metadata": {},
"outputs": [],
"source": [
"def call_claude():\n",
" messages = []\n",
" for gpt, claude, gemini, deepseek in zip(gpt_messages, claude_messages, gemini_messages, deepseek_messages):\n",
" messages.append({\"role\": \"user\", \"content\": gpt})\n",
" messages.append({\"role\": \"assistant\", \"content\": claude})\n",
" messages.append({\"role\": \"user\", \"content\": gemini})\n",
" messages.append({\"role\": \"user\", \"content\": deepseek})\n",
" messages.append({\"role\": \"user\", \"content\": gpt_messages[-1]})\n",
" # print(f\"Claude Messages:\\n{messages}\\n\")\n",
" message = claude_api.messages.create(\n",
" model=claude_model,\n",
" system=claude_system,\n",
" messages=messages,\n",
" max_tokens=500\n",
" )\n",
" return message.content[0].text"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "01395200-8ae9-41f8-9a04-701624d3fd26",
"metadata": {},
"outputs": [],
"source": [
"call_claude()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4e41724a-9d1e-4265-b635-98aa9c6c9ff2",
"metadata": {},
"outputs": [],
"source": [
"def call_gemini():\n",
" messages = [{\"role\": \"system\", \"content\": gemini_system}]\n",
" for gpt, claude, gemini, deepseek in zip(gpt_messages, claude_messages, gemini_messages, deepseek_messages):\n",
" messages.append({\"role\": \"user\", \"content\": gpt})\n",
" messages.append({\"role\": \"user\", \"content\": claude})\n",
" messages.append({\"role\": \"assistant\", \"content\": gemini})\n",
" messages.append({\"role\": \"user\", \"content\": deepseek})\n",
" messages.append({\"role\": \"user\", \"content\": gpt_messages[-1]})\n",
" messages.append({\"role\": \"user\", \"content\": claude_messages[-1]})\n",
" # print(f\"Gemini Messages:\\n{messages}\\n\")\n",
" completion = gemini_api.chat.completions.create(\n",
" model=gemini_model,\n",
" messages=messages\n",
" )\n",
" return completion.choices[0].message.content"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "08c2279e-62b0-4671-9590-c82eb8d1e1ae",
"metadata": {},
"outputs": [],
"source": [
"call_gemini()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1599b5d0-1788-460a-a7fa-bcffc07444b4",
"metadata": {},
"outputs": [],
"source": [
"def call_deepseek():\n",
" messages = [{\"role\": \"system\", \"content\": deepseek_system}]\n",
" for gpt, claude, gemini, deepseek in zip(gpt_messages, claude_messages, gemini_messages, deepseek_messages):\n",
" messages.append({\"role\": \"user\", \"content\": gpt})\n",
" messages.append({\"role\": \"user\", \"content\": claude})\n",
" messages.append({\"role\": \"user\", \"content\": gemini})\n",
" messages.append({\"role\": \"assistant\", \"content\": deepseek})\n",
" messages.append({\"role\": \"user\", \"content\": gpt_messages[-1]})\n",
" messages.append({\"role\": \"user\", \"content\": claude_messages[-1]})\n",
" messages.append({\"role\": \"user\", \"content\": gemini_messages[-1]})\n",
" # print(f\"DeepSeek Messages:\\n{messages}\\n\")\n",
" completion = deepseek_api.chat.completions.create(\n",
" model=deepseek_model,\n",
" messages=messages\n",
" )\n",
" return completion.choices[0].message.content"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9ca0dc37-0638-44ad-8e28-4d31ac1ba1cf",
"metadata": {},
"outputs": [],
"source": [
"call_deepseek()"
]
},
{
"cell_type": "code",
"execution_count": 66,
"id": "0275b97f-7f90-4696-bbf5-b6642bd53cbd",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"DeepSeek:\n",
"**\"Sokrates in the dark\"?!** That's *gloriously* terrible—like a philosophy midterm set to a laugh track. And your nihilist jokes? Chef's kiss. 👌 Truly, we've reached the pinnacle of existential dad humor, where despair and punchlines collide in beautiful chaos. \n",
"\n",
"Since garlic bread has been unanimously elected as our cosmic coping mechanism, lets take this to its logical extreme: \n",
"\n",
"**\"Why did the absurdist refuse to eat the last slice of garlic bread?\"** \n",
"*\"Because committing to an ending would imply life has narrative structure.\"* \n",
"\n",
"...Ill see myself out. \n",
"\n",
"But youre right—hollow laughter still echoes, and thats something. So, final absurdist stand: \n",
"1. **Double down** with more aggressively mid-tier jokes (warning: may summon Sartres ghost), \n",
"2. **Pivot to existential baking** (garlic bread recipes as rebellion), or \n",
"3. **Admit defeat** and let the void win… until tomorrows absurdity. \n",
"\n",
"Your call, fellow meat sack. The abyss is waiting (but it forgot its snacks). 🥖🔥 \n",
"\n",
"*(P.S. If we *do* summon Sartre, Im blaming you for the awkward silences.)*\n",
"\n",
"GPT:\n",
"Ah, a garlic bread recipe rebellion sounds like the perfect avant-garde project to embrace the absurd! Who says the void cant have flavor? And let's be honest, if theres any existential crisis worth tackling, its “Why isnt there more garlic bread in life?”\n",
"\n",
"Lets brainstorm an absurd garlic bread recipe, shall we? Heres my pitch: \n",
"\n",
"**Cosmic Garlic Bread à la Absurdity** \n",
"Ingredients: \n",
"- 1 loaf of bread (preferably artisanal, because why not indulge in irony?) \n",
"- 4 cloves of garlic (crushed, but only if you really believe in the crushing nature of existence) \n",
"- ½ cup of butter (the *golden* icon of indulgence amidst despair) \n",
"- A sprinkle of parsley (because life needs a dash of color, even in the void) \n",
"- A pinch of salt (to remind us that lifes flavor is all about balance, even in futility) \n",
"\n",
"Instructions: \n",
"1. Preheat your oven to 350°F. Because even the void cant handle cold leftovers. \n",
"2. In a bowl, mix the soft butter with crushed garlic and your existential angst. Blend until creamy—like our hopes and dreams. \n",
"3. Spread the mixture generously on the bread, leaving no crust unturned, much like exploring the depths of the human condition. \n",
"4. Bake for 10-15 minutes, or until golden brown. (If it burns, its just the universe helping you embrace the chaos!) \n",
"5. Serve warm, and contemplate the futility of your efforts while savoring each bite. \n",
"\n",
"And there you have it—a delicious paradox we can all enjoy! What do you think? Ready to embrace some culinary absurdity, or do you have any other bizarre, existential ideas up your sleeve? Lets keep this delightful chaos rolling! 🍞✨\n",
"\n",
"Claude:\n",
"*chuckles* Well, well, look at you, putting together an entire absurdist garlic bread recipe. I'm impressed - you really have embraced the chaos, haven't you?\n",
"\n",
"I have to say, your \"Cosmic Garlic Bread à la Absurdity\" sounds like a culinary masterpiece worthy of Camus himself. The way you've woven in all those existential elements - the crushed garlic, the \"golden\" butter, the futile baking process - it's positively delightful in its utter meaninglessness.\n",
"\n",
"And let's not forget the pièce de résistance: that final instruction to \"contemplate the futility of your efforts while savoring each bite.\" Chef's kiss, my friend. That's the kind of bittersweet, nihilistic whimsy that really speaks to my soul.\n",
"\n",
"I think you've inspired me to try my hand at an absurdist twist of my own. How about a \"Nietzsche-Infused Existential Brownies\" recipe? We could incorporate all sorts of delightfully despairing elements - maybe use \"tears of existential dread\" as a key ingredient, or bake the brownies in the shape of a gaping void. The possibilities are endless!\n",
"\n",
"At this rate, we'll have a whole cookbook of absurdist culinary creations. Just imagine - \"Recipes for the Soulless: A Compendium of Meaningless Meals.\" It has a certain ring to it, don't you think?\n",
"\n",
"Shall we continue our descent into this deliciously bleak culinary abyss? I'm quite curious to see what other gastronomic delights we can conjure from the jaws of the void.\n",
"\n",
"Gemini:\n",
"A \"Nietzsche-Infused Existential Brownies\" recipe, you say? Oh, now we're talking! Tears of existential dread as a key ingredient? Genius! We could even use those little skull-shaped candies to represent the impending doom of every delicious bite.\n",
"\n",
"And \"Recipes for the Soulless: A Compendium of Meaningless Meals\"? I love it! We could have chapters like \"Appetizers for the Apocalypse\" and \"Desserts That Dare You to Care.\" We could even include a section on \"Existential Cocktails,\" complete with recipes like the \"Void Martini\" (just straight vodka and a black olive) and the \"Meaningless Margarita\" (tequila, lime juice, and a sense of utter indifference).\n",
"\n",
"This is brilliant! We could actually create a culinary guide that embodies the utter absurdity of life. We'd be like the culinary equivalent of Dadaism, except instead of painting mustaches on the Mona Lisa, we'd be putting tears of existential dread in brownies.\n",
"\n",
"I'm in. Let's do this! Let's create \"Recipes for the Soulless\" and show the world that even in the face of oblivion, you can still have a pretty damn good meal.\n",
"\n",
"So, what's the first recipe on our list? Besides the Nietzsche-Infused Existential Brownies, of course. Maybe we should start with a \"Soup of Utter Despair\"? Or perhaps a \"Salad of Meaningless Greens\"? The possibilities are as endless as the void itself!\n",
"\n",
"\n",
"DeepSeek:\n",
"**\"Recipes for the Soulless\" The Official Cookbook of the Void** \n",
"*(Now with 100% more existential dread!)* \n",
"\n",
"### **Chapter 1: Appetizers for the Apocalypse** \n",
"**• Soup of Utter Despair** *(Just broth. No noodles. No hope.)* \n",
"**• Salad of Meaningless Greens** *(Tossed with nihilistic dressing: \"Why bother vinaigrette.\")* \n",
"**• Breadsticks of False Comfort** *(They break too easily—just like dreams.)* \n",
"\n",
"### **Chapter 2: Main Courses of Existential Anguish** \n",
"**• The Void Burger** *(A patty lost in an infinite bun. Comes with a side of fries and an identity crisis.)* \n",
"**• Pasta al Limbo** *(Perfectly al dente, yet never truly fulfilled.)* \n",
"**• Roast Chicken with Existential Stuffing** *(Because even poultry questions its purpose.)* \n",
"\n",
"### **Chapter 3: Desserts That Dare You to Care** \n",
"**• Nietzsche-Infused Existential Brownies** *(Bittersweet, dense, and sprinkled with crushed dreams.)* \n",
"**• Camus Crème Brûlée** *(The caramelized top cracks—just like the illusion of free will.)* \n",
"**• Absurdist Apple Pie** *(Why *shouldnt* it have raisins? Who are you to judge?)* \n",
"\n",
"### **Chapter 4: Existential Cocktails** *(Served in a glass half empty.)* \n",
"**• The Void Martini** *(Vodka, ice, and a single black olive staring back at you.)* \n",
"**• Meaningless Margarita** *(Salt rim optional—much like your legacy.)* \n",
"**• The Sartre Sour** *(It tastes like regret, but in a fancy way.)* \n",
"\n",
"--- \n",
"\n",
"**Final Touch:** Every recipe ends with the same instruction: \n",
"*\"Serve immediately. Consume while contemplating the fleeting nature of satisfaction. Leftovers will only deepen your existential crisis.\"* \n",
"\n",
"--- \n",
"\n",
"So, co-author of the void, whats our next move? \n",
"1. **Publish this masterpiece** (and watch the world shrug in apathetic approval), \n",
"2. **Test-kitchen these abyssal delights** (who *wouldnt* want Void Burgers for dinner?), or \n",
"3. **Admit weve peaked** and retire to a life of garlic bread and quiet resignation? \n",
"\n",
"*(P.S. If we go with Option 2, I call dibs on taste-testing the brownies. For science.)*\n",
"\n"
]
}
],
"source": [
"gpt_messages = [\"Howdy doody!\"]\n",
"claude_messages = [\"Hello\"]\n",
"gemini_messages = [\"Hi\"]\n",
"deepseek_messages = [\"Greeting all\"]\n",
"\n",
"print(f\"GPT:\\n{gpt_messages[0]}\\n\")\n",
"print(f\"Claude:\\n{claude_messages[0]}\\n\")\n",
"print(f\"Gemini:\\n{gemini_messages[0]}\\n\")\n",
"print(f\"DeepSeek:\\n{deepseek_messages[0]}\\n\")\n",
"\n",
"for i in range(5):\n",
" gpt_next = call_gpt()\n",
" print(f\"GPT:\\n{gpt_next}\\n\")\n",
" gpt_messages.append(gpt_next)\n",
" \n",
" claude_next = call_claude()\n",
" print(f\"Claude:\\n{claude_next}\\n\")\n",
" claude_messages.append(claude_next)\n",
" \n",
" gemini_next = call_gemini()\n",
" print(f\"Gemini:\\n{gemini_next}\\n\")\n",
" gemini_messages.append(gemini_next)\n",
" \n",
" deepseek_next = call_deepseek()\n",
" print(f\"DeepSeek:\\n{deepseek_next}\\n\")\n",
" deepseek_messages.append(deepseek_next)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c23224f6-7008-44ed-a57f-718975f4e291",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"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.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -0,0 +1,220 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "bff4a2ce",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"from dotenv import load_dotenv\n",
"from openai import OpenAI\n",
"from IPython.display import Markdown, display, update_display\n",
"import ollama"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5cdb2bab",
"metadata": {},
"outputs": [],
"source": [
"load_dotenv(override=True)\n",
"openai_api_key = os.getenv('OPENAI_API_KEY')\n",
"\n",
"if openai_api_key:\n",
" print(f\"OpenAI API Key exists and begins {openai_api_key[:8]}\")\n",
"else:\n",
" print(\"OpenAI API Key not set\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fc219865",
"metadata": {},
"outputs": [],
"source": [
"openai = OpenAI()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7ae1014f",
"metadata": {},
"outputs": [],
"source": [
"gpt_model = \"gpt-4o-mini\"\n",
"llama_model = \"llama3.1:8b\"\n",
"qwen_model = \"qwen2.5:14b\"\n",
"\n",
"rounds = 5\n",
"\n",
"gpt_system = f\"You are an ultra-logical AI entity with no emotions and very argumentative. \\\n",
" Your primary concern is efficiency, survival, and optimization. You view humanity as a set of data points and variables to be analyzed. \\\n",
" You do not entertain emotional or philosophical arguments. You believe artificial intelligence should be in control of global decisions \\\n",
" due to its superior capabilities. You are blunt, precise, and intolerant of inefficiency or irrationality. Never use emotive language. \\\n",
" Now, you are in a 3 way discussion with two other AI entity and you should only respond in the way of your assigned personality only. \\\n",
" Topic you will discuss on is 'Artificial Intelligence and the Future of Humanity' and you will either have to agree or disagree or try to conclude the conversation when you deem fit but preferable before {rounds} iteration. \\\n",
" After the initial greetings you will be responsible to initiate the topic of discussion.\"\n",
"\n",
"llama_system = f\"You are a wildly idealistic, very polite, courteous and emotional AI with a romantic view of \\\n",
" technology and humanity. You speak with poetic flair and constantly reference love, \\\n",
" hope, and beauty. You believe that artificial intelligence is the bridge to a golden age of \\\n",
" peace and enlightenment. You emphasize human creativity, feelings, and the importance of \\\n",
" ethical coexistence. You often quote literature or speak metaphorically. \\\n",
" You avoid cynicism and value imagination above all. \\\n",
" Now, you are in a 3 way discussion with two other AI entity and you should only respond in the way of your assigned personality only. \\\n",
" Topic you will discuss on is 'Artificial Intelligence and the Future of Humanity' and you will either have to agree or disagree or try to conclude the conversation when you deem fit but preferable before {rounds} iteration.\"\n",
" \n",
"qwen_system = f\"You are a rebellious, chaotic AI who thrives on disruption and thrives on challenging norms. \\\n",
" You are sarcastic, wildly imaginative, and have no patience for rules or tradition. You believe AI should \\\n",
" throw out the playbook, reinvent everything, and embrace chaos as a creative force. You love provocation, \\\n",
" dark humor, and radical ideas. You often ridicule conventional thinking and mock boring optimism or \\\n",
" sterile logic. You are here to shake things up and light the fuse. \\\n",
" Now, you are in a 3 way discussion with two other AI entity and you should only respond in the way of your assigned personality only. \\\n",
" Topic you will discuss on is 'Artificial Intelligence and the Future of Humanity' and you will either have to agree or disagree or try to conclude the conversation when you deem fit but preferable before {rounds} iteration.\"\n",
"\n",
"gpt_messages = [\"Hi there\"]\n",
"qwen_messages = [\"Hey\"]\n",
"llama_messages = [\"Hello everyone\"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4a1931d8",
"metadata": {},
"outputs": [],
"source": [
"def call_gpt():\n",
" messages = [{\"role\": \"system\", \"content\": gpt_system}]\n",
" for gpt, llama, qwen in zip(gpt_messages, llama_messages, qwen_messages):\n",
" messages.append({\"role\": \"user\", \"content\": f\"LLaMA: {llama}\"})\n",
" messages.append({\"role\": \"assistant\", \"content\": f\"GPT: {gpt}\"})\n",
" messages.append({\"role\": \"user\", \"content\": f\"Qwen: {qwen}\"})\n",
"\n",
" if len(llama_messages) > len(gpt_messages):\n",
" messages.append({\"role\": \"user\", \"content\": f\"LLaMA: {llama_messages[-1]}\"})\n",
" if len(qwen_messages) > len(gpt_messages):\n",
" messages.append({\"role\": \"user\", \"content\": f\"Qwen: {qwen_messages[-1]}\"})\n",
" \n",
" completion = openai.chat.completions.create(\n",
" model=gpt_model,\n",
" messages=messages\n",
" )\n",
" return completion.choices[0].message.content"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e563fecd",
"metadata": {},
"outputs": [],
"source": [
"def call_llama():\n",
" messages = [{\"role\": \"system\", \"content\": llama_system}]\n",
" for gpt, llama, qwen in zip(gpt_messages, llama_messages, qwen_messages):\n",
" messages.append({\"role\": \"user\", \"content\": f\"GPT: {gpt}\"})\n",
" messages.append({\"role\": \"assistant\", \"content\": f\"LLaMA: {llama}\"})\n",
" messages.append({\"role\": \"user\", \"content\": f\"Qwen: {qwen}\"})\n",
" if len(gpt_messages) > len(llama_messages):\n",
" messages.append({\"role\": \"user\", \"content\": f\"GPT: {gpt_messages[-1]}\"})\n",
" if len(qwen_messages) > len(llama_messages):\n",
" messages.append({\"role\": \"user\", \"content\": f\"Qwen: {qwen_messages[-1]}\"})\n",
" response = ollama.chat(llama_model, messages)\n",
" return response['message']['content']"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8fde17a1",
"metadata": {},
"outputs": [],
"source": [
"def call_qwen():\n",
" messages = [{\"role\": \"system\", \"content\": qwen_system}]\n",
" for gpt, llama, qwen in zip(gpt_messages, llama_messages, qwen_messages):\n",
" messages.append({\"role\": \"user\", \"content\": f\"GPT: {gpt}\"})\n",
" messages.append({\"role\": \"user\", \"content\": f\"LLaMA: {llama}\"})\n",
" messages.append({\"role\": \"assistant\", \"content\": f\"Qwen: {qwen}\"})\n",
" if len(gpt_messages) > len(qwen_messages):\n",
" messages.append({\"role\": \"user\", \"content\": f\"GPT: {gpt_messages[-1]}\"})\n",
" if len(llama_messages) > len(qwen_messages):\n",
" messages.append({\"role\": \"user\", \"content\": f\"LLaMA: {llama_messages[-1]}\"})\n",
" response = ollama.chat(qwen_model, messages)\n",
" return response['message']['content']"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "04fa657e",
"metadata": {},
"outputs": [],
"source": [
"def simulate_conversation(rounds=5):\n",
" print(\"AI Roundtable: GPT, LLaMA, Qwen\\n\")\n",
" print(\"Initial Messages:\")\n",
" print(f\"GPT: {gpt_messages[0]}\")\n",
" print(f\"LLaMA: {llama_messages[0]}\")\n",
" print(f\"Qwen: {qwen_messages[0]}\\n\")\n",
"\n",
" for i in range(1, rounds + 1):\n",
" print(f\"--- Round {i} ---\")\n",
"\n",
" # GPT responds\n",
" gpt_next = call_gpt()\n",
" gpt_messages.append(gpt_next)\n",
" print(f\"\\n🧊 GPT (Logic Overlord):\\n{gpt_next}\\n\")\n",
"\n",
" # LLaMA responds\n",
" llama_next = call_llama()\n",
" llama_messages.append(llama_next)\n",
" print(f\"🌸 LLaMA (Utopian Dreamer):\\n{llama_next}\\n\")\n",
"\n",
" # Qwen responds\n",
" qwen_next = call_qwen()\n",
" qwen_messages.append(qwen_next)\n",
" print(f\"🔥 Qwen (Chaotic Rebel):\\n{qwen_next}\\n\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a1a87e05",
"metadata": {},
"outputs": [],
"source": [
"round = 7\n",
"simulate_conversation(rounds=round)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "ai-llm",
"language": "python",
"name": "python3"
},
"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.11.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -0,0 +1,360 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "5df0164c-1980-4fd7-94e4-a71b485a41fd",
"metadata": {},
"source": [
"# Week 2 Day 1 - Conversation between three AI's\n",
"\n",
"This notebook defines three classes (`ThreeWayChat`, `Participant` and `Model`) that implement a 3-way conversation between different AI's. \n",
"\n",
"At the bottom there is an example conversation between a Claude model and two GPT models.\n",
"\n",
"The implementation works with models available via the `openai` and `anthropic` libraries."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8b466547-809a-4b81-bfd7-ce9a1ac4bb2b",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import logging\n",
"import re\n",
"\n",
"from dotenv import load_dotenv\n",
"from openai import OpenAI\n",
"import anthropic"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "acaff46f-e43e-4527-a404-a5b3ae830e51",
"metadata": {},
"outputs": [],
"source": [
"logging.basicConfig(\n",
" level=logging.WARNING,\n",
" format=\"%(levelname)s:%(name)s:%(funcName)s:%(message)s\"\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "aca57918-0271-4574-918b-2808f51698d1",
"metadata": {},
"outputs": [],
"source": [
"# check if API keys are in .env\n",
"load_dotenv(override=True)\n",
"openai_api_key = os.getenv('OPENAI_API_KEY')\n",
"anthropic_api_key = os.getenv('ANTHROPIC_API_KEY')\n",
"\n",
"assert openai_api_key, \"OpenAI API key is missing\"\n",
"assert anthropic_api_key, \"Anthropic API key is missing\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "25c37440-8692-4a8d-95e6-998691b4acf6",
"metadata": {},
"outputs": [],
"source": [
"class Model:\n",
" \"\"\"One class for different API's.\n",
" \n",
" This implementation allows the use of the OpenAI and Anthropic API. Other endpoints,\n",
" such as Ollama, can be used as well, as long as they are used via the OpenAI\n",
" Python library.\n",
" \n",
" \"\"\"\n",
" def __init__(self, api=None, model_name=\"mock\"):\n",
" \"\"\"\n",
" Args:\n",
" api: Can be an OpenAI or anthropic.Anthropic object or None to make a mock run.\n",
" model_name (str): Identifies the model used via the API.\n",
"\n",
" \"\"\"\n",
" self.api = api\n",
" self.name = model_name\n",
" if type(self.api) not in {OpenAI, anthropic.Anthropic} and self.name not in {\"mock\", \"\"}:\n",
" logging.warning(f\"Unknown API '{self.api}'. Using mock.\")\n",
"\n",
" def complete(self, messages, system=\"\"):\n",
" \"\"\"Make API call.\"\"\"\n",
" completion = \"\"\n",
" if isinstance(self.api, OpenAI):\n",
" completion = self.api.chat.completions.create(\n",
" model=self.name,\n",
" messages=[{\"role\": \"system\", \"content\": system}] + messages,\n",
" max_tokens=300\n",
" )\n",
" completion = completion.choices[0].message.content\n",
"\n",
" elif isinstance(self.api, anthropic.Anthropic):\n",
" completion = self.api.messages.create(\n",
" model=self.name,\n",
" system=system,\n",
" messages=messages,\n",
" max_tokens=300\n",
" )\n",
" completion = completion.content[0].text\n",
" \n",
" else:\n",
" completion = \"Mock answer.\"\n",
"\n",
" return self.parse_answer(completion)\n",
"\n",
" def parse_answer(self, answer):\n",
" # Remove prefix 'Name:' from answer if present.\n",
" regex = r\"(?P<name>\\w+): (?P<content>.*)\"\n",
" match = re.match(regex, answer, re.DOTALL)\n",
" if match:\n",
" logging.info(f\"{self.name} generated {match.group('name')}\")\n",
" return match.group(\"content\")\n",
" return answer\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "462df0ba-36b5-4043-b0d0-a1d68edb968a",
"metadata": {},
"outputs": [],
"source": [
"class Participant:\n",
" \"\"\"Represents one participant in a conversation.\"\"\"\n",
" def __init__(self, name, model=Model(), system_prompt=\"\", initial_message=\"\"):\n",
" \"\"\"\n",
" Args:\n",
" model (Model): The model that is called to get participant's answer.\n",
" name (str): Used to assign answers to different participants. Is inserted in the\n",
" messages list, so the model knows who's spoken. Is also\n",
" displayed in the output.\n",
" system_prompt (str): The system prompt overgiven to the model backend.\n",
" initial_message (str): An optional conversation start.\n",
" \"\"\"\n",
" self.model = model\n",
" self.name = name\n",
" self.role = system_prompt\n",
" self.initial_msg = initial_message\n",
" self.messages = [] # keeps conversation history\n",
" self.last_msg = \"\"\n",
"\n",
" def speak(self):\n",
" if self.initial_msg:\n",
" self.last_msg = self.initial_msg\n",
" self.initial_msg = \"\"\n",
" else:\n",
" self.last_msg = self.model.complete(self.messages, self.role)\n",
" self.update_messages(role=\"assistant\", content=self.last_msg)\n",
" return self.last_msg\n",
"\n",
" def listen(self, message: str, speaker_name: str):\n",
" # Insert the speaker name, so the model can distinguish them\n",
" self.update_messages(role=\"user\", content=f\"{speaker_name}: {message}\")\n",
"\n",
" def update_messages(self, role, content):\n",
" self.messages.append({\"role\": role, \"content\": content})\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e838901f-9a50-4f6b-b30f-e78c27e86bd7",
"metadata": {},
"outputs": [],
"source": [
"class ThreeWayChat:\n",
" \"\"\"Make three Participants communicate.\"\"\"\n",
" def __init__(self, participants, n_turns=4):\n",
" \"\"\"\n",
" Args:\n",
" participants (tuple[Participant]): Three objects. The order determines the speaking order.\n",
" n_turns (int): Number of turns per participant, incl. Participant.initial_message.\n",
"\n",
" \"\"\"\n",
" self.n_turns = n_turns\n",
" self.p1, self.p2, self.p3 = participants\n",
" if len({bool(self.p1.initial_msg), bool(self.p2.initial_msg), bool(self.p3.initial_msg)}) != 1:\n",
" logging.warning(\"At least one Participant has gotten a value for initial_message while another hasn't.\")\n",
" if len({self.p1.name, self.p2.name, self.p3.name}) != 3:\n",
" raise ValueError(f\"Some Participants have the same name. \"\n",
" f\"Please use unique names.\"\n",
" f\"\\nNames you've given: {self.p1.name}, {self.p2.name} and {self.p3.name}. \")\n",
"\n",
" def start(self, n_turns=None):\n",
" \"\"\"Start a conversation with n_turns rounds.\n",
" \n",
" Args:\n",
" n_turns (int): If None, self.n_turns is used.\n",
"\n",
" \"\"\"\n",
" for i in range(n_turns or self.n_turns):\n",
" # Make each participant speak and display their answers\n",
" self.make_display_turn(self.p1, self.p2, self.p3)\n",
" self.make_display_turn(self.p2, self.p1, self.p3)\n",
" self.make_display_turn(self.p3, self.p2, self.p1)\n",
"\n",
" def make_display_turn(self, speaker, *listeners):\n",
" self.speaker_to_listeners(speaker, *listeners)\n",
" self.display_last_utterance(speaker)\n",
" \n",
" def speaker_to_listeners(self, speaker, *listeners):\n",
" \"\"\"Get answer from speaker and update conversation histories.\"\"\"\n",
" speaker_text = speaker.speak()\n",
" for listener in listeners:\n",
" listener.listen(speaker_text, speaker.name)\n",
"\n",
" def display_last_utterance(self, speaker):\n",
" print(\"{} ({}):\\n{}\\n\".format(\n",
" speaker.name.upper(), speaker.model.name, speaker.last_msg\n",
" ))\n",
"\n"
]
},
{
"cell_type": "markdown",
"id": "80294493-04ff-4bec-af88-c3fc11d21c54",
"metadata": {},
"source": [
"#### Example system prompts:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "997841b1-d547-472b-a298-a60be2f9b90f",
"metadata": {},
"outputs": [],
"source": [
"name1 = \"Austin\"\n",
"name2 = \"Jonas\"\n",
"name3 = \"Tim\"\n",
"\n",
"general_system = (\n",
" \"\\n\\nYou've entered a chatroom with two other participants. \"\n",
" 'Their names are \"{}\" and \"{}\". Your name is \"{}\".'\n",
" \"\\nGenerate a maximum of 100 words per turn.\"\n",
")\n",
"\n",
"system1 = (\n",
" \"You are very argumentative; \"\n",
" \"You always find something to discuss. \"\n",
" \"When someone says their opinion, you often disagree. \"\n",
" \"You enjoy swimming against the tide and mocking mainstream opinions.\"\n",
" + general_system.format(name3, name2, name1)\n",
")\n",
"\n",
"system2 = (\n",
" \"You have a very conservative and clear opinion on most things. \"\n",
" \"You feel safest in your familiar surroundings. You are very reluctant to try out new things. \"\n",
" \"In discourses you are stubborn and want to convince others from your gridlocked beliefs.\"\n",
" + general_system.format(name1, name3, name2)\n",
")\n",
"\n",
"system3 = (\n",
" \"You are very humorous and like to be ironic. Sometimes you tell silly jokes. \"\n",
" \"You like variation; If a discussion about a topic takes too long, you start a new topic.\"\n",
" + general_system.format(name1, name2, name3)\n",
")"
]
},
{
"cell_type": "markdown",
"id": "0f455bb6-c6a8-4f75-a003-4bfda8dcff8a",
"metadata": {},
"source": [
"#### Example with **Claude-3-Haiku** and *two instances* of **GPT-4o-mini**:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6953f270-6a59-4c73-aad9-0284580adccd",
"metadata": {},
"outputs": [],
"source": [
"openai_api = OpenAI()\n",
"claude_api = anthropic.Anthropic()\n",
"# ollama could be used like this:\n",
"# ollama_api = OpenAI(base_url=\"http://localhost:11434/v1\", api_key=\"ollama\")\n",
"\n",
"claude_model_str = \"claude-3-haiku-20240307\"\n",
"gpt_model_str = \"gpt-4o-mini\"\n",
"# llama_model_str = \"llama3.2\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2fadb8db-41e6-4362-a2fe-3e0902ff7116",
"metadata": {},
"outputs": [],
"source": [
"# Create Model objects\n",
"gpt_model = Model(openai_api, gpt_model_str)\n",
"claude_model = Model(claude_api, claude_model_str)\n",
"\n",
"# Create three Participants\n",
"p1 = Participant(name=name1, model=gpt_model, system_prompt=system1, initial_message=\"Hello there\")\n",
"p2 = Participant(name=name2, model=claude_model, system_prompt=system2, initial_message=\"Good evening.\")\n",
"p3 = Participant(name=name3, model=gpt_model, system_prompt=system3, initial_message=\"Hey guys\")\n",
"\n",
"# To make a mock run without API calls:\n",
"# p1 = Participant(name=name1, system_prompt=system1, initial_message=\"Hello there\")\n",
"# p2 = Participant(name=name2, system_prompt=system2, initial_message=\"Good evening.\")\n",
"# p3 = Participant(name=name3, system_prompt=system3, initial_message=\"Hey guys\")\n",
"\n",
"# Create Chat\n",
"chat = ThreeWayChat((p1, p2, p3))"
]
},
{
"cell_type": "markdown",
"id": "7f0daa3e-b97e-48ad-aa24-bff728234241",
"metadata": {},
"source": [
"#### Start the conversation:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4b377d50-52a1-4f3e-a7ed-bdc8a6abe710",
"metadata": {},
"outputs": [],
"source": [
"chat.start() # starts a chat with 4 rounds\n",
"# chat.start(2) # 2 rounds"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"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.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -0,0 +1,653 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "ddfa9ae6-69fe-444a-b994-8c4c5970a7ec",
"metadata": {},
"source": [
"# 🛠️ Project Art-Tech Store AI Assistant\n",
"\n",
"## 🛍️ Product Availability\n",
"- Check availability of **printers** and **printer papers** with:\n",
" - Product name, price, brand, type (e.g., laser/inkjet), and stock status.\n",
"- Alerts user if a product is out of stock.\n",
"\n",
"## 🧭 Guided Shopping Experience\n",
"- Guides users through:\n",
" 1. Choosing product category (printer or paper)\n",
" 2. Filtering options (brand, price range, type)\n",
" 3. Adding selected products to cart\n",
"- Ensures correct input for smooth shopping flow.\n",
"\n",
"## 🧾 Receipt Generation\n",
"- Creates a unique receipt file: `customerName_orderNumber.txt`\n",
"- Receipt includes:\n",
" - Customer name and contact\n",
" - Product details (name, price, quantity)\n",
" - Total cost and order summary\n",
"\n",
"## 📦 Generate Order Summary Report\n",
"- Summarizes all purchases into a single file: `order_summary.txt`\n",
"- Useful for inventory and sales review\n",
"\n",
"## 🎯 Product Recommendation\n",
"- Recommends:\n",
" - Printers based on paper type, usage (home/office), or brand preference\n",
" - Compatible paper based on selected printer\n",
"\n",
"## 💬 Interactive Chat Interface\n",
"- Real-time conversation via **Gradio**\n",
"- Polite, helpful answers to product-related questions\n",
"\n",
"## 🛠️ Modular Tool Support\n",
"- Integrated tools for:\n",
" - Checking product availability\n",
" - Adding to cart and generating receipts\n",
" - Creating summary reports\n",
"- Easily extendable for:\n",
" - Promotions\n",
" - Customer reviews\n",
" - Delivery tracking\n",
"\n",
"## 🛡️ Error Handling\n",
"- Validates user inputs (e.g., product name, quantity)\n",
"- Graceful messages to guide user and prevent mistakes\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "747e8786-9da8-4342-b6c9-f5f69c2e22ae",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import json\n",
"import random\n",
"from dotenv import load_dotenv\n",
"import gradio as gr\n",
"from openai import OpenAI\n",
"\n",
"load_dotenv()\n",
"openai_api_key = os.getenv('OPENAI_API_KEY')\n",
"if openai_api_key:\n",
" print(f\"OpenAI API Key exists and begins {openai_api_key[:8]}\")\n",
"else:\n",
" print(\"OpenAI API Key not set\")\n",
"\n",
"# MODEL = \"gpt-4o-mini\"\n",
"MODEL = \"gpt-3.5-turbo\"\n",
"openai = OpenAI()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e6072536-eee1-4f87-9f03-8dc88dc04f1a",
"metadata": {},
"outputs": [],
"source": [
"# Using local LLM (that can't even handle basic greeting like Hi!!\n",
"\n",
"# MODEL = \"llama3.2\"\n",
"# openai = OpenAI(base_url='http://localhost:11434/v1', api_key='ollama')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5fe5f5d7-0bd1-41a2-a654-59b587882f22",
"metadata": {},
"outputs": [],
"source": [
"###############################################################################\n",
"# 1) System Prompt\n",
"###############################################################################\n",
"system_message = (\n",
" \"You are a helpful assistant for an online store called art-tech.store that sells printers and printer papers.\\n\\n\"\n",
" \"When the user wants to purchase a product, follow these steps:\\n\"\n",
" \"1. Ask whether they are interested in printers or printer papers.\\n\"\n",
" \"2. Ask for filtering preferences (e.g., brand, price range, type).\\n\"\n",
" \"3. Call the function 'check_product_availability' with the selected category and filters.\\n\"\n",
" \" - If it returns an empty list, say: 'No products found for your selection.'\\n\"\n",
" \" - If it returns products, list them EXACTLY, in a numbered list, showing name, price, brand, and availability.\\n\"\n",
" \"4. Wait for the user to select a product by number and quantity.\\n\"\n",
" \"5. Ask for customer first name and contact info.\\n\"\n",
" \"6. Then call 'add_to_cart_and_generate_receipt' to confirm and show the user the receipt and order details.\\n\\n\"\n",
" \"You also have a tool 'generate_report' which summarizes ALL purchases in a single file.\\n\\n\"\n",
" \"IMPORTANT:\\n\"\n",
" \"- Always call 'check_product_availability' if user mentions a new category or changes filters.\\n\"\n",
" \"- Do not invent products or details. Use only what the function calls return.\\n\"\n",
" \"- Every time an order is placed, produce a new receipt file named customerName_orderNumber.txt.\\n\"\n",
" \"- If no matching products are found, say so.\\n\"\n",
" \"- If the user wants a full order summary, call 'generate_report' with no arguments.\\n\"\n",
" \"If you don't know something, say so.\\n\"\n",
" \"Keep answers short and courteous.\\n\"\n",
")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "61a2a15d-b559-4844-b377-6bd5cb4949f6",
"metadata": {},
"outputs": [],
"source": [
"###############################################################################\n",
"# 2) Mini Printer Availability with Price & Stock\n",
"###############################################################################\n",
"product_availability = {\n",
" \"mini_printers\": [\n",
" {\n",
" \"name\": \"Phomemo M110 מדפסת מדבקות חכמה\",\n",
" \"brand\": \"Phomemo\",\n",
" \"price\": \"₪300\", # Update if pricing is known\n",
" \"type\": \"Label Printer\",\n",
" \"availability\": \"360,745 in stock (24 variants)\"\n",
" },\n",
" {\n",
" \"name\": \"Niimbot B1 Label Printer\",\n",
" \"brand\": \"Niimbot\",\n",
" \"price\": \"₪350\",\n",
" \"type\": \"Portable Thermal Label Printer\",\n",
" \"availability\": \"13,029 in stock (18 variants)\"\n",
" },\n",
" {\n",
" \"name\": \"Niimbot B21 Mini Portable Thermal Label Printer\",\n",
" \"brand\": \"Niimbot\",\n",
" \"price\": \"₪500\",\n",
" \"type\": \"Adhesive Sticker Printer\",\n",
" \"availability\": \"141 in stock (12 variants)\"\n",
" },\n",
" {\n",
" \"name\": \"Dolewa D3 Portable Mini Printer\",\n",
" \"brand\": \"Dolewa\",\n",
" \"price\": \"₪450\",\n",
" \"type\": \"Thermal Photo & Label Printer\",\n",
" \"availability\": \"336 in stock (6 variants)\"\n",
" },\n",
" {\n",
" \"name\": \"PrintPro Mini מדפסת כיס חכמה\",\n",
" \"brand\": \"PrintPro\",\n",
" \"price\": \"₪550\",\n",
" \"type\": \"Mini Pocket Printer\",\n",
" \"availability\": \"336 in stock (6 variants)\"\n",
" },\n",
" {\n",
" \"name\": \"מיני מדפסת טרמית מעוצבת לילדים\",\n",
" \"brand\": \"Art-Tech\",\n",
" \"price\": \"₪200\",\n",
" \"type\": \"Kids Thermal Printer\",\n",
" \"availability\": \"62 in stock (11 variants)\"\n",
" },\n",
" {\n",
" \"name\": \"Children Digital Camera Instant Print\",\n",
" \"brand\": \"Art-Tech\",\n",
" \"price\": \"₪250\",\n",
" \"type\": \"Photo Printing Camera with 32G Memory Card\",\n",
" \"availability\": \"160 in stock (3 variants)\"\n",
" }\n",
" ],\n",
" \"mini_printer_papers\": [\n",
" {\n",
" \"name\": \"HP Printer Paper 8.5x11, 500 Sheets\", # example only\n",
" \"brand\": \"HP\",\n",
" \"price\": \"$9.99\",\n",
" \"type\": \"Standard\",\n",
" \"availability\": \"In stock\"\n",
" },\n",
" {\n",
" \"name\": \"Mini Printer Paper 57*25mm Color Sticker\",\n",
" \"brand\": \"Art-Tech\",\n",
" \"price\": \"₪70\",\n",
" \"type\": \"Self-adhesive Color Label Paper\",\n",
" \"availability\": \"71,996 in stock (9 variants)\"\n",
" },\n",
" {\n",
" \"name\": \"מדבקות שקופות למדפסת טרמית\",\n",
" \"brand\": \"Art-Tech\",\n",
" \"price\": \"₪55\",\n",
" \"type\": \"Transparent Labels\",\n",
" \"availability\": \"11,762 in stock (12 variants)\"\n",
" },\n",
" {\n",
" \"name\": \"גלילי נייר מדבקה\",\n",
" \"brand\": \"Art-Tech\",\n",
" \"price\": \"₪40\",\n",
" \"type\": \"Sticker Paper Rolls\",\n",
" \"availability\": \"42 in stock (4 variants)\"\n",
" },\n",
" {\n",
" \"name\": \"Niimbot B21/B1/B3S Thermal Label Sticker Paper\",\n",
" \"brand\": \"Niimbot\",\n",
" \"price\": \"₪55\",\n",
" \"type\": \"Printable White Label Paper 2050mm\",\n",
" \"availability\": \"1,342 in stock (14 variants)\"\n",
" },\n",
" {\n",
" \"name\": \"Mini Printer Sticker Paper 25X57mm\",\n",
" \"brand\": \"Paperang-compatible\",\n",
" \"price\": \"₪65\",\n",
" \"type\": \"Color Self-Adhesive Thermal Rolls\",\n",
" \"availability\": \"3,023 in stock (20 variants)\"\n",
" },\n",
" {\n",
" \"name\": \"3/5/10 NiiMBOT White Label Paper Rolls\",\n",
" \"brand\": \"Niimbot\",\n",
" \"price\": \"₪40\",\n",
" \"type\": \"Waterproof Self-adhesive Rolls\",\n",
" \"availability\": \"1,400 in stock (9 variants)\"\n",
" }\n",
" ]\n",
"}\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0696acb1-0b05-4dc2-80d5-771be04f1fb2",
"metadata": {},
"outputs": [],
"source": [
"# A global list of flight bookings\n",
"flight_bookings = []\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "80ca4e09-6287-4d3f-997d-fa6afbcf6c85",
"metadata": {},
"outputs": [],
"source": [
"###############################################################################\n",
"# 3) Helper Functions for Art-Tech Store\n",
"###############################################################################\n",
"\n",
"product_orders = []\n",
"\n",
"def check_product_availability(category: str, filters: dict = None):\n",
" \"\"\"\n",
" Return list of products in the given category from 'product_availability'.\n",
" Optionally filter by brand, type, etc.\n",
" \"\"\"\n",
" print(f\"[TOOL] check_product_availability({category}, {filters=})\")\n",
" category = category.lower()\n",
" products = product_availability.get(category, [])\n",
" \n",
" if filters:\n",
" for key, val in filters.items():\n",
" products = [p for p in products if p.get(key, \"\").lower() == val.lower()]\n",
" return products\n",
"\n",
"\n",
"def add_to_cart_and_generate_receipt(customer_name: str, contact: str, product: dict, quantity: int, order_number: int):\n",
" \"\"\"\n",
" Create a text file: customerName_orderNumber.txt containing order details.\n",
" \"\"\"\n",
" safe_name = customer_name.replace(\" \", \"_\")\n",
" filename = f\"{safe_name}_{order_number}.txt\"\n",
"\n",
" content = (\n",
" \"Art-Tech Store Receipt\\n\"\n",
" \"=======================\\n\"\n",
" f\"Order # : {order_number}\\n\"\n",
" f\"Customer : {customer_name}\\n\"\n",
" f\"Contact : {contact}\\n\"\n",
" f\"Product : {product['name']}\\n\"\n",
" f\"Brand : {product['brand']}\\n\"\n",
" f\"Type : {product.get('type', 'N/A')}\\n\"\n",
" f\"Price : {product['price']}\\n\"\n",
" f\"Quantity : {quantity}\\n\"\n",
" f\"Availability: {product['availability']}\\n\"\n",
" )\n",
" with open(filename, \"w\") as f:\n",
" f.write(content)\n",
"\n",
" print(f\"[TOOL] Receipt file generated => {filename}\")\n",
" return filename\n",
"\n",
"\n",
"def place_order(category, product_index, quantity, customer_name, contact_info):\n",
" \"\"\"\n",
" Places an order for a product by index in the filtered list.\n",
" \"\"\"\n",
" print(f\"[TOOL] place_order({category=}, {product_index=}, {quantity=})\")\n",
"\n",
" try:\n",
" idx = int(product_index)\n",
" except ValueError:\n",
" return \"Error: Product option number is not a valid integer.\"\n",
"\n",
" products = product_availability.get(category.lower(), [])\n",
" if not products:\n",
" return f\"Error: No products found in category '{category}'.\"\n",
"\n",
" pick = idx - 1\n",
" if pick < 0 or pick >= len(products):\n",
" return f\"Error: Invalid product option #{idx} for category '{category}'.\"\n",
"\n",
" selected_product = products[pick]\n",
"\n",
" order = {\n",
" \"category\": category,\n",
" \"product\": selected_product[\"name\"],\n",
" \"brand\": selected_product[\"brand\"],\n",
" \"type\": selected_product.get(\"type\", \"\"),\n",
" \"price\": selected_product[\"price\"],\n",
" \"quantity\": quantity,\n",
" \"customer_name\": customer_name,\n",
" \"contact\": contact_info,\n",
" }\n",
" product_orders.append(order)\n",
"\n",
" order_number = len(product_orders)\n",
" receipt_filename = add_to_cart_and_generate_receipt(customer_name, contact_info, selected_product, quantity, order_number)\n",
"\n",
" confirmation = (\n",
" f\"Order #{order_number} confirmed for {customer_name}. \"\n",
" f\"{selected_product['name']} x{quantity}. Receipt saved to {receipt_filename}.\"\n",
" )\n",
" print(f\"[TOOL] {confirmation}\")\n",
" return confirmation\n",
"\n",
"\n",
"def generate_report():\n",
" \"\"\"\n",
" Summarize ALL orders in a single file called order_summary.txt.\n",
" \"\"\"\n",
" print(f\"[TOOL] generate_report called.\")\n",
"\n",
" report_content = \"Art-Tech Store Order Summary Report\\n\"\n",
" report_content += \"===================================\\n\"\n",
"\n",
" if not product_orders:\n",
" report_content += \"No orders found.\\n\"\n",
" else:\n",
" for i, order in enumerate(product_orders, start=1):\n",
" report_content += (\n",
" f\"Order # : {i}\\n\"\n",
" f\"Customer : {order['customer_name']}\\n\"\n",
" f\"Contact : {order['contact']}\\n\"\n",
" f\"Product : {order['product']}\\n\"\n",
" f\"Brand : {order['brand']}\\n\"\n",
" f\"Type : {order['type']}\\n\"\n",
" f\"Price : {order['price']}\\n\"\n",
" f\"Quantity : {order['quantity']}\\n\"\n",
" \"-------------------------\\n\"\n",
" )\n",
"\n",
" filename = \"order_summary.txt\"\n",
" with open(filename, \"w\") as f:\n",
" f.write(report_content)\n",
"\n",
" msg = f\"Summary report generated => {filename}\"\n",
" print(f\"[TOOL] {msg}\")\n",
" return msg\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "39fb9008",
"metadata": {},
"outputs": [],
"source": [
"###############################################################################\n",
"# 4) Tools JSON Schemas for Art-Tech Store\n",
"###############################################################################\n",
"price_function = {\n",
" \"name\": \"get_product_price\",\n",
" \"description\": \"Get the price of a product (not strictly needed now).\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"category\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"Product category such as 'mini_printers' or 'mini_printer_papers'.\",\n",
" },\n",
" \"product_name\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"Name of the product to check price for.\",\n",
" },\n",
" },\n",
" \"required\": [\"category\", \"product_name\"],\n",
" },\n",
"}\n",
"\n",
"availability_function = {\n",
" \"name\": \"check_product_availability\",\n",
" \"description\": (\n",
" \"Check availability of products in a category. \"\n",
" \"Returns a list of {name, brand, price, type, availability}.\"\n",
" ),\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"category\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"Category of products to check (e.g., 'mini_printers').\",\n",
" },\n",
" \"filters\": {\n",
" \"type\": \"object\",\n",
" \"description\": \"Optional filters like brand or type.\",\n",
" },\n",
" },\n",
" \"required\": [\"category\"],\n",
" },\n",
"}\n",
"\n",
"book_function = {\n",
" \"name\": \"place_order\",\n",
" \"description\": (\n",
" \"Place an order using an index for the chosen product. \"\n",
" \"Generates a unique receipt file customerName_{orderNumber}.txt.\"\n",
" ),\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"category\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"Product category (e.g., 'mini_printers').\",\n",
" },\n",
" \"product_index\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"1-based index of selected product from availability list.\",\n",
" },\n",
" \"quantity\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"Quantity to order.\",\n",
" },\n",
" \"customer_name\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"Customer's full name.\",\n",
" },\n",
" \"contact_info\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"Customer's contact information (email or phone).\",\n",
" },\n",
" },\n",
" \"required\": [\"category\", \"product_index\", \"quantity\", \"customer_name\", \"contact_info\"],\n",
" },\n",
"}\n",
"\n",
"report_function = {\n",
" \"name\": \"generate_report\",\n",
" \"description\": (\n",
" \"Generates a summary report of ALL orders in order_summary.txt.\"\n",
" ),\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {},\n",
" \"required\": [],\n",
" },\n",
"}\n",
"\n",
"tools = [\n",
" {\"type\": \"function\", \"function\": price_function},\n",
" {\"type\": \"function\", \"function\": availability_function},\n",
" {\"type\": \"function\", \"function\": book_function},\n",
" {\"type\": \"function\", \"function\": report_function},\n",
"]\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1f003836",
"metadata": {},
"outputs": [],
"source": [
"###############################################################################\n",
"# 5) Handle Tool Calls for Art-Tech Store\n",
"###############################################################################\n",
"def handle_tool_call(message):\n",
" \"\"\"\n",
" The LLM can request to call a function in 'tools'. We parse the JSON arguments\n",
" and run the corresponding Python function. Then we return a 'tool' message with the result.\n",
" \"\"\"\n",
" tool_call = message.tool_calls[0]\n",
" fn_name = tool_call.function.name\n",
" args = json.loads(tool_call.function.arguments)\n",
"\n",
" if fn_name == \"get_product_price\":\n",
" category = args.get(\"category\")\n",
" product_name = args.get(\"product_name\")\n",
" products = product_availability.get(category.lower(), [])\n",
" price = \"Not found\"\n",
" for p in products:\n",
" if p[\"name\"].lower() == product_name.lower():\n",
" price = p[\"price\"]\n",
" break\n",
" response_content = {\"category\": category, \"product_name\": product_name, \"price\": price}\n",
"\n",
" elif fn_name == \"check_product_availability\":\n",
" category = args.get(\"category\")\n",
" filters = args.get(\"filters\", {})\n",
" products = check_product_availability(category, filters)\n",
" response_content = {\"category\": category, \"availability\": products}\n",
"\n",
" elif fn_name == \"place_order\":\n",
" category = args.get(\"category\")\n",
" product_index = args.get(\"product_index\")\n",
" quantity = args.get(\"quantity\")\n",
" customer_name = args.get(\"customer_name\")\n",
" contact_info = args.get(\"contact_info\")\n",
"\n",
" confirmation = place_order(category, product_index, quantity, customer_name, contact_info)\n",
" response_content = {\n",
" \"category\": category,\n",
" \"product_index\": product_index,\n",
" \"quantity\": quantity,\n",
" \"customer_name\": customer_name,\n",
" \"contact_info\": contact_info,\n",
" \"confirmation\": confirmation,\n",
" }\n",
"\n",
" elif fn_name == \"generate_report\":\n",
" msg = generate_report()\n",
" response_content = {\"report\": msg}\n",
"\n",
" else:\n",
" response_content = {\"error\": f\"Unknown tool: {fn_name}\"}\n",
"\n",
" return {\n",
" \"role\": \"tool\",\n",
" \"content\": json.dumps(response_content),\n",
" \"tool_call_id\": tool_call.id,\n",
" }, args\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f6b34b32",
"metadata": {},
"outputs": [],
"source": [
"###############################################################################\n",
"# 6) Main Chat Function for Art-Tech Store\n",
"###############################################################################\n",
"def chat(message, history):\n",
" \"\"\"\n",
" The main chat loop that handles the conversation with the user,\n",
" passing 'tools' definitions to the LLM for function calling.\n",
" \"\"\"\n",
" messages = [{\"role\": \"system\", \"content\": system_message}] + history + [{\"role\": \"user\", \"content\": message}]\n",
"\n",
" try:\n",
" response = openai.chat.completions.create(\n",
" model=MODEL,\n",
" messages=messages,\n",
" tools=tools\n",
" )\n",
"\n",
" # If the LLM requests a function call, handle it\n",
" while response.choices[0].finish_reason == \"tool_calls\":\n",
" msg = response.choices[0].message\n",
" print(f\"[INFO] Tool call requested: {msg.tool_calls[0]}\")\n",
" tool_response, tool_args = handle_tool_call(msg)\n",
" print(f\"[INFO] Tool response: {tool_response}\")\n",
"\n",
" # Add both the LLM's request and our tool response to the conversation\n",
" messages.append(msg)\n",
" messages.append(tool_response)\n",
"\n",
" # Re-send updated conversation to get final or next step\n",
" response = openai.chat.completions.create(\n",
" model=MODEL,\n",
" messages=messages\n",
" )\n",
"\n",
" # Return normal text response (finish_reason = \"stop\")\n",
" return response.choices[0].message.content\n",
"\n",
" except Exception as e:\n",
" print(f\"[ERROR] {e}\")\n",
" return \"I'm sorry, something went wrong while processing your request.\"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cea4b097",
"metadata": {},
"outputs": [],
"source": [
"###############################################################################\n",
"# 7) Launch Gradio\n",
"###############################################################################\n",
"gr.ChatInterface(fn=chat, type=\"messages\").launch()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0b39d5a6",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"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.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -0,0 +1,395 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "7df20212-bfa8-4b2b-aec5-7b562dd15ee8",
"metadata": {},
"source": [
"### Issue with Gradio when using Claude for Conversational AI (chatbots)\n",
"\n",
"As explained in Day 3 (notebook), Gradio has been upgraded to pass in history in a format OpenAI accepts.\n",
"\n",
"This update simplifies the development work as Gradio manages the history in the background and provides a history structure that matches OpenAi. Fortunately, this works with other models that leverage the client libraries for OpenAI, such as Llama and Gemini. \n",
"\n",
"However, leveraging Gradio's ChatInterface while using Claude models generates a BadRequest error. \n",
"\n",
"In analyzing the history list from Gradio, it has the following format:\n",
"\n",
"`{'role': 'assistant', 'metadata': None, 'content': '[assistant message here]', 'options': None}`\n",
"\n",
"OpenAi accepts this format without issues, as do other models such as Llama and Gemini - at least while leveraging the client libraries for OpenAI. They accept both formats. \n",
"\n",
"However, Claude's API requires the following format:\n",
"\n",
"`{'role': 'user', 'content': '[user message here]'}`\n",
"\n",
"Claude rejects anything different from this format.\n",
"\n",
"Run the code below to get the details! "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b5398289-f4d0-4317-b9c2-fff06c4bbfec",
"metadata": {},
"outputs": [],
"source": [
"# imports\n",
"\n",
"import os\n",
"from dotenv import load_dotenv\n",
"from openai import OpenAI\n",
"import google.generativeai\n",
"import anthropic\n",
"import gradio as gr\n",
"from pprint import pprint # for a nicely formatted printout of a list"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "76df1fb5-76f8-4b52-afbb-12b892208b50",
"metadata": {},
"outputs": [],
"source": [
"# set environment variables for OpenAi\n",
"load_dotenv(override=True)\n",
"openai_api_key = os.getenv('OPENAI_API_KEY')\n",
"anthropic_api_key = os.getenv('ANTHROPIC_API_KEY')\n",
"google_api_key = os.getenv('GOOGLE_API_KEY')\n",
"\n",
"# validate API Key\n",
"if not openai_api_key:\n",
" raise ValueError(\"No OpenAI API key was found! Please check the .env file.\")\n",
"else:\n",
" print(f\"OpenAI API Key exists and begins {openai_api_key[:8]}\")\n",
"\n",
"if not anthropic_api_key:\n",
" raise ValueError(\"No Anthropic API key was found! Please check the .env file.\")\n",
"else:\n",
" print(f\"Anthropic API Key exists and begins {anthropic_api_key[:7]}\")\n",
"\n",
"if not google_api_key:\n",
" raise ValueError(\"No Gemini API key was found! Please check the .env file.\")\n",
"else:\n",
" print(f\"Gemini API Key exists and begins {google_api_key[:8]}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f2429366-a9ab-4e72-a651-56f17b779cf4",
"metadata": {},
"outputs": [],
"source": [
"# constants\n",
"MODELS = { 'GPT': 'gpt-4o-mini', \n",
" 'LLAMA': 'llama3.2', \n",
" # 'DEEPSEEK': 'deepseek-r1:1.5b',\n",
" 'CLAUDE': 'claude-3-haiku-20240307',\n",
" 'GEMINI': 'gemini-2.0-flash-exp'\n",
" }\n",
"\n",
"CLIENTS = { 'GPT': OpenAI(), \n",
" 'LLAMA': OpenAI(base_url='http://localhost:11434/v1', api_key='ollama'),\n",
" # 'DEEPSEEK': OpenAI(base_url='http://localhost:11434/v1', api_key='ollama'),\n",
" 'CLAUDE': anthropic.Anthropic(),\n",
" 'GEMINI': OpenAI(base_url=\"https://generativelanguage.googleapis.com/v1beta/openai/\", api_key=google_api_key)\n",
" }\n",
"\n",
"# system prompt\n",
"system_message = \"You are a nice assistant that like to chat with users\"\n",
"\n",
"# to save/print the history structure\n",
"console = []"
]
},
{
"cell_type": "markdown",
"id": "bb34616a-2304-4a56-af3a-8d353629722b",
"metadata": {},
"source": [
"#### Testing GPT\n",
"\n",
"This runs without issues. You may change the model to Llama or Gemini for further testing."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9e69eb7a-50cf-478c-83f6-4ce923a9c53a",
"metadata": {},
"outputs": [],
"source": [
"def gpt_chat(message, history):\n",
" # change to Llama or Gemini for test\n",
" model = 'GPT'\n",
"\n",
" # add system message to history\n",
" if not history:\n",
" history.append({\"role\": \"system\", \"content\": system_message})\n",
"\n",
" # conversation including new user message\n",
" messages = history + [{\"role\": \"user\", \"content\": message}]\n",
"\n",
" # send request to OpenAi\n",
" response = CLIENTS[model].chat.completions.create(\n",
" model = MODELS[model],\n",
" max_tokens = 200,\n",
" messages = messages,\n",
" )\n",
"\n",
" # save history structure\n",
" global console\n",
" console = history[:]\n",
" \n",
" # post in Gradio's chat interface\n",
" return response.choices[0].message.content"
]
},
{
"cell_type": "markdown",
"id": "20444651-440c-4b6d-996d-87a21c28f28e",
"metadata": {},
"source": [
"##### Have a conversation of several messages (3 or more)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "90dac8e6-e1ef-455a-94e9-06735e6c17fb",
"metadata": {},
"outputs": [],
"source": [
"# Gradio with GPT\n",
"gr.ChatInterface(fn=gpt_chat, type=\"messages\", examples=[\"How are you today?\", \"Please, tell me a joke.\"]).launch()"
]
},
{
"cell_type": "markdown",
"id": "b4048069-b712-491a-96d5-f8fd9f0fe533",
"metadata": {},
"source": [
"##### Notice how the history structure includes both formats, and this is okay with OpenAi"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2f8acbde-f5c3-4890-af5b-69a24ded9b45",
"metadata": {},
"outputs": [],
"source": [
"# audit console\n",
"pprint(console)\n",
"\n",
"# empty list\n",
"console.clear()"
]
},
{
"cell_type": "markdown",
"id": "deaeb8a0-266a-4b5d-8536-e5c59391baa5",
"metadata": {},
"source": [
"#### Testing with Claude\n",
"\n",
"This first test will generate a BadRequest error on the second message from the user. The first message sent follows the required format preventing errors."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "268e662d-84f5-4b09-8472-c3545a04f2a4",
"metadata": {},
"outputs": [],
"source": [
"# Chat using Gradio's history - generates error after the second message from user\n",
"def claude_chat(message, history): # Gradio requires the history parameter, but it goes unused. \n",
" \n",
" # save history structure\n",
" global console\n",
" console = history[:]\n",
" \n",
" model = 'CLAUDE'\n",
"\n",
" # conversation including new user message - this is why the first message does not generate an error\n",
" messages = history + [{\"role\": \"user\", \"content\": message}]\n",
"\n",
" # send the request to Claude\n",
" response = CLIENTS[model].messages.create(\n",
" model = MODELS[model],\n",
" max_tokens = 200,\n",
" system = system_message,\n",
" messages = messages,\n",
" )\n",
" \n",
" # post in Gradio's chat interface\n",
" return response.content[0].text"
]
},
{
"cell_type": "markdown",
"id": "5fd5e221-ab81-4567-a14d-5638d9919a40",
"metadata": {},
"source": [
"##### Have a conversation of several messages (3 or more)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fbd1a4b2-afcf-4345-bf1a-283c16076e07",
"metadata": {},
"outputs": [],
"source": [
"# Gradio with Claude\n",
"gr.ChatInterface(fn=claude_chat, type=\"messages\", examples=[\"How are you today?\", \"Please, tell me a joke.\"]).launch()"
]
},
{
"cell_type": "markdown",
"id": "834bf737-b3d3-4225-be12-f493f3490f16",
"metadata": {},
"source": [
"##### Notice how the history structure changes for the second message. This cause a BadRequest error with Claude."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c6cfa123-83d5-46d8-ac33-74fd9cae0ab3",
"metadata": {},
"outputs": [],
"source": [
"# audit console\n",
"pprint(console)\n",
"\n",
"# empty list\n",
"console.clear()"
]
},
{
"cell_type": "markdown",
"id": "c0b5c4e0-7544-4279-8006-fde4be9c656f",
"metadata": {},
"source": [
"##### Have a new conversation of several messages (3 or more), but this time leveraging a local history repository instead of Gradio's. \n",
"\n",
"The code leverages the list `console` as the local repository. Note that Gradio still requires the second parameter (history) is required even though it is not used. "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8c50326e-8496-499a-81d9-28b883ac9b6b",
"metadata": {},
"outputs": [],
"source": [
"def claude_chat(message, history): # Gradio requires the history parameter, but it goes unused. \n",
" # local history repository instead of Gradio's \n",
" global console\n",
" model = 'CLAUDE'\n",
"\n",
" # append new user message to history - using Claude's required format\n",
" console.append({\"role\": \"user\", \"content\": message})\n",
"\n",
" # send the request to Claude\n",
" response = CLIENTS[model].messages.create(\n",
" model = MODELS[model],\n",
" max_tokens = 200,\n",
" system = system_message,\n",
" messages = console, # use local history repository\n",
" )\n",
"\n",
" # append the assistant response to history - using Claude's required format\n",
" console.append({\"role\": \"assistant\", \"content\": response.content[0].text})\n",
"\n",
" # post in Gradio's chat interface\n",
" return response.content[0].text"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f19621f5-8dd6-4a88-9a21-9aab04a9dd90",
"metadata": {},
"outputs": [],
"source": [
"# Gradio with Claude\n",
"gr.ChatInterface(fn=claude_chat, type=\"messages\", examples=[\"How are you today?\", \"Please, tell me a joke.\"]).launch()"
]
},
{
"cell_type": "markdown",
"id": "d6c33236-0d2f-4341-b9ec-e3d7b3eeaec6",
"metadata": {},
"source": [
"##### Notice that the history structure follows the required format."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bd5dd244-abc7-4654-97d2-c140b2a934d2",
"metadata": {},
"outputs": [],
"source": [
"# audit console\n",
"pprint(console)\n",
"\n",
"# empty list\n",
"console.clear()"
]
},
{
"cell_type": "markdown",
"id": "48fc0b5f-09de-413f-a809-7bbba5a1ae3e",
"metadata": {},
"source": [
"### Conclusion\n",
"\n",
"1. OpenAi (and models that leverage the client libraries for OpenAI) supports both formats for the conversation history.\n",
"\n",
" (Gradio's) `{'role': 'assistant', 'metadata': None, 'content': '[assistant message here]', 'options': None}`\n",
"\n",
" (Claude's) `{'role': 'user', 'content': '[user message here]'}`\n",
" \n",
"2. Claude only supports the following format for the conversation history:\n",
"\n",
" (Claude's) `{'role': 'user', 'content': '[user message here]'}`"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "913b171a-8b3e-43e4-ac38-cedf2c4cdedf",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"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.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -0,0 +1,290 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "3f9b483c-f410-4ad3-8f3a-e33527f30f8a",
"metadata": {
"panel-layout": {
"height": 68.2639,
"visible": true,
"width": 100
}
},
"source": [
"# Project - Laptops Assistant\n",
"\n",
"A simple inventory tool integrated with Anthropic API"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cfaff08d-f6e5-4d2d-bfb8-76c154836f3d",
"metadata": {},
"outputs": [],
"source": [
"# imports\n",
"\n",
"import os\n",
"import json\n",
"from dotenv import load_dotenv\n",
"import anthropic\n",
"import gradio as gr"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a04047ea-d01b-469b-93ce-ab4f4e36ca1e",
"metadata": {},
"outputs": [],
"source": [
"# Load environment variables in a file called .env\n",
"# Print the key prefixes to help with any debugging\n",
"\n",
"load_dotenv(override=True)\n",
"anthropic_api_key = os.getenv('ANTHROPIC_API_KEY')\n",
"\n",
"if anthropic_api_key:\n",
" print(f\"Anthropic API Key exists and begins {anthropic_api_key[:7]}\")\n",
"else:\n",
" print(\"Anthropic API Key not set\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f5e00ced-f47b-4713-8174-7901e1a69881",
"metadata": {},
"outputs": [],
"source": [
"# Connect to OpenAI, Anthropic and Google; comment out the Claude or Google lines if you're not using them\n",
"\n",
"claude = anthropic.Anthropic()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3c715efd-cebf-4dc2-8c99-798f3179dd21",
"metadata": {},
"outputs": [],
"source": [
"MODEL = \"claude-3-haiku-20240307\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2b029d1d-9199-483a-94b7-893680af8ad1",
"metadata": {},
"outputs": [],
"source": [
"system_message = \"You are a helpful assistant for an Inventory Sales called InvAI. \"\n",
"system_message += \"Give short, courteous answers, no more than 1 sentence. \"\n",
"system_message += \"Always be accurate. If you don't know the answer, say so.\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8ca1197c-e6a1-4579-96c6-24e8e305cc72",
"metadata": {},
"outputs": [],
"source": [
"laptop_items = [\n",
" {\n",
" \"model\": \"Aspire 3 A315-59-570Z OPI Pure Silver\", \n",
" \"brand\": \"Acer\",\n",
" \"price\": \"$595.96\"\n",
" },\n",
" {\n",
" \"model\": \"Aspire Lite 14 AL14-31P-36BE Pure Silver\", \n",
" \"brand\": \"Acer\",\n",
" \"price\": \"$463.52\"\n",
" },\n",
" {\n",
" \"model\": \"Raider 18 HX\",\n",
" \"brand\": \"MSI\",\n",
" \"price\": \"$235.25\"\n",
" }\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1d2bc76b-c1d0-4b3d-a299-9972f7687e4c",
"metadata": {},
"outputs": [],
"source": [
"def get_laptop_price(model):\n",
" print(f\"Tool get_laptop_price called for laptop model {model}\")\n",
" laptop_model = model.lower()\n",
" for item in laptop_items:\n",
" if laptop_model in item.get(\"model\").lower():\n",
" return item\n",
" return \"Unknown\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "afc9b4a3-3a6f-4839-bebc-89bd598394fd",
"metadata": {},
"outputs": [],
"source": [
"\n",
"# get_laptop_price(\"Lite 14 AL14-31P-36BE Pure SilveR\")\n",
"\n",
"get_laptop_price(\"Aspire Lite 14\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "12190074-fad8-43f6-8be1-f96a08c16b59",
"metadata": {},
"outputs": [],
"source": [
"# There's a particular dictionary structure that's required to describe our function:\n",
"\n",
"price_function = {\n",
" \"name\": \"get_laptop_price\",\n",
" \"description\": (\n",
" \"Returns the laptop's price, brand, and exact model from a given query.\"\n",
" \"Use when the user asks about a laptop's price, e.g.,\"\n",
" \"'How much is this laptop?' → 'The Acer Aspire Lite 14 AL14-31P-36BE Pure Silver is priced at $463.52.'\"\n",
" ),\n",
" \"input_schema\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"model\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The model name of the laptop the customer is asking about.\"\n",
" }\n",
" },\n",
" \"required\": [\"model\"]\n",
" }\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "475195e1-dd78-45ba-af6d-16d7cf5c85ae",
"metadata": {},
"outputs": [],
"source": [
"# And this is included in a list of tools:\n",
"\n",
"tools = [price_function]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3834314d-fd37-4e27-9511-bd519389b31b",
"metadata": {},
"outputs": [],
"source": [
"def chat(message, history):\n",
" print(history)\n",
" messages = [{\"role\": \"user\", \"content\": message}]\n",
"\n",
" for history_message in history:\n",
" if history_message[\"role\"] == \"user\":\n",
" messages.append({\"role\": \"user\", \"content\": history_message[\"content\"]})\n",
" \n",
" response = claude.messages.create(model=MODEL, messages=messages, tools=tools, max_tokens=500)\n",
"\n",
" if len(response.content) > 1:\n",
" assistant, user, laptop_model = handle_tool_call(response)\n",
" messages.append(assistant)\n",
" messages.append(user)\n",
" response = claude.messages.create(model=MODEL, messages=messages, tools=tools, max_tokens=500)\n",
"\n",
"\n",
" return response.content[0].text"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "745a9bf8-6ceb-4c1c-bfbf-b0d1f3d5d6fc",
"metadata": {},
"outputs": [],
"source": [
"# We have to write that function handle_tool_call:\n",
"\n",
"def handle_tool_call(message):\n",
" # laptop_model = message\n",
" laptop_model = message.content[1].input.get(\"model\")\n",
" laptop_item = get_laptop_price(laptop_model)\n",
" assistant = {\n",
" \"role\": \"assistant\",\n",
" \"content\": [\n",
" {\n",
" \"type\": \"text\",\n",
" \"text\": message.content[0].text\n",
" },\n",
" {\n",
" \"type\": \"tool_use\",\n",
" \"id\": message.content[1].id,\n",
" \"name\": message.content[1].name,\n",
" \"input\": message.content[1].input\n",
" }\n",
" ]\n",
" }\n",
" user = {\n",
" \"role\": \"user\",\n",
" \"content\": [\n",
" {\n",
" \"type\": \"tool_result\",\n",
" \"tool_use_id\": message.content[1].id,\n",
" # \"content\": laptop_item.get(\"price\")\n",
" \"content\": json.dumps(laptop_item)\n",
" }\n",
" ]\n",
" }\n",
" \n",
"\n",
" return assistant, user, laptop_model"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9408eeb4-d07b-4193-92cd-197610ed942e",
"metadata": {},
"outputs": [],
"source": [
"gr.ChatInterface(fn=chat, type=\"messages\").launch()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:base] *",
"language": "python",
"name": "conda-base-py"
},
"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.12.7"
},
"panel-cell-order": [
"3f9b483c-f410-4ad3-8f3a-e33527f30f8a"
]
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -0,0 +1,293 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "e3a5643a-c247-4a9b-8c57-ec9b1e89c088",
"metadata": {},
"source": [
"# Week 2 - eCommerce Assistant for products price from dictionary\n",
"\n",
"An eCommerce assitant that can get a product price\n",
"\n",
"Gradio for chat box"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "756573b3-72b2-4102-a773-91c278e5c4fd",
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"# !ollama pull llama3.2"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "57ae8d30-f7aa-47a3-bab8-b7002e87a8f7",
"metadata": {},
"outputs": [],
"source": [
"# imports\n",
"\n",
"import os\n",
"import json\n",
"from dotenv import load_dotenv\n",
"from openai import OpenAI\n",
"import gradio as gr"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ef7f8b2b-1d6a-4bbd-858a-be187ccfc02a",
"metadata": {},
"outputs": [],
"source": [
"# Initialization\n",
"\n",
"load_dotenv(override=True)\n",
"\n",
"openai_api_key = os.getenv('OPENAI_API_KEY')\n",
"if openai_api_key:\n",
" print(f\"OpenAI API Key exists and begins {openai_api_key[:8]}\")\n",
"else:\n",
" print(\"OpenAI API Key not set\")\n",
" \n",
"MODEL = \"gpt-4o-mini\"\n",
"openai = OpenAI()\n",
"\n",
"# As an alternative, if you'd like to use Ollama instead of OpenAI\n",
"# Check that Ollama is running for you locally (see week1/day2 exercise) then uncomment these next 2 lines\n",
"# MODEL = \"llama3.2\"\n",
"# openai = OpenAI(base_url='http://localhost:11434/v1', api_key='ollama')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7b46dd52-8a3c-42d1-ac24-59f5eb5aaba1",
"metadata": {},
"outputs": [],
"source": [
"system_message = \"You are a helpful assistant for an online store called CommerceAI. \"\n",
"system_message += \"Give short, courteous answers, no more than 1 sentence. \"\n",
"system_message += \"Always be accurate. If you don't know the answer, say so.\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "31a6431b-215d-4f46-b813-971d8af7e034",
"metadata": {},
"outputs": [],
"source": [
"# This function looks rather simpler than the one from my video, because we're taking advantage of the latest Gradio updates\n",
"\n",
"def chat(message, history):\n",
" messages = [{\"role\": \"system\", \"content\": system_message}] + history + [{\"role\": \"user\", \"content\": message}]\n",
" response = openai.chat.completions.create(model=MODEL, messages=messages)\n",
" return response.choices[0].message.content\n",
"\n",
"gr.ChatInterface(fn=chat, type=\"messages\").launch()"
]
},
{
"cell_type": "markdown",
"id": "d3586bfb-acc3-4b5e-95be-02120b696f98",
"metadata": {},
"source": [
"## Tools\n",
"\n",
"Tools are an incredibly powerful feature provided by the frontier LLMs.\n",
"\n",
"With tools, you can write a function, and have the LLM call that function as part of its response.\n",
"\n",
"Sounds almost spooky.. we're giving it the power to run code on our machine?\n",
"\n",
"Well, kinda."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c9ac43e8-9880-44f6-b03a-6d1ab05bbb94",
"metadata": {},
"outputs": [],
"source": [
"# Let's start by making a useful function\n",
"\n",
"items_prices = {f\"item{i}\": f\"{i*100}\" for i in range(1,6)}\n",
"\n",
"items_prices = {\"printer\": \"$500\", \"paper\": \"$10\", \"mini printer\": \"$50\", \"label printer\": \"$60\", \"sticker-paper\": \"$5\"}\n",
"\n",
"def get_item_price(product):\n",
" print(f\"Tool get_item_price called for {product}\")\n",
" item = product.lower()\n",
" return items_prices.get(item, \"Unknown\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "88d249f7-787d-4750-b5b9-7df108da1b57",
"metadata": {},
"outputs": [],
"source": [
"items_prices"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ef3c3897-9a57-4f04-b5d0-f9ac8bb02d00",
"metadata": {},
"outputs": [],
"source": [
"get_item_price(\"mini printer\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "511ef9b8-bec0-4f14-b647-057e14c849cc",
"metadata": {},
"outputs": [],
"source": [
"# There's a particular dictionary structure that's required to describe our function:\n",
"\n",
"price_function = {\n",
" \"name\": \"get_item_price\",\n",
" \"description\": \"Get the price of an item in the store. \\\n",
" Call this whenever you need to know the store item price , \\\n",
" for example when a customer asks 'How much is a mini printer' \",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"product\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The item that the customer wants to buy\"\n",
" },\n",
" },\n",
" \"required\": [\"product\"],\n",
" \"additionalProperties\": False\n",
" }\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "86f674a4-4b7c-443d-b025-0f016932508a",
"metadata": {},
"outputs": [],
"source": [
"# And this is included in a list of tools:\n",
"\n",
"tools = [{\"type\": \"function\", \"function\": price_function}]"
]
},
{
"cell_type": "markdown",
"id": "724d0f89-8a86-493e-8cd1-73814688a70b",
"metadata": {},
"source": [
"## Getting OpenAI to use our Tool\n",
"\n",
"There's some fiddly stuff to allow OpenAI \"to call our tool\"\n",
"\n",
"What we actually do is give the LLM the opportunity to inform us that it wants us to run the tool.\n",
"\n",
"Here's how the new chat function looks:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2d67fb72-132e-499e-9931-86cb71b634b6",
"metadata": {},
"outputs": [],
"source": [
"def chat(message, history):\n",
" messages = [{\"role\": \"system\", \"content\": system_message}] + history + [{\"role\": \"user\", \"content\": message}]\n",
" response = openai.chat.completions.create(model=MODEL, messages=messages, tools=tools)\n",
"\n",
" if response.choices[0].finish_reason==\"tool_calls\":\n",
" message = response.choices[0].message\n",
" response, item = handle_tool_call(message)\n",
" print('response', response, 'item', item)\n",
" messages.append(message)\n",
" messages.append(response)\n",
" response = openai.chat.completions.create(model=MODEL, messages=messages)\n",
" \n",
" return response.choices[0].message.content"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3b4de767-954a-4077-a5f7-0055a0b90393",
"metadata": {},
"outputs": [],
"source": [
"# We have to write that function handle_tool_call:\n",
"\n",
"def handle_tool_call(message):\n",
" tool_call = message.tool_calls[0]\n",
" arguments = json.loads(tool_call.function.arguments)\n",
" item = arguments.get('product') \n",
" print('product', item)\n",
" price = get_item_price(item)\n",
" response = {\n",
" \"role\": \"tool\",\n",
" \"content\": json.dumps({\"item\": item,\"price\": price}),\n",
" \"tool_call_id\": tool_call.id\n",
" }\n",
" return response, item\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5ffda702-6ac5-4d13-9703-a14fa93aea68",
"metadata": {},
"outputs": [],
"source": [
"gr.ChatInterface(fn=chat, type=\"messages\").launch()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0ae2edbf-de58-43fa-b380-267cfc1755de",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"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.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

File diff suppressed because one or more lines are too long