2023-05-12 20:20:07 +00:00
|
|
|
|
|
|
|
import os
|
|
|
|
from langchain.vectorstores import Chroma
|
|
|
|
from langchain.embeddings import OpenAIEmbeddings
|
2023-05-18 20:50:21 +00:00
|
|
|
from langchain.text_splitter import CharacterTextSplitter
|
2023-05-12 20:20:07 +00:00
|
|
|
from langchain.document_loaders import TextLoader
|
|
|
|
|
|
|
|
base_path = os.environ.get('OPENAI_API_BASE', 'http://localhost:8080/v1')
|
|
|
|
|
|
|
|
# Load and process the text
|
|
|
|
loader = TextLoader('state_of_the_union.txt')
|
|
|
|
documents = loader.load()
|
|
|
|
|
|
|
|
text_splitter = CharacterTextSplitter(chunk_size=300, chunk_overlap=70)
|
|
|
|
texts = text_splitter.split_documents(documents)
|
|
|
|
|
|
|
|
# Embed and store the texts
|
|
|
|
# Supplying a persist_directory will store the embeddings on disk
|
|
|
|
persist_directory = 'db'
|
|
|
|
|
2023-07-27 16:41:53 +00:00
|
|
|
embedding = OpenAIEmbeddings(model="text-embedding-ada-002", openai_api_base=base_path)
|
2023-05-12 20:20:07 +00:00
|
|
|
vectordb = Chroma.from_documents(documents=texts, embedding=embedding, persist_directory=persist_directory)
|
|
|
|
|
|
|
|
vectordb.persist()
|
2023-07-27 16:41:53 +00:00
|
|
|
vectordb = None
|