赞
踩
大模型,包括部署微调prompt/Agent应用开发、知识库增强、数据库增强、知识图谱增强、自然语言处理、多模态等大模型应用开发内容
从0起步,扬帆起航。
import requests import json from concurrent.futures import ThreadPoolExecutor, as_completed import pandas as pd # API的URL url = 'http://localhost:7861/chat/knowledge_base_chat' # 定义发送请求的函数 def send_request(question, index): data = { "query": question, "knowledge_base_name": "ytzw_240327", "top_k": 3, "score_threshold": 1, "stream": False, "model_name": "ytzw_llm-Chat", "temperature": 0.7, "max_tokens": 0, "prompt_name": "default" } json_data = json.dumps(data) response = requests.post(url, data=json_data, headers={'Content-Type': 'application/json'}) response_text = response.text # 找到'data: '的位置 prefix_pos = response_text.find('data: ') if prefix_pos != -1: # 如果找到了'data: ',则从这个位置加上它的长度开始解析 json_str = response_text[prefix_pos + len('data: '):] else: # 如果没有找到'data: ',则假设整个响应就是JSON json_str = response_text # 移除可能存在的尾部空白字符和控制字符 json_str = json_str.strip() # 解析JSON字符串 try: response_json = json.loads(json_str) print(response_json) answer = response_json.get('answer', '') docs = response_json.get('docs', []) # 返回答复、文档和问题索引 return answer, docs, index except json.JSONDecodeError as e: print(f"解析JSON时出错: {e}") # 发生错误时也返回索引,以便能够将错误信息放在正确的位置 return '', [], index data = pd.read_excel(r'C:\Users\12258\Desktop\test_data.xlsx') # 生成问题列表 questions = data['question'][:1] # 初始化列表收集答复和文档 answers = [''] * len(data) # 使用与df相同长度的列表,初始值为空字符串 docs_list = [[] for _ in range(len(data))] # 使用与df相同长度的列表,初始值为空列表 # 使用ThreadPoolExecutor来并发发送请求 with ThreadPoolExecutor(max_workers=1) as executor: # 在提交任务时传递问题的索引 future_to_index = {executor.submit(send_request, question, i): i for i, question in enumerate(questions)} print(future_to_index) for future in as_completed(future_to_index): result = future.result() answer, docs, index = result # 使用返回的索引更新答复和文档 answers[index] = answer docs_list[index] = docs print(f'序号: {index}, 答案: {answer}, 来源: {docs}') # 每处理10个问题,保存一次df if (index + 1) % 10 == 0 or (index + 1) == len(questions): pd.DataFrame(answers).to_csv("test_data_updated.csv", index=False)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。