因此,我在这里尝试做的是让我的代码读取和interpet一个excel表,并使用语义搜索来使用向量和FAISS找到最接近的数据点,当我调用. getembeddings时遇到了一个问题。
这是我的代码
import os
import pandas as pd
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.text_splitter import CharacterTextSplitter
from langchain.vectorstores import FAISS
# Set OpenAI API Key
os.environ["OPENAI_API_KEY"] = ""
# Initialize classes
embedding = OpenAIEmbeddings()
# Load data from Excel file
df = pd.read_excel('sampledata.xlsx')
# We'll use the 'Item' column for creating our vector database
texts = df['Item'].tolist()
text_splitter = CharacterTextSplitter.from_tiktoken_encoder(chunk_size=10, chunk_overlap=0)
split_texts = []
for text in texts:
docs = text_splitter.split_text(text)
for d in docs:
split_texts.append(d)
# Vector DB creation
vectordb = FAISS.from_texts(texts=split_texts, embedding=embedding) # remove persist_directory
# Query
query = "Pen"
query_vector = embedding.get_embeddings([query])
# Search
top_k = 10 # number of results to return
distances, indices = vectordb.search(query_vector, top_k)
# Print the results
for i in range(top_k):
print(f"Item: {texts[indices[i]]}, Distance: {distances[i]}")
vectordb = None
vectordb = None
我收到的错误是
> File "c:\Users\rbalakrishnan\OneDrive - Eastside Preparatory School\Terawe\pdfreader.py", line 33, in <module>
> query_vector = embedding.get_embeddings([query])
query_vector = embedding.get_embeddings([query])
^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'OpenAIEmbeddings' object has no attribute 'get_embeddings'
1条答案
按热度按时间nbnkbykc1#
我相信你可能会被openai挡住。也许尝试切换到VPN,看看效果如何?
干杯
编辑:
因为这个错误现在只与get_embeddings调用有关。我相信你应该先尝试定义你的get_embeddings函数。
OpenAI文档:https://platform.openai.com/docs/guides/embeddings/use-cases