Spaces:
Runtime error
Runtime error
File size: 3,917 Bytes
bc65f06 27da65e bc65f06 a2f6a75 bc65f06 9ac037b bc65f06 9ac037b bc65f06 eb6da40 bc65f06 bea5856 92b28aa bc65f06 |
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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
import gradio as gr
from huggingface_hub import InferenceClient
from llama_index.core import Document
import pandas as pd
import getpass
import os
from llama_index.core.retrievers import VectorIndexRetriever
from llama_index.core import VectorStoreIndex
from llama_index.core import QueryBundle
from IPython.display import display, HTML
from llama_index.core.postprocessor import LLMRerank
import logging
import sys
from llama_index.core.memory import ChatMemoryBuffer
from llama_index.llms.openai import OpenAI
# Load the JSON file
file_path = 'response_metropol.json'
data = pd.read_json(file_path)
data.head()
documents = [Document(text=row['values'],metadata={"filename": row['file_name'], "description":row['file_description']},) for index, row in data.iterrows()]
os.environ["OPENAI_API_KEY"] = os.getenv('api_key_openai')
#pd.set_option("display.max_colwidth", -1)
# build index
index = VectorStoreIndex.from_documents(documents)
def get_retrieved_nodes(
query_str, vector_top_k=10, reranker_top_n=5, with_reranker=False
):
query_bundle = QueryBundle(query_str)
# configure retriever
retriever = VectorIndexRetriever(
index=index,
similarity_top_k=vector_top_k,
)
retrieved_nodes = retriever.retrieve(query_bundle)
if with_reranker:
# configure reranker
reranker = LLMRerank(
choice_batch_size=5,
top_n=reranker_top_n,
)
retrieved_nodes = reranker.postprocess_nodes(
retrieved_nodes, query_bundle
)
return retrieved_nodes
def pretty_print(df):
return display(HTML(df.to_html().replace("\\n", "")))
def visualize_retrieved_nodes(nodes) -> None:
result_dicts = []
for node in nodes:
result_dict = {"Score": node.score, "Text": node.node.get_text()}
result_dicts.append(result_dict)
pretty_print(pd.DataFrame(result_dicts))
new_nodes = get_retrieved_nodes(
"quel sont les agence qui sont adaptée aux personnes à mobilité réduite",
vector_top_k=10,
reranker_top_n=10,
with_reranker=True,
)
visualize_retrieved_nodes(new_nodes)
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))
def get_all_text(new_nodes):
"""
This function takes a list of nodes and returns a single string containing the text of each node, joined together.
Args:
new_nodes (list): A list of nodes from which text is to be extracted.
Returns:
str: A single string containing the text from each node, joined together.
"""
texts = []
for node in new_nodes:
texts.append(node.get_text())
return ' '.join(texts)
get_texts = get_all_text(new_nodes)
print(get_texts)
memory = ChatMemoryBuffer.from_defaults(token_limit=6500)
chat_engine = index.as_chat_engine(
llm = OpenAI(temperature=0, model="gpt-4"),
chat_mode="context",
memory=memory,
system_prompt=(
"Assist public agents in providing responses to the residents and citizens of the metropolis in nice, guiding them to the appropriate services that best address their requests for assistance. This involves equipping public agents with the necessary tools and information to efficiently and effectively direct citizens to the services that can fulfill their needs.answer using french"
),
similarity_top_k=10,
node_postprocessors=[
LLMRerank(
choice_batch_size=5,
top_n=5,
)
],
response_mode="tree_summarize",
)
def process(input, history):
response = chat_engine.stream_chat(input)
output = ""
for token in response.response_gen:
print(token, end="")
output += token
return output
iface = gr.ChatInterface(
fn=process,
title="Métropole Chat_Openai",
description="Provide a question and get a response.",
)
iface.launch()
|