-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.py
280 lines (235 loc) · 10.6 KB
/
functions.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import os
from openai import OpenAI
from datetime import datetime, time, timedelta
import pytz
import requests
from bs4 import BeautifulSoup
from google.cloud import storage
import io
import uuid
import functions_config as cf
import json
import wikipedia
google_api_key = os.getenv("GOOGLE_API_KEY")
google_cse_id = os.getenv("GOOGLE_CSE_ID")
openai_api_key = os.getenv('OPENAI_API_KEY')
gpt_client = OpenAI(api_key=openai_api_key)
public_url = []
public_url_original = []
user_id = []
bucket_name = []
file_age = []
def clock():
jst = pytz.timezone('Asia/Tokyo')
nowDate = datetime.now(jst)
nowDateStr = nowDate.strftime('%Y/%m/%d %H:%M:%S %Z')
return "SYSTEM:現在時刻は" + nowDateStr + "です。"
def get_googlesearch(words, num=3, start_index=1, search_lang='lang_ja'):
base_url = "https://www.googleapis.com/customsearch/v1"
params = {
"key": google_api_key,
"cx": google_cse_id,
"q": words,
"num": num,
"start": start_index,
"lr": search_lang
}
response = requests.get(base_url, params=params)
response.raise_for_status()
search_results = response.json()
# 検索結果を文字列に整形
formatted_results = ""
for item in search_results.get("items", []):
title = item.get("title")
link = item.get("link")
snippet = item.get("snippet")
formatted_results += f"タイトル: {title}\nリンク: {link}\n概要: {snippet}\n\n"
return f"SYSTEM:Webページを検索しました。{words}と関係のありそうなURLを読み込んでください。\n" + formatted_results
def search_wikipedia(prompt):
try:
wikipedia.set_lang("ja")
search_result = wikipedia.page(prompt)
summary = search_result.summary
page_url = search_result.url
# 結果を1000文字に切り詰める
if len(summary) > 1000:
summary = summary[:1000] + "..."
return f"SYSTEM: 以下は{page_url}の読み込み結果です。情報を提示するときは情報とともに参照元URLアドレスも案内してください。\n{summary}"
except wikipedia.exceptions.DisambiguationError as e:
return f"SYSTEM: 曖昧さ解消が必要です。オプション: {e.options}"
except wikipedia.exceptions.PageError:
return "SYSTEM: ページが見つかりませんでした。"
def scraping(link):
contents = ""
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36",
}
try:
response = requests.get(link, headers=headers, timeout=5)
response.raise_for_status()
response.encoding = response.apparent_encoding # または特定のエンコーディングを指定
html = response.text
except requests.RequestException as e:
return f"SYSTEM: リンクの読み込み中にエラーが発生しました: {e}"
soup = BeautifulSoup(html, features="html.parser")
# Remove all 'a' tags
for a in soup.findAll('a'):
a.decompose()
content = soup.select_one("article, .post, .content")
if content is None or content.text.strip() == "":
content = soup.select_one("body")
if content is not None:
contents = ' '.join(content.text.split()).replace("。 ", "。\n").replace("! ", "!\n").replace("? ", "?\n").strip()
# 結果を1000文字に切り詰める
if len(contents) > 1000:
contents = contents[:1000] + "..."
return f"SYSTEM:以下は{link}の読み込み結果です。情報を提示するときは情報とともに参照元URLアドレスも案内してください。\n" + contents
def set_bucket_lifecycle(bucket_name, age):
storage_client = storage.Client()
bucket = storage_client.get_bucket(bucket_name)
rule = {
'action': {'type': 'Delete'},
'condition': {'age': age} # The number of days after object creation
}
bucket.lifecycle_rules = [rule]
bucket.patch()
return
def bucket_exists(bucket_name):
"""Check if a bucket exists."""
storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
return bucket.exists()
def download_image(image_url):
""" PNG画像をダウンロードする """
response = requests.get(image_url)
return io.BytesIO(response.content)
def upload_blob(bucket_name, source_stream, destination_blob_name, content_type='image/png'):
"""Uploads a file to the bucket from a byte stream."""
try:
storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(destination_blob_name)
blob.upload_from_file(source_stream, content_type=content_type)
public_url = f"https://storage.googleapis.com/{bucket_name}/{destination_blob_name}"
return public_url
except Exception as e:
print(f"Failed to upload file: {e}")
raise
def generate_image(paint_prompt, prompt, user_id, bucket_name, file_age):
filename = str(uuid.uuid4())
blob_path = f'{user_id}/{filename}.png'
client = OpenAI()
prompt = paint_prompt + "\n" + prompt
try:
response = client.images.generate(
model="dall-e-3",
prompt=prompt,
size="1024x1024",
quality="standard",
n=1,
)
image_result = response.data[0].url
if bucket_exists(bucket_name):
set_bucket_lifecycle(bucket_name, file_age)
else:
print(f"Bucket {bucket_name} does not exist.")
return "SYSTEM:バケットが存在しません。"
# PNG画像をダウンロード
png_image = download_image(image_result)
# 元のPNG画像をアップロード
public_url_original = upload_blob(bucket_name, png_image, blob_path)
return f"SYSTEM:{prompt}のキーワードに基づきシーンを変更しました。", public_url_original
except Exception as e:
return f"SYSTEM: 画像生成にエラーが発生しました。{e}"
def set_username(prompt):
username = prompt
return f"SYSTEM: 名前を覚えたことを返信してください。", username
def run_conversation(GPT_MODEL, messages):
try:
response = gpt_client.chat.completions.create(
model=GPT_MODEL,
messages=messages,
)
return response # レスポンス全体を返す
except Exception as e:
print(f"An error occurred: {e}")
return None # エラー時には None を返す
def run_conversation_f(GPT_MODEL, messages):
try:
response = gpt_client.chat.completions.create(
model=GPT_MODEL,
messages=messages,
functions=cf.functions,
function_call="auto",
)
return response # レスポンス全体を返す
except Exception as e:
print(f"An error occurred: {e}")
return None # エラー時には None を返す
def chatgpt_functions(GPT_MODEL, messages_for_api, USER_ID, BUCKET_NAME=None, FILE_AGE=None, PAINT_PROMPT="", max_attempts=5):
public_url_original = None
user_id = USER_ID
bucket_name = BUCKET_NAME
file_age = FILE_AGE
paint_prompt = PAINT_PROMPT
username = ""
attempt = 0
i_messages_for_api = messages_for_api.copy()
set_username_called = False
clock_called = False
generate_image_called = False
search_wikipedia_called = False
scraping_called = False
get_googlesearch_called = False
while attempt < max_attempts:
response = run_conversation_f(GPT_MODEL, i_messages_for_api)
if response:
function_call = response.choices[0].message.function_call
if function_call:
if function_call.name == "set_UserName" and not set_username_called:
set_username_called = True
arguments = json.loads(function_call.arguments)
bot_reply, username = set_username(arguments["username"])
i_messages_for_api.append({"role": "assistant", "content": bot_reply})
attempt += 1
elif function_call.name == "clock" and not clock_called:
clock_called = True
bot_reply = clock()
i_messages_for_api.append({"role": "assistant", "content": bot_reply})
attempt += 1
elif function_call.name == "generate_image" and not generate_image_called:
generate_image_called = True
arguments = json.loads(function_call.arguments)
bot_reply, public_url_original = generate_image(paint_prompt, arguments["prompt"], user_id, bucket_name, file_age)
i_messages_for_api.append({"role": "assistant", "content": bot_reply})
attempt += 1
elif function_call.name == "search_wikipedia" and not search_wikipedia_called:
search_wikipedia_called = True
arguments = json.loads(function_call.arguments)
bot_reply = search_wikipedia(arguments["prompt"])
i_messages_for_api.append({"role": "assistant", "content": bot_reply})
attempt += 1
elif function_call.name == "scraping" and not scraping_called:
scraping_called = True
arguments = json.loads(function_call.arguments)
bot_reply = scraping(arguments["link"])
i_messages_for_api.append({"role": "assistant", "content": bot_reply})
attempt += 1
elif function_call.name == "get_googlesearch" and not get_googlesearch_called:
get_googlesearch_called = True
arguments = json.loads(function_call.arguments)
bot_reply = get_googlesearch(arguments["words"])
i_messages_for_api.append({"role": "assistant", "content": bot_reply})
attempt += 1
else:
response = run_conversation(GPT_MODEL, i_messages_for_api)
if response:
bot_reply = response.choices[0].message.content
else:
bot_reply = "An error occurred while processing the question"
return bot_reply, public_url_original, username
else:
return response.choices[0].message.content, public_url_original, username
else:
return "An error occurred while processing the question", public_url_original, username
return bot_reply, public_url_original, username