更新config

This commit is contained in:
yuruo
2024-04-16 00:16:37 +08:00
parent b526ac2e81
commit 35d6d4b45d
11 changed files with 167 additions and 119 deletions

View File

@@ -1,14 +0,0 @@
openai:
# key
api_key: xxx
# xxxxx/v1/
api_url: xx
model: gpt-4-1106-preview
# local or remote
data_position: local
# 如果是 remote 需要配置 leancloud远程环境
# leancloud:
# key: xxx
# id: xxxx

24
config_tmp.yaml Normal file
View File

@@ -0,0 +1,24 @@
base:
数据存储位置:
description: 数据存储位置
config:
数据存储位置: local
components:
openai:
description: 用于配置openai相关信息
config:
api_key: 请输入api_key
api_url: https://api.openai.com/v1/
model: gpt-4-1106-preview
feishu:
description: 配置飞书相关信息,用于与飞书进行交互
config:
app_id: 请输入api_id
app_secret: 请输入app_secret
browser:
description: 配置本电脑的浏览器信息,用于浏览器自动化
config:
brow: edge

View File

@@ -5,6 +5,7 @@ from PyQt6.QtWidgets import QLabel, QTextEdit, QListWidgetItem, QSpacerItem, QSi
from agent.woker_agent import WorkerAgent
from pages.bse_page import BasePage
from pages.config_page import ConfigPage
from pages.func_list_page import FuncListPage
from utils.config import Config
from utils.qt_util import QtUtil
@@ -92,25 +93,9 @@ class ChatPage(BasePage):
self.ui.hide()
def open_setting_page(self):
self.setting_page = QtUtil.load_ui("setting_page.ui")
config = Config()
self.setting_page.openai_key.setText(config.OPEN_AI.get("openai_key"))
self.setting_page.openai_url.setText(config.OPEN_AI.get("openai_url"))
self.setting_page.openai_model.setText(config.OPEN_AI.get("openai_model"))
self.setting_page.save_btn.clicked.connect(self.save_setting)
self.setting_page.cancel_btn.clicked.connect(self.cancel_btn)
self.setting_page = ConfigPage()
self.setting_page.show()
def save_setting(self):
config = Config()
config.update_config(Config.OPENAI, Config.OPENAI_KEY, self.setting_page.openai_key.text())
config.update_config(Config.OPENAI, Config.OPENAI_URL, self.setting_page.openai_url.text())
config.update_config(Config.OPENAI, Config.OPENAI_MODEL, self.setting_page.openai_model.text())
self.setting_page.close()
def cancel_btn(self):
self.setting_page.close()
def new_conversation(self, text, role):
text = text.replace("\n", "<br>")

61
pages/config_page.py Normal file
View File

@@ -0,0 +1,61 @@
from utils.config import Config
from utils.qt_util import QtUtil
from PyQt6.QtWidgets import QMainWindow, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QPushButton, QWidget
interface_ui = QtUtil.load_ui_type("config_page.ui")
class ConfigPage(QMainWindow, interface_ui):
def __init__(self):
super().__init__()
self.setupUi(self)
self.setup_up()
self.save_button.clicked.connect(self.save_setting)
self.cancel_button.clicked.connect(self.cancel_btn)
def setup_up(self):
self.config = Config()
config_list_widget = self.config_list
for type_key, type_value in self.config.config.items():
widget = QWidget()
config_list_widget.addWidget(widget)
for sub_title, sub_value in type_value.items():
'''
components:
feishu:
description: 配置飞书相关信息,用于与飞书进行交互
config:
app_id: 请输入api_id
app_secret: 请输入app_secret
'''
sub_title_label = QLabel()
# feishu:配置飞书相关信息,用于与飞书进行交互
sub_title_label.setText(sub_title + ":"+ sub_value["description"])
v_box_layout = QVBoxLayout()
widget.setLayout(v_box_layout)
v_box_layout.addWidget(sub_title_label)
# app_id, 请输入api_id
# app_secret, 请输入app_secret
for config_key, config_value in sub_value["config"].items():
h_box_layout = QHBoxLayout()
config_key_label = QLabel()
# app_id
config_key_label.setText(config_key)
line_edit = QLineEdit()
# 请输入api_id
line_edit.setText(config_value)
h_box_layout.addWidget(config_key_label)
h_box_layout.addWidget(line_edit)
v_box_layout.addLayout(h_box_layout)
def save_setting(self):
self.config.update_config()
self.setting_page.close()
def cancel_btn(self):
self.setting_page.close()

View File

