zzck_code/llm_process.py

75 lines
4.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- coding: utf-8 -*-
from dashscope import Application
import pika
import json
import re
from config import *
news1 = "越捷航空(VietJet Aviation JSC)已将空客SE宽体飞机的订单增加一倍再订购20架a330neo以支持其未来10年的扩张计划。根据该航空公司的一份声明20架a330 -900neo飞机的新订单是在法国总统埃马纽埃尔·马克龙访问河内期间签署的。越捷航空董事长阮氏芳邵在声明中说:“我们对现代化、环保的机队的长期投资反映了我们加强越南和法国之间经济和技术联系的承诺。”这笔交易将使越捷积压的空客飞机数量达到近140架。该航空公司还订购了200架波音737 Max飞机。该公司去年订购了20架a330neo并在新加坡航展上签署了初步协议。每架A330-900neo的标价为3.74亿美元在不考虑行业惯例折扣的情况下最新交易的价格为75亿美元。越捷航空拥有115架空客飞机主要服务于国内航线和中国、泰国、韩国以及澳大利亚、印度和哈萨克斯坦等地区目的地。据彭博亿万富翁指数(Bloomberg Billionaires Index)显示该航空公司的多数股权由Thao持有他的身价约为15亿美元。然而这家越南最大的私营航空公司正面临债权人的麻烦。上个月该航空公司在伦敦高等法院的一起案件中败诉并被命令支付约1.8亿美元用于收购菲茨沃尔特资本有限公司的飞机租赁部门以解决在新冠肺炎大流行期间开始的长达数年的法律纠纷。上周越捷航空在上诉期间未能支付法院要求支付的三笔6,050万美元赔偿金中的第一笔。在诉讼过程中法官对越捷航空无力支付FitzWalter Capital的问题提出了质疑法官指出越捷航空仍有能力签署协议包括购买飞机。ZHXG"
def get_label(news, source):
def _clean_text(text: str) -> str:
"""清理文本去除HTML标签和特殊字符"""
# 移除HTML标签
text = re.sub(r'<[^>]+>', '', text)
# 移除特殊字符
text = re.sub(r'[^\w\s\u4e00-\u9fff]', '', text)
return text.strip()
news = _clean_text(news)
if not news:
return None
biz_params = {
"news_source": source
}
"""获取新闻的标签"""
try:
response = Application.call(
api_key="sk-23d05aac75f4458a94fc053d036de6eb",
app_id='024c0273115e4018a451a2393066e8d5',# 替换为实际的应用 ID
prompt=news,
biz_params=biz_params)
result_str = response.output.text.strip()
# 处理返回的字符串,去掉代码块标记
if result_str.startswith('```json'):
result_str = result_str[7:]
if result_str.endswith('```'):
result_str = result_str[:-3]
result = json.loads(result_str.strip())
return result if isinstance(result, dict) else {}
except Exception as e:
print(f"百炼工具流打标签失败: {str(e)}")
return None
def send_mq(news):
# 注意这里的news是一个字典,而且必须包含id
if not isinstance(news, dict) or 'id' not in news:
raise ValueError("news must be a dictionary and contain 'id' key")
try:
# 连接 RabbitMQ
credentials = pika.PlainCredentials(mq_user, mq_password)
connection = pika.BlockingConnection(
pika.ConnectionParameters(host='localhost', credentials=credentials, heartbeat=300)
)
channel = connection.channel()
channel.exchange_declare(exchange='zzck_llm_exchange', exchange_type='fanout')
# 发送消息
channel.basic_publish( exchange='zzck_llm_exchange',
routing_key='',
body=json.dumps(news),
properties=pika.BasicProperties(
delivery_mode=2, # 确保消息持久化
)
)
connection.close()
except Exception as e:
print(f"发送消息到MQ失败: {str(e)}")
return
if __name__ == "__main__":
# 测试
print("测试新闻1")
print(get_label(news1, "彭博社"))