File size: 7,387 Bytes
1e4288a |
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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
import requests
import json
import spacy
import string
def generate_prompt(context, question, history):
# history_summary = ""
# if history:
# for user_query, bot_response in history[-3:]:
# history_summary += f"User: {user_query}\n Assistant: {bot_response}\n"
if context:
prompt_context = context
else:
prompt_context = "No context provided."
prompt = f"""
<s>[INST] <<SYS>> You are a helpful, respectful and honest assistant. Always answer as helpfully as possible based on the context, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content.<</SYS>>
Context:
{prompt_context}
[INST] {question} [/INST]
Response:
"""
return prompt
# ==============================================================================================================================================
def llama(prompt):
url = "https://api.edenai.run/v2/text/generation"
payload = {
"providers": "meta/llama2-13b-chat-v1",
"response_as_dict": True,
"attributes_as_list": False,
"show_original_response": False,
"temperature": 0,
"max_tokens": 256,
"text": prompt
}
headers = {
"accept": "application/json",
"content-type": "application/json",
"authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiMmYzMDE3MTEtOTJmNy00ZDU3LTg4N2MtNjU2MmE5MTU5MWZhIiwidHlwZSI6ImFwaV90b2tlbiJ9.vWvooRwxmr-uY1c61V97uugyDGpXmZGjX8oCFWKCUeM"
}
response = requests.post(url, json=payload, headers=headers)
result = response.json()
return result['meta/llama2-13b-chat-v1']['generated_text']
# ==============================================================================================================================================
def question_answering(question):
"""
Sends a question answering request to the EdenAI API.
Args:
question: The question to be answered.
Returns:
The answer provided by the LLM model (string),
or None if an error occurs.
"""
headers = {"Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiMmYzMDE3MTEtOTJmNy00ZDU3LTg4N2MtNjU2MmE5MTU5MWZhIiwidHlwZSI6ImFwaV90b2tlbiJ9.vWvooRwxmr-uY1c61V97uugyDGpXmZGjX8oCFWKCUeM"}
url = "https://api.edenai.run/v2/text/question_answer"
payload = {
"providers": "openai",
"texts": [
"Linux is a family of open-source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991, by Linus Torvalds.",
"Just like Windows, iOS, and Mac OS, Linux is an operating system. "
],
'question': question,
"examples": [["What is human life expectancy in the United States?", "78 years."]],
"fallback_providers": ""
}
try:
response = requests.post(url, json=payload, headers=headers)
result = json.loads(response.text)
return result['openai']['answers'] if result['openai']['answers'] else None
except Exception as e:
print(f"Error communicating with LLM model: {e}")
return None
# ==============================================================================================================================================
def normalize_text(s):
"""Removing stopwords and punctuation, and standardizing whitespace are all typical text processing steps."""
nlp = spacy.load("en_core_web_sm")
def remove_stop(text):
return " ".join([word for word in text.split() if not nlp.vocab[word].is_stop])
def lemma(text):
return " ".join([word.lemma_ for word in nlp(text)])
def white_space_fix(text):
return " ".join(text.split()) # this function removes leading and trailing whitespaces and condenses all other whitespaces to a single space
def remove_punc(text):
exclude = set(string.punctuation)
return "".join(ch for ch in text if ch not in exclude)
def lower(text):
return text.lower()
return white_space_fix(lemma(remove_stop(remove_punc(lower(s)))))
# ==============================================================================================================================================
def get_relevance_docs(documents_score, threshold):
"""
Calculate relevance scores for the retrieved documents based on their relevance to the correct answer.
Parameters:
documents_score (list): List of scores for the retrieved documents.
threshold (float): Threshold value to determine relevance.
Returns:
list: List of relevance scores for the retrieved documents.
"""
relevance_scores = []
for score in documents_score:
if score >= threshold:
relevance_scores.append(1) # Relevant document
else:
relevance_scores.append(0) # Non-relevant document
return relevance_scores
# ==============================================================================================================================================
def get_docs_by_indices(docs, indices):
"""
Retrieve document contexts from a list of indexed documents based on provided indices.
Args:
- docs (list): List of documents.
- indices (list): List of indices corresponding to the desired documents.
Returns:
- list: List of document contexts corresponding to the provided indices.
"""
return [docs[index] for index in indices]
def query_rewriter(query):
headers = {"Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiMTA5MTBlYTktOWYwOC00N2E2LTg3MDktOTlhODExZjkwZDA2IiwidHlwZSI6ImFwaV90b2tlbiJ9._wiFq518MhMRvG8waWbg_7Eogf50isgyzqh3e2ypvOU"}
url = "https://api.edenai.run/v2/text/code_generation"
payload = {
"providers": "openai",
# "instruction": "You are an expert at world knowledge. Your task is to step back and paraphrase a question to a more generic step-back question, which is easier to answer. Here are a few examples:Original Question: Which position did Knox Cunningham hold from May 1955 to Apr 1956? Stepback Question: Which positions have Knox Cunning- ham held in his career? , Now this Question: Who was the spouse of Anna Karina from 1968 to 1974?",
"prompt": "",
"model": "gpt-3.5-turbo",
"instruction": f"""You are an expert in document retrieval and search optimization.
Your task is to rewrite the following query to enhance its relevance and usefulness for retrieving information
from a database or search engine :
Original Query: {query} \n """,
"temperature": 0.6,
"max_tokens": 512,
"fallback_providers": " ['openai']"
}
response = requests.post(url, json=payload, headers=headers)
result = json.loads(response.text)
if(len(result['openai']['generated_text']) > len(query)):
try:
return split_text(result['openai']['generated_text'])
except:
return result['openai']['generated_text']
else:
return query
# ==============================================================================================================================================
def split_text(text):
return text.split(":")[1].strip() if ":" in text else text |