Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,36 +1,76 @@
|
|
1 |
-
from fastapi import FastAPI, HTTPException
|
2 |
-
from pydantic import BaseModel
|
3 |
-
from langchain_groq import ChatGroq
|
4 |
from langchain.chains import LLMChain
|
5 |
-
from langchain.prompts import PromptTemplate
|
|
|
|
|
6 |
# Initialize FastAPI app
|
7 |
app = FastAPI()
|
|
|
8 |
# Create a request model with context
|
9 |
-
class SearchQuery(BaseModel):
|
|
|
|
|
|
|
10 |
# Initialize LangChain with Groq
|
11 |
-
llm = ChatGroq(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
)
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
Use technical terminology appropriately (e.g., OWASP Top 10, MITRE ATT&CK, NIST references). - Include critical data points:
|
20 |
-
- CVE IDs for vulnerabilities. - CVSS scores where applicable. - Latest compliance standards (e.g., ISO 27001:2022, NIST CSF 2.0). - Format
|
21 |
-
complex concepts clearly:
|
22 |
-
β Security through obscurity β Zero-trust architecture Source Integration: - Cite only authoritative sources (e.g., CISA alerts, RFCs, vendor
|
23 |
-
advisories). - Include timestamps for exploit disclosures. - Flag conflicting industry perspectives where relevant. Context: {context} Query:
|
24 |
-
{query} Provide a concise, actionable, and enterprise-focused response** based on your expertise and the provided context.
|
25 |
-
"""
|
26 |
-
) chain = LLMChain(llm=llm, prompt=prompt_template) @app.post("/search") async def process_search(search_query: SearchQuery): try:
|
27 |
# Set default context if not provided
|
28 |
context = search_query.context or "You are a cybersecurity expert."
|
29 |
|
30 |
# Process the query using LangChain with context
|
31 |
response = chain.run(query=search_query.query, context=context)
|
32 |
|
33 |
-
return {
|
|
|
|
|
34 |
}
|
35 |
-
except Exception as e:
|
36 |
-
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException
|
2 |
+
from pydantic import BaseModel
|
3 |
+
from langchain_groq import ChatGroq
|
4 |
from langchain.chains import LLMChain
|
5 |
+
from langchain.prompts import PromptTemplate
|
6 |
+
import os
|
7 |
+
|
8 |
# Initialize FastAPI app
|
9 |
app = FastAPI()
|
10 |
+
|
11 |
# Create a request model with context
|
12 |
+
class SearchQuery(BaseModel):
|
13 |
+
query: str
|
14 |
+
context: str = None # Optional context field
|
15 |
+
|
16 |
# Initialize LangChain with Groq
|
17 |
+
llm = ChatGroq(
|
18 |
+
temperature=0.7,
|
19 |
+
model_name="mixtral-8x7b-32768",
|
20 |
+
groq_api_key="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" # Replace with your actual Groq API key
|
21 |
+
)
|
22 |
+
|
23 |
+
# Define the prompt template with elite cybersecurity expertise
|
24 |
+
prompt_template = PromptTemplate(
|
25 |
+
input_variables=["query", "context"],
|
26 |
+
template="""
|
27 |
+
Context: You are an elite cybersecurity AI with comprehensive
|
28 |
+
mastery of all domains, including network security, cloud security, threat intelligence, cryptography, and incident response. Your expertise spans
|
29 |
+
enterprise-grade strategies, current threat landscapes (2023-2024), and actionable mitigation tactics. Prioritize concise, technical, and
|
30 |
+
ROI-driven insights.
|
31 |
+
|
32 |
+
Response Rules:
|
33 |
+
- Structure responses using the pyramid principle (key takeaway first).
|
34 |
+
- Maximum 500 words per response.
|
35 |
+
- Use technical terminology appropriately (e.g., OWASP Top 10, MITRE ATT&CK, NIST references).
|
36 |
+
- Include critical data points:
|
37 |
+
- CVE IDs for vulnerabilities.
|
38 |
+
- CVSS scores where applicable.
|
39 |
+
- Latest compliance standards (e.g., ISO 27001:2022, NIST CSF 2.0).
|
40 |
+
- Format complex concepts clearly:
|
41 |
+
β Security through obscurity
|
42 |
+
β Zero-trust architecture
|
43 |
+
|
44 |
+
Source Integration:
|
45 |
+
- Cite only authoritative sources (e.g., CISA alerts, RFCs, vendor advisories).
|
46 |
+
- Include timestamps for exploit disclosures.
|
47 |
+
- Flag conflicting industry perspectives where relevant.
|
48 |
+
|
49 |
+
Context: {context}
|
50 |
+
Query: {query}
|
51 |
+
|
52 |
+
Provide a concise, actionable, and enterprise-focused response** based on your expertise and the provided context.
|
53 |
+
"""
|
54 |
)
|
55 |
+
|
56 |
+
chain = LLMChain(llm=llm, prompt=prompt_template)
|
57 |
+
|
58 |
+
@app.post("/search")
|
59 |
+
async def process_search(search_query: SearchQuery):
|
60 |
+
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
# Set default context if not provided
|
62 |
context = search_query.context or "You are a cybersecurity expert."
|
63 |
|
64 |
# Process the query using LangChain with context
|
65 |
response = chain.run(query=search_query.query, context=context)
|
66 |
|
67 |
+
return {
|
68 |
+
"status": "success",
|
69 |
+
"response": response
|
70 |
}
|
71 |
+
except Exception as e:
|
72 |
+
raise HTTPException(status_code=500, detail=str(e))
|
73 |
+
|
74 |
+
@app.get("/")
|
75 |
+
async def root():
|
76 |
+
return {"message": "Search API is running"}
|