Spaces:
Sleeping
Sleeping
Update utils/openai_utils.py
Browse files- utils/openai_utils.py +25 -11
utils/openai_utils.py
CHANGED
|
@@ -1,16 +1,30 @@
|
|
| 1 |
import openai
|
| 2 |
|
| 3 |
-
|
| 4 |
-
openai
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
)
|
| 12 |
-
return response['choices'][0]['text']
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
| 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
|