I have a program that extracts the information form a previously-created and working json file, and then uses chatGPT API to query this document. I need to input the initial user input and then the response to his input all within the bot scrolling up normally, and I need to use the gr.Chatbot option (gr.Interface is not an option). Unfortunately the grChatbot() docs are not detailed enough so I can't solve it. I have created the function question_answer_NEW and also tried with the "bot" function provided as example in Gradio docs, but no results. Thank you. Here's the code:
import gradio as gr
import random
import time
from llama_index import SimpleDirectoryReader, GPTListIndex, readers, GPTSimpleVectorIndex, LLMPredictor, PromptHelper
from langchain.chat_models import ChatOpenAI
from langchain import OpenAI
import sys
import os
from IPython.display import Markdown, display
import gradio, openai
os.environ["OPENAI_API_KEY"] = 'sk-...' #present in code
messages = [{"role": "system", "content": "You are a helpful customer service assistant."}]
global query
global question
global response
def question_answer_NEW(question):
index = GPTSimpleVectorIndex.load_from_disk('index.json')
for i in range(3): #while True:
query = question
response = index.query(query, response_mode="compact")
messages.append({"User asks": question, "System response": response.response.strip()})
#print(messages[-1])''
print(query, response.response.strip())
return query, response.response.strip()
with gr.Blocks() as demo:
chatbot = gr.Chatbot()
msg = gr.Textbox() #placeholder = 'test'
clear = gr.Button("Clear")
global user_message
def user(user_message, history):
print('I am user message', user_message)
print(history + [[user_message, None]])
return "", history + [[user_message, None]]
def bot(history):
index = GPTSimpleVectorIndex.load_from_disk('index.json')
bot_message = index.query(user_message, response_mode="compact")
print('I am bot message', bot_message)
history[-1][1] = bot_message
time.sleep(1)
return history
return bot_message
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(bot, chatbot, chatbot)
clear.click(lambda: None, None, chatbot, queue=False)
if __name__ == "__main__":
demo.launch()