File size: 1,620 Bytes
24f5aac
 
 
 
 
 
 
 
 
276b173
24f5aac
 
 
 
 
 
 
 
 
 
9ea53a3
24f5aac
a62856d
 
 
24f5aac
 
 
 
 
86b0e9f
24f5aac
9ea53a3
d7e810b
7d599a1
 
24f5aac
c3ec180
da2bcca
 
 
 
c94a056
da2bcca
 
7d599a1
da2bcca
 
23121e2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.prompts import PromptTemplate
from langchain.chains.question_answering import load_qa_chain
from langchain.llms import OpenAI
from langchain.text_splitter import CharacterTextSplitter
from langchain.vectorstores import FAISS
import gradio as gr
import os

with open("./knowledge") as f:
    state_of_the_union = f.read()
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
texts = text_splitter.split_text(state_of_the_union)

embeddings = OpenAIEmbeddings()
docs = text_splitter.create_documents(texts)

db = FAISS.from_documents(docs, embeddings)


def emojobot(query):
    blob = db.similarity_search(query)
    prompt_template = """You are the marketing director for Emojo electric trike. This is related information:
    {context}
    Write the marketing content to make sure the following is accurate and perfect in English:{question}
    Answer:"""
    PROMPT = PromptTemplate(
        template=prompt_template, input_variables=["context", "question"]
    )

    chain = load_qa_chain(OpenAI(model_name='gpt-4',temperature=0.8), chain_type="stuff", prompt=PROMPT)
    t = chain({"input_documents": blob, "question": query}, return_only_outputs=True)
    return t["output_text"]
    
input = gr.inputs.Textbox(label="Customer Question")
output = gr.outputs.Textbox(label="Response")


with gr.Blocks() as demo:
    
    gr.Markdown(
    """
    # Emojo AI bot | Internal Tool
    """
    )
    responseBox = gr.Interface(fn=emojobot, inputs=[input],outputs=[output])

if __name__ == "__main__":        
    demo.launch()