Skip to content

Viynne/Embeddings_Vectors_Search

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

Generative Q&A with ChromDB and HuggingFace Transformers

This Python notebook demonstrates how to build a simple generative question-answering (Q&A) system using ChromDB and HuggingFace Transformers. The notebook provides step-by-step instructions and code snippets to help you understand and implement the Q&A model.

Prerequisites

To run this notebook, you need the following prerequisites:

  1. Python 3.7 or above
  2. ChromDB installed (Refer to ChromDB's documentation for installation instructions)
  3. Necessary Python libraries: torch, transformers, nltk, numpy

Setup

  1. Install the required Python libraries by running the following command:
pip install torch transformers nltk numpy
  1. Import the necessary libraries in your Python notebook:
import torch
from transformers import GPT2LMHeadModel, GPT2Tokenizer
from nltk.tokenize import sent_tokenize
import numpy as np
  1. Load the pre-trained GPT-2 model and tokenizer:
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline

# model_id = 'EleutherAI/gpt-neo-125M'
model_id = 'gpt2'
tokenizer = AutoTokenizer.from_pretrained(model_id)
lm_model = AutoModelForCausalLM.from_pretrained(model_id)

pipe = pipeline(
    "text-generation", model=lm_model, tokenizer=tokenizer, max_new_tokens=256, device_map="auto", handle_long_generation="hole"
)
  1. Ask a question and get the generated answer:
question = 'Why is society ignoring the potentially devastating consequences of AI development'

# Retrieve context from vector database
results = talks_collection.query(
    query_texts=question,
    n_results=5
)

context = results['documents'][0][0]

prompt_template = f"Answer the given question only using the context provided. Do not Hallucinate.\n\nContext: {context}\n\nQuestion: {question}\n\n\
Answer:"

lm_response = pipe(prompt_template)
print(lm_response[0]["generated_text"])

Customizing and Improving the Q&A Model

To customize and improve the Q&A model, you can experiment with the following techniques:

  1. Fine-tuning: Fine-tune the pre-trained GPT-2 model on a specific dataset to make it more domain-specific and improve its performance on certain types of questions.

  2. Data preprocessing: Perform additional data preprocessing steps such as sentence tokenization, removing irrelevant text, or applying specific filters to improve the quality of the generated answers.

  3. Model selection: Experiment with different pre-trained language models available in HuggingFace Transformers to find the one that best suits your needs.

  4. Hyperparameter tuning: Adjust the hyperparameters of the GPT-2 model, such as the maximum sequence length, temperature, or repetition penalty, to control the quality and diversity of the generated answers.

Conclusion

This notebook provides a basic implementation of a generative Q&A system using ChromDB and HuggingFace Transformers. By following the instructions and experimenting with different approaches, you can build and customize your own Q&A model for various applications.

Remember to refer to the ChromDB and HuggingFace Transformers documentation for more detailed information and advanced techniques.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published