hjxdl 0.3.15__py3-none-any.whl → 0.3.17__py3-none-any.whl
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.
- hdl/_version.py +2 -2
- hdl/utils/desc/tools.py +21 -0
- hdl/utils/llm/llm_wrapper.py +154 -0
- {hjxdl-0.3.15.dist-info → hjxdl-0.3.17.dist-info}/METADATA +2 -1
- {hjxdl-0.3.15.dist-info → hjxdl-0.3.17.dist-info}/RECORD +8 -6
- {hjxdl-0.3.15.dist-info → hjxdl-0.3.17.dist-info}/LICENSE +0 -0
- {hjxdl-0.3.15.dist-info → hjxdl-0.3.17.dist-info}/WHEEL +0 -0
- {hjxdl-0.3.15.dist-info → hjxdl-0.3.17.dist-info}/top_level.txt +0 -0
hdl/_version.py
CHANGED
hdl/utils/desc/tools.py
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
TOOL_DICT = {
|
2
|
+
"get_weather": {
|
3
|
+
"type": "function",
|
4
|
+
"function": {
|
5
|
+
"name": "get_weather",
|
6
|
+
"description": "Get current temperature for a given location.",
|
7
|
+
"parameters": {
|
8
|
+
"type": "object",
|
9
|
+
"properties": {
|
10
|
+
"location": {
|
11
|
+
"type": "string",
|
12
|
+
"description": "City name e.g. Bogotá"
|
13
|
+
}
|
14
|
+
},
|
15
|
+
"required": ["location"],
|
16
|
+
"additionalProperties": False
|
17
|
+
},
|
18
|
+
"strict": True
|
19
|
+
}
|
20
|
+
},
|
21
|
+
}
|
@@ -0,0 +1,154 @@
|
|
1
|
+
import yaml
|
2
|
+
import typing as t
|
3
|
+
|
4
|
+
from openai import OpenAI
|
5
|
+
import instructor
|
6
|
+
|
7
|
+
|
8
|
+
class OpenAIWrapper(object):
|
9
|
+
def __init__(
|
10
|
+
self,
|
11
|
+
client_conf: dict = None,
|
12
|
+
client_conf_dir: str = None,
|
13
|
+
load_conf: bool = True,
|
14
|
+
*args,
|
15
|
+
**kwargs
|
16
|
+
):
|
17
|
+
self.client_conf = {}
|
18
|
+
if client_conf is None:
|
19
|
+
assert client_conf_dir is not None
|
20
|
+
self.client_conf_path = client_conf_dir
|
21
|
+
if load_conf:
|
22
|
+
self.load_clients()
|
23
|
+
else:
|
24
|
+
self.client_conf = client_conf
|
25
|
+
|
26
|
+
# self.clients = {}
|
27
|
+
for _, conf in self.client_conf.items():
|
28
|
+
conf["client"] = OpenAI(
|
29
|
+
base_url=conf["host"],
|
30
|
+
api_key=conf.get("api_key", "dummy_key"),
|
31
|
+
*args,
|
32
|
+
**kwargs
|
33
|
+
)
|
34
|
+
|
35
|
+
def add_client(
|
36
|
+
self,
|
37
|
+
client_id: str,
|
38
|
+
host: str,
|
39
|
+
port: int = None,
|
40
|
+
model: str = "default_model",
|
41
|
+
api_key: str = "dummy_key",
|
42
|
+
**kwargs
|
43
|
+
):
|
44
|
+
self.client_conf[client_id] = {}
|
45
|
+
if not host.startswith('http') and port:
|
46
|
+
host = f"http://{host}:{port}/v1"
|
47
|
+
self.client_conf[client_id]['host'] = host
|
48
|
+
self.client_conf[client_id]['model'] = model
|
49
|
+
self.client_conf[client_id]['client'] = OpenAI(
|
50
|
+
base_url=host,
|
51
|
+
api_key=api_key,
|
52
|
+
**kwargs
|
53
|
+
)
|
54
|
+
|
55
|
+
def load_clients(self):
|
56
|
+
with open(self.client_conf_path, 'r') as file:
|
57
|
+
data = yaml.safe_load(file)
|
58
|
+
|
59
|
+
# 更新 host 字段
|
60
|
+
for _, value in data.items():
|
61
|
+
host = value.get('host', '')
|
62
|
+
port = value.get('port', '')
|
63
|
+
if not host.startswith('http') and port: # 确保有 port 才处理
|
64
|
+
value['host'] = f"http://{host}:{port}/v1"
|
65
|
+
self.client_conf = data
|
66
|
+
|
67
|
+
def get_resp(
|
68
|
+
self,
|
69
|
+
prompt,
|
70
|
+
client_id: str = None,
|
71
|
+
history: list = None,
|
72
|
+
sys_info: str = None,
|
73
|
+
assis_info: str = None,
|
74
|
+
images: list = None,
|
75
|
+
image_keys: tuple = ("image_url", "url"),
|
76
|
+
model: str=None,
|
77
|
+
tools: list = None,
|
78
|
+
stream: bool = True,
|
79
|
+
response_model = None,
|
80
|
+
**kwargs: t.Any,
|
81
|
+
):
|
82
|
+
if not model:
|
83
|
+
model = self.client_conf[client_id]['model']
|
84
|
+
|
85
|
+
client = self.client_conf[client_id]['client']
|
86
|
+
if response_model:
|
87
|
+
client = instructor.from_openai(client)
|
88
|
+
|
89
|
+
messages = []
|
90
|
+
|
91
|
+
if sys_info:
|
92
|
+
messages.append({
|
93
|
+
"role": "system",
|
94
|
+
"content": sys_info
|
95
|
+
})
|
96
|
+
|
97
|
+
if history:
|
98
|
+
messages.extend(history)
|
99
|
+
# history 需要符合以下格式,其中system不是必须
|
100
|
+
# history = [
|
101
|
+
# {"role": "system", "content": "You are a helpful assistant."},
|
102
|
+
# {"role": "user", "content": "message 1 content."},
|
103
|
+
# {"role": "assistant", "content": "message 2 content"},
|
104
|
+
# {"role": "user", "content": "message 3 content"},
|
105
|
+
# {"role": "assistant", "content": "message 4 content."},
|
106
|
+
# {"role": "user", "content": "message 5 content."}
|
107
|
+
# ]
|
108
|
+
|
109
|
+
if not model:
|
110
|
+
model = self.client_conf[client_id]["model"]
|
111
|
+
# Adjust the image_keys to be a tuple of length 3 based on its current length
|
112
|
+
if isinstance(image_keys, str):
|
113
|
+
image_keys = (image_keys,) * 3
|
114
|
+
elif len(image_keys) == 2:
|
115
|
+
image_keys = (image_keys[0],) + tuple(image_keys)
|
116
|
+
elif len(image_keys) == 1:
|
117
|
+
image_keys = (image_keys[0],) * 3
|
118
|
+
|
119
|
+
content = []
|
120
|
+
if images:
|
121
|
+
if isinstance(images, str):
|
122
|
+
images = [images]
|
123
|
+
for img in images:
|
124
|
+
content.append({
|
125
|
+
"type": image_keys[0],
|
126
|
+
image_keys[1]: {
|
127
|
+
image_keys[2]: img
|
128
|
+
}
|
129
|
+
})
|
130
|
+
else:
|
131
|
+
# If no images are provided, content is simply the prompt text
|
132
|
+
content = prompt
|
133
|
+
|
134
|
+
# Add the user's input as a message
|
135
|
+
messages.append({
|
136
|
+
"role": "user",
|
137
|
+
"content": content
|
138
|
+
})
|
139
|
+
|
140
|
+
if assis_info:
|
141
|
+
messages.append({
|
142
|
+
"role": "assistant",
|
143
|
+
"content": assis_info
|
144
|
+
})
|
145
|
+
|
146
|
+
resp = client.chat.completions.create(
|
147
|
+
model=model,
|
148
|
+
messages=messages,
|
149
|
+
tools=tools,
|
150
|
+
stream=stream
|
151
|
+
)
|
152
|
+
return resp
|
153
|
+
|
154
|
+
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.2
|
2
2
|
Name: hjxdl
|
3
|
-
Version: 0.3.
|
3
|
+
Version: 0.3.17
|
4
4
|
Summary: A collection of functions for Jupyter notebooks
|
5
5
|
Home-page: https://github.com/huluxiaohuowa/hdl
|
6
6
|
Author: Jianxing Hu
|
@@ -24,6 +24,7 @@ Requires-Dist: Pillow
|
|
24
24
|
Requires-Dist: open_clip_torch
|
25
25
|
Requires-Dist: natsort
|
26
26
|
Requires-Dist: matplotlib
|
27
|
+
Requires-Dist: instructor
|
27
28
|
Dynamic: author
|
28
29
|
Dynamic: author-email
|
29
30
|
Dynamic: classifier
|
@@ -1,5 +1,5 @@
|
|
1
1
|
hdl/__init__.py,sha256=GffnD0jLJdhkd-vo989v40N90sQbofkayRBwxc6TVhQ,72
|
2
|
-
hdl/_version.py,sha256=
|
2
|
+
hdl/_version.py,sha256=EanR9QKHDmsyYNdsvPdG4re1cWDyd1A_td5gwFF-ouQ,413
|
3
3
|
hdl/args/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
4
4
|
hdl/args/loss_args.py,sha256=s7YzSdd7IjD24rZvvOrxLLFqMZQb9YylxKeyelSdrTk,70
|
5
5
|
hdl/controllers/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
@@ -124,6 +124,7 @@ hdl/utils/database_tools/web.py,sha256=awJ8lafL-2KRjf3V1uuij8JIvX9U5fI8fLZKOkOvq
|
|
124
124
|
hdl/utils/desc/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
125
125
|
hdl/utils/desc/func_desc.py,sha256=sHmVZZmV7Zgii--gnHqMs6fTb7HVkqTOf8Pl_0F6qlI,3808
|
126
126
|
hdl/utils/desc/template.py,sha256=Kf_tbL-XkDCKNQ3UncbCuYEeUgXEa7kRVCf9TD2b8og,2526
|
127
|
+
hdl/utils/desc/tools.py,sha256=KNxmmTFFL3BZPh-2NGvNPz2VygHPJ3BWFa4VY2jy58g,619
|
127
128
|
hdl/utils/general/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
128
129
|
hdl/utils/general/glob.py,sha256=Zuf7WHU0UdUPOs9UrhxmrCiMC8GrHxQU6n3mTThv6yc,1120
|
129
130
|
hdl/utils/general/runners.py,sha256=x7QBolp3MrqNV6L4rB6Ueybr26bqkRFZTuXhY0SwyLk,3061
|
@@ -133,14 +134,15 @@ hdl/utils/llm/chatgr.py,sha256=5F5PJHe8vz3iCfi4TT54DCLRi1UeJshECdVtgvvvao0,3696
|
|
133
134
|
hdl/utils/llm/embs.py,sha256=Tf0FOYrOFZp7qQpEPiSCXzlgyHH0X9HVTUtsup74a9E,7174
|
134
135
|
hdl/utils/llm/extract.py,sha256=2sK_WJzmYIc8iuWaM9DA6Nw3_6q1O4lJ5pKpcZo-bBA,6512
|
135
136
|
hdl/utils/llm/llama_chat.py,sha256=watcHGOaz-bv3x-yDucYlGk5f8FiqfFhwWogrl334fk,4387
|
137
|
+
hdl/utils/llm/llm_wrapper.py,sha256=vtvNJvsnf04rHRegxvkGCVX8Yvq37rsmLev19uMzog4,4603
|
136
138
|
hdl/utils/llm/vis.py,sha256=SSP6tOwKLq0hWcpM3twI9TitqzBmKjlcGrnXEWYlCzM,26055
|
137
139
|
hdl/utils/llm/visrag.py,sha256=0i-VrxqgiV-J7R3VPshu9oc7-rKjFJOldYik3HDXj6M,10176
|
138
140
|
hdl/utils/schedulers/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
139
141
|
hdl/utils/schedulers/norm_lr.py,sha256=bDwCmdEK-WkgxQMFBiMuchv8Mm7C0-GZJ6usm-PQk14,4461
|
140
142
|
hdl/utils/weather/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
141
143
|
hdl/utils/weather/weather.py,sha256=k11o6wM15kF8b9NMlEfrg68ak-SfSYLN3nOOflFUv-I,4381
|
142
|
-
hjxdl-0.3.
|
143
|
-
hjxdl-0.3.
|
144
|
-
hjxdl-0.3.
|
145
|
-
hjxdl-0.3.
|
146
|
-
hjxdl-0.3.
|
144
|
+
hjxdl-0.3.17.dist-info/LICENSE,sha256=lkMiSbeZHBQLB9LJEkS9-L3Z-LBC4yGnKrzHSG8RkPM,2599
|
145
|
+
hjxdl-0.3.17.dist-info/METADATA,sha256=QGqsQnHm_3fG6ryexNqcamc4Jquyho5ZVezH_5oBwEA,1336
|
146
|
+
hjxdl-0.3.17.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
|
147
|
+
hjxdl-0.3.17.dist-info/top_level.txt,sha256=-kxwTM5JPhylp06z3zAVO3w6_h7wtBfBo2zgM6YZoTk,4
|
148
|
+
hjxdl-0.3.17.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|