chatbot-g-rag / app.py
Nicolas-Schuhl's picture
Update app.py
3546e99 verified
raw
history blame
2.57 kB
import os
import gradio as gr
from llama_index import VectorStoreIndex, SimpleDirectoryReader, SummaryIndex
from llama_index.readers import SimpleWebPageReader
from llama_index.llms import MistralAI
from llama_index.embeddings import MistralAIEmbedding
from llama_index import ServiceContext
from llama_index.query_engine import RetrieverQueryEngine
title = "Gaia Mistral Chat Demo"
description = "Exemple d'assistant avec Gradio et Mistral AI via son API"
placeholder = "Posez moi une question sur l'agriculture"
placeholder_url = "Donner moi une url qui va servir de contexte agricole complémentaire"
placeholder_api_key = "API key"
examples = ["Comment fait on pour produire du maïs ?", "Rédige moi une lettre pour faire un stage dans une exploitation agricole", "Comment reprendre une exploitation agricole ?"]
query_engine = None
with gr.Blocks() as demo:
gr.Markdown(""" ### Welcome to Level 2 Demo
Add an url and your API key at the bottom of the bottom before interacting with the Chat. This demo allows you to interact with a webpage and then ask questions to Mistral APIs. Mistral will answer with the context extracted from the webpage.
""")
chatbot = gr.Chatbot()
msg = gr.Textbox()
clear = gr.ClearButton([msg, chatbot])
with gr.Row():
api_key_text_box = gr.Textbox(placeholder=placeholder_api_key, container=False, scale=7)
def setup_with_url(url, api_key):
global query_engine
# Set-up clients
llm = MistralAI(api_key=api_key,model="mistral-medium")
embed_model = MistralAIEmbedding(model_name='mistral-embed', api_key=api_key)
service_context = ServiceContext.from_defaults(chunk_size=1024, llm=llm, embed_model=embed_model)
# Set-up db
documents = SimpleWebPageReader(html_to_text=True).load_data([url])
index = VectorStoreIndex.from_documents(documents, service_context=service_context)
query_engine = index.as_query_engine(similarity_top_k=15)
return "I'm ready, please add a question here."
with gr.Row():
url_msg = gr.Textbox(placeholder=placeholder_url, container=False, scale=7)
url_btn = gr.Button(value="Set-up API and process url ✅", interactive=True)
url_btn.click(setup_with_url, [url_msg, api_key_text_box], msg, show_progress= "full")
def respond(message, chat_history):
response = query_engine.query(message)
chat_history.append((message, str(response)))
return chat_history
msg.submit(respond, [msg, chatbot], [chatbot])
demo.launch()