Spaces:
Runtime error
Runtime error
DiamondYin
commited on
Commit
·
db6b197
1
Parent(s):
9b281dd
Update app_utils.py
Browse files- app_utils.py +18 -1
app_utils.py
CHANGED
@@ -14,6 +14,21 @@ from langchain.vectorstores import Chroma # Chroma is a class in the langchain.v
|
|
14 |
from langchain.document_loaders import DirectoryLoader #
|
15 |
from langchain.embeddings.openai import OpenAIEmbeddings # OpenAIGPTEmbeddings
|
16 |
from langchain.text_splitter import CharacterTextSplitter # CharacterTextSplitter is a class in the langchain.text_splitter module that can be used to split text into chunks.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
|
19 |
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
|
@@ -47,7 +62,9 @@ def initialize_knowledge_base():
|
|
47 |
openAI_embeddings = OpenAIEmbeddings()
|
48 |
for doc in doc_texts:
|
49 |
text = str(doc)
|
50 |
-
embedding = openAI_embeddings.embed_documents(text)
|
|
|
|
|
51 |
embeddings.append(embedding)
|
52 |
|
53 |
vStore = np.concatenate(embeddings, axis=0)
|
|
|
14 |
from langchain.document_loaders import DirectoryLoader #
|
15 |
from langchain.embeddings.openai import OpenAIEmbeddings # OpenAIGPTEmbeddings
|
16 |
from langchain.text_splitter import CharacterTextSplitter # CharacterTextSplitter is a class in the langchain.text_splitter module that can be used to split text into chunks.
|
17 |
+
from tenacity import (
|
18 |
+
retry,
|
19 |
+
stop_after_attempt,
|
20 |
+
wait_random_exponential,
|
21 |
+
)
|
22 |
+
|
23 |
+
# FUNCTIONS
|
24 |
+
|
25 |
+
# get embeddings
|
26 |
+
@retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(6))
|
27 |
+
@st.cache_data
|
28 |
+
def embedding_from_string(input: str, model: str) -> list:
|
29 |
+
response = openai.Embedding.create(input=input, model=model)
|
30 |
+
embedding = response["data"][0]["embedding"]
|
31 |
+
return embedding
|
32 |
|
33 |
|
34 |
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
|
|
|
62 |
openAI_embeddings = OpenAIEmbeddings()
|
63 |
for doc in doc_texts:
|
64 |
text = str(doc)
|
65 |
+
#embedding = openAI_embeddings.embed_documents(text)
|
66 |
+
#embeddings.append(embedding)
|
67 |
+
embedding = embedding_from_string(text, "text-embedding-ada-002")
|
68 |
embeddings.append(embedding)
|
69 |
|
70 |
vStore = np.concatenate(embeddings, axis=0)
|