mirror of
https://github.com/csunny/DB-GPT.git
synced 2025-09-09 21:08:59 +00:00
feat(core): More AWEL operators and new prompt manager API (#972)
Co-authored-by: csunny <cfqsunny@163.com>
This commit is contained in:
@@ -1,33 +1,64 @@
|
||||
"""This is an auto-generated model file
|
||||
You can define your own models and DAOs here
|
||||
"""
|
||||
from typing import Union, Any, Dict
|
||||
from datetime import datetime
|
||||
from sqlalchemy import Column, Integer, String, Index, Text, DateTime, UniqueConstraint
|
||||
from dbgpt.storage.metadata import Model, BaseDao, db
|
||||
from typing import Any, Dict, Union
|
||||
|
||||
from sqlalchemy import Column, DateTime, Index, Integer, String, Text, UniqueConstraint
|
||||
|
||||
from dbgpt.storage.metadata import BaseDao, Model, db
|
||||
|
||||
from ..api.schemas import ServeRequest, ServerResponse
|
||||
from ..config import ServeConfig, SERVER_APP_TABLE_NAME
|
||||
from ..config import SERVER_APP_TABLE_NAME, ServeConfig
|
||||
|
||||
|
||||
class ServeEntity(Model):
|
||||
__tablename__ = "prompt_manage"
|
||||
__table_args__ = (
|
||||
UniqueConstraint("prompt_name", "sys_code", name="uk_prompt_name_sys_code"),
|
||||
UniqueConstraint(
|
||||
"prompt_name",
|
||||
"sys_code",
|
||||
"prompt_language",
|
||||
"model",
|
||||
name="uk_prompt_name_sys_code",
|
||||
),
|
||||
)
|
||||
id = Column(Integer, primary_key=True, comment="Auto increment id")
|
||||
|
||||
chat_scene = Column(String(100))
|
||||
sub_chat_scene = Column(String(100))
|
||||
prompt_type = Column(String(100))
|
||||
prompt_name = Column(String(512))
|
||||
content = Column(Text)
|
||||
user_name = Column(String(128))
|
||||
chat_scene = Column(String(100), comment="Chat scene")
|
||||
sub_chat_scene = Column(String(100), comment="Sub chat scene")
|
||||
prompt_type = Column(String(100), comment="Prompt type(eg: common, private)")
|
||||
prompt_name = Column(String(256), comment="Prompt name")
|
||||
content = Column(Text, comment="Prompt content")
|
||||
input_variables = Column(
|
||||
String(1024), nullable=True, comment="Prompt input variables(split by comma))"
|
||||
)
|
||||
model = Column(
|
||||
String(128),
|
||||
nullable=True,
|
||||
comment="Prompt model name(we can use different models for different prompt",
|
||||
)
|
||||
prompt_language = Column(
|
||||
String(32), index=True, nullable=True, comment="Prompt language(eg:en, zh-cn)"
|
||||
)
|
||||
prompt_format = Column(
|
||||
String(32),
|
||||
index=True,
|
||||
nullable=True,
|
||||
default="f-string",
|
||||
comment="Prompt format(eg: f-string, jinja2)",
|
||||
)
|
||||
user_name = Column(String(128), index=True, nullable=True, comment="User name")
|
||||
sys_code = Column(String(128), index=True, nullable=True, comment="System code")
|
||||
gmt_created = Column(DateTime, default=datetime.now, comment="Record creation time")
|
||||
gmt_modified = Column(DateTime, default=datetime.now, comment="Record update time")
|
||||
|
||||
def __repr__(self):
|
||||
return f"ServeEntity(id={self.id}, chat_scene='{self.chat_scene}', sub_chat_scene='{self.sub_chat_scene}', prompt_type='{self.prompt_type}', prompt_name='{self.prompt_name}', content='{self.content}',user_name='{self.user_name}', gmt_created='{self.gmt_created}', gmt_modified='{self.gmt_modified}')"
|
||||
return (
|
||||
f"ServeEntity(id={self.id}, chat_scene='{self.chat_scene}', sub_chat_scene='{self.sub_chat_scene}', "
|
||||
f"prompt_type='{self.prompt_type}', prompt_name='{self.prompt_name}', content='{self.content}',"
|
||||
f"user_name='{self.user_name}', gmt_created='{self.gmt_created}', gmt_modified='{self.gmt_modified}')"
|
||||
)
|
||||
|
||||
|
||||
class ServeDao(BaseDao[ServeEntity, ServeRequest, ServerResponse]):
|
||||
|
56
dbgpt/serve/prompt/models/prompt_template_adapter.py
Normal file
56
dbgpt/serve/prompt/models/prompt_template_adapter.py
Normal file
@@ -0,0 +1,56 @@
|
||||
from typing import Type
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from dbgpt.core.interface.prompt import PromptTemplateIdentifier, StoragePromptTemplate
|
||||
from dbgpt.core.interface.storage import StorageItemAdapter
|
||||
|
||||
from .models import ServeEntity
|
||||
|
||||
|
||||
class PromptTemplateAdapter(StorageItemAdapter[StoragePromptTemplate, ServeEntity]):
|
||||
def to_storage_format(self, item: StoragePromptTemplate) -> ServeEntity:
|
||||
return ServeEntity(
|
||||
chat_scene=item.chat_scene,
|
||||
sub_chat_scene=item.sub_chat_scene,
|
||||
prompt_type=item.prompt_type,
|
||||
prompt_name=item.prompt_name,
|
||||
content=item.content,
|
||||
input_variables=item.input_variables,
|
||||
model=item.model,
|
||||
prompt_language=item.prompt_language,
|
||||
prompt_format=item.prompt_format,
|
||||
user_name=item.user_name,
|
||||
sys_code=item.sys_code,
|
||||
)
|
||||
|
||||
def from_storage_format(self, model: ServeEntity) -> StoragePromptTemplate:
|
||||
return StoragePromptTemplate(
|
||||
chat_scene=model.chat_scene,
|
||||
sub_chat_scene=model.sub_chat_scene,
|
||||
prompt_type=model.prompt_type,
|
||||
prompt_name=model.prompt_name,
|
||||
content=model.content,
|
||||
input_variables=model.input_variables,
|
||||
model=model.model,
|
||||
prompt_language=model.prompt_language,
|
||||
prompt_format=model.prompt_format,
|
||||
user_name=model.user_name,
|
||||
sys_code=model.sys_code,
|
||||
)
|
||||
|
||||
def get_query_for_identifier(
|
||||
self,
|
||||
storage_format: Type[ServeEntity],
|
||||
resource_id: PromptTemplateIdentifier,
|
||||
**kwargs,
|
||||
):
|
||||
session: Session = kwargs.get("session")
|
||||
if session is None:
|
||||
raise Exception("session is None")
|
||||
query_obj = session.query(ServeEntity)
|
||||
for key, value in resource_id.to_dict().items():
|
||||
if value is None:
|
||||
continue
|
||||
query_obj = query_obj.filter(getattr(ServeEntity, key) == value)
|
||||
return query_obj
|
Reference in New Issue
Block a user