mirror of
https://github.com/langchain-ai/langchain.git
synced 2026-10-05 17:35:28 +03:00
**About**
Specify encoding to avoid UnicodeDecodeError when reading .txt for users
who are following the tutorial.
**Reference**
```
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 1205: character maps to <undefined>
```
**Environment**
OS: Win 11
Python: 3.8
13 KiB
13 KiB
In [2]:
from abc import ABC, abstractmethod
from typing import List
from langchain.schema import Document
class BaseRetriever(ABC):
@abstractmethod
def get_relevant_documents(self, query: str) -> List[Document]:
"""Get texts relevant for a query.
Args:
query: string to find relevant texts for
Returns:
List of relevant documents
"""In [3]:
from langchain.chains import RetrievalQA
from langchain.llms import OpenAIIn [19]:
from langchain.document_loaders import TextLoader
loader = TextLoader('../state_of_the_union.txt', encoding='utf8')In [5]:
from langchain.indexes import VectorstoreIndexCreatorIn [6]:
index = VectorstoreIndexCreator().from_loaders([loader])Running Chroma using direct local API. Using DuckDB in-memory for database. Data will be transient.
In [7]:
query = "What did the president say about Ketanji Brown Jackson"
index.query(query)Out [7]:
" The president said that Ketanji Brown Jackson is one of the nation's top legal minds, a former top litigator in private practice, a former federal public defender, and from a family of public school educators and police officers. He also said that she is a consensus builder and has received a broad range of support from the Fraternal Order of Police to former judges appointed by Democrats and Republicans."
In [8]:
query = "What did the president say about Ketanji Brown Jackson"
index.query_with_sources(query)Out [8]:
{'question': 'What did the president say about Ketanji Brown Jackson',
'answer': " The president said that he nominated Circuit Court of Appeals Judge Ketanji Brown Jackson, one of the nation's top legal minds, to continue Justice Breyer's legacy of excellence, and that she has received a broad range of support from the Fraternal Order of Police to former judges appointed by Democrats and Republicans.\n",
'sources': '../state_of_the_union.txt'}In [9]:
index.vectorstoreOut [9]:
<langchain.vectorstores.chroma.Chroma at 0x119aa5940>
In [10]:
index.vectorstore.as_retriever()Out [10]:
VectorStoreRetriever(vectorstore=<langchain.vectorstores.chroma.Chroma object at 0x119aa5940>, search_kwargs={})In [11]:
documents = loader.load()In [12]:
from langchain.text_splitter import CharacterTextSplitter
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
texts = text_splitter.split_documents(documents)In [13]:
from langchain.embeddings import OpenAIEmbeddings
embeddings = OpenAIEmbeddings()In [14]:
from langchain.vectorstores import Chroma
db = Chroma.from_documents(texts, embeddings)Running Chroma using direct local API. Using DuckDB in-memory for database. Data will be transient.
In [15]:
retriever = db.as_retriever()In [16]:
qa = RetrievalQA.from_chain_type(llm=OpenAI(), chain_type="stuff", retriever=retriever)In [17]:
query = "What did the president say about Ketanji Brown Jackson"
qa.run(query)Out [17]:
" The President said that Judge Ketanji Brown Jackson is one of the nation's top legal minds, a former top litigator in private practice, a former federal public defender, and from a family of public school educators and police officers. He said she is a consensus builder and has received a broad range of support from organizations such as the Fraternal Order of Police and former judges appointed by Democrats and Republicans."
In [14]:
index_creator = VectorstoreIndexCreator(
vectorstore_cls=Chroma,
embedding=OpenAIEmbeddings(),
text_splitter=CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
)In [ ]: