add rag pipeline
Browse files
appStore/__pycache__/target.cpython-310.pyc
ADDED
|
Binary file (2.89 kB). View file
|
|
|
appStore/__pycache__/vulnerability_analysis.cpython-310.pyc
ADDED
|
Binary file (4.74 kB). View file
|
|
|
appStore/rag.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import numpy as np
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import openai
|
| 5 |
+
from haystack.schema import Document
|
| 6 |
+
import streamlit as st
|
| 7 |
+
from tenacity import retry, stop_after_attempt, wait_random_exponential
|
| 8 |
+
from huggingface_hub import InferenceClient
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
# Get openai API key
|
| 12 |
+
hf_token = os.environ["HF_API_KEY"]
|
| 13 |
+
|
| 14 |
+
# define a special function for putting the prompt together (as we can't use haystack)
|
| 15 |
+
def get_prompt(context, label):
|
| 16 |
+
base_prompt="Summarize the following context efficiently in bullet points, the less the better - but keep concrete goals. \
|
| 17 |
+
Summarize only elements of the context that address vulnerability of "+label+" to climate change. \
|
| 18 |
+
If there is no mention of "+label+" in the context, return: 'No clear references to vulnerability of "+label+" found'. \
|
| 19 |
+
Do not include an introduction sentence, just the bullet points as per below. \
|
| 20 |
+
Formatting example: \
|
| 21 |
+
- Bullet point 1 \
|
| 22 |
+
- Bullet point 2 \
|
| 23 |
+
"
|
| 24 |
+
|
| 25 |
+
prompt = base_prompt+"; Context: "+context+"; Answer:"
|
| 26 |
+
|
| 27 |
+
return prompt
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
# # exception handling for issuing multiple API calls to openai (exponential backoff)
|
| 31 |
+
# @retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(6))
|
| 32 |
+
# def completion_with_backoff(**kwargs):
|
| 33 |
+
# return openai.ChatCompletion.create(**kwargs)
|
| 34 |
+
|
| 35 |
+
class ChatCompletionResult:
|
| 36 |
+
def __init__(self):
|
| 37 |
+
self.content = ""
|
| 38 |
+
|
| 39 |
+
def add_content(self, text):
|
| 40 |
+
self.content += text
|
| 41 |
+
|
| 42 |
+
def get_full_content(self):
|
| 43 |
+
return self.content.strip()
|
| 44 |
+
|
| 45 |
+
def run_query(context, label, model_sel_name):
|
| 46 |
+
'''
|
| 47 |
+
Summarize provided test
|
| 48 |
+
'''
|
| 49 |
+
chatbot_role = """You are an analyst specializing in climate change impact assessments and producing insights from policy documents."""
|
| 50 |
+
messages = [{"role": "system", "content": chatbot_role},{"role": "user", "content": get_prompt(context, label)}]
|
| 51 |
+
|
| 52 |
+
# Initialize the client, pointing it to one of the available models
|
| 53 |
+
client = InferenceClient(model_sel_name, token=hf_token)
|
| 54 |
+
|
| 55 |
+
# Instantiate ChatCompletion as a generator object (stream is set to True)
|
| 56 |
+
chat_completion = client.chat.completions.create(
|
| 57 |
+
messages=messages,
|
| 58 |
+
stream=True
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
# Create an object to store the full chat completion
|
| 62 |
+
completion_result = ChatCompletionResult()
|
| 63 |
+
res_box = st.empty()
|
| 64 |
+
|
| 65 |
+
# Iterate through the streamed output
|
| 66 |
+
for chunk in chat_completion:
|
| 67 |
+
# Extract the object containing the text
|
| 68 |
+
if chunk.choices is not None:
|
| 69 |
+
chunk_message = chunk.choices[0].delta
|
| 70 |
+
if 'content' in chunk_message:
|
| 71 |
+
completion_result.add_content(chunk_message['content']) # Store the message
|
| 72 |
+
# Add the latest text and merge it with all previous
|
| 73 |
+
result = completion_result.get_full_content()
|
| 74 |
+
res_box.success(result) # Output to response text box
|
| 75 |
+
|
| 76 |
+
# Return the stored chat completion object for later use
|
| 77 |
+
return completion_result
|
utils/__pycache__/target_classifier.cpython-310.pyc
ADDED
|
Binary file (3.56 kB). View file
|
|
|