1- from typing import List , Union
1+ import textwrap
2+ from typing import List , Optional , TypeVar , Union
23
34from camel .agents import ChatAgent
45from camel .models import BaseModelBackend
@@ -17,7 +18,7 @@ class DDLRecord(BaseModel):
1718class DMLRecord (BaseModel ):
1819 id : str
1920 summary : str
20- sql : str
21+ dataset : str
2122
2223
2324class QueryRecord (BaseModel ):
@@ -26,6 +27,14 @@ class QueryRecord(BaseModel):
2627 sql : str
2728
2829
30+ RecordType = TypeVar ("RecordType" , DDLRecord , DMLRecord , QueryRecord )
31+
32+
33+ class SchemaParseResponse (BaseModel ):
34+ data : List [RecordType ]
35+ usage : Optional [dict ]
36+
37+
2938class DDLRecordResponseFormat (BaseModel ):
3039 items : List [DDLRecord ]
3140
@@ -52,36 +61,74 @@ def __init__(
5261 )
5362
5463 @timing
55- def parse_ddl_record (self , text : str ) -> List [ DDLRecord ] :
64+ def parse_ddl_record (self , text : str ) -> SchemaParseResponse :
5665 """Parsing DDL SQL statements"""
5766 prompt = (
58- "The following are some DDL script. Please read the script in its "
59- "entirety and provide descriptions for the tables and fields to "
60- "generate summary information and extract the SQL script for each "
61- "table.\n \n "
67+ "Translate the following information into a JSON array format, "
68+ "with each JSON object in the array containing three "
69+ "elements: "
70+ "\" id\" for the table name, "
71+ "\" summary\" for a summary of the table, and "
72+ "\" sql\" for the SQL statement of the table creation.\n \n "
6273 )
63- prompt += f"```sql\n { text } ```\n \n "
64- prompt += "Please output the summary information and SQL script in JSON format."
74+ if text .startswith ("```sql" ):
75+ prompt += f"{ text } \n \n "
76+ else :
77+ prompt += f"```sql\n { text } ```\n \n "
78+
79+ # 非 openai 模型要增加以下片段
80+ prompt += textwrap .dedent (
81+ "Output Format:\n "
82+ "{"
83+ " \" items\" :"
84+ " ["
85+ " {"
86+ " \" id\" : \" <table name>\" ,"
87+ " \" summary\" : \" <table summary>\" ,"
88+ " \" sql\" : \" <table ddl script>\" "
89+ " }"
90+ " ]"
91+ "}\n \n "
92+ )
93+ prompt += "Now, directly output the JSON array without explanation."
6594 response = self .parsing_agent .step (prompt , response_format = DDLRecordResponseFormat )
6695 ddl_record_response = DDLRecordResponseFormat .model_validate_json (response .msgs [0 ].content )
67- return ddl_record_response .items
96+ return SchemaParseResponse ( data = ddl_record_response .items , usage = response . info [ "usage" ])
6897
6998 @timing
70- def parse_dml_record (self , text : str ) -> List [ DMLRecord ] :
99+ def parse_dml_record (self , text : str ) -> SchemaParseResponse :
71100 """Parsing DML SQL statements"""
72101 prompt = (
73- "The following are some DML statements from which you need "
74- "to extract table names, field names, and generate summary "
75- "information, as well as extract each SQL statement.\n \n "
102+ "Translate the following information into a JSON array format, "
103+ "with each JSON object in the array containing three "
104+ "elements: "
105+ "\" id\" for the table name, "
106+ "\" summary\" for a summary of the table, and "
107+ "\" dataset\" for the Markdown of the data.\n \n "
76108 )
77- prompt += f"```sql\n { text } ```\n "
78- prompt += "Please output the summary information and SQL script in JSON format."
109+ prompt += f"{ text } \n \n "
110+
111+ # 非 openai 模型要增加以下片段
112+ prompt += textwrap .dedent (
113+ "Output Format:\n "
114+ "{"
115+ " \" items\" :"
116+ " ["
117+ " {"
118+ " \" id\" : \" <table name>\" ,"
119+ " \" summary\" : \" <table summary>\" ,"
120+ " \" dataset\" : \" <markdown dataset>\" "
121+ " }"
122+ " ]"
123+ "}\n \n "
124+ )
125+ prompt += "Now, directly output the JSON array without explanation."
79126 response = self .parsing_agent .step (prompt , response_format = DMLRecordResponseFormat )
80127 dml_record_response = DMLRecordResponseFormat .model_validate_json (response .msgs [0 ].content )
81- return dml_record_response .items
128+ return SchemaParseResponse ( data = dml_record_response .items , usage = response . info [ "usage" ])
82129
83130 @timing
84- def parse_query_record (self , text : str ) -> List [ QueryRecord ] :
131+ def parse_query_record (self , text : str ) -> SchemaParseResponse :
85132 """Parsing Query SQL statements"""
86133 prompt = (
87134 "The following is an analysis of user query requirements, "
@@ -94,4 +141,4 @@ def parse_query_record(self, text: str) -> List[QueryRecord]:
94141 query_record_response = QueryRecordResponseFormat .model_validate_json (
95142 response .msgs [0 ].content
96143 )
97- return query_record_response .items
144+ return SchemaParseResponse ( data = query_record_response .items , usage = response . info [ "usage" ])
0 commit comments