mirror of
https://github.com/openai/openai-cookbook
synced 2024-11-09 19:10:56 +00:00
93 lines
2.0 KiB
Plaintext
93 lines
2.0 KiB
Plaintext
{
|
|
"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": [
|
|
"1536"
|
|
]
|
|
},
|
|
"execution_count": 1,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"import openai\n",
|
|
"\n",
|
|
"embedding = openai.Embedding.create(\n",
|
|
" input=\"Your text goes here\", model=\"text-embedding-ada-002\"\n",
|
|
")[\"data\"][0][\"embedding\"]\n",
|
|
"len(embedding)\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"1536\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"import openai\n",
|
|
"from tenacity import retry, wait_random_exponential, stop_after_attempt\n",
|
|
"\n",
|
|
"\n",
|
|
"@retry(wait=wait_random_exponential(min=1, max=20), stop=stop_after_attempt(6))\n",
|
|
"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",
|
|
"\n",
|
|
"\n",
|
|
"embedding = get_embedding(\"Your text goes here\", model=\"text-embedding-ada-002\")\n",
|
|
"print(len(embedding))\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3.9.9 ('openai')",
|
|
"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.9.9"
|
|
},
|
|
"orig_nbformat": 4,
|
|
"vscode": {
|
|
"interpreter": {
|
|
"hash": "365536dcbde60510dc9073d6b991cd35db2d9bac356a11f5b64279a5e6708b97"
|
|
}
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|