Update openai calls to current lib version in Question Answering Using Embedding example (#886)

Co-authored-by: bigears <mark.forsyth@yourbigears.com>
Co-authored-by: Will DePue <will@depue.net>
pull/912/head^2
markbigears 6 months ago committed by GitHub
parent 7a980b6a97
commit bff44b0607
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -121,19 +121,19 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 3,
"id": "9e3839a6-9146-4f60-b74b-19abbc24278d",
"metadata": {},
"outputs": [],
"source": [
"# imports\n",
"import ast # for converting embeddings saved as strings back to arrays\n",
"import openai # for calling the OpenAI API\n",
"from openai import OpenAI # for calling the OpenAI API\n",
"import pandas as pd # for storing text and embeddings data\n",
"import tiktoken # for counting tokens\n",
"import os # for getting API token from env variable OPENAI_API_KEY\n",
"from scipy import spatial # for calculating vector similarities for search\n",
"\n",
"\n",
"# models\n",
"EMBEDDING_MODEL = \"text-embedding-ada-002\"\n",
"GPT_MODEL = \"gpt-3.5-turbo\""
@ -178,7 +178,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 4,
"id": "a167516c-7c19-4bda-afa5-031aa0ae13bb",
"metadata": {},
"outputs": [
@ -186,7 +186,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"I'm sorry, but as an AI language model, I don't have information about the future events. The 2022 Winter Olympics will be held in Beijing, China, from February 4 to 20, 2022. The curling events will take place during the games, and the winners of the gold medal in curling will be determined at that time.\n"
"As an AI language model, I don't have real-time data. However, I can provide you with general information. The gold medalists in curling at the 2022 Winter Olympics will be determined during the event. The winners will be the team that finishes in first place in the respective men's and women's curling competitions. To find out the specific gold medalists, you can check the official Olympic website or reliable news sources for the most up-to-date information.\n"
]
}
],
@ -194,7 +194,10 @@
"# an example question about the 2022 Olympics\n",
"query = 'Which athletes won the gold medal in curling at the 2022 Winter Olympics?'\n",
"\n",
"response = openai.ChatCompletion.create(\n",
"openai = OpenAI(\n",
" api_key = os.environ.get(\"OPENAI_API_KEY\"), # this is also the default, it can be omitted\n",
")\n",
"response = openai.chat.completions.create(\n",
" messages=[\n",
" {'role': 'system', 'content': 'You answer questions about the 2022 Winter Olympics.'},\n",
" {'role': 'user', 'content': query},\n",
@ -203,7 +206,7 @@
" temperature=0,\n",
")\n",
"\n",
"print(response['choices'][0]['message']['content'])"
"print(response.choices[0].message.content)"
]
},
{
@ -221,7 +224,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 5,
"id": "02e7281d",
"metadata": {},
"outputs": [],
@ -532,7 +535,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 6,
"id": "fceaf665-2602-4788-bc44-9eb256a6f955",
"metadata": {},
"outputs": [
@ -540,7 +543,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"There were three events in curling at the 2022 Winter Olympics, so there were three sets of athletes who won gold medals. The gold medalists in men's curling were Sweden's Niklas Edin, Oskar Eriksson, Rasmus Wranå, Christoffer Sundgren, and Daniel Magnusson. The gold medalists in women's curling were Great Britain's Eve Muirhead, Vicky Wright, Jennifer Dodds, Hailey Duff, and Mili Smith. The gold medalists in mixed doubles curling were Italy's Stefania Constantini and Amos Mosaner.\n"
"In the men's curling event, the gold medal was won by Sweden. In the women's curling event, the gold medal was won by Great Britain. In the mixed doubles curling event, the gold medal was won by Italy.\n"
]
}
],
@ -554,7 +557,7 @@
"\n",
"Question: Which athletes won the gold medal in curling at the 2022 Winter Olympics?\"\"\"\n",
"\n",
"response = openai.ChatCompletion.create(\n",
"response = openai.chat.completions.create(\n",
" messages=[\n",
" {'role': 'system', 'content': 'You answer questions about the 2022 Winter Olympics.'},\n",
" {'role': 'user', 'content': query},\n",
@ -563,7 +566,7 @@
" temperature=0,\n",
")\n",
"\n",
"print(response['choices'][0]['message']['content'])"
"print(response.choices[0].message.content)"
]
},
{
@ -596,7 +599,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 7,
"id": "46d50792",
"metadata": {},
"outputs": [],
@ -610,7 +613,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 8,
"id": "70307f8e",
"metadata": {},
"outputs": [],
@ -621,7 +624,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 9,
"id": "424162c2",
"metadata": {},
"outputs": [
@ -741,7 +744,7 @@
"[6059 rows x 2 columns]"
]
},
"execution_count": 7,
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
@ -770,7 +773,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 10,
"id": "b9a8c713-c8a9-47dc-85a4-871ee1395566",
"metadata": {},
"outputs": [],
@ -783,11 +786,11 @@
" top_n: int = 100\n",
") -> tuple[list[str], list[float]]:\n",
" \"\"\"Returns a list of strings and relatednesses, sorted from most related to least.\"\"\"\n",
" query_embedding_response = openai.Embedding.create(\n",
" query_embedding_response = openai.embeddings.create(\n",
" model=EMBEDDING_MODEL,\n",
" input=query,\n",
" )\n",
" query_embedding = query_embedding_response[\"data\"][0][\"embedding\"]\n",
" query_embedding = query_embedding_response.data[0].embedding\n",
" strings_and_relatednesses = [\n",
" (row[\"text\"], relatedness_fn(query_embedding, row[\"embedding\"]))\n",
" for i, row in df.iterrows()\n",
@ -799,7 +802,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 11,
"id": "da034bd2",
"metadata": {},
"outputs": [
@ -912,7 +915,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 12,
"id": "1f45cecc",
"metadata": {},
"outputs": [],
@ -961,12 +964,12 @@
" {\"role\": \"system\", \"content\": \"You answer questions about the 2022 Winter Olympics.\"},\n",
" {\"role\": \"user\", \"content\": message},\n",
" ]\n",
" response = openai.ChatCompletion.create(\n",
" response = openai.chat.completions.create(\n",
" model=model,\n",
" messages=messages,\n",
" temperature=0\n",
" )\n",
" response_message = response[\"choices\"][0][\"message\"][\"content\"]\n",
" response_message = response.choices[0].message.content\n",
" return response_message\n",
"\n"
]
@ -984,17 +987,17 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 13,
"id": "e11f53ab",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"There were two gold medal-winning teams in curling at the 2022 Winter Olympics: the Swedish men's team consisting of Niklas Edin, Oskar Eriksson, Rasmus Wranå, Christoffer Sundgren, and Daniel Magnusson, and the British women's team consisting of Eve Muirhead, Vicky Wright, Jennifer Dodds, Hailey Duff, and Mili Smith.\""
"\"In the men's curling tournament, the gold medal was won by the team from Sweden, consisting of Niklas Edin, Oskar Eriksson, Rasmus Wranå, Christoffer Sundgren, and Daniel Magnusson. In the women's curling tournament, the gold medal was won by the team from Great Britain, consisting of Eve Muirhead, Vicky Wright, Jennifer Dodds, Hailey Duff, and Mili Smith.\""
]
},
"execution_count": 11,
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
@ -1036,7 +1039,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 14,
"id": "aa965e36",
"metadata": {},
"outputs": [
@ -1274,36 +1277,6 @@
"\n",
"==Results summary==\n",
"\n",
"===Women's tournament===\n",
"\n",
"====Playoffs====\n",
"\n",
"=====Bronze medal game=====\n",
"\n",
"''Saturday, 19 February, 20:05''\n",
"{{#lst:Curling at the 2022 Winter Olympics Women's tournament|BM}}\n",
"{{Player percentages\n",
"| team1 = {{flagIOC|SUI|2022 Winter}}\n",
"| [[Melanie Barbezat]] | 79%\n",
"| [[Esther Neuenschwander]] | 75%\n",
"| [[Silvana Tirinzoni]] | 81%\n",
"| [[Alina Pätz]] | 64%\n",
"| teampct1 = 75%\n",
"| team2 = {{flagIOC|SWE|2022 Winter}}\n",
"| [[Sofia Mabergs]] | 89%\n",
"| [[Agnes Knochenhauer]] | 80%\n",
"| [[Sara McManus]] | 81%\n",
"| [[Anna Hasselborg]] | 76%\n",
"| teampct2 = 82%\n",
"}}\n",
"\"\"\"\n",
"\n",
"Wikipedia article section:\n",
"\"\"\"\n",
"Curling at the 2022 Winter Olympics\n",
"\n",
"==Results summary==\n",
"\n",
"===Mixed doubles tournament===\n",
"\n",
"====Playoffs====\n",
@ -1329,16 +1302,46 @@
"|}\n",
"\"\"\"\n",
"\n",
"Wikipedia article section:\n",
"\"\"\"\n",
"Curling at the 2022 Winter Olympics\n",
"\n",
"==Results summary==\n",
"\n",
"===Women's tournament===\n",
"\n",
"====Playoffs====\n",
"\n",
"=====Bronze medal game=====\n",
"\n",
"''Saturday, 19 February, 20:05''\n",
"{{#lst:Curling at the 2022 Winter Olympics Women's tournament|BM}}\n",
"{{Player percentages\n",
"| team1 = {{flagIOC|SUI|2022 Winter}}\n",
"| [[Melanie Barbezat]] | 79%\n",
"| [[Esther Neuenschwander]] | 75%\n",
"| [[Silvana Tirinzoni]] | 81%\n",
"| [[Alina Pätz]] | 64%\n",
"| teampct1 = 75%\n",
"| team2 = {{flagIOC|SWE|2022 Winter}}\n",
"| [[Sofia Mabergs]] | 89%\n",
"| [[Agnes Knochenhauer]] | 80%\n",
"| [[Sara McManus]] | 81%\n",
"| [[Anna Hasselborg]] | 76%\n",
"| teampct2 = 82%\n",
"}}\n",
"\"\"\"\n",
"\n",
"Question: Which athletes won the gold medal in curling at the 2022 Winter Olympics?\n"
]
},
{
"data": {
"text/plain": [
"\"There were two gold medal-winning teams in curling at the 2022 Winter Olympics: the Swedish men's team consisting of Niklas Edin, Oskar Eriksson, Rasmus Wranå, Christoffer Sundgren, and Daniel Magnusson, and the British women's team consisting of Eve Muirhead, Vicky Wright, Jennifer Dodds, Hailey Duff, and Mili Smith.\""
"\"In the men's tournament, the Swedish team consisting of Niklas Edin, Oskar Eriksson, Rasmus Wranå, Christoffer Sundgren, and Daniel Magnusson won the gold medal in curling at the 2022 Winter Olympics. In the women's tournament, the British team consisting of Eve Muirhead, Vicky Wright, Jennifer Dodds, Hailey Duff, and Mili Smith won the gold medal.\""
]
},
"execution_count": 12,
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
@ -1361,17 +1364,17 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 15,
"id": "d6cb292f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"The gold medal winners in curling at the 2022 Winter Olympics are as follows:\\n\\nMen's tournament: Team Sweden, consisting of Niklas Edin, Oskar Eriksson, Rasmus Wranå, Christoffer Sundgren, and Daniel Magnusson.\\n\\nWomen's tournament: Team Great Britain, consisting of Eve Muirhead, Vicky Wright, Jennifer Dodds, Hailey Duff, and Mili Smith.\\n\\nMixed doubles tournament: Team Italy, consisting of Stefania Constantini and Amos Mosaner.\""
"\"The athletes who won the gold medal in curling at the 2022 Winter Olympics are:\\n\\nMen's tournament: Niklas Edin, Oskar Eriksson, Rasmus Wranå, Christoffer Sundgren, and Daniel Magnusson from Sweden.\\n\\nWomen's tournament: Eve Muirhead, Vicky Wright, Jennifer Dodds, Hailey Duff, and Mili Smith from Great Britain.\\n\\nMixed doubles tournament: Stefania Constantini and Amos Mosaner from Italy.\""
]
},
"execution_count": 13,
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
@ -1402,17 +1405,17 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 16,
"id": "05fb04ef",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'A number of world records (WR) and Olympic records (OR) were set in various skating events at the 2022 Winter Olympics in Beijing, China. However, the exact number of records set is not specified in the given articles.'"
"'I could not find an answer.'"
]
},
"execution_count": 14,
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
@ -1424,17 +1427,17 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 17,
"id": "30da5271",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Jamaica had more athletes at the 2022 Winter Olympics with a total of 7 athletes (6 men and 1 woman) competing in 2 sports, while Cuba did not participate in the 2022 Winter Olympics.'"
"\"Jamaica had more athletes at the 2022 Winter Olympics. According to the provided information, Jamaica had a total of 7 athletes (6 men and 1 woman) competing in 2 sports, while there is no information about Cuba's participation in the 2022 Winter Olympics.\""
]
},
"execution_count": 15,
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
@ -1446,17 +1449,17 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 18,
"id": "42449926",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'I could not find an answer. The entertainment value of Olympic sports is subjective and varies from person to person.'"
"'I could not find an answer.'"
]
},
"execution_count": 16,
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
@ -1468,7 +1471,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 19,
"id": "34e4b7e1",
"metadata": {},
"outputs": [
@ -1478,7 +1481,7 @@
"'I could not find an answer.'"
]
},
"execution_count": 17,
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
@ -1490,17 +1493,17 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 20,
"id": "57d13b1f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'With a beak so grand and wide,\\nThe Shoebill Stork glides with pride,\\nElegant in every stride,\\nA true beauty of the wild.'"
"'I could not find an answer.'"
]
},
"execution_count": 18,
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
@ -1512,17 +1515,17 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 21,
"id": "f997e261",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'I could not find an answer.'"
"\"In the marsh, the Shoebill stands tall and stark,\\nWith a grace that lights up the day's dark.\\nIts elegance in flight, a breathtaking art,\\nA living masterpiece, nature's work of heart.\""
]
},
"execution_count": 19,
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
@ -1534,17 +1537,17 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 22,
"id": "0d3dad92",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"There were multiple gold medalists in curling at the 2022 Winter Olympics. The women's team from Great Britain and the men's team from Sweden both won gold medals in their respective tournaments.\""
"\"According to the provided information, the gold medal winners in curling at the 2022 Winter Olympics were:\\n\\n- Men's tournament: Sweden (Niklas Edin, Oskar Eriksson, Rasmus Wranå, Christoffer Sundgren, Daniel Magnusson)\\n- Women's tournament: Great Britain (Eve Muirhead, Vicky Wright, Jennifer Dodds, Hailey Duff, Mili Smith)\\n- Mixed doubles tournament: Italy (Stefania Constantini, Amos Mosaner)\""
]
},
"execution_count": 20,
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
@ -1556,7 +1559,7 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 23,
"id": "afa3b95f",
"metadata": {},
"outputs": [
@ -1566,7 +1569,7 @@
"'I could not find an answer.'"
]
},
"execution_count": 21,
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
@ -1578,17 +1581,17 @@
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": 24,
"id": "627e131e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'I could not find an answer. This question is not related to the provided articles on the 2022 Winter Olympics.'"
"'I could not find an answer.'"
]
},
"execution_count": 22,
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
@ -1600,17 +1603,17 @@
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": 25,
"id": "c5aad00d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"The COVID-19 pandemic had a significant impact on the 2022 Winter Olympics. The qualifying process for some sports was changed due to the cancellation of tournaments in 2020, and all athletes were required to remain within a bio-secure bubble for the duration of their participation, which included daily COVID-19 testing. Only residents of the People's Republic of China were permitted to attend the Games as spectators, and ticket sales to the general public were canceled. Some top athletes, considered to be medal contenders, were not able to travel to China after having tested positive, even if asymptomatic. There were also complaints from athletes and team officials about the quarantine facilities and conditions they faced. Additionally, there were 437 total coronavirus cases detected and reported by the Beijing Organizing Committee since January 23, 2022.\""
"'COVID-19 had several impacts on the 2022 Winter Olympics. Here are some of the effects:\\n\\n1. Changes in Qualification: The qualifying process for curling and women\\'s ice hockey had to be altered due to the cancellation of tournaments in 2020. Qualification for curling was based on placement in the 2021 World Curling Championships and an Olympic Qualification Event. The women\\'s tournament qualification was based on existing IIHF World Rankings.\\n\\n2. Biosecurity Protocols: The International Olympic Committee (IOC) announced biosecurity protocols for the Games, which included a \"closed-loop management system\" where athletes had to remain within a bio-secure bubble. Athletes were required to undergo daily COVID-19 testing and could only travel to and from Games-related venues. Only residents of China were allowed to attend the Games as spectators.\\n\\n3. NHL Player Withdrawal: The National Hockey League (NHL) and National Hockey League Players\\' Association (NHLPA) announced that NHL players would not participate in the men\\'s hockey tournament due to concerns over COVID-19 and the need to make up postponed games.\\n\\n4. Limited Spectators: Ticket sales to the general public were canceled, and only limited numbers of spectators were admitted by invitation only. The Games were closed to the general public, with spectators only present at events held in Beijing and Zhangjiakou.\\n\\n5. Use of My2022 App: Everyone present at the Games, including athletes, staff, and attendees, were required to use the My2022 mobile app as part of the biosecurity protocols. The app was used for health reporting, COVID-19 vaccination and testing records, customs declarations, and messaging.\\n\\n6. Athlete Absences: Some top athletes, including Austrian ski jumper Marita Kramer and Russian skeletonist Nikita Tregubov, were unable to travel to China after testing positive for COVID-19, even if asymptomatic.\\n\\n7. COVID-19 Cases: There were a total of 437 COVID-19 cases linked to the 2022 Winter Olympics, with 171 cases among the COVID-19 protective bubble residents and the rest detected through airport testing of games-related arrivals.\\n\\nPlease note that this answer is based on the provided articles and may not include all possible impacts of COVID-19 on the 2022 Winter Olympics.'"
]
},
"execution_count": 23,
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
@ -1637,7 +1640,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.9"
"version": "3.10.5"
},
"vscode": {
"interpreter": {

Loading…
Cancel
Save