WaysAhead Global commited on
Commit
77649db
·
verified ·
1 Parent(s): 97cfd28

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +12 -11
main.py CHANGED
@@ -16,6 +16,7 @@ from langchain.prompts import (
16
  )
17
  import pytz
18
  import uvicorn
 
19
 
20
  # Load environment variables
21
  load_dotenv()
@@ -24,7 +25,7 @@ if not OPENAI_API_KEY:
24
  raise ValueError("OPENAI_API_KEY is not set.")
25
 
26
  # Initialize FastAPI
27
- app = FastAPI(title="PMO360 Prompt-Based Weekly Reporter", version="5.1")
28
 
29
  # Connect to DB
30
  print("Connecting to SQL Server...")
@@ -131,17 +132,16 @@ name_extractor = ChatOpenAI(model="gpt-3.5-turbo", temperature=0, openai_api_key
131
  name_prompt = PromptTemplate(
132
  input_variables=["text"],
133
  template="""
134
- You will extract the name and optional date range from the prompt for generating a weekly update.
135
 
136
  Examples:
137
- - "Show Anirban Chatterjee's weekly update" → {"first_name": "Anirban", "last_name": "Chatterjee"}
138
- - "Weekly update for Aditya from April 1 to April 7" → {"first_name": "Aditya", "start_date": "2025-04-01", "end_date": "2025-04-07"}
139
- - "DPR for Rahul Kumar between 15 April and 20 April" → {"first_name": "Rahul", "last_name": "Kumar", "start_date": "2025-04-15", "end_date": "2025-04-20"}
140
 
141
- Respond strictly in JSON with:
142
- - first_name (required)
143
- - last_name (optional)
144
- - start_date and end_date in YYYY-MM-DD format (optional)
145
 
146
  Prompt: {text}
147
  """
@@ -155,11 +155,12 @@ class PromptRequest(BaseModel):
155
  @app.post("/weekly-dpr")
156
  async def get_prompt_based_dpr(request: PromptRequest):
157
  try:
158
- from json import loads
159
  prompt_text = request.prompt.strip()
160
  name_response = name_extractor.invoke(name_prompt.format_prompt(text=prompt_text).to_string())
161
  print("LLM raw output:", name_response.content)
162
- name_json = loads(name_response.content.strip().strip("'").strip('"'))
 
 
163
 
164
  first = name_json.get("first_name")
165
  last = name_json.get("last_name")
 
16
  )
17
  import pytz
18
  import uvicorn
19
+ import json
20
 
21
  # Load environment variables
22
  load_dotenv()
 
25
  raise ValueError("OPENAI_API_KEY is not set.")
26
 
27
  # Initialize FastAPI
28
+ app = FastAPI(title="PMO360 Prompt-Based Weekly Reporter", version="5.2")
29
 
30
  # Connect to DB
31
  print("Connecting to SQL Server...")
 
132
  name_prompt = PromptTemplate(
133
  input_variables=["text"],
134
  template="""
135
+ Extract user's first name, last name, and date range from the given prompt.
136
 
137
  Examples:
138
+ "Weekly report for Anirban Chatterjee from April 15 to April 20"
139
+ → {"first_name": "Anirban", "last_name": "Chatterjee", "start_date": "2025-04-15", "end_date": "2025-04-20"}
 
140
 
141
+ "DPR for Rahul this week"
142
+ → {"first_name": "Rahul"}
143
+
144
+ Respond ONLY with a valid Python dictionary, no explanation or quotes around it.
145
 
146
  Prompt: {text}
147
  """
 
155
  @app.post("/weekly-dpr")
156
  async def get_prompt_based_dpr(request: PromptRequest):
157
  try:
 
158
  prompt_text = request.prompt.strip()
159
  name_response = name_extractor.invoke(name_prompt.format_prompt(text=prompt_text).to_string())
160
  print("LLM raw output:", name_response.content)
161
+
162
+ # Safely parse AI-native dictionary
163
+ name_json = eval(name_response.content)
164
 
165
  first = name_json.get("first_name")
166
  last = name_json.get("last_name")