|
|
|
|
|
import streamlit as st |
|
import os |
|
import json |
|
from dotenv import load_dotenv |
|
from haystack.nodes.prompt import PromptNode, PromptTemplate |
|
from haystack.nodes import EmbeddingRetriever |
|
from haystack import Pipeline |
|
import numpy as np |
|
import pandas as pd |
|
from haystack.document_stores import FAISSDocumentStore |
|
from haystack.nodes import EmbeddingRetriever |
|
from haystack.schema import Document |
|
|
|
|
|
|
|
openai_key = os.environ["OPENAI_API_KEY"] |
|
|
|
|
|
model_name = "gpt-3.5-turbo" |
|
|
|
|
|
template = PromptTemplate( |
|
prompt=""" |
|
Answer the given question using the provided documents. The answer should be in the style of an academic report and should provide example quotes and references. Always start the response by stating the name of the country relevant to the documents. When relevant, use bullet points and lists to structure your answers. When relevant, use facts and numbers from the following documents in your answer. Whenever you use information from a document, reference it at the end of the sentence (ex: [doc 2]). You don't have to use all documents, only if it makes sense in the conversation. If no relevant information to answer the question is present in the documents, just say you don't have enough information to answer. |
|
|
|
Context: {join(documents)}\n\nQuestion: {query}\n\nAnswer:""", |
|
) |
|
|
|
|
|
country_options = ['Angola','Botswana','Lesotho','Malawi','Mozambique','Namibia','South Africa','Zambia','Zimbabwe'] |
|
|
|
|
|
examples = [ |
|
"-", |
|
"What specific initiatives are presented in the context to address the needs of groups such women and children to the effects climate change?", |
|
"In addition to gender, children, and youth, is there any mention of other groups facing disproportional impacts from climate change due to their geographic location, socio-economic status, age, gender, health, and occupation?" |
|
] |
|
|
|
def get_docs(input_query, country = None): |
|
|
|
if country: |
|
query = "For the country of "+country+", "+input_query |
|
else: |
|
query = input_query |
|
|
|
|
|
docs = retriever.retrieve(query=query,top_k = 150) |
|
|
|
docs = [{**x.meta,"score":x.score,"content":x.content} for x in docs] |
|
df_docs = pd.DataFrame(docs) |
|
if country: |
|
df_docs = df_docs.query('country in @country') |
|
|
|
df_docs = df_docs.head(10) |
|
|
|
ls_dict = [] |
|
for doc, _ in df_docs.iterrows(): |
|
x = Document(df_docs['content'][doc]) |
|
ls_dict.append(x) |
|
return(ls_dict) |
|
|
|
def run_query(input_text): |
|
docs = get_docs(input_text) |
|
res = pipe.run(query=input_text, documents=docs) |
|
output = res["results"][0] |
|
st.write('Response') |
|
st.success(output) |
|
|
|
|
|
|
|
retriever = EmbeddingRetriever( |
|
document_store=FAISSDocumentStore.load( |
|
index_path="./cpv_test_2.faiss", |
|
config_path="./cpv_test_2.json", |
|
), |
|
embedding_model="sentence-transformers/multi-qa-mpnet-base-dot-v1", |
|
model_format="sentence_transformers", |
|
progress_bar=False, |
|
) |
|
|
|
|
|
pn = PromptNode(model_name_or_path=model_name, default_prompt_template=template, api_key=openai_key, max_length=700) |
|
|
|
|
|
pipe = Pipeline() |
|
pipe.add_node(component=pn, name="prompt_node", inputs=["Query"]) |
|
|
|
|
|
|
|
st.title('Climate Policy Documents: Vulnerabilities Analysis Q&A (test)') |
|
st.markdown('This tool seeks to provide an interface for quering national climate policy documents (NDCs, LTS etc.). The current version is powered by chatGPT (3.5) and limited to 9 Southern African countries (Angola, Botswana, Eswatini, Lesotho, Malawi, Mozambique, Namibia, South Africa, Zambia, Zimbabwe). The intended use case is to allow users to interact with the documents and obtain valuable insights on various vulnerable groups affected by climate change.') |
|
|
|
|
|
|
|
|
|
country = st.selectbox('Select a country:', country_options) |
|
|
|
|
|
selected_example = st.radio("Example questions", examples) |
|
|
|
if selected_example == "-": |
|
text = st.text_area('Enter your question in the text box below using natural language or select an example from above:') |
|
else: |
|
text = st.text_area('Enter your question in the text box below using natural language or select an example from above:', value=selected_example) |
|
|
|
|
|
if st.button('Submit'): |
|
run_query(text) |