openai/openai-python

Public

mirrored fromhttps://github.com/openai/openai-pythonAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.15.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

examples/embeddings/Get_embeddings.ipynb

107lines · modecode

1{
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "metadata": {},
6 "source": [
7 "## Get embeddings\n",
8 "\n",
9 "The function `get_embedding` will give us an embedding for an input text."
10 ]
11 },
12 {
13 "cell_type": "code",
14 "execution_count": 1,
15 "metadata": {},
16 "outputs": [
17 {
18 "data": {
19 "text/plain": [
20 "12288"
21 ]
22 },
23 "execution_count": 1,
24 "metadata": {},
25 "output_type": "execute_result"
26 }
27 ],
28 "source": [
29 "import openai\n",
30 "\n",
31 "embedding = openai.Embedding.create(input=\"Sample document text goes here\", engine=\"text-similarity-davinci-001\")['data'][0]['embedding']\n",
32 "len(embedding)"
33 ]
34 },
35 {
36 "cell_type": "code",
37 "execution_count": 2,
38 "metadata": {},
39 "outputs": [
40 {
41 "name": "stdout",
42 "output_type": "stream",
43 "text": [
44 "1024\n"
45 ]
46 }
47 ],
48 "source": [
49 "import openai\n",
50 "from tenacity import retry, wait_random_exponential, stop_after_attempt\n",
51 "\n",
52 "@retry(wait=wait_random_exponential(min=1, max=20), stop=stop_after_attempt(6))\n",
53 "def get_embedding(text: str, engine=\"text-similarity-davinci-001\") -> List[float]:\n",
54 "\n",
55 " # replace newlines, which can negatively affect performance.\n",
56 " text = text.replace(\"\\n\", \" \")\n",
57 "\n",
58 " return openai.Embedding.create(input=[text], engine=engine)[\"data\"][0][\"embedding\"]\n",
59 "\n",
60 "embedding = get_embedding(\"Sample query text goes here\", engine=\"text-search-ada-query-001\")\n",
61 "print(len(embedding))"
62 ]
63 },
64 {
65 "cell_type": "code",
66 "execution_count": 53,
67 "metadata": {},
68 "outputs": [
69 {
70 "name": "stdout",
71 "output_type": "stream",
72 "text": [
73 "1024\n"
74 ]
75 }
76 ],
77 "source": [
78 "embedding = get_embedding(\"Sample document text goes here\", engine=\"text-search-ada-doc-001\")\n",
79 "print(len(embedding))"
80 ]
81 }
82 ],
83 "metadata": {
84 "interpreter": {
85 "hash": "be4b5d5b73a21c599de40d6deb1129796d12dc1cc33a738f7bac13269cfcafe8"
86 },
87 "kernelspec": {
88 "display_name": "Python 3.7.3 64-bit ('base': conda)",
89 "name": "python3"
90 },
91 "language_info": {
92 "codemirror_mode": {
93 "name": "ipython",
94 "version": 3
95 },
96 "file_extension": ".py",
97 "mimetype": "text/x-python",
98 "name": "python",
99 "nbconvert_exporter": "python",
100 "pygments_lexer": "ipython3",
101 "version": "3.7.3"
102 },
103 "orig_nbformat": 4
104 },
105 "nbformat": 4,
106 "nbformat_minor": 2
107}
108