Files
langchain/docs/extras/integrations/text_embedding/openai.ipynb
T
Charles LanahanandBagatur a2588d6c57 Update openai embeddings notebook with correct embedding model in section 2 (#5831)
In second section it looks like a copy/paste from the first section and
doesn't include the specific embedding model mentioned in the example so
I added it for clarity.
---------

Co-authored-by: Bagatur <baskaryan@gmail.com>
2023-08-10 19:02:10 -07:00

5.3 KiB

OpenAI

Let's load the OpenAI Embedding class.

In [6]:
from langchain.embeddings import OpenAIEmbeddings
In [29]:
embeddings = OpenAIEmbeddings()
In [30]:
text = "This is a test document."
In [31]:
query_result = embeddings.embed_query(text)
In [32]:
query_result[:5]
Out [32]:
[-0.003186025367556387,
 0.011071979803637493,
 -0.004020420763285827,
 -0.011658221276953042,
 -0.0010534035786864363]
In [33]:
doc_result = embeddings.embed_documents([text])
In [34]:
doc_result[0][:5]
Out [34]:
[-0.003186025367556387,
 0.011071979803637493,
 -0.004020420763285827,
 -0.011658221276953042,
 -0.0010534035786864363]

Let's load the OpenAI Embedding class with first generation models (e.g. text-search-ada-doc-001/text-search-ada-query-001). Note: These are not recommended models - see here

In [1]:
from langchain.embeddings.openai import OpenAIEmbeddings
In [23]:
embeddings = OpenAIEmbeddings(model="text-search-ada-doc-001")
In [24]:
text = "This is a test document."
In [25]:
query_result = embeddings.embed_query(text)
In [26]:
query_result[:5]
Out [26]:
[0.004452846988523035,
 0.034550655976098514,
 -0.015029939040690051,
 0.03827273883655212,
 0.005785414075152477]
In [27]:
doc_result = embeddings.embed_documents([text])
In [28]:
doc_result[0][:5]
Out [28]:
[0.004452846988523035,
 0.034550655976098514,
 -0.015029939040690051,
 0.03827273883655212,
 0.005785414075152477]
In [ ]:
# if you are behind an explicit proxy, you can use the OPENAI_PROXY environment variable to pass through
os.environ["OPENAI_PROXY"] = "http://proxy.yourcompany.com:8080"