Spaces:
Sleeping
Sleeping
yash bhaskar
commited on
Commit
·
df8ed4e
1
Parent(s):
a639959
Adding QueryModification Pipeline
Browse files
Query_Modification/QueryModification.py
ADDED
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import pandas as pd
|
3 |
+
import google.generativeai as genai
|
4 |
+
|
5 |
+
# Function to process text input with Gemini model
|
6 |
+
def query_Modifier(input_text):
|
7 |
+
|
8 |
+
with open('config.json', 'r') as file:
|
9 |
+
config = json.load(file)
|
10 |
+
|
11 |
+
gemini_key = config.get("GEMINI")
|
12 |
+
|
13 |
+
# Initialize the API key
|
14 |
+
genai.configure(api_key=gemini_key)
|
15 |
+
|
16 |
+
# print(gemini_key)
|
17 |
+
|
18 |
+
# Load the prompt from file
|
19 |
+
with open("Query_Modification/prompt.txt", 'r') as file:
|
20 |
+
PROMPT_TEMPLATE = file.read()
|
21 |
+
|
22 |
+
# Safety settings for Gemini model
|
23 |
+
safe = [
|
24 |
+
{
|
25 |
+
"category": "HARM_CATEGORY_DANGEROUS",
|
26 |
+
"threshold": "BLOCK_NONE",
|
27 |
+
},
|
28 |
+
{
|
29 |
+
"category": "HARM_CATEGORY_HARASSMENT",
|
30 |
+
"threshold": "BLOCK_NONE",
|
31 |
+
},
|
32 |
+
{
|
33 |
+
"category": "HARM_CATEGORY_HATE_SPEECH",
|
34 |
+
"threshold": "BLOCK_NONE",
|
35 |
+
},
|
36 |
+
{
|
37 |
+
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
|
38 |
+
"threshold": "BLOCK_NONE",
|
39 |
+
},
|
40 |
+
{
|
41 |
+
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
|
42 |
+
"threshold": "BLOCK_NONE",
|
43 |
+
},
|
44 |
+
]
|
45 |
+
|
46 |
+
generation_config = {
|
47 |
+
"temperature": 1,
|
48 |
+
"top_p": 0.95,
|
49 |
+
"top_k": 40,
|
50 |
+
"max_output_tokens": 8192,
|
51 |
+
"response_mime_type": "text/plain",
|
52 |
+
}
|
53 |
+
|
54 |
+
# Initialize the generative model
|
55 |
+
model = genai.GenerativeModel("gemini-1.5-flash", generation_config=generation_config)
|
56 |
+
|
57 |
+
|
58 |
+
full_prompt = f"{input_text}\n\n{PROMPT_TEMPLATE}"
|
59 |
+
|
60 |
+
# Call the generative model for text input
|
61 |
+
result = model.generate_content([full_prompt], safety_settings=safe)
|
62 |
+
return result.text
|
63 |
+
|
64 |
+
|
65 |
+
def getKeywords(input_text):
|
66 |
+
# Extract keywords from the input text
|
67 |
+
|
68 |
+
with open('config.json', 'r') as file:
|
69 |
+
config = json.load(file)
|
70 |
+
|
71 |
+
gemini_key = config.get("GEMINI")
|
72 |
+
|
73 |
+
# Initialize the API key
|
74 |
+
genai.configure(api_key=gemini_key)
|
75 |
+
|
76 |
+
# Safety settings for Gemini model
|
77 |
+
safe = [
|
78 |
+
{
|
79 |
+
"category": "HARM_CATEGORY_DANGEROUS",
|
80 |
+
"threshold": "BLOCK_NONE",
|
81 |
+
},
|
82 |
+
{
|
83 |
+
"category": "HARM_CATEGORY_HARASSMENT",
|
84 |
+
"threshold": "BLOCK_NONE",
|
85 |
+
},
|
86 |
+
{
|
87 |
+
"category": "HARM_CATEGORY_HATE_SPEECH",
|
88 |
+
"threshold": "BLOCK_NONE",
|
89 |
+
},
|
90 |
+
{
|
91 |
+
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
|
92 |
+
"threshold": "BLOCK_NONE",
|
93 |
+
},
|
94 |
+
{
|
95 |
+
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
|
96 |
+
"threshold": "BLOCK_NONE",
|
97 |
+
},
|
98 |
+
]
|
99 |
+
|
100 |
+
generation_config = {
|
101 |
+
"temperature": 1,
|
102 |
+
"top_p": 0.95,
|
103 |
+
"top_k": 40,
|
104 |
+
"max_output_tokens": 8192,
|
105 |
+
"response_mime_type": "text/plain",
|
106 |
+
}
|
107 |
+
|
108 |
+
# Initialize the generative model
|
109 |
+
model = genai.GenerativeModel("gemini-1.5-flash", generation_config=generation_config)
|
110 |
+
|
111 |
+
|
112 |
+
full_prompt = f"{input_text} \n\n Give the Keywords for the above sentence and output nothing else."
|
113 |
+
|
114 |
+
# Call the generative model for text input
|
115 |
+
result = model.generate_content([full_prompt], safety_settings=safe)
|
116 |
+
|
117 |
+
response = result.text
|
118 |
+
response = response.replace("Keywords:", "")
|
119 |
+
response = response.replace(",", "")
|
120 |
+
|
121 |
+
return response.strip()
|
Query_Modification/prompt.txt
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Modify the following query to improve its suitability for a Retrieval Augmented Generation (RAG) system using a semantic search engine like Cosign:
|
2 |
+
|
3 |
+
Original Query: [Original query here]
|
4 |
+
|
5 |
+
Guidelines:
|
6 |
+
|
7 |
+
Clarity and Specificity: Make the query more specific and focused.
|
8 |
+
Keyword Optimization: Identify and include relevant keywords that align with the dataset.
|
9 |
+
Semantic Relevance: Consider the underlying meaning and context of the query.
|
10 |
+
Question Formulation: Frame the query as a question to facilitate direct answer extraction.
|
11 |
+
Contextual Clues: If applicable, provide additional context or background information.
|
12 |
+
|
13 |
+
Example:
|
14 |
+
|
15 |
+
Original Query: "Tell me about the French Revolution"
|
16 |
+
|
17 |
+
Modified Query: "What were the main causes and effects of the French Revolution, and who were its key figures?"
|
18 |
+
|
19 |
+
Guardrail : Output only the Modified Query.
|