eagle0504 commited on
Commit
4232961
1 Parent(s): 39db86b

Create utils/my_openai.py

Browse files
Files changed (1) hide show
  1. utils/my_openai.py +32 -0
utils/my_openai.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import openai
3
+ import streamlit as st
4
+
5
+ openai.api_key = os.environ["OPENAI_API_KEY"]
6
+
7
+
8
+ def call_chatcompletion(
9
+ messages: list, model: str = "gpt-4", temperature: int = 0
10
+ ) -> str:
11
+ """
12
+ Get the completion response from a list of messages using OpenAI's ChatCompletion API.
13
+
14
+ Parameters:
15
+ - messages (list): A list of messages which includes role ("user" or "assistant") and content.
16
+ - model (str): The name of the OpenAI model to use. Default is "gpt-4".
17
+ - temperature (int): The temperature parameter for generating more random or deterministic responses. Default is 0.
18
+
19
+ Returns:
20
+ - str: The content of the first response choice in the completed message.
21
+
22
+ """
23
+ # Call OpenAI's ChatCompletion API with the specified parameters
24
+ response = openai.ChatCompletion.create(
25
+ model=model,
26
+ messages=messages,
27
+ temperature=temperature,
28
+ max_tokens=4000,
29
+ )
30
+
31
+ # Get the content of the first response choice in the completed message
32
+ return response.choices[0].message["content"]