Spaces:
Sleeping
Sleeping
Create utils/openai_utils.py
Browse files- utils/openai_utils.py +16 -0
utils/openai_utils.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import openai
|
| 2 |
+
|
| 3 |
+
# Set your OpenAI API key
|
| 4 |
+
openai.api_key = "your-openai-api-key"
|
| 5 |
+
|
| 6 |
+
def call_openai(prompt, max_tokens=150):
|
| 7 |
+
response = openai.Completion.create(
|
| 8 |
+
model="text-davinci-003",
|
| 9 |
+
prompt=prompt,
|
| 10 |
+
max_tokens=max_tokens
|
| 11 |
+
)
|
| 12 |
+
return response['choices'][0]['text']
|
| 13 |
+
|
| 14 |
+
def get_ai_explanation(question):
|
| 15 |
+
prompt = f"Explain the following machine learning concept in simple terms: {question}"
|
| 16 |
+
return call_openai(prompt, max_tokens=150)
|