-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from sarthak247/llama
Add support from Llama2 model from Llama-cpp
- Loading branch information
Showing
5 changed files
with
180 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,6 @@ venv/* | |
.env | ||
*.pkl | ||
models/ | ||
*.faiss/ | ||
__pycache__/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
""" | ||
Filename: openai.py | ||
Description: Implements functions needed to work around with Llama.cpp for QA | ||
""" | ||
import os | ||
from langchain_community.embeddings import LlamaCppEmbeddings | ||
from langchain_community.vectorstores import FAISS | ||
from langchain_community.llms import LlamaCpp | ||
from langchain_core.prompts import PromptTemplate | ||
|
||
# Setup LLM | ||
llm = LlamaCpp(model_path = './models/llama-2-7b-chat.Q2_K.gguf', | ||
temperature = 0.75, | ||
max_tokens = 2000, | ||
top_p = 1, | ||
verbose = False, | ||
n_gpu_layers = -1, | ||
n_batch = 128, | ||
n_ctx = 1024) | ||
|
||
# Sample Template | ||
TEMPLATE = """Use the following pieces of context to answer the question at the end. | ||
If you don't know the answer, just say that you don't know, don't try to make up an answer. | ||
{context} | ||
Question: {question} | ||
Answer:""" | ||
prompt = PromptTemplate.from_template(TEMPLATE) | ||
|
||
def get_llama_embeddings(chunks, store_name): | ||
""" | ||
Parameters: | ||
- chunks: text to turn into embeddings | ||
- store_name : The name of the store from which to load in | ||
case of existing embeddings or create and save to | ||
Return: An instance of FAISS Vectorstore | ||
""" | ||
embeddings = LlamaCppEmbeddings(model_path = './models/llama-2-7b-chat.Q2_K.gguf', | ||
n_gpu_layers = -1, verbose = False) | ||
if os.path.exists(store_name): | ||
vectorstore = FAISS.load_local(store_name, embeddings, allow_dangerous_deserialization=True) | ||
else: | ||
# Convert chunks -> Embeddings | ||
vectorstore = FAISS.from_texts(chunks, embedding=embeddings) | ||
vectorstore.save_local(store_name) | ||
return vectorstore | ||
|
||
def get_llama_answers(vectorstore, query, k): | ||
""" | ||
Parameters: | ||
- vectorstore: Vector Store of chunks of texts and their embeddings | ||
- Query: Question to ask to the LLM | ||
- k: Number of top k matching documents from similarity search | ||
Return: Response from llama model | ||
""" | ||
docs = vectorstore.similarity_search(query, k) | ||
|
||
# Extract context | ||
context = '' | ||
for doc in docs: | ||
context += doc.page_content | ||
|
||
# Setup chain | ||
llm_chain = prompt | llm | ||
response = llm_chain.invoke({'context' : context, 'question' : query}) | ||
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
""" | ||
Filename: openai.py | ||
Description: Implements functions needed to work around with OpenAI API | ||
""" | ||
import os | ||
from langchain_openai import OpenAIEmbeddings, ChatOpenAI | ||
from langchain_community.vectorstores import FAISS | ||
from langchain.chains.question_answering import load_qa_chain | ||
from langchain_community.callbacks.manager import get_openai_callback | ||
|
||
def get_openai_embeddings(chunks, store_name): | ||
""" | ||
Parameters: | ||
- chunks: text to turn into embeddings | ||
- store_name : The name of the store from which to load in | ||
case of existing embeddings or create and save to | ||
Return: An instance of FAISS Vectorstore | ||
""" | ||
embeddings = OpenAIEmbeddings() | ||
if os.path.exists(store_name): | ||
vectorstore = FAISS.load_local(store_name, embeddings, allow_dangerous_deserialization=True) | ||
else: | ||
# Convert chunks -> Embeddings | ||
vectorstore = FAISS.from_texts(chunks, embedding=embeddings) | ||
vectorstore.save_local(store_name) | ||
return vectorstore | ||
|
||
|
||
def get_openai_answers(vectorstore, query, k): | ||
""" | ||
Parameters: | ||
- vectorstore: Vector Store of chunks of texts and their embeddings | ||
- Query: Question to ask to the LLM | ||
- k: Number of top k matching documents from similarity search | ||
Return: Response from OpenAI API | ||
""" | ||
docs = vectorstore.similarity_search(query, k) | ||
# Setup LLM | ||
llm = ChatOpenAI(temperature=0, model_name = "gpt-3.5-turbo") | ||
|
||
# Setup QA Chain and query it | ||
chain = load_qa_chain(llm = llm, chain_type = "stuff") | ||
input_data = {'input_documents' : docs, 'question' : query} | ||
with get_openai_callback() as cb: | ||
response = chain.invoke(input=input_data) | ||
print(cb) | ||
return response['output_text'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters