dn-scribe commited on
Commit
c753287
·
unverified ·
1 Parent(s): e49a7cb

fix: upstream openai

Browse files
Files changed (1) hide show
  1. app/utils.py +10 -6
app/utils.py CHANGED
@@ -2,11 +2,14 @@
2
  from datetime import datetime, timedelta
3
  import aiofiles
4
  import json
5
- import openai
6
  import os
7
 
8
  REPORT_DIR = "app/reports"
9
 
 
 
 
10
  async def report_exists(cve_id: str, suffix: str = ".md") -> bool:
11
  path = os.path.join(REPORT_DIR, f"{cve_id}{suffix}")
12
  if not os.path.exists(path):
@@ -29,22 +32,23 @@ async def write_report(cve_id: str, content, suffix: str = ".md"):
29
 
30
  async def generate_openai_stream(cve_id: str):
31
  prompt = f"Generate a Markdown-formatted technical CVE analysis for {cve_id}."
32
- response = await openai.ChatCompletion.acreate(
33
  model="gpt-4",
34
  messages=[{"role": "user", "content": prompt}],
35
  stream=True,
36
  )
37
- async for chunk in response:
38
- delta = chunk["choices"][0]["delta"].get("content", "")
39
  if delta:
40
  yield delta
41
 
42
  async def generate_openai_json(cve_id: str):
43
  prompt = f"Return a structured JSON risk profile for CVE ID {cve_id}. Include fields: severity, description, affected_components, risk_score (0–10)."
44
- response = await openai.ChatCompletion.acreate(
45
  model="gpt-4",
46
  messages=[{"role": "user", "content": prompt}],
47
  )
48
- text = response["choices"][0]["message"]["content"]
49
  return json.loads(text)
50
 
 
 
2
  from datetime import datetime, timedelta
3
  import aiofiles
4
  import json
5
+ from openai import AsyncOpenAI
6
  import os
7
 
8
  REPORT_DIR = "app/reports"
9
 
10
+ client = AsyncOpenAI(api_key=os.getenv("OPENAI_API_KEY"))
11
+
12
+
13
  async def report_exists(cve_id: str, suffix: str = ".md") -> bool:
14
  path = os.path.join(REPORT_DIR, f"{cve_id}{suffix}")
15
  if not os.path.exists(path):
 
32
 
33
  async def generate_openai_stream(cve_id: str):
34
  prompt = f"Generate a Markdown-formatted technical CVE analysis for {cve_id}."
35
+ stream = await client.chat.completions.create(
36
  model="gpt-4",
37
  messages=[{"role": "user", "content": prompt}],
38
  stream=True,
39
  )
40
+ async for chunk in stream:
41
+ delta = chunk.choices[0].delta.content or ""
42
  if delta:
43
  yield delta
44
 
45
  async def generate_openai_json(cve_id: str):
46
  prompt = f"Return a structured JSON risk profile for CVE ID {cve_id}. Include fields: severity, description, affected_components, risk_score (0–10)."
47
+ response = await client.chat.completions.create(
48
  model="gpt-4",
49
  messages=[{"role": "user", "content": prompt}],
50
  )
51
+ text = response.choices[0].message.content
52
  return json.loads(text)
53
 
54
+