Skip to content

Commit 1090320

Browse files
fixed ch08 and others
1 parent 49371cb commit 1090320

File tree

4 files changed

+30
-28
lines changed

4 files changed

+30
-28
lines changed

04-prompt-engineering-fundamentals/1-introduction.ipynb

+2
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@
9090
"## Updated\n",
9191
"import os\n",
9292
"from openai import AzureOpenAI\n",
93+
"from dotenv import load_dotenv\n",
94+
"load_dotenv()\n",
9395
"\n",
9496
"client = AzureOpenAI(\n",
9597
" api_key=os.environ['AZURE_OPENAI_KEY'], # this is also the default, it can be omitted\n",

06-text-generation-apps/notebook-azure-openai.ipynb

+2
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,8 @@
235235
"source": [
236236
"import os\n",
237237
"from openai import AzureOpenAI\n",
238+
"from dotenv import load_dotenv\n",
239+
"load_dotenv()\n",
238240
"\n",
239241
"client = AzureOpenAI(\n",
240242
" api_key=os.environ['AZURE_OPENAI_KEY'], \n",

07-building-chat-applications/notebook-azure-openai.ipynb

+2-2
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@
708708
},
709709
"source": [
710710
"## Embeddings\n",
711-
"This section will show how to retrieve embeddings, and find similarities between words, sentences, and documents. In order to run the following noteboooks you need to deploy a model that uses `text-embedding-ada-002` as base model."
711+
"This section will show how to retrieve embeddings, and find similarities between words, sentences, and documents. In order to run the following noteboooks you need to deploy a model that uses `text-embedding-ada-002` as base model and set his deployment name inside .env file."
712712
]
713713
},
714714
{
@@ -787,7 +787,7 @@
787787
"outputs": [],
788788
"source": [
789789
"text = 'the quick brown fox jumped over the lazy dog'\n",
790-
"model=\"<name-of-your-model-using-text-embedding-ada-002>\"\n",
790+
"model= os.environ['AZURE_OPENAI_EMBEDDINGS_DEPLOYMENT']\n",
791791
"client.embeddings.create(input='[text]', model=model).data[0].embedding"
792792
]
793793
},

08-building-search-applications/solution.ipynb

