Omkar008 commited on
Commit
31a8117
·
verified ·
1 Parent(s): 7ba0d38

Update services/location_service.py

Browse files
Files changed (1) hide show
  1. services/location_service.py +26 -1
services/location_service.py CHANGED
@@ -30,6 +30,31 @@ class LocationService:
30
  return {k: v for k, v in {"city": city, "state": state, "country": country}.items() if v is not None}
31
  else:
32
  return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  @staticmethod
34
  def system_prompt(location : str) -> str:
35
  return f"""You are a Text Analyser where you will extract city , state , country from given piece of text given below.You will strictly extract following keys from the text country , state , city.
@@ -58,7 +83,7 @@ class LocationService:
58
  if location:
59
  # Assuming `app.nlp` is already initialized elsewhere and accessible
60
  llm_prompt = LocationService.system_prompt(location)
61
- response = model.generate_content(llm_prompt)
62
  # Extract city, state, and country using the logic from extract_location_entities
63
  location_entities = json.loads(response.text)
64
 
 
30
  return {k: v for k, v in {"city": city, "state": state, "country": country}.items() if v is not None}
31
  else:
32
  return None
33
+
34
+ @staticmethod
35
+ def get_llm_response(system_prompt) -> str:
36
+ url = "https://api.openai.com/v1/chat/completions"
37
+ headers = {
38
+ "Content-Type": "application/json",
39
+ "Authorization": f"Bearer {os.getenv('OPENAI_API_KEY')}",
40
+ "OpenAI-Organization": os.getenv('ORG_ID')
41
+ }
42
+
43
+ data = {
44
+ "model": "gpt-4o-mini",
45
+ "max_tokens": 2000,
46
+ "messages": [{"role": "user", "content": f"{system_prompt}"}],
47
+ "temperature": 0.0
48
+ }
49
+ try:
50
+ response = requests.post(url, headers=headers, json=data)
51
+ output = response.json()
52
+ except Exception as e:
53
+ print(f"Error Occurred {e}")
54
+ return f"Error Occurred {e}"
55
+
56
+ return output['choices'][0]['message']['content']
57
+
58
  @staticmethod
59
  def system_prompt(location : str) -> str:
60
  return f"""You are a Text Analyser where you will extract city , state , country from given piece of text given below.You will strictly extract following keys from the text country , state , city.
 
83
  if location:
84
  # Assuming `app.nlp` is already initialized elsewhere and accessible
85
  llm_prompt = LocationService.system_prompt(location)
86
+ response = LocationService.get_llm_response(llm_prompt)
87
  # Extract city, state, and country using the logic from extract_location_entities
88
  location_entities = json.loads(response.text)
89