prompt engineering.

This commit is contained in:
timothyafolami
2024-10-21 23:19:15 +01:00
commit 0680807bfd
2 changed files with 439 additions and 0 deletions
+438
View File
@@ -0,0 +1,438 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from openai import OpenAI\n",
"import os\n",
"import requests\n",
"from dotenv import load_dotenv\n",
"\n",
"load_dotenv()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"API_KEY = os.getenv('PERPLEXITY_AI_API')"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"def get_chat_completion(prompt, api_key=API_KEY):\n",
" url = \"https://api.perplexity.ai/chat/completions\"\n",
"\n",
" payload = {\n",
" \"model\": \"llama-3.1-sonar-small-128k-online\",\n",
" \"messages\": [\n",
" {\n",
" \"role\": \"system\",\n",
" \"content\": \"Be precise and concise.\"\n",
" },\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": prompt\n",
" }\n",
" ],\n",
" \"temperature\": 0.2,\n",
" \"top_p\": 0.9,\n",
" \"return_citations\": True,\n",
" \"search_domain_filter\": [\"perplexity.ai\"],\n",
" \"return_images\": False,\n",
" \"return_related_questions\": False,\n",
" \"search_recency_filter\": \"month\",\n",
" \"top_k\": 0,\n",
" \"stream\": False,\n",
" \"presence_penalty\": 0,\n",
" \"frequency_penalty\": 1\n",
" }\n",
" \n",
" headers = {\n",
" \"Authorization\": f\"Bearer {api_key}\",\n",
" \"Content-Type\": \"application/json\"\n",
" }\n",
" \n",
" response = requests.post(url, json=payload, headers=headers)\n",
" \n",
" # Check if the request was successful\n",
" if response.status_code == 200:\n",
" response_data = response.json()\n",
" try:\n",
" # Extract the message content\n",
" message_content = response_data['choices'][0]['message']['content']\n",
" return message_content\n",
" except (KeyError, IndexError):\n",
" return \"Unexpected response format.\"\n",
" else:\n",
" return f\"Request failed with status code: {response.status_code}\""
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"There are approximately 100 billion stars in the Milky Way Galaxy.\n"
]
}
],
"source": [
"prompt = \"How many stars are there in our galaxy?\"\n",
"\n",
"result = get_chat_completion(prompt)\n",
"print(result)\n"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"```python\n",
"states = [\n",
" \"Alabama\", \"Alaska\", \"Arizona\", \"Arkansas\", \"California\", \"Colorado\", \"Connecticut\",\n",
" \"Delaware\", \"Florida\", \"Georgia\", \"Hawaii\", \"Idaho\", \"Illinois\", \"Indiana\",\n",
" \"Iowa\", \"Kansas\", \"Kentucky\", \"Louisiana\", \"Maine\", \"Maryland\", \"Massachusetts\",\n",
" \"Michigan\", \"Minnesota\", \"Mississippi\", \"Missouri\", \"Montana\", \"Nebraska\",\n",
" \"Nevada\", \"New Hampshire\", \"New Jersey\", \"New Mexico\", \"New York\", \"North Carolina\",\n",
" \"North Dakota\", \"Ohio\", \"Oklahoma\", \"Oregon\", \"Pennsylvania\", \"Rhode Island\",\n",
" \"South Carolina\", \"South Dakota\", \"Tennessee\", \"Texas\", \"Utah\", \"Vermont\",\n",
" \"Virginia\", \"Washington\", \"West Virginia\", \"Wisconsin\", \"Wyoming\"\n",
"]\n",
"```\n"
]
}
],
"source": [
"prompt = \"Give me the list of the states in the US. Return as a python list. Don't add any other explanations to it.\"\n",
"\n",
"result = get_chat_completion(prompt)\n",
"print(result)\n"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"```python\n",
"['England', 'Scotland', 'Wales', 'Northern Ireland']\n",
"```\n"
]
}
],
"source": [
"prompt = \"Give me the list of the states in the UK or London. Return as a python list. Don't add any other explanations to it.\"\n",
"\n",
"result = get_chat_completion(prompt)\n",
"print(result)\n"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"```python\n",
"states = [\n",
" \"Baden-Württemberg\",\n",
" \"Bavaria\",\n",
" \"Berlin\",\n",
" \"Brandenburg\",\n",
" \"Bremen\",\n",
" \"Hamburg\",\n",
" \"Hesse\",\n",
" \"Lower Saxony\",\n",
" \"Mecklenburg-Vorpommern\",\n",
" \"North Rhine-Westphalia\",\n",
" \"Rhineland-Palatinate\",\n",
" \"Saarland\",\n",
" \"Saxony\",\n",
" \"Saxony-Anhalt\",\n",
" \"Schleswig-Holstein\",\n",
" \"Thuringia\"\n",
"]\n",
"```\n"
]
}
],
"source": [
"prompt = \"Give me the list of the states in Germany. Return as a python list. Don't add any other explanations to it.\"\n",
"\n",
"result = get_chat_completion(prompt)\n",
"print(result)\n"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"```python\n",
"['Retail', 'Fashion', 'Department Stores', 'Online Shops', 'Grocery Stores', 'Supercenters', 'Hypermarkets', 'Neobanking', 'Neobrokerage', 'Fintech', 'Automotive', 'Finance', 'Consumer Goods', 'Sneakers', 'Sportswear', 'Soft Drinks', 'Jewelry', 'Cosmetics', 'Insurance', 'Telecommunications', 'Hotels', 'Airlines', 'Banks', 'Online Grocery Delivery', 'Men\\'s Shop', 'NBA', 'NCAA', 'NFL', 'College Football']\n",
"```\n"
]
}
],
"source": [
"prompt = \"Give me a list of the categories of brands we have in the US, you can list up to 30 of them. Return as a python list. Don't add any other explanations to it.\"\n",
"\n",
"result = get_chat_completion(prompt)\n",
"print(result)\n"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1. **Name** | **Email/Contact** | **Location** | **Facebook Username** | **Instagram Username** | **Facebook Followers** | **Instagram Followers**\n",
"2. **Sharaaa** | N/A | Birmingham, AL | N/A | @kashararachele | 11.9k | 11.9k\n",
"3. **Jaz** | N/A | Birmingham, AL | N/A | @birmingham_finest | 11.1k | 11.1k\n",
"4. **Randall Woodfin** | N/A | Birmingham, AL | N/A | @randall_woodfin | N/A | 17.2k\n",
"5. **Micah Myricks** | N/A | Birmingham, AL | N/A | @thecoolestguyuno | N/A | 10.2k\n",
"6. **KC** | N/A | Birmingham, AL | N/A | @_imonlykc | N/A | 12k\n",
"7. **The Raveen Smith** | N/A | Birmingham, AL | N/A | @noflashplzz | N/A | 15.8k\n",
"8. **Bianca Sha'nel Patton** | N/A | Birmingham, AL | N/A | @biancasobad | N/A | 35.1k\n",
"9. **Mare Thomas** | N/A | Birmingham, AL | N/A | @savannahbrasfield | N/A | 15.5k\n",
"10. **Destinee Ariee** | N/A | Birmingham, AL | N/A | @therealddestiny | N/A | 24k\n",
"11. **Dewayne Mcbride** | N/A | Birmingham, AL | N/A | @queen.slim | N/A | 10.9k\n",
"\n",
"Note: The email and contact details are not available in the provided sources.\n"
]
}
],
"source": [
"prompt = \"\"\"\n",
" I'm looking for influencers that i need to promote my sneakers brand. My current location is Alabama in the US. \\n\n",
" I need help with looking for influencers in my state. People that i can reach out to, and they will help me promote my sneakers brand. \\n\n",
" I need your help with a list of all the influencers i have in this state. Here's how i want it. \\n\n",
" 1. Name \\n\n",
" 2. Email or contant \\n\n",
" 3. Location \\n\n",
" 4. Facebook Username\\n\n",
" 5. Instagram Username \\n\n",
" 6. Number of facebook followers \\n\n",
" 7. Number of instagram followers \\n.\n",
" This information will help me track who to reach out to. \\n \n",
" I'm expecting 7 lists as output, it should follow the pattern i listed.\\n\n",
" Do not add other explanations to it, just return the lists alone.\n",
"\"\"\"\n",
"\n",
"result = get_chat_completion(prompt)\n",
"print(result)\n"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"```python\n",
"# List of influencers in Alabama\n",
"\n",
"name = [\n",
" \"J6lack_\",\n",
" \"Sharaaa\",\n",
" \"Jaz 🖤\",\n",
" \"Randall Woodfin\",\n",
" \"Micah Myricks\",\n",
" \"_imonlykc\",\n",
" \"THE RAVEN SMITH\",\n",
" \"Bianca Sha'nel Patton\",\n",
" \"Mare Thomas\",\n",
" \"Destinee Ariee\",\n",
" \"Dewayne Mcbride\"\n",
"]\n",
"\n",
"contact_info = [\n",
" \"Check contact details\",\n",
" \"Check contact details\",\n",
" \"Check contact details\",\n",
" \"Check contact details\",\n",
" \"Check contact details\",\n",
" \"Check contact details\",\n",
" \"Check contact details\",\n",
" \"Check contact details\",\n",
" \"Check contact details\",\n",
" \"Check contact details\",\n",
" \"Check contact details\"\n",
"]\n",
"\n",
"location = [\n",
" \"Birmingham\",\n",
" \"Birmingham\",\n",
" \"Birmingham\",\n",
" \"Birmingham\",\n",
" \"Birmingham\",\n",
" \"Birmingham\",\n",
" \"Birmingham\",\n",
" \"Birmingham\",\n",
" \"Birmingham\",\n",
" \"Birmingham\",\n",
" \"Birmingham\"\n",
"]\n",
"\n",
"facebook_username = [\n",
" None,\n",
" None,\n",
" None,\n",
" None,\n",
" None,\n",
" None,\n",
" None,\n",
" None,\n",
" None,\n",
" None,\n",
" None\n",
"]\n",
"\n",
"instagram_username = [\n",
" \"@j6lack_\",\n",
" \"@kashararachele\",\n",
" \"@birmingham_finest\",\n",
" \"@randall_woodfin\",\n",
" \"@thecoolestguyuno\",\n",
" \"@_imonlykc\",\n",
" \"@noflashplzz\",\n",
" \"@biancasobad\",\n",
" \"@savannahbrasfield\",\n",
" \"@therealddestiny\",\n",
" \"@queen.slim\"\n",
"]\n",
"\n",
"number_of_facebook_followers = [\n",
" None,\n",
" None,\n",
" None,\n",
" None,\n",
" None,\n",
" None,\n",
" None,\n",
" None,\n",
" None,\n",
" None,\n",
" None\n",
"]\n",
"\n",
"number_of_instagram_followers = [\n",
" \"15.1k\",\n",
" \"11.9k\",\n",
" \"11.1k\",\n",
" \"17.2k\",\n",
" \"10.2k\",\n",
" \"12k\",\n",
" \"15.8k\",\n",
" \"35.1k\",\n",
" \"15.5k\",\n",
" \"24k\",\n",
" \"10.9k\"\n",
"]\n",
"```\n",
"\n",
"This list includes the requested information for the top 11 influencers in Alabama, as per the provided data. Note that Facebook usernames and follower counts are not available in the source data, so they are listed as `None` in the respective lists.\n"
]
}
],
"source": [
"prompt = \"\"\"\n",
" I'm looking for influencers that i need to promote my sneakers brand. My current location is Alabama in the US. \\n\n",
" I need help with looking for influencers in my state. People that i can reach out to, and they will help me promote my sneakers brand. \\n\n",
" I need your help with a list of 50 influencers i have in this state. Here's how i want it. \\n\n",
" 1. Name \\n\n",
" 2. Contant info \\n\n",
" 3. Location \\n\n",
" 4. Facebook Username\\n\n",
" 5. Instagram Username \\n\n",
" 6. Number of facebook followers \\n\n",
" 7. Number of instagram followers \\n.\n",
" This information will help me track who to reach out to. \\n \n",
" I'm expecting 7 lists as output, it should follow the pattern i listed.\\n\n",
" Do not add other explanations to it, just return the lists alone. Make them come out in separate python lists, like this []. So i will have 7 in total. \\n\n",
" Place them in order, name: [], Contact info: [], Facebook Username: [], and so on.\\n\n",
" Please do this carefully.\n",
"\"\"\"\n",
"\n",
"result = get_chat_completion(prompt)\n",
"print(result)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"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.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}