erginousAI / app.py
jobanpreet123's picture
Upload 2 files
4d7bb70 verified
from langchain.chat_models import ChatOpenAI
# from langchain_community.document_loaders import WebBaseLoader
# from langchain.text_splitter import RecursiveCharacterTextSplitter
import gradio as gr
from langchain_pinecone import Pinecone
from langchain_openai import OpenAIEmbeddings , ChatOpenAI
from langchain.prompts import PromptTemplate
from langchain.chains import RetrievalQA
import os
os.environ["PINECONE_API_KEY"] = "9952de06-975b-4ca4-9908-491e8d08328a"
os.environ["OPENAI_API_KEY"]="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"
# create embeddings and store in vector database (Pinecone)
embeddings=OpenAIEmbeddings()
#docsearch=Pinecone.from_texts([t.page_content for t in document_chunks],embeddings,index_name="index_name")
llm = ChatOpenAI(temperature=0, model='gpt-3.5-turbo-16k')
# If you already have embeddings stored , you can load it using below code
vector_search=Pinecone.from_existing_index("erginouswebsite",embeddings)
template = """You are provided with a content related to the company. You will be asked any question related to the company. If you don't know ,just say I don't know the answer'.
{context}
Question: {question}
Helpful Answer:"""
QA_CHAIN_PROMPT = PromptTemplate(input_variables=["question"],template=template)
qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=vector_search.as_retriever(),chain_type_kwargs={"prompt": QA_CHAIN_PROMPT})
def predict(message, history):
gpt_response = qa.invoke(message)
return gpt_response['result']
gr.ChatInterface(predict,chatbot=gr.Chatbot(height=300),
textbox=gr.Textbox(placeholder="Ask me a question", container=False, scale=7),
title="AI Assistant Chatbot",
description="Chatbot AI Assistant",
theme="soft",
examples=["Provide address of the company", "Solutions provided by the company", "Career opportunities in company"],
cache_examples=True,
retry_btn=None,
undo_btn="Delete Previous",
clear_btn="Clear",).launch(share=True , server_name="0.0.0.0" ,server_port=8080)