구글 시트로 프롬프트 관리 방식 변경
This commit is contained in:
1 parent
f8c1738aa2
commit
ae9f0b3c62
11 files changed
+551
-43
No files matched your search
@@ -1,13 +1,18 @@
|
||||
import os, json
|
||||
import gspread
|
||||
from pydantic import BaseModel
|
||||
from google.oauth2.service_account import Credentials
|
||||
from config import prompt_settings
|
||||
from app.utils.logger import get_logger
|
||||
from app.utils.prompts.schemas import *
|
||||
from functools import lru_cache
|
||||
import openpyxl
|
||||
|
||||
logger = get_logger("prompt")
|
||||
|
||||
_SCOPES = [
|
||||
"https://www.googleapis.com/auth/spreadsheets.readonly",
|
||||
"https://www.googleapis.com/auth/drive.readonly"
|
||||
]
|
||||
|
||||
class Prompt():
|
||||
sheet_name: str
|
||||
prompt_template: str
|
||||
@@ -20,24 +25,20 @@ class Prompt():
|
||||
self.sheet_name = sheet_name
|
||||
self.prompt_input_class = prompt_input_class
|
||||
self.prompt_output_class = prompt_output_class
|
||||
self.prompt_template, self.prompt_model = self._read_from_excel()
|
||||
self.prompt_template, self.prompt_model = self._read_from_sheets()
|
||||
|
||||
def _read_from_excel(self) -> tuple[str, str]:
|
||||
wb = openpyxl.load_workbook(prompt_settings.PROMPT_EXCEL_FILE, read_only=True)
|
||||
try:
|
||||
ws = wb[self.sheet_name]
|
||||
data = {}
|
||||
for row in ws.iter_rows(min_row=2, values_only=True):
|
||||
key, value = row[0], row[1]
|
||||
if key and value:
|
||||
data[key] = value
|
||||
finally:
|
||||
wb.close()
|
||||
|
||||
return data["input"], data["model"]
|
||||
def _read_from_sheets(self) -> tuple[str, str]:
|
||||
creds = Credentials.from_service_account_file(
|
||||
prompt_settings.GOOGLE_SERVICE_ACCOUNT_JSON, scopes=_SCOPES
|
||||
)
|
||||
gc = gspread.authorize(creds)
|
||||
ws = gc.open_by_key(prompt_settings.PROMPT_SPREADSHEET).worksheet(self.sheet_name)
|
||||
model = ws.cell(2, 2).value
|
||||
input_text = ws.cell(3, 2).value
|
||||
return input_text, model
|
||||
|
||||
def _reload_prompt(self):
|
||||
self.prompt_template, self.prompt_model = self._read_from_excel()
|
||||
self.prompt_template, self.prompt_model = self._read_from_sheets()
|
||||
|
||||
def build_prompt(self, input_data: dict) -> str:
|
||||
verified_input = self.prompt_input_class(**input_data)
|
||||
|
||||
Reference in new issue
Block a user