|
@@ -0,0 +1,122 @@
|
|
|
+import json
|
|
|
+
|
|
|
+from openai import OpenAI
|
|
|
+
|
|
|
+from app.admin.schema.intent_org import CurrentIntentOrgIns
|
|
|
+from app.call_center.crud.crud_form_records import form_records_dao
|
|
|
+from app.call_center.schema.form_records import CreateFormRecordsParam
|
|
|
+from core.conf import settings
|
|
|
+from database.db_mysql import async_db_session
|
|
|
+from database.db_redis import redis_client
|
|
|
+from common.log import log
|
|
|
+from utils.serializers import select_as_dict
|
|
|
+
|
|
|
+
|
|
|
+class FormRecordsService:
|
|
|
+ @staticmethod
|
|
|
+ async def create(*, obj: CreateFormRecordsParam) -> None:
|
|
|
+ # 从缓存中获取机构信息
|
|
|
+ key = f'{settings.TOKEN_CALL_REDIS_PREFIX}:{obj.org_id}'
|
|
|
+ org_json = await redis_client.get(key)
|
|
|
+ if not org_json:
|
|
|
+ # 缓存中没有,从数据库中获取
|
|
|
+ from app.admin.crud.crud_intent_org import intent_org_dao
|
|
|
+ async with async_db_session.begin() as db:
|
|
|
+ org = await intent_org_dao.get(db, obj.org_id)
|
|
|
+ if not org and org.status is not 1:
|
|
|
+ log.error(f"表单信息提取时,机构不存在 org_id: {obj.org_id}")
|
|
|
+ return None
|
|
|
+ org_data = CurrentIntentOrgIns(**select_as_dict(org))
|
|
|
+ # 将数据放进缓存
|
|
|
+ await redis_client.setex(
|
|
|
+ key,
|
|
|
+ settings.JWT_USER_REDIS_EXPIRE_SECONDS,
|
|
|
+ org_data.model_dump_json(),
|
|
|
+ )
|
|
|
+ else:
|
|
|
+ org_data = CurrentIntentOrgIns(**json.loads(org_json))
|
|
|
+
|
|
|
+ # 开始提取
|
|
|
+ intent_schema = {
|
|
|
+ "name": "array",
|
|
|
+ "schema": { # 添加 schema 字段
|
|
|
+ "type": "object",
|
|
|
+ "description": "从通话记录中提取表单值",
|
|
|
+ "properties": {
|
|
|
+ "dataIndex": {"type": "string", "description": "表单ID"},
|
|
|
+ "value": {
|
|
|
+ "type": "array",
|
|
|
+ "description": "表单值",
|
|
|
+ "items": {"type": "string"}
|
|
|
+ },
|
|
|
+ },
|
|
|
+ "required": ["dataIndex", "value"]
|
|
|
+ }
|
|
|
+ }
|
|
|
+ messages = [
|
|
|
+ {"role": "system", "content": f"""# 任务
|
|
|
+请帮助user从通话记录中提取表单值,并返回一个JSON格式的表单值。
|
|
|
+
|
|
|
+# 返回值示例
|
|
|
+* 如表单类型为 input、autoComplete、textarea,返回示例:["表单值"]
|
|
|
+* 如表单类型为 radio、select,返回示例:["值1"]
|
|
|
+* 如表单类型为 checkbox,返回示例:["值1", "值2"]
|
|
|
+* 如表单类型为 cascader,返回示例:["一级值1", "二级值3"]
|
|
|
+* 如表单类型为 date,返回示例:["2025-01-01"]"""
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "role": "user",
|
|
|
+ "content": f"""# 表单数据
|
|
|
+{obj.form_data}
|
|
|
+
|
|
|
+# 聊天记录
|
|
|
+{obj.chat_history}
|
|
|
+"""
|
|
|
+ }
|
|
|
+ ]
|
|
|
+ response_data = generate_json(org_data.openai_key, org_data.openai_base, messages, intent_schema)
|
|
|
+ if response_data and isinstance(response_data.choices, list) and len(response_data.choices) > 0:
|
|
|
+ first_choice = response_data.choices[0]
|
|
|
+ if first_choice and first_choice.message:
|
|
|
+ response_json = first_choice.message.content
|
|
|
+ if response_json:
|
|
|
+ form_value = json.loads(response_json)
|
|
|
+ obj.form_value = form_value
|
|
|
+ async with async_db_session.begin() as db:
|
|
|
+ await form_records_dao.create(db, obj)
|
|
|
+ return form_value
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+def generate_json(api_key: str, openai_base: str, messages: list[dict], json_schema: dict):
|
|
|
+ try:
|
|
|
+ client_args = {}
|
|
|
+ if api_key:
|
|
|
+ client_args["api_key"] = api_key
|
|
|
+ if openai_base:
|
|
|
+ client_args["base_url"] = openai_base
|
|
|
+
|
|
|
+ oai_client = OpenAI(**client_args)
|
|
|
+
|
|
|
+ completion = oai_client.chat.completions.create(
|
|
|
+ model="gpt-4o",
|
|
|
+ messages=messages,
|
|
|
+ response_format={
|
|
|
+ "type": "json_schema",
|
|
|
+ "json_schema": json_schema
|
|
|
+ }
|
|
|
+ )
|
|
|
+ log.error(f"[oai] api_key failed: {api_key}")
|
|
|
+ log.error(f"[oai] openai_base failed: {openai_base}")
|
|
|
+ log.error(f"[oai] completion failed: {completion}")
|
|
|
+ if completion and isinstance(completion.choices, list) and len(completion.choices) > 0:
|
|
|
+ first_choice = completion.choices[0]
|
|
|
+ if first_choice and first_choice.message:
|
|
|
+ # return first_choice.message.content
|
|
|
+ return completion
|
|
|
+ except Exception as e:
|
|
|
+ log.error(f"[oai] generate_json failed: {e}")
|
|
|
+
|
|
|
+form_records_service: FormRecordsService = FormRecordsService()
|