-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathai_helper.py
209 lines (166 loc) · 6.19 KB
/
ai_helper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
def ai_helper_webxpath(user_input='',always=False):
'''
Written by 方涛 at 2024/09
网页解析函数,参数:
user_input:输入要解析的网站url,并直接说出需要提取的内容
'''
import requests
import json
user_input = str( user_input )
# 替换为你的实际 API Key
api_key = "***"
# API 端点
url = "https://ark.cn-.com/api/v3/bots/chat/completions"
# 请求头
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
model = "bot-**-6hcxm" #该模型使用doubao-pro-4k创建的智能体
def chat_with_ai(message):
"""与 AI 进行聊天"""
data = {
"model": model, # 或者其他你选择的模型
"messages": [
{"role": "user", "content": message}
]
}
response = requests.post(url, headers=headers, data=json.dumps(data))
if response.status_code == 200:
result = response.json()
ai_response = result["choices"][0]["message"]["content"]
return ai_response
else:
return f"请求失败,状态码:{response.status_code}"
ai_output = chat_with_ai(user_input)
print(f"AI: {ai_output}")
def ai_helper_websearcher(user_input = '当前你不需要做任何事情,只需要输出一段文字"可以开始冲浪了"',always = False):
'''
搜索网页内容;
参数:
user_input = url,网站地址;
always = [False,True],缺省False,一次搜索。如果为True则使用对话形式,可多次输出。
'''
import requests
import json
user_input = str( user_input )
# 替换为你的实际 API Key
api_key = ***"
# API 端点
url = "https://ark.cn-beijing.volces.com/api/v3/bots/chat/completions"
# 请求头
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
model = "bot--t5sxz" #该模型使用doubao-pro-4k创建的智能体
def chat_with_ai(message):
"""与 AI 进行聊天"""
data = {
"model": model, # 或者其他你选择的模型
"messages": [
{"role": "user", "content": message}
]
}
response = requests.post(url, headers=headers, data=json.dumps(data))
if response.status_code == 200:
result = response.json()
ai_response = result["choices"][0]["message"]["content"]
return ai_response
else:
return f"请求失败,状态码:{response.status_code}"
ai_output = chat_with_ai(user_input)
print(f"AI: {ai_output}")
def ai_helper_requests(user_input = '方涛', always = False):
'''
大模型问答内容;
参数:
user_input :用户输入内容;大模型根据自建内容回复;
always = [False,True],缺省False,一次性回答。如果为True则使用对话形式,可多次输出。建议只使用False,如果需要多次输出,使用ai_helper_stream。
'''
import requests
import json
user_input = str( user_input )
always = bool(always)
# 替换为你的实际 API Key
api_key = "***"
# API 端点
url = "https://ark.cn-beijing.volces.com/api/v3/chat/completions"
# 请求头
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
model = "ep-hfmjg" # 该模型使用doubao-pro-4k
def chat_with_ai(message):
"""与 AI 进行聊天"""
data = {
"model": model, # 或者其他你选择的模型
"messages": [
{"role": "user", "content": message}
]
}
response = requests.post(url, headers=headers, data=json.dumps(data))
if response.status_code == 200:
result = response.json()
ai_response = result["choices"][0]["message"]["content"]
return ai_response
else:
return f"请求失败,状态码:{response.status_code}"
ai_output = chat_with_ai(user_input)
print(f"AI: {ai_output}")
# 开始聊天循环
while always:
user_input = input("你: ")
if user_input.lower() in ["exit", "quit", "bye"]:
break
ai_output = chat_with_ai(user_input)
print(f"AI: {ai_output}")
def ai_helper_stream(user_input = '方涛', always = False):
'''
大模型流式问答
参数:
user_input :用户输入内容;大模型根据自建内容回复;
always = [False,True],缺省False,一次性回答。如果为True则使用对话形式,可多次输出并记忆上下文。
'''
from volcenginesdkarkruntime import Ark
user_input = str( user_input )
always = bool(always)
client = Ark(
base_url="https://ark.cn-beijing.volces.com/api/v3",
api_key = "***",
)
messages = [
{"role": "system", "content": "你是豆包,是由字节跳动开发的 AI 人工智能助手"},
{"role": "user", "content": user_input}
]
# Streaming:
print("-----------------------------------")
stream = client.chat.completions.create(
model="ep-3-wzrg6",
messages = messages,
stream=True
)
for chunk in stream:
if not chunk.choices:
continue
print(chunk.choices[0].delta.content, end="")
# 开始聊天循环
while always:
print('\n')
print("------")
user_input = input("*你:")
messages.append({'role':'user','content':user_input})
if user_input.lower() in ["exit", "quit", "bye"]:
break
stream = client.chat.completions.create(
model="ep--wzrg6",
messages = messages,
stream=True
)
print('*AI: ',end='')
for chunk in stream:
if not chunk.choices:
continue
print(chunk.choices[0].delta.content, end="")
print("\n-----------------------------------")