Spaces:
Running
Running
Rename utils/openai_utils.py to utils/aiml_api_utils.py
Browse files- utils/aiml_api_utils.py +37 -0
- utils/openai_utils.py +0 -30
utils/aiml_api_utils.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from openai import OpenAI
|
3 |
+
|
4 |
+
# Set the AI/ML API key and base URL
|
5 |
+
aiml_api_key = os.getenv("AIML_API_KEY")
|
6 |
+
base_url = "https://api.aimlapi.com/"
|
7 |
+
|
8 |
+
# Initialize the OpenAI client for the AI/ML API
|
9 |
+
client = OpenAI(
|
10 |
+
api_key=aiml_api_key,
|
11 |
+
base_url=base_url
|
12 |
+
)
|
13 |
+
|
14 |
+
# Function to call the AI/ML API
|
15 |
+
def call_aiml_api(prompt, max_tokens=2000):
|
16 |
+
try:
|
17 |
+
# Call the AI/ML API with the given prompt and max tokens
|
18 |
+
chat_completion = client.chat.completions.create(
|
19 |
+
model="o1-mini",
|
20 |
+
messages=[
|
21 |
+
{"role": "user", "content": prompt},
|
22 |
+
],
|
23 |
+
max_tokens=max_tokens,
|
24 |
+
)
|
25 |
+
|
26 |
+
# Extract and return the response content
|
27 |
+
response = chat_completion.choices[0].message.content
|
28 |
+
return response
|
29 |
+
|
30 |
+
except Exception as e:
|
31 |
+
return f"Error while calling the AI/ML API: {str(e)}"
|
32 |
+
|
33 |
+
# Example usage (for testing purposes)
|
34 |
+
if __name__ == "__main__":
|
35 |
+
test_prompt = "Explain how backpropagation works in simple terms."
|
36 |
+
response = call_aiml_api(test_prompt)
|
37 |
+
print("API Response:", response)
|
utils/openai_utils.py
DELETED
@@ -1,30 +0,0 @@
|
|
1 |
-
import openai
|
2 |
-
|
3 |
-
import os
|
4 |
-
from openai import OpenAI
|
5 |
-
|
6 |
-
# Function to call the AI/ML API with your key and input prompt
|
7 |
-
def call_aiml_api(prompt, max_tokens=2000):
|
8 |
-
# Define the AI/ML API key and base URL
|
9 |
-
aiml_api_key = os.getenv('AIML_API_KEY') # Make sure you set this in your environment
|
10 |
-
base_url = "https://api.aimlapi.com/"
|
11 |
-
|
12 |
-
# Initialize the OpenAI client with the custom base URL and API key
|
13 |
-
client = OpenAI(
|
14 |
-
api_key=aiml_api_key,
|
15 |
-
base_url=base_url
|
16 |
-
)
|
17 |
-
|
18 |
-
# Create a chat completion request using the provided model and prompt
|
19 |
-
chat_completion = client.chat.completions.create(
|
20 |
-
model="o1-mini",
|
21 |
-
messages=[
|
22 |
-
{"role": "user", "content": prompt},
|
23 |
-
],
|
24 |
-
max_tokens=max_tokens,
|
25 |
-
)
|
26 |
-
|
27 |
-
# Extract the response from the API response object
|
28 |
-
response = chat_completion.choices[0].message.content
|
29 |
-
|
30 |
-
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|