+24-26
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,33 @@
44
"cell_type": "markdown",
55
"metadata": {},
66
"source": [
7-
"The first step is to import the libraries and set the OpenAI API key and endpoint. You'll need to set the following environment variables:\n",
8-
"\n",
9-
"- `AZURE_OPENAI_API_KEY` - Your OpenAI API key\n",
10-
"- `AZURE_OPENAI_ENDPOINT` - Your OpenAI endpoint"
7+
"In order to run the following noteboooks, if you haven't done yet, you need to deploy a model that uses `text-embedding-ada-002` as base model and set his deployment name inside .env file as `AZURE_OPENAI_EMBEDDINGS_ENDPOINT`"
118
]
129
},
1310
{
1411
"cell_type": "code",
15-
"execution_count": null,
12+
"execution_count": 1,
1613
"metadata": {},
1714
"outputs": [],
1815
"source": [
1916
"import os\n",
2017
"import pandas as pd\n",
21-
"import openai\n",
22-
"from openai.embeddings_utils import cosine_similarity, get_embedding\n",
18+
"import numpy as np\n",
19+
"from openai import AzureOpenAI\n",
20+
"from dotenv import load_dotenv\n",
2321
"\n",
24-
"OPENAI_EMBEDDING_ENGINE = \"text-embedding-ada-002\"\n",
25-
"SIMILARITIES_RESULTS_THRESHOLD = 0.75\n",
26-
"DATASET_NAME = \"embedding_index_3m.json\"\n",
22+
"from sklearn.metrics.pairwise import cosine_similarity\n",
23+
"load_dotenv()\n",
2724
"\n",
28-
"openai.api_type = \"azure\"\n",
29-
"openai.api_key = os.environ[\"AZURE_OPENAI_API_KEY\"]\n",
30-
"openai.api_base = os.environ[\"AZURE_OPENAI_ENDPOINT\"]\n",
31-
"openai.api_version = \"2023-07-01-preview\"\n",
25+
"client = AzureOpenAI(\n",
26+
" api_key=os.environ['AZURE_OPENAI_KEY'], # this is also the default, it can be omitted\n",
27+
" api_version = \"2023-05-15\"\n",
28+
" )\n",
3229
"\n",
33-
"OPENAI_EMBEDDING_DEPLOYMENT_NAME = os.environ[\n",
34-
" \"AZURE_OPENAI_EMBEDDING_MODEL_DEPLOYMENT_NAME\"\n",
35-
"]"
30+
"model = os.environ['AZURE_OPENAI_EMBEDDINGS_DEPLOYMENT']\n",
31+
"\n",
32+
"SIMILARITIES_RESULTS_THRESHOLD = 0.75\n",
33+
"DATASET_NAME = \"embedding_index_3m.json\""
3634
]
3735
},
3836
{
@@ -44,7 +42,7 @@
4442
},
4543
{
4644
"cell_type": "code",
47-
"execution_count": null,
45+
"execution_count": 2,
4846
"metadata": {},
4947
"outputs": [],
5048
"source": [
@@ -69,7 +67,7 @@
6967
},
7068
{
7169
"cell_type": "code",
72-
"execution_count": null,
70+
"execution_count": 3,
7371
"metadata": {},
7472
"outputs": [],
7573
"source": [
@@ -79,12 +77,12 @@
7977
" # create a copy of the dataset\n",
8078
" video_vectors = dataset.copy()\n",
8179
"\n",
82-
" # get the embeddings for the query\n",
83-
" query_embeddings = get_embedding(query, OPENAI_EMBEDDING_ENGINE)\n",
80+
" # get the embeddings for the query \n",
81+
" query_embeddings = client.embeddings.create(input=query, model=model).data[0].embedding\n",
8482
"\n",
8583
" # create a new column with the calculated similarity for each row\n",
8684
" video_vectors[\"similarity\"] = video_vectors[\"ada_v2\"].apply(\n",
87-
" lambda x: cosine_similarity(query_embeddings, x)\n",
85+
" lambda x: cosine_similarity(np.array(query_embeddings).reshape(1,-1), np.array(x).reshape(1,-1))\n",
8886
" )\n",
8987
"\n",
9088
" # filter the videos by similarity\n",
@@ -109,7 +107,7 @@
109107
},
110108
{
111109
"cell_type": "code",
112-
"execution_count": null,
110+
"execution_count": 4,
113111
"metadata": {},
114112
"outputs": [],
115113
"source": [
@@ -119,7 +117,7 @@
119117
" return f\"https://youtu.be/{video_id}?t={seconds}\"\n",
120118
"\n",
121119
" print(f\"\\nVideos similar to '{query}':\")\n",
122-
" for index, row in videos.iterrows():\n",
120+
" for row in videos.iterrows():\n",
123121
" youtube_url = _gen_yt_url(row[\"videoId\"], row[\"seconds\"])\n",
124122
" print(f\" - {row['title']}\")\n",
125123
" print(f\" Summary: {' '.join(row['summary'].split()[:15])}...\")\n",
@@ -153,7 +151,7 @@
153151
},
154152
{
155153
"cell_type": "code",
156-
"execution_count": null,
154+
"execution_count": 5,
157155
"metadata": {},
158156
"outputs": [],
159157
"source": [
@@ -190,7 +188,7 @@
190188
"name": "python",
191189
"nbconvert_exporter": "python",
192190
"pygments_lexer": "ipython3",
193-
"version": "3.11.6"
191+
"version": "3.10.8"
194192
}
195193
},
196194
"nbformat": 4,

0 commit comments

Comments
 (0)