dindizz commited on
Commit
f38168e
·
verified ·
1 Parent(s): 5e5ab0e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -20
app.py CHANGED
@@ -9,43 +9,32 @@ api_key = os.getenv("OPENAI_API_KEY")
9
  # Ensure the API key doesn't have any leading/trailing whitespace or newline characters
10
  openai.api_key = api_key.strip()
11
 
12
- # Memory to store the conversation history
13
- conversation_history = []
14
-
15
  # Function to interact with the GPT-4o-mini model via the OpenAI API
16
  def subsurface_sentinel(user_input):
17
- # Add user input to the conversation history
18
- conversation_history.append({"role": "user", "content": user_input})
19
-
20
- # Prepare the messages for the API call
21
- messages = [
22
- {"role": "system", "content": "You are Subsurface Sentinel, a virtual assistant for professionals in the oil and gas industry, focusing on field development planning."}
23
- ] + conversation_history
24
 
25
  # Retry mechanism in case of connection errors
26
  for _ in range(3): # Try up to 3 times
27
  try:
28
  response = openai.chat.completions.create(
29
  model="gpt-4o-mini", # Use the gpt-4o-mini model
30
- messages=messages,
 
 
 
31
  max_tokens=150,
32
  temperature=0.7,
33
  )
34
  # Extract the response text and remove any leading/trailing whitespace
35
- assistant_message = response.choices[0].message['content'].strip()
36
-
37
- # Add assistant response to the conversation history
38
- conversation_history.append({"role": "assistant", "content": assistant_message})
39
-
40
- return assistant_message
41
  except openai.error.APIConnectionError as e:
42
  print(f"Connection error: {e}. Retrying...")
43
  time.sleep(2) # Wait for 2 seconds before retrying
44
- except openai.error.InvalidRequestError as e:
45
  return f"Invalid request: {e}"
46
- except openai.error.AuthenticationError as e:
47
  return f"Authentication failed: {e}"
48
- except openai.error.RateLimitError as e:
49
  return f"Rate limit exceeded: {e}"
50
  except Exception as e:
51
  return f"An unexpected error occurred: {e}"
 
9
  # Ensure the API key doesn't have any leading/trailing whitespace or newline characters
10
  openai.api_key = api_key.strip()
11
 
 
 
 
12
  # Function to interact with the GPT-4o-mini model via the OpenAI API
13
  def subsurface_sentinel(user_input):
14
+ prompt = f"User: {user_input}"
 
 
 
 
 
 
15
 
16
  # Retry mechanism in case of connection errors
17
  for _ in range(3): # Try up to 3 times
18
  try:
19
  response = openai.chat.completions.create(
20
  model="gpt-4o-mini", # Use the gpt-4o-mini model
21
+ messages=[
22
+ {"role": "system", "content": "You are Subsurface Sentinel, a virtual assistant for professionals in the oil and gas industry, focusing on field development planning."},
23
+ {"role": "user", "content": prompt}
24
+ ],
25
  max_tokens=150,
26
  temperature=0.7,
27
  )
28
  # Extract the response text and remove any leading/trailing whitespace
29
+ return response.choices[0].message.content.strip()
 
 
 
 
 
30
  except openai.error.APIConnectionError as e:
31
  print(f"Connection error: {e}. Retrying...")
32
  time.sleep(2) # Wait for 2 seconds before retrying
33
+ except openai.InvalidRequestError as e:
34
  return f"Invalid request: {e}"
35
+ except openai.AuthenticationError as e:
36
  return f"Authentication failed: {e}"
37
+ except openai.RateLimitError as e:
38
  return f"Rate limit exceeded: {e}"
39
  except Exception as e:
40
  return f"An unexpected error occurred: {e}"