refactor(deepseek_integrator): 使用Pathlib改进.env文件加载路径
修改环境变量加载逻辑,使用Pathlib获取项目根目录路径来定位.env文件,提高代码的可维护性和跨平台兼容性
This commit is contained in:
parent
dbebd707ba
commit
4f941ca7d5
Binary file not shown.
Binary file not shown.
@ -5,6 +5,7 @@ import requests
|
|||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
class DeepSeekIntegrator:
|
class DeepSeekIntegrator:
|
||||||
def __init__(self, api_key=None):
|
def __init__(self, api_key=None):
|
||||||
@ -13,7 +14,10 @@ class DeepSeekIntegrator:
|
|||||||
|
|
||||||
# 如果没有提供API密钥,尝试从环境变量加载
|
# 如果没有提供API密钥,尝试从环境变量加载
|
||||||
if not api_key:
|
if not api_key:
|
||||||
load_dotenv()
|
# 获取项目根目录的.env文件路径
|
||||||
|
project_root = Path(__file__).parent.parent
|
||||||
|
env_path = project_root / ".env"
|
||||||
|
load_dotenv(dotenv_path=env_path)
|
||||||
self.api_key = os.getenv("DEEPSEEK_API_KEY")
|
self.api_key = os.getenv("DEEPSEEK_API_KEY")
|
||||||
|
|
||||||
# 模拟数据 - 实际使用时替换为真实API调用
|
# 模拟数据 - 实际使用时替换为真实API调用
|
||||||
@ -60,7 +64,9 @@ class DeepSeekIntegrator:
|
|||||||
def _call_api(self, prompt, max_tokens=1000, temperature=0.7):
|
def _call_api(self, prompt, max_tokens=1000, temperature=0.7):
|
||||||
"""调用DeepSeek API"""
|
"""调用DeepSeek API"""
|
||||||
# 重新加载环境变量,确保使用最新的API密钥
|
# 重新加载环境变量,确保使用最新的API密钥
|
||||||
load_dotenv()
|
project_root = Path(__file__).parent.parent
|
||||||
|
env_path = project_root / ".env"
|
||||||
|
load_dotenv(dotenv_path=env_path)
|
||||||
current_api_key = os.getenv("DEEPSEEK_API_KEY")
|
current_api_key = os.getenv("DEEPSEEK_API_KEY")
|
||||||
|
|
||||||
# 如果实例化时没有提供API密钥,使用当前环境变量中的值
|
# 如果实例化时没有提供API密钥,使用当前环境变量中的值
|
||||||
@ -293,4 +299,4 @@ if __name__ == "__main__":
|
|||||||
print("\n=== 测试传播预测 ===")
|
print("\n=== 测试传播预测 ===")
|
||||||
initial_impact = "目前已有500名乘客在社交媒体上表达不满,相关话题讨论量达到1000条"
|
initial_impact = "目前已有500名乘客在社交媒体上表达不满,相关话题讨论量达到1000条"
|
||||||
propagation = integrator.predict_propagation(current_event, initial_impact)
|
propagation = integrator.predict_propagation(current_event, initial_impact)
|
||||||
print(propagation)
|
print(propagation)
|
||||||
Loading…
Reference in New Issue
Block a user