yashbyname commited on
Commit
1b6df32
·
verified ·
1 Parent(s): 7c8eb62

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -27
app.py CHANGED
@@ -20,35 +20,64 @@ logger = logging.getLogger(__name__)
20
  api_key = 'KGSjxB1uptfSk8I8A7ciCuNT9Xa3qWC3'
21
  external_user_id = 'plugin-1717464304'
22
 
23
- # Step 1: Create a chat session
24
  def create_chat_session():
25
- create_session_url = 'https://api.on-demand.io/chat/v1/sessions'
26
- create_session_headers = {
27
- 'apikey': api_key
28
- }
29
- create_session_body = {
30
- "pluginIds": [],
31
- "externalUserId": external_user_id
32
- }
33
- # Make the request to create a chat session
34
- response = requests.post(create_session_url, headers=create_session_headers, json=create_session_body)
35
- response_data = response.json()
36
- session_id = response_data['data']['id']
37
- return session_id
38
-
 
 
 
 
 
39
  def submit_query(session_id, query):
40
- submit_query_url = f'https://api.on-demand.io/chat/v1/sessions/{session_id}/query'
41
- submit_query_headers = {
42
- 'apikey': api_key
43
- }
44
- submit_query_body = {
45
- "endpointId": "predefined-openai-gpt4o",
46
- "query": query,
47
- "pluginIds": ["plugin-1712327325", "plugin-1713962163"],
48
- "responseMode": "sync"
49
- }
50
- response = requests.post(submit_query_url, headers=submit_query_headers, json=submit_query_body)
51
- return response.json()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
 
54
  def extract_json_from_answer(answer):
 
20
  api_key = 'KGSjxB1uptfSk8I8A7ciCuNT9Xa3qWC3'
21
  external_user_id = 'plugin-1717464304'
22
 
 
23
  def create_chat_session():
24
+ try:
25
+ create_session_url = 'https://api.on-demand.io/chat/v1/sessions'
26
+ create_session_headers = {
27
+ 'apikey': api_key,
28
+ 'Content-Type': 'application/json'
29
+ }
30
+ create_session_body = {
31
+ "pluginIds": [],
32
+ "externalUserId": external_user_id
33
+ }
34
+
35
+ response = requests.post(create_session_url, headers=create_session_headers, json=create_session_body)
36
+ response.raise_for_status()
37
+ return response.json()['data']['id']
38
+
39
+ except requests.exceptions.RequestException as e:
40
+ logger.error(f"Error creating chat session: {str(e)}")
41
+ raise
42
+
43
  def submit_query(session_id, query):
44
+ try:
45
+ submit_query_url = f'https://api.on-demand.io/chat/v1/sessions/{session_id}/query'
46
+ submit_query_headers = {
47
+ 'apikey': api_key,
48
+ 'Content-Type': 'application/json'
49
+ }
50
+
51
+ structured_query = f"""
52
+ Based on the following patient information, provide a detailed medical analysis in JSON format:
53
+
54
+ {query}
55
+
56
+ Return only valid JSON with these fields:
57
+ - diagnosis_details
58
+ - probable_diagnoses (array)
59
+ - treatment_plans (array)
60
+ - lifestyle_modifications (array)
61
+ - medications (array of objects with name and dosage)
62
+ - additional_tests (array)
63
+ - precautions (array)
64
+ - follow_up (string)
65
+ """
66
+
67
+ submit_query_body = {
68
+ "endpointId": "predefined-openai-gpt4o",
69
+ "query": structured_query,
70
+ "pluginIds": ["plugin-1712327325", "plugin-1713962163"],
71
+ "responseMode": "sync"
72
+ }
73
+
74
+ response = requests.post(submit_query_url, headers=submit_query_headers, json=submit_query_body)
75
+ response.raise_for_status()
76
+ return response.json()
77
+
78
+ except requests.exceptions.RequestException as e:
79
+ logger.error(f"Error submitting query: {str(e)}")
80
+ raise
81
 
82
 
83
  def extract_json_from_answer(answer):