File size: 963 Bytes
2c28a0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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"]