Spaces:
Runtime error
Runtime error
| 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() |