xp3-tool 3.0__tar.gz → 4.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: xp3_tool
3
- Version: 3.0
3
+ Version: 4.0
4
4
  Summary: xp_t3_tool
5
5
  Home-page:
6
6
  Author: XuPeng
@@ -16,6 +16,7 @@ Requires-Python: >=3.10.0
16
16
  Description-Content-Type: text/markdown
17
17
  Requires-Dist: openai
18
18
  Requires-Dist: pandas
19
+ Requires-Dist: oss2
19
20
 
20
21
 
21
- 本模块提供了一个简单的 CallAi 类,用于与 OpenAI 兼容的 API 服务进行交互,支持设置提示词并发送对话请求。同时包含一个随机字符串生成工具函数,可用于生成指定长度的字母数字混合字符串。
22
+ 本模块提供了一个简单的 CallAi 类,用于与 OpenAI 兼容的 API 服务进行交互,支持设置提示词并发送对话请求。一个处理Excel文件与阿里云OSS的交互,并提供转换为pandas DataFrame的类。同时包含一个随机字符串生成工具函数,可用于生成指定长度的字母数字混合字符串。
xp3_tool-4.0/README.md ADDED
@@ -0,0 +1 @@
1
+ 本模块提供了一个简单的 CallAi 类,用于与 OpenAI 兼容的 API 服务进行交互,支持设置提示词并发送对话请求。一个处理Excel文件与阿里云OSS的交互,并提供转换为pandas DataFrame的类。同时包含一个随机字符串生成工具函数,可用于生成指定长度的字母数字混合字符串。
@@ -18,11 +18,11 @@ URL = ''
18
18
  EMAIL = 'xupeng23456@126.com'
19
19
  AUTHOR = 'XuPeng'
20
20
  REQUIRES_PYTHON = '>=3.10.0'
21
- VERSION = '3.0'
21
+ VERSION = '4.0'
22
22
 
23
23
  # What packages are required for this module to be executed?
24
24
  REQUIRED = [
25
- 'openai', 'pandas'
25
+ 'openai', 'pandas','oss2'
26
26
  ]
27
27
 
28
28
  # What packages are optional?
@@ -2,7 +2,8 @@ from .xp3_tool import uuid, CallAi
2
2
 
3
3
  __all__ = [
4
4
  "uuid",
5
- "CallAi"
5
+ "CallAi",
6
+ "ExcelOSSHandler"
6
7
  ]
7
8
 
8
- __version__ = "3.0"
9
+ __version__ = "4.0"
@@ -0,0 +1,125 @@
1
+ from openai import OpenAI
2
+ import string
3
+ import random
4
+
5
+ def uuid(length=16):
6
+ chars = string.ascii_letters + string.digits
7
+ return ''.join(random.choice(chars) for _ in range(length))
8
+
9
+ class CallAi:
10
+ def __init__(self,api_key,base_url,model=None):
11
+ self.api_key = api_key
12
+ self.base_url = base_url
13
+ self.client = OpenAI(
14
+ api_key = api_key,
15
+ base_url= base_url,
16
+ )
17
+ self.model = model if model else 'qwen-plus'
18
+ self._prompt = ""
19
+ self.inquiry = ""
20
+
21
+ @property
22
+ def prompt(self):
23
+ return self._prompt
24
+
25
+ @prompt.setter
26
+ def prompt(self,content):
27
+ self._prompt = content
28
+
29
+ def chat(self,text,top_p = 0.9, temperature = 0.7):
30
+ completion = self.client.chat.completions.create(
31
+ model= self.model,
32
+ messages=[
33
+ {'role': 'system', 'content': f'{self._prompt}'},
34
+ {'role': 'user', 'content': text}],
35
+ temperature = temperature,
36
+ top_p = top_p
37
+ )
38
+ reply = completion.choices[0].message.content
39
+ return reply
40
+
41
+ class ExcelOSSHandler:
42
+ """处理Excel文件与阿里云OSS的交互,并提供转换为pandas DataFrame的功能"""
43
+
44
+ def __init__(self, access_key_id: str, access_key_secret: str, endpoint: str, bucket_name: str):
45
+ """
46
+ 初始化OSS连接
47
+
48
+ :param access_key_id: 阿里云访问密钥ID
49
+ :param access_key_secret: 阿里云访问密钥Secret
50
+ :param endpoint: OSS服务的访问域名
51
+ :param bucket_name: OSS存储桶名称
52
+ """
53
+ # 初始化OSS认证
54
+ self.auth = oss2.Auth(access_key_id, access_key_secret)
55
+ # 获取存储桶对象
56
+ self.bucket = oss2.Bucket(self.auth, endpoint, bucket_name)
57
+
58
+ def upload_excel_to_oss(self, local_file_path: str, oss_file_path: str) -> bool:
59
+ """
60
+ 将本地Excel文件上传到OSS
61
+
62
+ :param local_file_path: 本地Excel文件路径
63
+ :param oss_file_path: OSS中保存的文件路径
64
+ :return: 上传成功返回True,否则返回False
65
+ """
66
+ try:
67
+ # 检查本地文件是否存在
68
+ if not os.path.exists(local_file_path):
69
+ print(f"错误: 本地文件 {local_file_path} 不存在")
70
+ return False
71
+
72
+ # 检查文件是否为Excel文件
73
+ if not (local_file_path.endswith('.xlsx') or local_file_path.endswith('.xls')):
74
+ print(f"错误: {local_file_path} 不是Excel文件")
75
+ return False
76
+
77
+ # 上传文件
78
+ self.bucket.put_object_from_file(oss_file_path, local_file_path)
79
+ print(f"文件 {local_file_path} 已成功上传至OSS: {oss_file_path}")
80
+ return True
81
+
82
+ except ClientError as e:
83
+ print(f"OSS上传错误: {str(e)}")
84
+ return False
85
+ except Exception as e:
86
+ print(f"上传文件时发生错误: {str(e)}")
87
+ return False
88
+
89
+ def get_excel_from_oss(self, oss_file_path: str) -> Optional[pd.DataFrame]:
90
+ """
91
+ 从OSS获取Excel文件并转换为pandas DataFrame
92
+
93
+ :param oss_file_path: OSS中的Excel文件路径
94
+ :return: 转换后的DataFrame,如果出错则返回None
95
+ """
96
+ try:
97
+ # 检查文件是否存在于OSS
98
+ if not self.bucket.object_exists(oss_file_path):
99
+ print(f"错误: OSS文件 {oss_file_path} 不存在")
100
+ return None
101
+
102
+ # 检查文件是否为Excel文件
103
+ if not (oss_file_path.endswith('.xlsx') or oss_file_path.endswith('.xls')):
104
+ print(f"错误: {oss_file_path} 不是Excel文件")
105
+ return None
106
+
107
+ # 从OSS下载文件到内存
108
+ response = self.bucket.get_object(oss_file_path)
109
+ excel_content = response.read()
110
+
111
+ # 将内容转换为DataFrame
112
+ # 使用BytesIO创建内存文件对象
113
+ with io.BytesIO(excel_content) as excel_file:
114
+ # 读取Excel文件,这里假设只有一个工作表
115
+ df = pd.read_excel(excel_file)
116
+
117
+ print(f"已成功从OSS获取文件 {oss_file_path} 并转换为DataFrame")
118
+ return df
119
+
120
+ except ClientError as e:
121
+ print(f"OSS下载错误: {str(e)}")
122
+ return None
123
+ except Exception as e:
124
+ print(f"获取并转换文件时发生错误: {str(e)}")
125
+ return None
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: xp3_tool
3
- Version: 3.0
3
+ Version: 4.0
4
4
  Summary: xp_t3_tool
