Spaces:
Running
Running
drguilhermeapolinario
commited on
Commit
•
2c28a0b
1
Parent(s):
2de1396
Create call_to_api.py
Browse files- call_to_api.py +31 -0
call_to_api.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from openai import OpenAI
|
2 |
+
import streamlit as st
|
3 |
+
|
4 |
+
def call_openai_api(prompt, model="gpt-4o-mini", api_key=None):
|
5 |
+
"""
|
6 |
+
Call the OpenAI API to generate a response based on a given prompt.
|
7 |
+
|
8 |
+
Parameters:
|
9 |
+
prompt (str): The prompt to generate a response for.
|
10 |
+
model (str): The OpenAI model to use for generating the response.
|
11 |
+
api_key (str): The API key for accessing OpenAI.
|
12 |
+
|
13 |
+
Returns:
|
14 |
+
str: The generated response.
|
15 |
+
"""
|
16 |
+
if api_key is None:
|
17 |
+
st.info("Insira sua chave API OpenAI para continuar.")
|
18 |
+
st.stop()
|
19 |
+
|
20 |
+
openai.api_key = api_key # Set the API key for OpenAI
|
21 |
+
response = openai.chat.completions.create(
|
22 |
+
model=model,
|
23 |
+
messages=[
|
24 |
+
{"role": "system", "content": "Você é um assistente útil."},
|
25 |
+
{"role": "user", "content": prompt}
|
26 |
+
],
|
27 |
+
temperature=0.4,
|
28 |
+
max_tokens=1500,
|
29 |
+
)
|
30 |
+
return response.choices[0].message["content"]
|
31 |
+
|