@@ -11,34 +11,10 @@
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
<string>设置</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QWidget" name="verticalLayoutWidget">
<property name="geometry">
<rect>
<x>20</x>
<y>60</y>
<width>531</width>
<height>261</height>
</rect>
</property>
<layout class="QVBoxLayout" name="config_list"/>
</widget>
<widget class="QScrollBar" name="verticalScrollBar">
<property name="geometry">
<rect>
<x>560</x>
<y>40</y>
<width>20</width>
<height>321</height>
</rect>
</property>
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
</widget>
<widget class="QPushButton" name="saveButton">
<widget class="QPushButton" name="save_button">
<property name="geometry">
<rect>
<x>460</x>
@@ -51,7 +27,7 @@
<string>保存</string>
</property>
</widget>
<widget class="QPushButton" name="cancelButton">
<widget class="QPushButton" name="cancel_button">
<property name="geometry">
<rect>
<x>520</x>
@@ -64,43 +40,45 @@
<string>取消</string>
</property>
</widget>
<widget class="QLabel" name="label">
<widget class="QStackedWidget" name="config_list">
<property name="geometry">
<rect>
<x>90</x>
<y>20</y>
<width>471</width>
<height>281</height>
</rect>
</property>
<property name="currentIndex">
<number>1</number>
</property>
<widget class="QWidget" name="basic"/>
<widget class="QWidget" name="components"/>
</widget>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>10</x>
<y>30</y>
<width>54</width>
<height>12</height>
</rect>
</property>
<property name="text">
<string>配置</string>
</property>
</widget>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>54</width>
<height>12</height>
</rect>
</property>
<property name="text">
<string>description</string>
</property>
</widget>
<widget class="Line" name="line">
<property name="geometry">
<rect>
<x>0</x>
<y>10</y>
<width>581</width>
<height>16</height>
<width>61</width>
<height>21</height>
</rect>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
<property name="text">
<string>基础配置</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_2">
<property name="geometry">
<rect>
<x>10</x>
<y>50</y>
<width>61</width>
<height>21</height>
</rect>
</property>
<property name="text">
<string>组件配置</string>
</property>
</widget>
</widget>

View File

@@ -1,4 +1,3 @@
from pages.bse_page import BasePage
from pages.edit_action_list_view import ActionList
from pages.edit_function_page import FunctionListView
from pages.global_util import GlobalUtil

13
tools/feishu_tool.py Normal file
View File

@@ -0,0 +1,13 @@
import os
from langchain.tools import BaseTool
class FindDesktopPath(BaseTool):
name = "上传文档到飞书"
description = "上传文档到飞书"
# args_schema = None
def _run(self):
url = "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal"
desktop_path = os.path.join(os.path.expanduser("~"), "Desktop")
return desktop_path

View File

@@ -1,4 +1,5 @@
import os
import shutil
import yaml
@@ -6,38 +7,29 @@ from utils.qt_util import QtUtil
class Config:
OPENAI_KEY = "openai_key"
OPENAI_URL = "openai_url"
OPENAI_MODEL = "openai_model"
OPENAI = "openai"
BROWSER = "browser"
def __init__(self):
# 项目根目录
self.path = os.path.join(QtUtil.get_root_path(), "config.yaml")
self.config_path = os.path.join(QtUtil.get_root_path(), "config.yaml")
# 上一层目录
self.config = self._load_config(self.path)
self.OPEN_AI = self.config[self.OPENAI]
self.BROWSER_CONFIG = self.config[self.BROWSER]
self.config = self.load_config()
@staticmethod
# Load content from a yaml file and return as variables
def _load_config(file_path):
# 如果文件不存在则生成一个yaml文件
if not os.path.exists(file_path):
with open(file_path, 'w') as file:
data = {Config.OPENAI: {Config.OPENAI_KEY: "your_api_key",
Config.OPENAI_URL: "https://api.openai.com/v1/",
Config.OPENAI_MODEL: "gpt-4-1106-preview"},
"data_position": "local",
Config.BROWSER: "edge"}
yaml.dump(data, file)
def get_config_from_base(self, title, key):
return self.config["base"][title]["config"][key]
with open(file_path, 'r') as file:
def get_config_from_component(self, title, key):
return self.config["components"][title]["config"][key]
def load_config(self):
# 如果文件不存在则生成一个yaml文件
if not os.path.exists(self.config_path):
# 将 config_tmp.yaml 复制并改名为 config.yaml
shutil.copyfile(os.path.join(QtUtil.get_root_path(), "config_tmp.yaml"), self.config_path)
with open(self.config_path, 'r', encoding='utf-8') as file:
config = yaml.safe_load(file)
return config
def update_config(self, loc, key, value):
self.config[loc][key] = value
def update_config(self):
# 将 self.config 写入 yaml 文件
with open(self.path, 'w') as file:
yaml.dump(self.config, file)

9
utils/feishu_util.py Normal file
View File

@@ -0,0 +1,9 @@
import requests
class FeiShuUtil:
def get_token(self):
app_id = ""
app_secret = ""
url = "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal"
res = requests.post(url)
print(res)

View File

@@ -1,3 +1,4 @@
from utils.config import Config
@@ -5,9 +6,9 @@ class LLM_Util:
def __init__(self):
super().__init__()
self.config = Config()
self.api_key = self.config.OPEN_AI.get(Config.OPENAI_KEY)
self.base_url = self.config.OPEN_AI.get(Config.OPENAI_URL)
self.open_ai_model = self.config.OPEN_AI.get(Config.OPENAI_MODEL)
self.api_key = self.config.get_config_from_component("openai", "api_key")
self.base_url = self.config.get_config_from_component("openai", "api_url")
self.open_ai_model = self.config.get_config_from_component("openai", "model")
def llm(self):
from langchain_openai import ChatOpenAI

View File

@@ -12,7 +12,7 @@ from utils.config import Config
class SeleniumUtil:
def __init__(self):
config = Config()
browser_type = config.BROWSER_CONFIG.get("browser_type")
browser_type = config.get_config_from_component("browser", "浏览器类型")
if browser_type == "chrome":
options = webdriver.ChromeOptions()
options.add_argument("--headless") # Enable headless mode