Spaces:
Running
Running
File size: 12,085 Bytes
85807eb 04b4725 85807eb d17c60a 1476c30 85807eb 38c100a 85807eb d17c60a 85807eb 2bf42e1 85807eb 2bf42e1 85807eb 2bf42e1 85807eb 38c100a 1071d26 2bf42e1 d17c60a 85807eb 1071d26 85807eb |
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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from langchain_groq import ChatGroq
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
import os
# Initialize FastAPI app
app = FastAPI()
# Create a request model with context
class SearchQuery(BaseModel):
query: str
context: str = None # Optional context field
# Initialize LangChain with Groq
llm = ChatGroq(
temperature=0.7,
model_name="mixtral-8x7b-32768",
groq_api_key="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" # Replace with your actual Groq API key
)
# Define all prompt templates
prompt_templates = {
# Cybersecurity Threats and Mitigation
"common_threats": PromptTemplate(
input_variables=["query", "context"],
template="""
Context: {context}
Query: {query}
Provide a comprehensive overview of the most common cybersecurity threats faced by organizations on a daily basis, including details on threat_1, threat_2, and threat_3. Also, provide effective measures to mitigate these risks and protect critical data and systems.
"""
),
"task_prioritization": PromptTemplate(
input_variables=["query", "context"],
template="""
Context: {context}
Query: {query}
Provide a guide on how cybersecurity professionals prioritize their tasks and responsibilities, focusing on the most critical areas such as threat detection, response times, and resource allocation.
"""
),
"network_traffic_tools": PromptTemplate(
input_variables=["query", "context"],
template="""
Context: {context}
Query: {query}
List and describe the most effective tools and software used for monitoring network traffic. Include tools for real-time analysis, anomaly detection, and reporting.
"""
),
"vulnerability_assessments": PromptTemplate(
input_variables=["query", "context"],
template="""
Context: {context}
Query: {query}
Provide best practices for conducting vulnerability assessments and penetration tests, including recommended frequencies and methodologies to ensure systems are adequately tested for vulnerabilities.
"""
),
"security_policies": PromptTemplate(
input_variables=["query", "context"],
template="""
Context: {context}
Query: {query}
Explain the role cybersecurity professionals have in developing, updating, and enforcing security policies within an organization. Include considerations for evolving threats and compliance requirements.
"""
),
"staying_updated": PromptTemplate(
input_variables=["query", "context"],
template="""
Context: {context}
Query: {query}
Describe the methods and tools cybersecurity professionals use to stay up-to-date on the latest cybersecurity threats, trends, and vulnerabilities, including ongoing education and industry resources.
"""
),
"immediate_incidents": PromptTemplate(
input_variables=["query", "context"],
template="""
Context: {context}
Query: {query}
Identify and describe the types of cybersecurity incidents that require immediate attention, such as data breaches, malware attacks, and denial-of-service attacks. Provide guidance on how to respond to each incident type.
"""
),
"collaboration_it_teams": PromptTemplate(
input_variables=["query", "context"],
template="""
Context: {context}
Query: {query}
Discuss how cybersecurity professionals work with IT teams to ensure system security, focusing on areas such as patch management, incident response, and ongoing risk management.
"""
),
"incident_investigation": PromptTemplate(
input_variables=["query", "context"],
template="""
Context: {context}
Query: {query}
Outline the steps involved in investigating and resolving a security incident, including initial detection, containment, root cause analysis, and reporting.
"""
),
"securing_remote_workers": PromptTemplate(
input_variables=["query", "context"],
template="""
Context: {context}
Query: {query}
Provide strategies for securing remote workers and their devices, including the use of VPNs, multi-factor authentication, and endpoint protection measures.
"""
),
"disaster_recovery": PromptTemplate(
input_variables=["query", "context"],
template="""
Context: {context}
Query: {query}
Explain the responsibilities of cybersecurity professionals in ensuring that disaster recovery and business continuity plans are developed, tested, and maintained to address security challenges.
"""
),
"user_access_management": PromptTemplate(
input_variables=["query", "context"],
template="""
Context: {context}
Query: {query}
Describe the best practices for managing user access and privileges, including role-based access control (RBAC), least privilege principles, and audit trails for sensitive systems.
"""
),
"cloud_security": PromptTemplate(
input_variables=["query", "context"],
template="""
Context: {context}
Query: {query}
Provide a list of best practices for securing cloud-based infrastructure, including the use of strong authentication, data encryption, and continuous monitoring.
"""
),
"security_kpis": PromptTemplate(
input_variables=["query", "context"],
template="""
Context: {context}
Query: {query}
Discuss the key performance indicators (KPIs) used by cybersecurity professionals to measure the effectiveness of security programs, such as incident response times, patching cycles, and vulnerability remediation rates.
"""
),
"employee_security_education": PromptTemplate(
input_variables=["query", "context"],
template="""
Context: {context}
Query: {query}
Describe the methods used by cybersecurity professionals to educate employees on security best practices, including training programs, phishing simulations, and awareness campaigns.
"""
),
"common_challenges": PromptTemplate(
input_variables=["query", "context"],
template="""
Context: {context}
Query: {query}
Identify and discuss the common challenges that cybersecurity professionals face, including resource limitations, evolving threats, and the complexities of compliance.
"""
),
"compliance_standards": PromptTemplate(
input_variables=["query", "context"],
template="""
Context: {context}
Query: {query}
Provide an overview of how cybersecurity professionals ensure compliance with industry standards and regulations, such as GDPR, HIPAA, and PCI DSS, including regular audits and reporting.
"""
),
"encryption_role": PromptTemplate(
input_variables=["query", "context"],
template="""
Context: {context}
Query: {query}
Explain the role of encryption in protecting sensitive data, focusing on encryption methods, data-at-rest vs. data-in-transit, and how encryption helps mitigate the risks of data breaches.
"""
),
"mobile_device_security": PromptTemplate(
input_variables=["query", "context"],
template="""
Context: {context}
Query: {query}
Provide strategies for managing and securing mobile devices and applications, including mobile device management (MDM), app whitelisting, and secure communication methods.
"""
),
"security_audits": PromptTemplate(
input_variables=["query", "context"],
template="""
Context: {context}
Query: {query}
Outline the steps involved in conducting security audits and risk assessments, including identifying potential threats, assessing vulnerabilities, and recommending mitigation strategies.
"""
),
"patch_management": PromptTemplate(
input_variables=["query", "context"],
template="""
Context: {context}
Query: {query}
Describe the best practices for managing patch updates and ensuring software security, including patch management policies, vulnerability scanning, and prioritizing patches based on risk.
"""
),
"wireless_iot_security": PromptTemplate(
input_variables=["query", "context"],
template="""
Context: {context}
Query: {query}
Provide a comprehensive guide on securing wireless networks and IoT devices, including the use of encryption, network segmentation, and regular vulnerability assessments.
"""
),
# Add all other prompts here...
}
# Initialize chains for each prompt
chains = {key: LLMChain(llm=llm, prompt=prompt) for key, prompt in prompt_templates.items()}
# Classify user input to determine the appropriate prompt
def classify_query(query: str) -> str:
# Simple keyword-based classification
if "common threats" in query.lower():
return "common_threats"
elif "task prioritization" in query.lower():
return "task_prioritization"
elif "network traffic tools" in query.lower():
return "network_traffic_tools"
elif "vulnerability assessments" in query.lower():
return "vulnerability_assessments"
elif "security policies" in query.lower():
return "security_policies"
elif "staying updated" in query.lower():
return "staying_updated"
elif "immediate incidents" in query.lower():
return "immediate_incidents"
elif "collaboration with IT teams" in query.lower():
return "collaboration_it_teams"
elif "incident investigation" in query.lower():
return "incident_investigation"
elif "securing remote workers" in query.lower():
return "securing_remote_workers"
elif "disaster recovery" in query.lower():
return "disaster_recovery"
elif "user access management" in query.lower():
return "user_access_management"
elif "cloud security" in query.lower():
return "cloud_security"
elif "security KPIs" in query.lower():
return "security_kpis"
elif "employee security education" in query.lower():
return "employee_security_education"
elif "common challenges" in query.lower():
return "common_challenges"
elif "compliance standards" in query.lower():
return "compliance_standards"
elif "encryption role" in query.lower():
return "encryption_role"
elif "mobile device security" in query.lower():
return "mobile_device_security"
elif "security audits" in query.lower():
return "security_audits"
elif "patch management" in query.lower():
return "patch_management"
elif "wireless and IoT security" in query.lower():
return "wireless_iot_security"
# Add more conditions for other prompts...
else:
return "general"
@app.post("/search")
async def process_search(search_query: SearchQuery):
try:
# Set default context if not provided
context = search_query.context or "You are a cybersecurity expert."
# Classify the query
query_type = classify_query(search_query.query)
# Process the query using the appropriate chain
if query_type in chains:
response = chains[query_type].run(query=search_query.query, context=context)
else:
response = "I am not sure how to help with that. Please provide more details."
return {
"status": "success",
"response": response
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/")
async def root():
return {"message": "Search API is running"} |