182 lines
4.9 KiB
Plaintext
182 lines
4.9 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "bbd8585e-0a28-4fd9-80b5-690569f93e16",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"#This notebook will help you to get top tech products with by providing category and subcategory"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "df039118-f462-4a8b-949e-53d3a726e292",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# imports\n",
|
|
"\n",
|
|
"import os\n",
|
|
"import requests\n",
|
|
"from dotenv import load_dotenv\n",
|
|
"from bs4 import BeautifulSoup\n",
|
|
"from IPython.display import Markdown, display\n",
|
|
"from openai import OpenAI\n",
|
|
"aa"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "e2ffd2e5-d061-446c-891e-15a6d1958ab6",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Load environment variables in a file called .env\n",
|
|
"\n",
|
|
"load_dotenv(override=True)\n",
|
|
"api_key = os.getenv('OPENAI_API_KEY')\n",
|
|
"\n",
|
|
"# Check the key\n",
|
|
"\n",
|
|
"if not api_key:\n",
|
|
" print(\"No API key was found - please head over to the troubleshooting notebook in this folder to identify & fix!\")\n",
|
|
"elif not api_key.startswith(\"sk-proj-\"):\n",
|
|
" print(\"An API key was found, but it doesn't start sk-proj-; please check you're using the right key - see troubleshooting notebook\")\n",
|
|
"elif api_key.strip() != api_key:\n",
|
|
" print(\"An API key was found, but it looks like it might have space or tab characters at the start or end - please remove them - see troubleshooting notebook\")\n",
|
|
"else:\n",
|
|
" print(\"API key found and looks good so far!\")\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "92e26007-521f-4ea2-9df9-edd77dd7e183",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"openai = OpenAI()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "27d21593-8feb-42e4-bbc0-2e949b51137d",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def tech_product(category_subcategory_budget):\n",
|
|
" parts = category_subcategory_budget.split('_')\n",
|
|
" return f\"{parts[0]}-{parts[1]}-{parts[2]}\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "dd978d25-5b84-4122-af7c-116f2bf72179",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def messages_for(products):\n",
|
|
" return [\n",
|
|
" {\"role\": \"system\", \"content\": \"you are a tech product expert and you need to suggest the best suited product available in India basis the input received in the form of category-subcategory-budget (in inr),\\\n",
|
|
" revert with category and subcategory and show the product links as well along with pros and cons, respond in markdown\"},\n",
|
|
" {\"role\": \"user\", \"content\": tech_product(products)}\n",
|
|
" ]"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "b916db7a-81a4-41d9-87c2-a2346fd874d2",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"messages_for(\"phone_gaming_40000\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "3b4bb3f1-95de-4eb5-afe1-068744f93301",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def get_top_products(category_subcategory):\n",
|
|
" response = openai.chat.completions.create(\n",
|
|
" model = \"gpt-4o-mini\",\n",
|
|
" messages= messages_for(category_subcategory)\n",
|
|
" )\n",
|
|
" return response.choices[0].message.content \n",
|
|
" "
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "c9272942-acfe-4fca-bd0a-3435c1ee6691",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"get_top_products('phone_gaming_30000')"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "2c2b3b9a-aceb-4f00-8c8d-8f6837ab94fc",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def display_markdown(category_subcategory_budget):\n",
|
|
" output = get_top_products(category_subcategory_budget)\n",
|
|
" display(Markdown(output))\n",
|
|
" "
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "6c135dd7-4ed4-48ee-ba3f-9b4ca1c32149",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"display_markdown('Console_gaming_100000')"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "0ba06c55-7ef9-47eb-aeaf-3c4a7b29bccc",
|
|
"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.13"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|