Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,29 +4,51 @@ import os
|
|
| 4 |
import time
|
| 5 |
|
| 6 |
# Load your OpenAI API key from the environment variable
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# Function to interact with the GPT-4o-mini model via the OpenAI API
|
| 10 |
def subsurface_sentinel(user_input):
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
# Retry mechanism in case of connection errors
|
| 14 |
for _ in range(3): # Try up to 3 times
|
| 15 |
try:
|
| 16 |
response = openai.chat.completions.create(
|
| 17 |
model="gpt-4o-mini", # Use the gpt-4o-mini model
|
| 18 |
-
messages=
|
| 19 |
-
{"role": "system", "content": "You are Subsurface Sentinel, a virtual assistant for professionals in the oil and gas industry, focusing on field development planning."},
|
| 20 |
-
{"role": "user", "content": prompt}
|
| 21 |
-
],
|
| 22 |
max_tokens=150,
|
| 23 |
temperature=0.7,
|
| 24 |
)
|
| 25 |
# Extract the response text and remove any leading/trailing whitespace
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
except openai.error.APIConnectionError as e:
|
| 28 |
print(f"Connection error: {e}. Retrying...")
|
| 29 |
time.sleep(2) # Wait for 2 seconds before retrying
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
return "Failed to connect to OpenAI API after several attempts."
|
| 32 |
|
|
|
|
| 4 |
import time
|
| 5 |
|
| 6 |
# Load your OpenAI API key from the environment variable
|
| 7 |
+
api_key = os.getenv("OPENAI_API_KEY")
|
| 8 |
+
|
| 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}"
|
| 52 |
|
| 53 |
return "Failed to connect to OpenAI API after several attempts."
|
| 54 |
|