5
5
  Home-page:
6
6
  Author: XuPeng
@@ -16,6 +16,7 @@ Requires-Python: >=3.10.0
16
16
  Description-Content-Type: text/markdown
17
17
  Requires-Dist: openai
18
18
  Requires-Dist: pandas
19
+ Requires-Dist: oss2
19
20
 
20
21
 
21
- 本模块提供了一个简单的 CallAi 类,用于与 OpenAI 兼容的 API 服务进行交互,支持设置提示词并发送对话请求。同时包含一个随机字符串生成工具函数,可用于生成指定长度的字母数字混合字符串。
22
+ 本模块提供了一个简单的 CallAi 类,用于与 OpenAI 兼容的 API 服务进行交互,支持设置提示词并发送对话请求。一个处理Excel文件与阿里云OSS的交互,并提供转换为pandas DataFrame的类。同时包含一个随机字符串生成工具函数,可用于生成指定长度的字母数字混合字符串。
@@ -1,2 +1,3 @@
1
1
  openai
2
2
  pandas
3
+ oss2
xp3_tool-3.0/README.md DELETED
@@ -1 +0,0 @@
1
- 本模块提供了一个简单的 CallAi 类,用于与 OpenAI 兼容的 API 服务进行交互,支持设置提示词并发送对话请求。同时包含一个随机字符串生成工具函数,可用于生成指定长度的字母数字混合字符串。
@@ -1,39 +0,0 @@
1
- from openai import OpenAI
2
- import string
3
- import random
4
-
5
- def uuid(length=16):
6
- chars = string.ascii_letters + string.digits
7
- return ''.join(random.choice(chars) for _ in range(length))
8
-
9
- class CallAi:
10
- def __init__(self,api_key,base_url,model=None):
11
- self.api_key = api_key
12
- self.base_url = base_url
13
- self.client = OpenAI(
14
- api_key = api_key,
15
- base_url= base_url,
16
- )
17
- self.model = model if model else 'qwen-plus'
18
- self._prompt = ""
19
- self.inquiry = ""
20
-
21
- @property
22
- def prompt(self):
23
- return self._prompt
24
-
25
- @prompt.setter
26
- def prompt(self,content):
27
- self._prompt = content
28
-
29
- def chat(self,text,top_p = 0.9, temperature = 0.7):
30
- completion = self.client.chat.completions.create(
31
- model= self.model,
32
- messages=[
33
- {'role': 'system', 'content': f'{self._prompt}'},
34
- {'role': 'user', 'content': text}],
35
- temperature = temperature,
36
- top_p = top_p
37
- )
38
- reply = completion.choices[0].message.content
39
- return reply
File without changes