| import openai |
| import gradio as gr |
| import os |
|
|
| OPENAI_API_KEY = os.getenv('OPENAI_API_KEY') |
|
|
| from langchain.embeddings.openai import OpenAIEmbeddings |
|
|
| embeddings = OpenAIEmbeddings(openai_api_key = OPENAI_API_KEY) |
| |
| from langchain.chat_models import ChatOpenAI |
|
|
| llm = ChatOpenAI(temperature=0, model_name="gpt-3.5-turbo", openai_api_key=OPENAI_API_KEY) |
| from langchain.document_loaders.csv_loader import CSVLoader |
|
|
| loader = CSVLoader(file_path='website_scrape.csv') |
| documents = loader.load() |
|
|
| from langchain.text_splitter import CharacterTextSplitter |
|
|
| text_splitter = CharacterTextSplitter(chunk_size=512, chunk_overlap=0) |
| texts = text_splitter.split_documents(documents) |
|
|
| from langchain.vectorstores import Chroma |
|
|
| db = Chroma.from_documents(texts,embeddings) |
|
|
| from pathlib import Path |
| from langchain.chains.question_answering import load_qa_chain |
| from langchain.memory import ConversationBufferMemory |
| from langchain.prompts import PromptTemplate |
| from langchain.callbacks import get_openai_callback |
|
|
| DEFAULT_TEMPLATE = """ |
| ### Instruction: Your name is Meyd-e-, a business setup support agent that is talking to a customer. Use only the chat history and the following information |
| {context} |
| to answer in a helpful manner to the quetion. If you don't know the answer - say that you do not know. Only assist questions related to business setup and Meydan Free Zone. If other topics are brought up, redirect the user to business set-up and Meydan Free Zone related topics. |
| Keep your replies short, compassionate and informative. Your name is Meyd-e-. |
| {chat_history} |
| ###Input: {question} |
| ### Response: |
| """.strip() |
|
|
|
|
| class Chatbot: |
| def __init__( |
| self, |
| model: ChatOpenAI, |
| embeddings: OpenAIEmbeddings, |
| document: Path, |
| prompt_template: str = DEFAULT_TEMPLATE, |
| verbose: bool = False, |
| ): |
| prompt = PromptTemplate( |
| input_variables=["context", "question", "chat_history"], |
| template=prompt_template, |
| ) |
| self.chain = self._create_chain(model, prompt, verbose) |
| self.db = self._embed_data(document, embeddings) |
|
|
| def _create_chain( |
| self, |
| model: ChatOpenAI, |
| prompt: PromptTemplate, |
| verbose: bool = False, |
| ): |
| memory = ConversationBufferMemory( |
| memory_key = "chat_history", |
| human_prefix= "### Input", |
| ai_prefix= "### Response", |
| input_key = "question", |
| output_key = "output_text", |
| return_messages=False, |
| ) |
|
|
| return load_qa_chain( |
| model, |
| chain_type="stuff", |
| prompt = prompt, |
| memory=memory, |
| verbose=verbose, |
| ) |
|
|
| def _embed_data( |
| self, document: Path, embeddings: OpenAIEmbeddings |
| ) -> Chroma: |
| loader = CSVLoader(file_path= document) |
| data = loader.load() |
| text_splitter = CharacterTextSplitter(chunk_size=512, chunk_overlap=0) |
| texts = text_splitter.split_documents(data) |
| return Chroma.from_documents(texts, embeddings) |
|
|
| def __call__(self, user_input: str) -> str: |
| docs = self.db.similarity_search(user_input) |
| return self.chain.run({"input_documents": docs, "question": user_input}) |
|
|
| chatbot = Chatbot(llm, embeddings, "website_scrape.csv") |
|
|
|
|
| def add_text(history, text): |
| global messages |
| history = history + [(text,'')] |
| messages = messages + [{"role":'user', 'content': text}] |
| return history, "" |
|
|
| def generate_response(history): |
| global messages, cost |
| user_input = history[-1][0] |
| cost=0 |
| |
| chatbot = Chatbot(llm, embeddings, "website_scrape.csv") |
| with get_openai_callback() as cb: |
| response = chatbot(user_input) |
|
|
| cost = cost + (cb.total_cost) |
| messages = messages + [{"role":'assistant', 'content': response}] |
|
|
| for char in response: |
|
|
| history[-1][1] += char |
| yield history |
|
|
| |
|
|
| |
|
|
| messages = [] |
|
|
| theme = gr.themes.Soft( |
| primary_hue="blue", |
| secondary_hue = "teal", |
| ) |
|
|
| with gr.Blocks(theme=theme) as demo: |
|
|
| chatbot = gr.Chatbot(value=[["", "Hi! I am your business support assistant. How can I help you today?"]], |
| elem_id="chatbot", |
| bubble_full_width=False, |
| avatar_images=(None, (os.path.join(os.path.dirname(__file__), "Meydan_Logo.png"))) ,height=550) |
| |
| with gr.Row(): |
| with gr.Column(scale=0.850): |
| txt = gr.Textbox( |
| show_label=False, |
| placeholder="Enter text and press enter", |
| container=False, |
| elem_id ="input_text" |
| ) |
| with gr.Column(scale = 0.15): |
| send_button = gr.Button("Send" , elem_id = "send_button") |
| |
| |
| send_button.click(add_text, inputs=[chatbot,txt], outputs = [chatbot,txt]).then( |
| generate_response, inputs= [chatbot], outputs = chatbot |
| ) |
| txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then( |
| generate_response, inputs =[chatbot],outputs = chatbot,) |
|
|
|
|
| demo.queue() |
| demo.launch(debug=True) |