This commit is contained in:
Ted Sanders 2022-07-11 16:56:47 -07:00
parent 0938a054b3
commit da8725cdd7

View File

@ -28,8 +28,11 @@
"source": [ "source": [
"import openai\n", "import openai\n",
"\n", "\n",
"embedding = openai.Embedding.create(input=\"Sample document text goes here\", engine=\"text-similarity-davinci-001\")['data'][0]['embedding']\n", "embedding = openai.Embedding.create(\n",
"len(embedding)" " input=\"Sample document text goes here\",\n",
" engine=\"text-similarity-davinci-001\"\n",
")[\"data\"][0][\"embedding\"]\n",
"len(embedding)\n"
] ]
}, },
{ {
@ -49,21 +52,23 @@
"import openai\n", "import openai\n",
"from tenacity import retry, wait_random_exponential, stop_after_attempt\n", "from tenacity import retry, wait_random_exponential, stop_after_attempt\n",
"\n", "\n",
"\n",
"@retry(wait=wait_random_exponential(min=1, max=20), stop=stop_after_attempt(6))\n", "@retry(wait=wait_random_exponential(min=1, max=20), stop=stop_after_attempt(6))\n",
"def get_embedding(text: str, engine=\"text-similarity-davinci-001\") -> List[float]:\n", "def get_embedding(text: str, engine=\"text-similarity-davinci-001\") -> list[float]:\n",
"\n", "\n",
" # replace newlines, which can negatively affect performance.\n", " # replace newlines, which can negatively affect performance.\n",
" text = text.replace(\"\\n\", \" \")\n", " text = text.replace(\"\\n\", \" \")\n",
"\n", "\n",
" return openai.Embedding.create(input=[text], engine=engine)[\"data\"][0][\"embedding\"]\n", " return openai.Embedding.create(input=[text], engine=engine)[\"data\"][0][\"embedding\"]\n",
"\n", "\n",
"\n",
"embedding = get_embedding(\"Sample query text goes here\", engine=\"text-search-ada-query-001\")\n", "embedding = get_embedding(\"Sample query text goes here\", engine=\"text-search-ada-query-001\")\n",
"print(len(embedding))" "print(len(embedding))\n"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 53, "execution_count": 3,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -76,16 +81,14 @@
], ],
"source": [ "source": [
"embedding = get_embedding(\"Sample document text goes here\", engine=\"text-search-ada-doc-001\")\n", "embedding = get_embedding(\"Sample document text goes here\", engine=\"text-search-ada-doc-001\")\n",
"print(len(embedding))" "print(len(embedding))\n"
] ]
} }
], ],
"metadata": { "metadata": {
"interpreter": {
"hash": "be4b5d5b73a21c599de40d6deb1129796d12dc1cc33a738f7bac13269cfcafe8"
},
"kernelspec": { "kernelspec": {
"display_name": "Python 3.7.3 64-bit ('base': conda)", "display_name": "Python 3.9.9 ('openai')",
"language": "python",
"name": "python3" "name": "python3"
}, },
"language_info": { "language_info": {
@ -98,9 +101,14 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.7.3" "version": "3.9.9"
}, },
"orig_nbformat": 4 "orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "365536dcbde60510dc9073d6b991cd35db2d9bac356a11f5b64279a5e6708b97"
}
}
}, },
"nbformat": 4, "nbformat": 4,
"nbformat_minor": 2 "nbformat_minor": 2