|
|
import json |
|
|
import requests |
|
|
|
|
|
BASE_URL = "https://dashscope.aliyuncs.com/compatible-mode/v1" |
|
|
MODEL = "qwen3-max" |
|
|
TOKEN = "sk-ef26097310ec45c184e8d84b31ea9356" |
|
|
|
|
|
|
|
|
def call_llm(prompt: str): |
|
|
"""模拟调用大模型的函数(你可以替换为真实请求)""" |
|
|
import requests |
|
|
|
|
|
headers = { |
|
|
"Authorization": f"Bearer {TOKEN}", |
|
|
"Content-Type": "application/json" |
|
|
} |
|
|
data = { |
|
|
"model": MODEL, |
|
|
"messages": [{"role": "user", "content": prompt}], |
|
|
"stream": True |
|
|
} |
|
|
response = requests.post(f"{BASE_URL}/chat/completions", headers=headers, json=data, stream=True) |
|
|
for line in response.iter_lines(): |
|
|
if line: |
|
|
yield line.decode('utf-8') |
|
|
|
|
|
def call_llm_non_streaming(prompt: str): |
|
|
"""调用大模型的函数(非流式)""" |
|
|
import requests |
|
|
|
|
|
headers = { |
|
|
"Authorization": f"Bearer {TOKEN}", |
|
|
"Content-Type": "application/json" |
|
|
} |
|
|
data = { |
|
|
"model": MODEL, |
|
|
"messages": [{"role": "user", "content": prompt}], |
|
|
"stream": False |
|
|
} |
|
|
response = requests.post(f"{BASE_URL}/chat/completions", headers=headers, json=data) |
|
|
if response.status_code == 200: |
|
|
result = response.json() |
|
|
return result['choices'][0]['message']['content'] |
|
|
else: |
|
|
return "Error: Failed to get response from LLM" |
|
|
|
|
|
def get_invest_suggest( |
|
|
user_input: str |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
): |
|
|
if not user_input.strip(): |
|
|
return ['你没有输入任何内容'] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
prompt = f""" |
|
|
你是一个专业的投资建议助手,你会对公司${user_input}, |
|
|
|
|
|
给出以下投资建议。 |
|
|
|
|
|
投资建议分析: |
|
|
- 是否建议投资 |
|
|
- 买入卖出价格 |
|
|
- 止损止盈点 |
|
|
- 持有时间(短期1月内、长期1月以上) |
|
|
|
|
|
要求用语专业、言简意赅,给出具体的建议。: |
|
|
""" |
|
|
|
|
|
response = call_llm_non_streaming(prompt) |
|
|
|
|
|
return response |
|
|
|
|
|
def get_analysis_report(user_input: str): |
|
|
if not user_input.strip(): |
|
|
return ['你没有输入任何内容'] |
|
|
prompt = f""" |
|
|
# 请给出针对以下关键词的分析报告,条理清晰,要纯英文版的: |
|
|
# {user_input} |
|
|
# """ |
|
|
|
|
|
response = call_llm_non_streaming(prompt) |
|
|
|
|
|
return response |
|
|
|
|
|
def search_company(user_input: str): |
|
|
if not user_input.strip(): |
|
|
return ['你没有输入任何内容'] |
|
|
|
|
|
response = chat_with_bailian_non_streaming(user_input, "6f24ece2689c4716b53b78aa7512f19f") |
|
|
print(f"查询结果:{response}{user_input}") |
|
|
try: |
|
|
parsed_data = json.loads(response) |
|
|
|
|
|
if isinstance(parsed_data, list): |
|
|
|
|
|
choices = [(f"{item['NAME']} ({item['CODE']})", f"{item['NAME']} ({item['CODE']})") for item in parsed_data] |
|
|
else: |
|
|
|
|
|
choices = ["未知错误"] |
|
|
return choices |
|
|
except json.JSONDecodeError: |
|
|
return ["未知错误"] |
|
|
|
|
|
def search_news(user_input: str): |
|
|
if not user_input.strip(): |
|
|
return [] |
|
|
|
|
|
response = chat_with_bailian_non_streaming(user_input, "f7be17d71e704607b6558cff62c94954") |
|
|
print(f"查询结果:{response}{user_input}") |
|
|
try: |
|
|
parsed_data = json.loads(response) |
|
|
|
|
|
if isinstance(parsed_data, list): |
|
|
|
|
|
print(parsed_data) |
|
|
return parsed_data |
|
|
|
|
|
else: |
|
|
|
|
|
choices = [] |
|
|
return choices |
|
|
except json.JSONDecodeError: |
|
|
return [] |
|
|
|
|
|
def get_stock_price_from_bailian(user_input: str): |
|
|
if not user_input.strip(): |
|
|
return {} |
|
|
|
|
|
response = chat_with_bailian_non_streaming(user_input, "d2b919f9b32e4fa28a75234cbb78a787") |
|
|
print(f"查询结果:{response}{user_input}") |
|
|
try: |
|
|
parsed_data = json.loads(response) |
|
|
return parsed_data if isinstance(parsed_data, dict) else {} |
|
|
except json.JSONDecodeError: |
|
|
return {} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def chat_with_bailian_non_streaming(message, app_id): |
|
|
|
|
|
API_KEY = "sk-9bac83f8dccf4978a6bb1f8330a5404f" |
|
|
|
|
|
API_URL = f"https://dashscope.aliyuncs.com/api/v1/apps/{app_id}/completion" |
|
|
""" |
|
|
与百炼自定义应用对话的函数(非流式) |
|
|
""" |
|
|
|
|
|
headers = { |
|
|
"Authorization": f"Bearer {API_KEY}", |
|
|
"Content-Type": "application/json" |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
messages = [] |
|
|
|
|
|
|
|
|
messages.append({"role": "user", "content": message}) |
|
|
|
|
|
|
|
|
data = { |
|
|
"input": { |
|
|
"messages": messages |
|
|
}, |
|
|
"parameters": { |
|
|
"max_tokens": 2048, |
|
|
"temperature": 0.8, |
|
|
"top_p": 0.9 |
|
|
} |
|
|
} |
|
|
|
|
|
try: |
|
|
|
|
|
response = requests.post(API_URL, headers=headers, data=json.dumps(data)) |
|
|
|
|
|
|
|
|
if response.status_code == 200: |
|
|
result = response.json() |
|
|
if "output" in result and "text" in result["output"]: |
|
|
return result["output"]["text"] |
|
|
else: |
|
|
return f"API响应格式不正确: {str(result)}" |
|
|
else: |
|
|
return f"API调用失败,状态码:{response.status_code},响应:{response.text}" |
|
|
|
|
|
except requests.RequestException as e: |
|
|
return f"网络请求错误:{str(e)}" |
|
|
except Exception as e: |
|
|
return f"发生未知错误:{str(e)}" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def chat_bot(user_input: str, history: list): |
|
|
if not user_input.strip(): |
|
|
yield "", history |
|
|
return |
|
|
|
|
|
|
|
|
history.append({"role": "user", "content": user_input}) |
|
|
|
|
|
bot_response = "" |
|
|
history.append({"role": "assistant", "content": bot_response}) |
|
|
|
|
|
for chunk in call_llm(user_input): |
|
|
if not chunk or chunk == "[DONE]": |
|
|
continue |
|
|
|
|
|
if chunk.startswith("data: "): |
|
|
chunk = chunk[6:] |
|
|
try: |
|
|
response_data = json.loads(chunk) |
|
|
delta = response_data.get("choices", [{}])[0].get("delta", {}) |
|
|
content = delta.get("content", "") |
|
|
if content: |
|
|
bot_response += content |
|
|
|
|
|
history[-1]["content"] = bot_response |
|
|
yield "", history |
|
|
except json.JSONDecodeError: |
|
|
continue |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|