import gradio as gr
import openai, os
from langchain.chains import LLMChain, RetrievalQA
from langchain.chat_models import ChatOpenAI
from langchain.document_loaders import PyPDFLoader, WebBaseLoader
from langchain.document_loaders.blob_loaders.youtube_audio import YoutubeAudioLoader
from langchain.document_loaders.generic import GenericLoader
from langchain.document_loaders.parsers import OpenAIWhisperParser
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.prompts import PromptTemplate
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.vectorstores import Chroma
from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv())
#openai.api_key = os.environ["OPENAI_API_KEY"]
template = """If you don't know the answer, just say that you don't know, don't try to make up an answer. Keep the answer as concise as possible. Always say
"🔥 Thanks for using the app - Bernd Straehle." at the end of the answer. """
llm_template = "Answer the question at the end. " + template + "Question: {question} Helpful Answer: "
rag_template = "Use the following pieces of context to answer the question at the end. " + template + "{context} Question: {question} Helpful Answer: "
LLM_CHAIN_PROMPT = PromptTemplate(input_variables = ["question"],
template = llm_template)
RAG_CHAIN_PROMPT = PromptTemplate(input_variables = ["context", "question"],
template = rag_template)
CHROMA_DIR = "/data/chroma"
YOUTUBE_DIR = "/data/youtube"
PDF_URL = "https://arxiv.org/pdf/2303.08774.pdf"
WEB_URL = "https://openai.com/research/gpt-4"
YOUTUBE_URL_1 = "https://www.youtube.com/watch?v=--khbXchTeE"
YOUTUBE_URL_2 = "https://www.youtube.com/watch?v=hdhZwyf24mE"
YOUTUBE_URL_3 = "https://www.youtube.com/watch?v=vw-KWfKwvTQ"
MODEL_NAME = "gpt-4"
def invoke(openai_api_key, use_rag, prompt):
llm = ChatOpenAI(model_name = MODEL_NAME,
openai_api_key = openai_api_key,
temperature = 0)
if (use_rag):
# Document loading
#docs = []
# Load PDF
#loader = PyPDFLoader(PDF_URL)
#docs.extend(loader.load())
# Load Web
#loader = WebBaseLoader(WEB_URL)
#docs.extend(loader.load())
# Load YouTube
#loader = GenericLoader(YoutubeAudioLoader([YOUTUBE_URL_1,
# YOUTUBE_URL_2,
# YOUTUBE_URL_3], YOUTUBE_DIR),
# OpenAIWhisperParser())
#docs.extend(loader.load())
# Document splitting
#text_splitter = RecursiveCharacterTextSplitter(chunk_overlap = 150,
# chunk_size = 1500)
#splits = text_splitter.split_documents(docs)
# Document storage
#vector_db = Chroma.from_documents(documents = splits,
# embedding = OpenAIEmbeddings(disallowed_special = ()),
# persist_directory = CHROMA_DIR)
# Document retrieval
vector_db = Chroma(embedding_function = OpenAIEmbeddings(),
persist_directory = CHROMA_DIR)
rag_chain = RetrievalQA.from_chain_type(llm,
chain_type_kwargs = {"prompt": RAG_CHAIN_PROMPT},
retriever = vector_db.as_retriever(search_kwargs = {"k": 3}),
return_source_documents = True)
result = rag_chain({"query": prompt})
result = result["result"]
else:
chain = LLMChain(llm = llm, prompt = LLM_CHAIN_PROMPT)
result = chain.run({"question": prompt})
return result
description = """Overview: The app demonstrates how to use a Large Language Model (LLM) with Retrieval Augmented Generation (RAG)
on external data (private/public & structured/unstructured).\n\n
Instructions: Enter an OpenAI API key and perform LLM use cases (semantic search, summarization, translation, etc.) on
YouTube, PDF, and Web
data about GPT-4 (created after training cutoff).
- Set "Retrieval Augmented Generation" to "False" and submit prompt "What is GPT-4?" The LLM without RAG does not know the answer.
- Set "Retrieval Augmented Generation" to "True" and submit prompt "What is GPT-4?" The LLM with RAG knows the answer.
- Experiment with prompts, e.g. "What is GPT-4? Answer in one sentence.", "What are GPT-4's exam capabilities?", or "Write a Python program calling the GPT-4 API."
- Experiment more, for example "Write a poem about GPT-4." or "Tell a joke about GPT-4, answer in English, Arabic, Chinese, Spanish, and Russian in JSON format."
\n\n
Technology: Gradio UI using OpenAI API via AI-first
LangChain toolkit with GPT-4 foundation model and AI-native
Chroma embedding database."""
gr.close_all()
demo = gr.Interface(fn=invoke,
inputs = [gr.Textbox(label = "OpenAI API Key", value = "sk-", lines = 1),
gr.Radio([True, False], label="Retrieval Augmented Generation", value = False),
gr.Textbox(label = "Prompt", value = "What is GPT-4?", lines = 1)],
outputs = [gr.Textbox(label = "Completion", lines = 1)],
title = "Generative AI - LLM & RAG",
description = description)
demo.launch()