-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpt4all-local-file-live.py
74 lines (54 loc) · 2.6 KB
/
gpt4all-local-file-live.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# Local gpt4all LLM Query system wuth access to local files. (No remote or cloud systems used)
#
# Start here : https://github.com/nomic-ai/gpt4all
# Code taken/ideas from:
#
# https://clemenssiebler.com/posts/chatting-private-data-langchain-azure-openai-service/
# https://blog.ouseful.info/2023/04/04/langchain-query-gpt4all-against-knowledge-source/
#
# model : https://github.com/nomic-ai/gpt4all-chat#manual-download-of-models
#
# Save your short text file in store/txt
# There are a number of Python libraries needed
#
# pip3 install langchain tiktoken pyllamacpp pyllama transformers pygpt4all llama-cpp-python faiss-cpu
#
# I copied some data from Wikipedia of ex-President Obama (random politician) as a test and made a query
GPT4ALL_MODEL_PATH = "./models/ggml-gpt4all-l13b-snoozy.bin"
from langchain.llms import LlamaCpp
#v Generate text
#response = model("Once upon a time, ")
from langchain.document_loaders import DirectoryLoader
from langchain.document_loaders import TextLoader
from langchain.text_splitter import TokenTextSplitter
from langchain.embeddings import LlamaCppEmbeddings
embeddings = LlamaCppEmbeddings(model_path=GPT4ALL_MODEL_PATH)
loader = DirectoryLoader('store/txt', glob="*.txt", loader_cls=TextLoader)
documents = loader.load()
text_splitter = TokenTextSplitter(chunk_size=100, chunk_overlap=0)
docs = text_splitter.split_documents(documents)
from langchain.vectorstores import FAISS
db = FAISS.from_documents(documents=docs, embedding=embeddings)
from langchain.chains import ConversationalRetrievalChain
from langchain.prompts import PromptTemplate
# Adapt if needed
CONDENSE_QUESTION_PROMPT = PromptTemplate.from_template("""Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question.
Chat History:
{chat_history}
Follow Up Input: {question}
Standalone question:""")
llm = LlamaCpp(model_path=GPT4ALL_MODEL_PATH)
# added search_kwargs={"k": 1} to avoid "ValueError: Requested tokens exceed context window of 512"
qa = ConversationalRetrievalChain.from_llm(llm=llm,
retriever=db.as_retriever(search_kwargs={"k": 1}),
condense_question_prompt=CONDENSE_QUESTION_PROMPT,
return_source_documents=True,
verbose=False)
chat_history = []
query = """What did Obama do in his first 100 days ? Provide the answer in the form:
- ITEM 1
- ITEM 2
- ITEM 3"""
result = qa({"question": query, "chat_history": chat_history})
print("Question:", query)
print("Answer:", result["answer"])