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