File size: 1,058 Bytes
4232961
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import openai
import streamlit as st

openai.api_key = os.environ["OPENAI_API_KEY"]


def call_chatcompletion(
    messages: list, model: str = "gpt-4", temperature: int = 0
) -> str:
    """
    Get the completion response from a list of messages using OpenAI's ChatCompletion API.

    Parameters:
    - messages (list): A list of messages which includes role ("user" or "assistant") and content.
    - model (str): The name of the OpenAI model to use. Default is "gpt-4".
    - temperature (int): The temperature parameter for generating more random or deterministic responses. Default is 0.

    Returns:
    - str: The content of the first response choice in the completed message.

    """
    # Call OpenAI's ChatCompletion API with the specified parameters
    response = openai.ChatCompletion.create(
        model=model,
        messages=messages,
        temperature=temperature,
        max_tokens=4000,
    )

    # Get the content of the first response choice in the completed message
    return response.choices[0].message["content"]