Skip to content

Commit

Permalink
working version
Browse files Browse the repository at this point in the history
  • Loading branch information
IFFranciscoME committed Oct 21, 2024
1 parent 6133e7e commit 4926e87
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 39 deletions.
27 changes: 26 additions & 1 deletion python/molina/content/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
from langchain_core.documents import Document
# from langchain_community.vectorstores.chroma import Chroma
from langchain_community.embeddings import GPT4AllEmbeddings

from content import documents
from generators import gpt4all
# last
from langchain_chroma import Chroma

Expand Down Expand Up @@ -53,6 +54,9 @@ def create_vectordb(db_path: str, chunks: list[Document], embedding_model, verbo

return db3

# -- ------------------------------------------------------------------------------- -- #
# -- ------------------------------------------------------------------------------- -- #

def search_vectordb(client, query):

found_search = client.similarity_search(query)
Expand All @@ -62,3 +66,24 @@ def search_vectordb(client, query):
found_l.append(found_search[i].model_dump()["page_content"])

return found_l

# -- ------------------------------------------------------------------------------- -- #
# -- ------------------------------------------------------------------------------- -- #

def setup_vectordb(db_path: str, docs_path: str):
"""
Setup the vector database with documents.
"""

k_documents = documents.load_documents(data_path=docs_path)
ks_documents = documents.split_text(k_documents, c_size=1000, c_overlap=200)

chromadb = create_vectordb(
db_path=db_path,
chunks=ks_documents,
embedding_model=gpt4all.embedding_model,
verbose=True
)

return chromadb

24 changes: 15 additions & 9 deletions python/molina/generators/llama.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@
def embeddings_model():
"""
# to access to the model, for the case of meta-llama
# You need to agree to share your contact information
# For my user the following has already been granted
# https://huggingface.co/meta-llama/Llama-3.2-3B
# consequently, a login is needed
"""

checkpoint = "meta-llama/Llama-3.2-3B-Instruct"
Expand Down Expand Up @@ -43,7 +51,10 @@ def generation_model():
# ---------------------------------------------------------------------------------- -- #
# ---------------------------------------------------------------------------------- -- #

def ask_query(retrieved_texts, generation_query):
def ask_query(retrieved_texts, generation_query, generation_model):
"""
"""

# Prepare the messages for the text generation pipeline
messages = [
Expand All @@ -63,14 +74,9 @@ def ask_query(retrieved_texts, generation_query):
]

# Generate a response using the text generation pipeline
generator = generation_model()
response = generator(messages, max_new_tokens=256) #[-1]["generated_text"][-1]["content"]
# response = response[-1]['generated_text'][-1]['content']

#print(f"Query: \n\t{query}")
#print(f"Context: \n\t{retrieved_texts}")
#print(f"Answer: \n\t{response}")

# generator = generation_model()
response = generation_model(messages, max_new_tokens=128)
r_answer = {'query': generation_query, 'context': retrieved_texts, 'response': response}

return r_answer

70 changes: 41 additions & 29 deletions python/molina/main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,35 @@
import warnings
warnings.filterwarnings("ignore", category=UserWarning)

from content import documents, vector
from generators import llama, gpt4all
from content import vector
from generators import llama

def chat_with_user(chromadb, model_gen):
"""
Chat interface for user interaction.
"""

print("Welcome to the Chatbot! Type 'exit' to quit.")

while True:
context_query = input("\nEnter context query: ")
if context_query.lower() == 'exit':
break

generation_query = input("Enter your question: ")
if generation_query.lower() == 'exit':
break

# Retrieve relevant texts based on the context query
retrieved_texts = vector.search_vectordb(chromadb, context_query)

# Generate an answer based on the retrieved texts
llama_answer = llama.ask_query(retrieved_texts, generation_query, model_gen)
a_specific = llama_answer['response'][-1]['generated_text'][-1]['content']

# Output the results
print(f'\nProvided Context:\n{retrieved_texts}\n')
print(f'Agent Answer:\n{a_specific}\n')

def main():

Expand All @@ -15,40 +42,25 @@ def main():
db_path = "knowledge/chroma"
docs_path = "knowledge/molina_compatible"

wd_folder = "/Users/franciscome/git/iteralabs/molina"
in_knowledge = "/knowledge"
in_topic = "/molina_compatible"
in_folder = wd_folder + in_knowledge + in_topic

# -- -------------------------------------------- Extract/Load/Transform Content -- #
# -- --------------------------------------------------------------------------- -- #

k_documents = documents.load_documents(data_path=docs_path)
ks_documents = documents.split_text(k_documents, c_size=1000, c_overlap=100)

# -- ---------------------------------------------------------- Create Documents -- #
# -- ---------------------------------------------------------- ---------------- -- #

chromadb = vector.create_vectordb(db_path=db_path,
chunks=ks_documents,
embedding_model=gpt4all.embedding_model,
verbose=True)
chromadb = vector.setup_vectordb(db_path=db_path, docs_path=docs_path)

# -- ------------------------------------------------------ Create Embeddings DB -- #
# -- --------------------------------------------------------------------------- -- #

model_gen = llama.generation_model()

context_query = "large language models, llm"

generation_query = "what is the perplexity of a Retrieval-based language model?"

retrieved_texts = vector.search_vectordb(chromadb, context_query)
llama_answer = llama.ask_query(retrieved_texts, generation_query)
a_specific = llama_answer['response'][-1]['generated_text'][-1]['content']

# -- --------------------------------------------------------- Output Formatting -- #
# -- --------------------------------------------------------------------------- -- #

print(f'\n agent answer: ', a_specific, '\n')
"""
context: augmented retrieval generation RAG retrieval-based LLM Language Models
question: what are the major drawbacks of Retrival augmented generation RAG models ?
"""

# Start chat with user
chat_with_user(chromadb, model_gen)

if __name__ == "__main__":
main()
Expand Down

0 comments on commit 4926e87

Please sign in to comment.