{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from openai import OpenAI\n", "import json\n", "import re\n", "import time\n", "import tiktoken\n", "import shelve\n", "from tqdm.auto import tqdm" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "client = OpenAI(\n", " api_key='', # this is also the default, it can be omitted\n", ")" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "project = \"maven\"" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "# functions = json.load(open(\"p_functions_with_times.json\",\"r\",encoding=\"utf-8\"))\n", "issues = json.load(open(f\"{project}_issues_new.json\",\"r\"))" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "tokenizer = tiktoken.encoding_for_model(\"text-embedding-3-small\")\n", "def estimate_tokens_tiktoken(text):\n", " tokens = tokenizer.encode(text)\n", " return len(tokens)\n", "\n", "def get_text_embedding(text):\n", " return client.embeddings.create(input = [text], model=\"text-embedding-3-small\").data[0].embedding\n", "\n", "def split_text(text, max_tokens=8192):\n", " words = text.split()\n", " chunks = []\n", " current_chunk = []\n", " current_tokens = 0\n", "\n", " for word in words:\n", " current_tokens += estimate_tokens_tiktoken(word + ' ') # 단어와 공백 포함하여 토큰 계산\n", " if current_tokens > max_tokens:\n", " chunks.append(\" \".join(current_chunk))\n", " current_chunk = []\n", " current_tokens = estimate_tokens_tiktoken(word + ' ') # 새로운 청크의 첫 단어 토큰 수로 초기화\n", " current_chunk.append(word)\n", "\n", " if current_chunk:\n", " chunks.append(\" \".join(current_chunk))\n", "\n", " return chunks\n", "\n", "\n", "def get_long_text_embedding(text, max_tokens=8192, delay_per_token=0.02):\n", " chunks = split_text(text, max_tokens)\n", " # embeddings = [(len(chunk),get_text_embedding(chunk)) for chunk in chunks]\n", " embeddings = []\n", " for chunk in chunks:\n", " tokens = estimate_tokens_tiktoken(chunk)\n", " embeddings.append((len(chunk),get_text_embedding(chunk)))\n", " time.sleep(tokens * delay_per_token)\n", " return embeddings" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "# new_issues = []\n", "# for issue in tqdm(issues):\n", "# # print(issue.keys())\n", "# if issue[\"title\"] and issue[\"body\"]:\n", "# title = re.sub(r\"\\s+\",\" \",issue[\"title\"])\n", "# description = re.sub(r\"\\s+\",\" \",issue[\"body\"])\n", "# embeddings = get_long_text_embedding(\" \".join([title,description]))\n", "# # print(embeddings)\n", "# issue[\"embeddings\"] = embeddings\n", "# new_issues.append(issue)\n", "# # break\n", "\n", "# json.dump(new_issues,open(f\"{project}_issues_with_text-emberdding-3-small.json\",\"w\"))" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "af7c03b537a9445f88a613bf355e1279", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/283 [00:00