重整项目结构
This commit is contained in:
parent
3f10122a79
commit
643793e094
|
|
@ -1,124 +0,0 @@
|
||||||
import json
|
|
||||||
import requests
|
|
||||||
from ExcelHelper import ExcelHelper
|
|
||||||
|
|
||||||
def handle_response(data_dict):
|
|
||||||
message = data_dict.get("message")
|
|
||||||
if len(message) > 0:
|
|
||||||
content = message.get("content")
|
|
||||||
if len(content) > 0:
|
|
||||||
response_type = content.get("type")
|
|
||||||
if response_type == "text":
|
|
||||||
text = content.get("text", "No text provided")
|
|
||||||
return f"{text}"
|
|
||||||
|
|
||||||
elif response_type == "image":
|
|
||||||
images = content.get("image", [])
|
|
||||||
image_urls = ", ".join(image.get("image_url") for image in images)
|
|
||||||
return f"{image_urls}"
|
|
||||||
|
|
||||||
elif response_type == "code":
|
|
||||||
return f"{content.get('code')}"
|
|
||||||
|
|
||||||
elif response_type == "execution_output":
|
|
||||||
return f"{content.get('content')}"
|
|
||||||
|
|
||||||
elif response_type == "system_error":
|
|
||||||
return f"{content.get('content')}"
|
|
||||||
|
|
||||||
elif response_type == "tool_calls":
|
|
||||||
return f"{data_dict['tool_calls']}"
|
|
||||||
|
|
||||||
elif response_type == "browser_result":
|
|
||||||
content = json.loads(content.get("content", "{}"))
|
|
||||||
return f"Browser Result - Title: {content.get('title')} URL: {content.get('url')}"
|
|
||||||
|
|
||||||
def send_message(assistant_id, access_token, prompt, conversation_id=None, file_list=None, meta_data=None):
|
|
||||||
url = "https://chatglm.cn/chatglm/assistant-api/v1/stream"
|
|
||||||
headers = {
|
|
||||||
"Authorization": f"Bearer {access_token}",
|
|
||||||
"Content-Type": "application/json"
|
|
||||||
}
|
|
||||||
data = {
|
|
||||||
"assistant_id": assistant_id,
|
|
||||||
"prompt": prompt,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if conversation_id:
|
|
||||||
data["conversation_id"] = conversation_id
|
|
||||||
if file_list:
|
|
||||||
data["file_list"] = file_list
|
|
||||||
if meta_data:
|
|
||||||
data["meta_data"] = meta_data
|
|
||||||
|
|
||||||
with requests.post(url, json=data, headers=headers) as response:
|
|
||||||
if response.status_code == 200:
|
|
||||||
for line in response.iter_lines():
|
|
||||||
if line:
|
|
||||||
decoded_line = line.decode('utf-8')
|
|
||||||
if decoded_line.startswith('data:'):
|
|
||||||
data_dict = json.loads(decoded_line[5:])
|
|
||||||
output = handle_response(data_dict)
|
|
||||||
|
|
||||||
|
|
||||||
else:
|
|
||||||
return "Request failed", response.status_code
|
|
||||||
print(output)
|
|
||||||
return output
|
|
||||||
|
|
||||||
def get_access_token(api_key, api_secret):
|
|
||||||
url = "https://chatglm.cn/chatglm/assistant-api/v1/get_token"
|
|
||||||
data = {
|
|
||||||
"api_key": api_key,
|
|
||||||
"api_secret": api_secret
|
|
||||||
}
|
|
||||||
|
|
||||||
response = requests.post(url, json=data)
|
|
||||||
token_info = response.json()
|
|
||||||
return token_info['result']['access_token']
|
|
||||||
|
|
||||||
def process_nodes(process):
|
|
||||||
if (process == None):
|
|
||||||
return
|
|
||||||
item = process[0]
|
|
||||||
|
|
||||||
while (item != None):
|
|
||||||
prompt = item['prompt']
|
|
||||||
print(f'process node {item}')
|
|
||||||
result = send_message(assistant_id, access_token, prompt)
|
|
||||||
item['content'] = result
|
|
||||||
# if String result contains "继续处理"
|
|
||||||
if "正向处理" in result:
|
|
||||||
item = next((element for element in process if element['id'] == item["affirm_next"]), None)
|
|
||||||
else:
|
|
||||||
item = next((element for element in process if element['id'] == item["negative_next"]), None)
|
|
||||||
|
|
||||||
for item in process:
|
|
||||||
if item.get('content') is None:
|
|
||||||
item['content'] = ""
|
|
||||||
|
|
||||||
return process
|
|
||||||
|
|
||||||
|
|
||||||
# Here you need to replace the API Key and API Secret with your,I provide a test key and secret here
|
|
||||||
api_key = '25bda2c39c0f8ca0'
|
|
||||||
api_secret = 'e0008b9b9727cb8ceea5a132dbe62495'
|
|
||||||
token = get_access_token(api_key, api_secret)
|
|
||||||
|
|
||||||
assistant_id = "66a9feedfc354408c4de57dc"
|
|
||||||
access_token = token
|
|
||||||
|
|
||||||
excelData = []
|
|
||||||
|
|
||||||
# load customer aqusition process
|
|
||||||
for index in range(0, 4):
|
|
||||||
file_path = f'customer_aqusition_{index}.json'
|
|
||||||
customer_aqusition_process = json.loads(open(file_path, 'r',encoding='utf-8').read())
|
|
||||||
customer_aqusition_process = process_nodes(customer_aqusition_process)
|
|
||||||
excelData.append(customer_aqusition_process)
|
|
||||||
|
|
||||||
|
|
||||||
# 创建 ExcelHelper 实例并生成 Excel 文件
|
|
||||||
excel_helper = ExcelHelper(excelData)
|
|
||||||
excel_helper.create_excel('output.xlsx')
|
|
||||||
|
|
@ -0,0 +1,8 @@
|
||||||
|
from AgentProxy import AgentProxy
|
||||||
|
|
||||||
|
api_key = 'c6bbe7f48063a2c1'
|
||||||
|
api_secret = '5f8e7d3a97465cc099bf19bd1b70c266'
|
||||||
|
assistant_id = "66bb09a84673b57506fe7bbd"
|
||||||
|
agent = AgentProxy(assistant_id, api_key, api_secret)
|
||||||
|
|
||||||
|
print(agent.send_message("你好"))
|
||||||
Loading…
Reference in New Issue