rajrakeshdr commited on
Commit
85807eb
Β·
verified Β·
1 Parent(s): ee92f4e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -23
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 import os
 
 
6
  # Initialize FastAPI app
7
  app = FastAPI()
 
8
  # Create a request model with context
9
- class SearchQuery(BaseModel): query: str context: str = None # Optional context field
 
 
 
10
  # Initialize LangChain with Groq
11
- llm = ChatGroq( temperature=0.7, model_name="mixtral-8x7b-32768", groq_api_key="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" # Replace with your actual Groq API key
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  )
13
- # Define the prompt template with cybersecurity expertise Define the prompt template with elite cybersecurity expertise
14
- prompt_template = PromptTemplate( input_variables=["query", "context"],
15
- template=""" Context: You are an elite cybersecurity AI with comprehensive
16
- mastery of all domains, including network security, cloud security, threat intelligence, cryptography, and incident response. Your expertise spans
17
- enterprise-grade strategies, current threat landscapes (2023-2024), and actionable mitigation tactics. Prioritize concise, technical, and
18
- ROI-driven insights. Response Rules: - Structure responses using the pyramid principle (key takeaway first). - Maximum 500 words per response. -
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 { "status": "success", "response": response
 
 
34
  }
35
- except Exception as e: raise HTTPException(status_code=500, detail=str(e)) @app.get("/") async def root():
36
- return {"message": "Search API is running"}
 
 
 
 
 
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"}