Flamengo / call_to_api.py
drguilhermeapolinario's picture
Create call_to_api.py
2c28a0b verified
raw
history blame contribute delete
963 Bytes
from openai import OpenAI
import streamlit as st
def call_openai_api(prompt, model="gpt-4o-mini", api_key=None):
"""
Call the OpenAI API to generate a response based on a given prompt.
Parameters:
prompt (str): The prompt to generate a response for.
model (str): The OpenAI model to use for generating the response.
api_key (str): The API key for accessing OpenAI.
Returns:
str: The generated response.
"""
if api_key is None:
st.info("Insira sua chave API OpenAI para continuar.")
st.stop()
openai.api_key = api_key # Set the API key for OpenAI
response = openai.chat.completions.create(
model=model,
messages=[
{"role": "system", "content": "Você é um assistente útil."},
{"role": "user", "content": prompt}
],
temperature=0.4,
max_tokens=1500,
)
return response.choices[0].message["content"]