Skip to content

Instantly share code, notes, and snippets.

Created March 24, 2024 07:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brettbeeson/e3a6e59729a5d8b0a42bedb025ab129a to your computer and use it in GitHub Desktop.
Save brettbeeson/e3a6e59729a5d8b0a42bedb025ab129a to your computer and use it in GitHub Desktop.
The hello world of Chatbots, with domain specific text passed in
from langchain_community.chat_models import ChatOpenAI
from llama_index import SimpleDirectoryReader
from llama_index import GPTVectorStoreIndex, LLMPredictor, ServiceContext, PromptHelper, load_index_from_storage, StorageContext
import gradio as gr
def init_index(directory_path):
# model params - refer docs
max_input_size = 4096
num_outputs = 512
chunk_size_limit = 600
# llm predictor with langchain ChatOpenAI
# ChatOpenAI model is a part of the LangChain library and is used to interact with the GPT-3.5-turbo model provided by OpenAI
prompt_helper = PromptHelper(max_input_size, num_outputs, chunk_size_limit=chunk_size_limit)
llm_predictor = LLMPredictor(llm=ChatOpenAI(temperature=0.7, model_name="gpt-3.5-turbo", max_tokens=num_outputs))
# read documents from docs folder
documents = SimpleDirectoryReader(directory_path).load_data()
# This index is created using the LlamaIndex library. It processes the document content and constructs the index to facilitate efficient querying
service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor, prompt_helper=prompt_helper)
index = GPTVectorStoreIndex.from_documents(documents, service_context=service_context)
# save the created index
return index
def chatbot(input_text):
# rebuild storage context
storage_context = StorageContext.from_defaults(persist_dir="storage")
# load index
index = load_index_from_storage(storage_context)
# get response for the question
response = index.as_query_engine( response_mode="compact").query(input_text)
return response.response
def main():
# Replace with the path to your folder containing text files
# (If you have PDFs, extract text with "fitz" aka "PyMuPDF")
data = "data"
# create index
# create ui interface to interact with gpt-3 model
iface = gr.Interface(fn=chatbot,
inputs=gr.components.Textbox(lines=7, placeholder="Enter your question here"),
title="AI ChatBot: Your Knowledge Companion Powered-by ChatGPT",
description="Ask any question about the scanned docs",
if __name__ == "__main__":
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment