-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Paul Lin
authored and
Paul Lin
committed
Mar 28, 2024
1 parent
dc17f99
commit c9491d3
Showing
1 changed file
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 20, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from openai import OpenAI\n", | ||
"import json\n", | ||
"from tqdm import tqdm\n", | ||
"import os\n", | ||
"from dotenv import load_dotenv\n", | ||
"load_dotenv()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"OPENAI_API_KEY = os.getenv(\"OPENAI_API_KEY\")\n", | ||
"client = OpenAI(api_key=OPENAI_API_KEY)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 15, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def get_embedding(text):\n", | ||
" response = client.embeddings.create(\n", | ||
" model=\"text-embedding-3-small\",\n", | ||
" input=text\n", | ||
" )\n", | ||
" return response.data[0].embedding" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 21, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"100%|██████████| 1206/1206 [04:05<00:00, 4.91it/s]\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"with open('202501.json', 'r') as file:\n", | ||
" courses = json.load(file)\n", | ||
"\n", | ||
"for course in tqdm(courses):\n", | ||
" text_to_embed = f\"{course['short_title']}: {course['description']}\"\n", | ||
" embedding = get_embedding(text_to_embed) \n", | ||
" course['embedding'] = embedding\n", | ||
"\n", | ||
"with open('courses_with_embeddings.json', 'w') as file:\n", | ||
" json.dump(courses, file, indent=4)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"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.12.2" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |