-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
57 lines (44 loc) · 1.57 KB
/
utils.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from openai import OpenAI
from dotenv import load_dotenv
import os
import logging
import time
# Set up logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
# Create a file handler and set the formatter
file_handler = logging.FileHandler("openai.log", "a", "utf-8")
file_handler.setFormatter(formatter)
# Add the file handler to the logger
logger.addHandler(file_handler)
# setup azureopenai credentials
load_dotenv()
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
OPENAI_MODEL_NAME = os.getenv("OPENAI_MODEL_NAME")
def append_data(df, worksheet, number_of_cols):
values = worksheet.get_all_values()
col = colnumn_string(max([len(r) for r in values]) + 1)
worksheet.add_cols(number_of_cols)
worksheet.update(
values=df.values.tolist(),
range_name=col + "2",
value_input_option="USER_ENTERED",
)
def colnumn_string(n):
string = ""
while n > 0:
n, remainder = divmod(n - 2, 26)
string = chr(65 + remainder) + string
return string
def lm_completion(messages: list[dict]) -> str:
client = OpenAI(api_key=OPENAI_API_KEY)
logger.info("Messages: %s", messages)
start = time.perf_counter()
response = client.chat.completions.create(
messages=messages, model=OPENAI_MODEL_NAME
)
request_time = time.perf_counter() - start
logger.info("Response Time: %s", request_time)
logger.info("Response: %s", response.choices[0].message.content.strip())
return response.choices[0].message.content.strip()