197 lines
7.1 KiB
Python
Executable File
197 lines
7.1 KiB
Python
Executable File
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
"""
|
|
File: ModelTool.py
|
|
Date: 2025-05-15
|
|
Author: oyyz
|
|
Description: 模型工具类
|
|
"""
|
|
|
|
from openai import OpenAI
|
|
import httpx
|
|
import time
|
|
import logging # 导入 logging 模块
|
|
from langchain.embeddings.base import Embeddings
|
|
from typing import List, Any
|
|
import requests
|
|
import os
|
|
import logging
|
|
from rag2_0.tool.APIKeyManager import APIKeyManager
|
|
|
|
|
|
class SiliconFlowEmbeddings(Embeddings):
|
|
"""SiliconFlow嵌入模型封装"""
|
|
def __init__(self, api_key: str, model: str = "bge-m3"):
|
|
self.api_key = api_key
|
|
self.model = model
|
|
self.url = "http://10.1.16.39:9995/v1/embeddings"
|
|
self.headers = {
|
|
"Authorization": f"Bearer {self.api_key}",
|
|
"Content-Type": "application/json"
|
|
}
|
|
|
|
def _embed(self, input: List[str]) -> List[List[float]]:
|
|
payload = {
|
|
"model": self.model,
|
|
"input": input,
|
|
"encoding_format": "float"
|
|
}
|
|
response = requests.post(self.url, json=payload, headers=self.headers)
|
|
response.raise_for_status()
|
|
data = response.json()
|
|
return [item["embedding"] for item in data["data"]]
|
|
|
|
def embed_documents(self, texts: List[str]) -> List[List[float]]:
|
|
return self._embed(texts)
|
|
|
|
def embed_query(self, text: str) -> List[float]:
|
|
return self._embed([text])[0]
|
|
|
|
class SiliconFlowReRankerModel:
|
|
@staticmethod
|
|
def rerank(query: str, documents: List[str], top_k: int = 10) -> List[str]:
|
|
"""
|
|
使用硅流重排模型对文档进行重新排序
|
|
|
|
Args:
|
|
query: 用户查询文本
|
|
documents: 需要重新排序的文档列表
|
|
top_k: 返回排序后的前k个文档
|
|
|
|
Returns:
|
|
List[dict]: 重排序后的文档列表,每个元素包含document内容、相关性分数和原始索引
|
|
"""
|
|
url = "https://api.siliconflow.cn/v1/rerank"
|
|
payload = {
|
|
"model": "BAAI/bge-reranker-v2-m3",
|
|
"query": query,
|
|
"documents": documents,
|
|
"top_n": top_k,
|
|
"max_chunks_per_doc": 1024,
|
|
"overlap_tokens": 80,
|
|
"return_documents": True
|
|
}
|
|
api_key = APIKeyManager.get_api_key()
|
|
headers = {
|
|
"Authorization": f"Bearer {api_key}",
|
|
"Content-Type": "application/json"
|
|
}
|
|
try:
|
|
response = requests.post(url, json=payload, headers=headers)
|
|
response.raise_for_status()
|
|
results = response.json()
|
|
return [{"document": item["document"]["text"], "score": item["relevance_score"], "index": item["index"]} for item in results["results"]]
|
|
except requests.exceptions.RequestException as e:
|
|
logging.error(f"重排序请求失败: {str(e)}")
|
|
return []
|
|
|
|
class XinferenceReRankerModel:
|
|
"""重排模型封装"""
|
|
|
|
@staticmethod
|
|
def rerank(query: str, documents: List[str], top_k: int = 10) -> List[str]:
|
|
"""
|
|
使用重排序模型对文档进行重新排序
|
|
|
|
Args:
|
|
query: 用户查询文本
|
|
documents: 需要重新排序的文档列表
|
|
top_k: 返回排序后的前k个文档
|
|
|
|
Returns:
|
|
List[dict]: 重排序后的文档列表,每个元素包含document内容、相关性分数和原始索引
|
|
"""
|
|
url = "http://172.20.0.145:9995/v1/rerank"
|
|
|
|
|
|
params = {"documents": documents, "query": query, "top_n": top_k, "return_documents": True, "model": "bge-reranker-v2-m3"}
|
|
headers = {
|
|
"Authorization": "Bearer <token>", # 这里需要替换为实际的token
|
|
"Content-Type": "application/json"
|
|
}
|
|
|
|
try:
|
|
response = requests.post(url, json=params, headers=headers)
|
|
response.raise_for_status() # 检查响应状态
|
|
results = response.json()
|
|
|
|
# 返回重排序后的文档列表
|
|
return [{"document": item["document"]["text"], "score": item["relevance_score"], "index": item["index"]} for item in results["results"]]
|
|
|
|
except requests.exceptions.RequestException as e:
|
|
logging.error(f"XinferenceReRankerModel重排序请求失败: {str(e)}")
|
|
return []
|
|
|
|
|
|
|
|
class OpenAiLLM:
|
|
|
|
def __init__(self, **kwargs):
|
|
if kwargs.get("api_key") == None or kwargs.get("base_url") == None or kwargs.get("model") == None:
|
|
raise ValueError("api_key, base_url, model 不能为空")
|
|
|
|
self._api_key = kwargs.get("api_key")
|
|
self._url = kwargs.get("base_url")
|
|
self._model = kwargs.get("model")
|
|
|
|
kwargs.pop("api_key")
|
|
kwargs.pop("base_url")
|
|
kwargs.pop("model")
|
|
self._kwargs = kwargs
|
|
|
|
def invoke(self, user_prompt="你是谁?", need_retry=True):
|
|
# 初始化 OpenAI 客户端
|
|
|
|
max_retries = 3
|
|
retry_count = 0
|
|
if "timeout" not in self._kwargs:
|
|
timeout = httpx.Timeout(300.0)
|
|
self._kwargs["timeout"] = timeout
|
|
|
|
if need_retry:
|
|
while retry_count < max_retries:
|
|
try:
|
|
api_key = APIKeyManager.get_api_key()
|
|
# 使用with语句创建客户端,确保资源会被正确释放
|
|
with OpenAI(api_key=api_key, base_url=self._url) as client:
|
|
# 创建 Completion 请求. 超时120s
|
|
completion = client.chat.completions.create(
|
|
model=self._model,
|
|
messages=[{'role': 'user', 'content': user_prompt}],
|
|
**self._kwargs
|
|
)
|
|
return completion.choices[0].message
|
|
|
|
except Exception as e:
|
|
retry_count += 1
|
|
if retry_count == max_retries:
|
|
raise RuntimeError(f"调用LLM失败,已重试{max_retries}次: {str(e)}")
|
|
else:
|
|
time.sleep(5*retry_count) # 重试前等待5秒*重试次数
|
|
else:
|
|
# 创建 Completion 请求. 超时120s
|
|
api_key = APIKeyManager.get_api_key()
|
|
# 使用with语句创建客户端,确保资源会被正确释放
|
|
with OpenAI(api_key=api_key, base_url=self._url) as client:
|
|
completion = client.chat.completions.create(
|
|
model=self._model,
|
|
messages=[{'role': 'user', 'content': user_prompt}],
|
|
**self._kwargs
|
|
)
|
|
return completion.choices[0].message
|
|
|
|
if __name__ == "__main__":
|
|
# 测试重排模型
|
|
reranker = SiliconFlowReRankerModel()
|
|
|
|
# 测试用例1:简单问题
|
|
query = "如何通过【电力经济评价软件】的【打开】功能加载工程文件?"
|
|
documents = []
|
|
results = reranker.rerank(query, documents)
|
|
print(f"测试用例1 - 查询:{query}")
|
|
for idx, item in enumerate(results):
|
|
print(f"{idx+1}. 文档: {item['document']}, 分数: {item['score']}")
|
|
print("-" * 50)
|
|
|
|
|