2022-03-11 02:08:53 +00:00
|
|
|
{
|
|
|
|
"cells": [
|
|
|
|
{
|
|
|
|
"cell_type": "markdown",
|
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
|
|
|
"## Get embeddings\n",
|
|
|
|
"\n",
|
|
|
|
"The function `get_embedding` will give us an embedding for an input text."
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": 1,
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"data": {
|
|
|
|
"text/plain": [
|
2022-12-13 23:28:39 +00:00
|
|
|
"1536"
|
2022-03-11 02:08:53 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
"execution_count": 1,
|
|
|
|
"metadata": {},
|
|
|
|
"output_type": "execute_result"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"source": [
|
|
|
|
"import openai\n",
|
|
|
|
"\n",
|
2022-07-11 23:56:47 +00:00
|
|
|
"embedding = openai.Embedding.create(\n",
|
2023-01-10 18:37:25 +00:00
|
|
|
" input=\"Your text goes here\", model=\"text-embedding-ada-002\"\n",
|
2022-07-11 23:56:47 +00:00
|
|
|
")[\"data\"][0][\"embedding\"]\n",
|
|
|
|
"len(embedding)\n"
|
2022-03-11 02:08:53 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": 2,
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"name": "stdout",
|
|
|
|
"output_type": "stream",
|
|
|
|
"text": [
|
2022-12-13 23:28:39 +00:00
|
|
|
"1536\n"
|
2022-03-11 02:08:53 +00:00
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"source": [
|
|
|
|
"import openai\n",
|
|
|
|
"from tenacity import retry, wait_random_exponential, stop_after_attempt\n",
|
|
|
|
"\n",
|
2022-07-11 23:56:47 +00:00
|
|
|
"\n",
|
2022-03-11 02:08:53 +00:00
|
|
|
"@retry(wait=wait_random_exponential(min=1, max=20), stop=stop_after_attempt(6))\n",
|
2023-01-10 18:37:25 +00:00
|
|
|
"def get_embedding(text: str, model=\"text-embedding-ada-002\") -> list[float]:\n",
|
|
|
|
" return openai.Embedding.create(input=[text], model=model)[\"data\"][0][\"embedding\"]\n",
|
2022-03-11 02:08:53 +00:00
|
|
|
"\n",
|
|
|
|
"\n",
|
2023-01-10 18:37:25 +00:00
|
|
|
"embedding = get_embedding(\"Your text goes here\", model=\"text-embedding-ada-002\")\n",
|
2022-07-11 23:56:47 +00:00
|
|
|
"print(len(embedding))\n"
|
2022-03-11 02:08:53 +00:00
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"metadata": {
|
|
|
|
"kernelspec": {
|
2022-07-11 23:56:47 +00:00
|
|
|
"display_name": "Python 3.9.9 ('openai')",
|
|
|
|
"language": "python",
|
2022-03-11 02:08:53 +00:00
|
|
|
"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",
|
2022-07-11 23:56:47 +00:00
|
|
|
"version": "3.9.9"
|
2022-03-11 02:08:53 +00:00
|
|
|
},
|
2022-07-11 23:56:47 +00:00
|
|
|
"orig_nbformat": 4,
|
|
|
|
"vscode": {
|
|
|
|
"interpreter": {
|
|
|
|
"hash": "365536dcbde60510dc9073d6b991cd35db2d9bac356a11f5b64279a5e6708b97"
|
|
|
|
}
|
|
|
|
}
|
2022-03-11 02:08:53 +00:00
|
|
|
},
|
|
|
|
"nbformat": 4,
|
|
|
|
"nbformat_minor": 2
|
|
|
|
}
|