refactor: update configuration handling and OpenAI client initialization
This commit is contained in:
@@ -15,7 +15,7 @@ if not os.path.exists(CONFIG_DIR):
|
||||
os.makedirs(CONFIG_DIR)
|
||||
|
||||
|
||||
settings = Config("config/config.yaml")
|
||||
settings = Config(f"{CONFIG_DIR}/config.yaml")
|
||||
if not os.path.exists(settings.database.temp.expanduser()):
|
||||
settings.database.temp.expanduser().mkdir(parents=True, exist_ok=True)
|
||||
from .utils.icon import Icon
|
||||
|
||||
@@ -1,28 +1,37 @@
|
||||
from openai import OpenAI
|
||||
from src import settings
|
||||
import json
|
||||
from src import LOG_DIR
|
||||
|
||||
model = settings.openAI.model
|
||||
api_key = settings.openAI.api_key
|
||||
client = OpenAI(api_key = api_key)
|
||||
|
||||
|
||||
def init_client():
|
||||
"""Initialize the OpenAI client with the API key and model from settings."""
|
||||
global client, model, api_key
|
||||
if not settings.openAI.api_key:
|
||||
raise ValueError("OpenAI API key is not set in the configuration.")
|
||||
if not settings.openAI.model:
|
||||
raise ValueError("OpenAI model is not set in the configuration.")
|
||||
|
||||
model = settings.openAI.model
|
||||
api_key = settings.openAI.api_key
|
||||
client = OpenAI(api_key=api_key)
|
||||
return client
|
||||
def run_shortener(title:str, length:int):
|
||||
client = init_client()
|
||||
response = client.responses.create(
|
||||
model = model,
|
||||
instructions = """you are a sentence shortener. The next message will contain the string to shorten and the length limit.
|
||||
model=model,
|
||||
instructions="""you are a sentence shortener. The next message will contain the string to shorten and the length limit.
|
||||
You need to shorten the string to be under the length limit, while keeping as much detail as possible. The result may NOT be longer than the length limit.
|
||||
based on that, please reply only the shortened string. Give me 5 choices. if the length is too long, discard the string and try another one.Return the data as a python list containing the result as {"shortened_string": shortened_string, "length": lengthasInt}. Do not return the answer in a codeblock, use a pure string. Before answering, check the results and if ANY is longer than the needed_length, discard all and try again""",
|
||||
input = f'{{"string":"{title}", "needed_length":{length}}}'
|
||||
input=f'{{"string":"{title}", "needed_length":{length}}}',
|
||||
)
|
||||
print(length)
|
||||
answers = response.output_text
|
||||
print(answers)
|
||||
return eval(answers) # type: ignore
|
||||
#answers are strings in json format, so we need to convert them to a list of dicts
|
||||
|
||||
|
||||
def name_tester(name: str):
|
||||
client = init_client()
|
||||
response = client.responses.create(
|
||||
model = model,
|
||||
instructions="""you are a name tester, You are given a name and will have to split the name into first name, last name, and if present the title. Return the name in a json format with the keys "title", "first_name", "last_name". If no title is present, set title to none. Do NOt return the answer in a codeblock, use a pure json string. Assume the names are in the usual german naming scheme""",
|
||||
@@ -33,6 +42,7 @@ def name_tester(name: str):
|
||||
return json.loads(answers)
|
||||
|
||||
def semester_converter(semester:str):
|
||||
client = init_client()
|
||||
response = client.responses.create(
|
||||
model = model,
|
||||
instructions="""you are a semester converter. You will be given a string. Convert this into a string like this: SoSe YY or WiSe YY/YY+1. Do not return the answer in a codeblock, use a pure string.""",
|
||||
|
||||
@@ -160,6 +160,8 @@ class ZoteroController:
|
||||
zoterocfg = settings.zotero
|
||||
|
||||
def __init__(self):
|
||||
if self.zoterocfg.library_id is None:
|
||||
return
|
||||
self.zot = zotero.Zotero(
|
||||
self.zoterocfg.library_id,
|
||||
self.zoterocfg.library_type,
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
from PyQt6 import QtWidgets, QtCore
|
||||
from PyQt6.QtCore import QDate
|
||||
from PyQt6.QtGui import QColor, QPen
|
||||
from src.backend import Database
|
||||
import sys
|
||||
from typing import Any
|
||||
|
||||
import darkdetect
|
||||
import loguru
|
||||
import sys
|
||||
from PyQt6 import QtCore, QtWidgets
|
||||
from PyQt6.QtCore import QDate
|
||||
from PyQt6.QtGui import QColor, QPen
|
||||
|
||||
from src import LOG_DIR
|
||||
from src.backend import Database
|
||||
|
||||
log = loguru.logger
|
||||
log.remove()
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import random
|
||||
from typing import Union, Any
|
||||
import sys
|
||||
from typing import Any, Union
|
||||
|
||||
import loguru
|
||||
import pyqtgraph as pg
|
||||
from PyQt6 import QtWidgets
|
||||
import loguru
|
||||
import sys
|
||||
|
||||
from src import LOG_DIR
|
||||
|
||||
log = loguru.logger
|
||||
|
||||
@@ -1,16 +1,18 @@
|
||||
from .widget_sources.search_statistic_page_ui import Ui_Dialog
|
||||
from PyQt6 import QtWidgets, QtGui, QtCore
|
||||
from PyQt6.QtCore import pyqtSignal
|
||||
from src.backend import Database, Semester
|
||||
|
||||
from src.logic import custom_sort, Prof, sort_semesters_list
|
||||
from src.ui.dialogs import Mail_Dialog, ApparatExtendDialog, ReminderDialog
|
||||
from src.ui.widgets import DataGraph, StatusWidget
|
||||
from src import LOG_DIR
|
||||
from natsort import natsorted
|
||||
import loguru
|
||||
import sys
|
||||
|
||||
import loguru
|
||||
from natsort import natsorted
|
||||
from PyQt6 import QtCore, QtGui, QtWidgets
|
||||
from PyQt6.QtCore import pyqtSignal
|
||||
|
||||
from src import LOG_DIR
|
||||
from src.backend import Database, Semester
|
||||
from src.logic import Prof, custom_sort, sort_semesters_list
|
||||
from src.ui.dialogs import ApparatExtendDialog, Mail_Dialog, ReminderDialog
|
||||
from src.ui.widgets import DataGraph, StatusWidget
|
||||
|
||||
from .widget_sources.search_statistic_page_ui import Ui_Dialog
|
||||
|
||||
log = loguru.logger
|
||||
log.remove()
|
||||
log.add(sys.stdout, level="INFO")
|
||||
|
||||
Reference in New Issue
Block a user