Files
fzjg_local/update_provinces.py

75 lines
2.4 KiB
Python
Raw Normal View History

from openai import OpenAI
from fzjgact.huodong.models import Branch
client = OpenAI(
base_url="https://integrate.api.nvidia.com/v1",
api_key="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"
)
def get_correct_province(branch_name, current_location):
prompt = f"""请根据以下分支机构名称,返回一个正确的中国省份名称(全称,如:浙江省、北京市、上海市、广东省等)。
分支机构名称{branch_name}
当前省份{current_location}
要求
1. 只返回省份名称不要包含任何其他文字
2. 省份名称必须是中国的省级行政区全称
3. 如果无法确定返回当前省份名称"""
completion = client.chat.completions.create(
model="deepseek-ai/deepseek-r1",
messages=[{"role": "user", "content": prompt}],
temperature=0.3,
top_p=0.7,
max_tokens=100,
stream=False
)
reasoning = getattr(completion.choices[0].message, "reasoning_content", None)
if reasoning:
print(f"推理过程: {reasoning}")
result = completion.choices[0].message.content.strip()
print(f"原始结果: {result}")
return result
def update_branch_provinces():
branches = Branch.objects.all()
total = branches.count()
updated_count = 0
print(f"开始处理 {total} 个分支机构...")
print("=" * 80)
for index, branch in enumerate(branches, 1):
print(f"\n[{index}/{total}] 处理: {branch.name}")
print(f"当前省份: {branch.location}")
try:
correct_province = get_correct_province(branch.name, branch.location)
print(f"建议省份: {correct_province}")
if correct_province != branch.location:
old_location = branch.location
branch.location = correct_province
branch.save()
print(f"✓ 已更新: {old_location} -> {correct_province}")
updated_count += 1
else:
print("- 省份未变化")
except Exception as e:
print(f"✗ 处理失败: {e}")
print("-" * 80)
print(f"\n处理完成!")
print(f"总计: {total} 个分支机构")
print(f"已更新: {updated_count}")
print(f"未变化: {total - updated_count}")
if __name__ == "__main__":
update_branch_provinces()