Skip to content

Commit

Permalink
✨支持画图
Browse files Browse the repository at this point in the history
  • Loading branch information
AkashiCoin committed Nov 17, 2023
1 parent be65820 commit 0cc522e
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 11 deletions.
39 changes: 37 additions & 2 deletions nonebot_plugin_chatgpt_plus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,23 @@ async def ai_chat(bot: Bot, event: MessageEvent, state: T_State) -> None:
if not chat_bot.presets.get(played_name):
played_name = ""
cvst = session[event]
model = None
cmd = event.get_message().extract_plain_text().strip()
if cmd.startswith("gpt4m"):
model = "gpt-4-magic-create"
elif cmd.startswith("gpt4c"):
model = "gpt-4-code-interpreter"
elif cmd.startswith("gpt4d"):
model = "gpt-4-dalle"
elif cmd.startswith("gpt4g"):
model = "gpt-4-gizmo"
elif cmd.startswith("gpt4b"):
model = "gpt-4-browsing"
elif cmd.startswith("gpt4"):
model = "gpt-4"
elif cmd.startswith("gpt3"):
model = "text-davinci-002-render-sha"
else:
model = config.chatgpt_model
if cvst:
if not cvst["conversation_id"][-1]:
has_title = False
Expand Down Expand Up @@ -161,7 +177,14 @@ async def ai_chat(bot: Bot, event: MessageEvent, state: T_State) -> None:
finally:
lockers[event.user_id] = False
if msg_id:
await bot.delete_msg(message_id=msg_id)
try:
await bot.delete_msg(message_id=msg_id)
except Exception as e:
pass
images: list = []
if isinstance(msg, dict):
images = msg["images"]
msg = msg["message"]
if config.chatgpt_image:
if msg.count("```") % 2 != 0:
msg += "\n```"
Expand All @@ -172,6 +195,18 @@ async def ai_chat(bot: Bot, event: MessageEvent, state: T_State) -> None:
if not has_title:
await chat_bot(**session[event]).edit_title(session.id(event=event))
session.save_sessions()
if images:
await send_images(matcher, images)


async def send_images(matcher, images: list[str]):
for image in images:
image_id = image.split("//")[1]
image_url = await chat_bot.get_image_url_with_id(image_id=image_id)
if image_url:
await matcher.send(MessageSegment.image(image_url))
else:
await matcher.send("图片获取失败!")


refresh = on_command("刷新对话", aliases={"刷新会话"}, block=True, rule=to_me(), priority=1)
Expand Down
61 changes: 52 additions & 9 deletions nonebot_plugin_chatgpt_plus/chatgpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def headers(self) -> Dict[str, str]:
def get_played_info(self, name: str) -> Dict[str, Any]:
return {
"id": self.id,
"author": {"role": "system", "name": name, "metadata": {}},
"author": {"role": "system", "name": name},
"content": {
"content_type": "text",
"parts": [
Expand All @@ -114,6 +114,9 @@ def get_played_info(self, name: str) -> Dict[str, Any]:
else "",
],
},
"metadata": {
"timestamp_": "absolute"
},
"weight": 100,
}

Expand All @@ -123,6 +126,8 @@ def get_payload(self, prompt: str, is_continue: bool = False) -> Dict[str, Any]:
"conversation_id": self.conversation_id,
"parent_message_id": self.parent_id,
"model": self.model,
"history_and_training_disabled": False,
"arkose_token": None,
"timezone_offset_min": -480,
}
if not is_continue:
Expand All @@ -132,6 +137,9 @@ def get_payload(self, prompt: str, is_continue: bool = False) -> Dict[str, Any]:
"author": {"role": "user"},
"role": "user",
"content": {"content_type": "text", "parts": [prompt]},
"metadata": {
"timestamp_": "absolute"
},
}
]
if self.played_name:
Expand Down Expand Up @@ -218,12 +226,18 @@ async def get_chat_response(self, prompt: str, is_continue: bool = False) -> str
return await self.get_chat_response("", True)
else:
not_complete = "\nis_complete: False"
elif is_continue:
else:
if response["message"].get("end_turn"):
response = await self.get_conversasion_message_response(
self.conversation_id, self.parent_id
)
msg = "".join(response["message"]["content"]["parts"])
if isinstance(response, str):
return response
msg = "".join([text for text in response["message"]["content"]["parts"] if isinstance(text, str)])
images = [image["asset_pointer"] for image in response["message"]["content"]["parts"] if not isinstance(image, str)]
logger.info(response)
logger.info(msg)
logger.info(images)
if self.metadata:
msg += "\n---"
msg += (
Expand All @@ -232,7 +246,13 @@ async def get_chat_response(self, prompt: str, is_continue: bool = False) -> str
msg += not_complete
if is_continue:
msg += "\nauto_continue: True"
return msg
if images:
return {
"message": msg,
"images": images
}
else:
return msg

async def edit_title(self, title: str) -> bool:
async with httpx.AsyncClient(
Expand Down Expand Up @@ -296,6 +316,26 @@ async def get_conversasion(self, conversation_id: str):
)
return response.json()

async def get_image_url_with_id(self, image_id: str):
async with httpx.AsyncClient(
headers=self.headers,
proxies=self.proxies,
timeout=self.timeout,
) as client:
response = await client.get(
urljoin(self.api_url, f"backend-api/files/{image_id}/download")
)
try:
if response.status_code == 200:
resp_json = response.json()
return resp_json["download_url"]
else:
return False
except Exception as e:
logger.opt(colors=True, exception=e).error(
f"获取图片失败: <r>HTTP{response.status_code}</r> {response.text}"
)

async def get_conversasion_message_response(
self, conversation_id: str, message_id: str
):
Expand All @@ -306,11 +346,14 @@ async def get_conversasion_message_response(
if messages := conversation.get("mapping"):
resp = messages[message_id]
message = messages[resp["parent"]]
while message["message"]["author"]["role"] == "assistant":
resp["message"]["content"]["parts"] = (
message["message"]["content"]["parts"]
+ resp["message"]["content"]["parts"]
)
while message["message"]["author"]["role"] == "assistant" or message["message"]["author"]["role"] == "tool":
logger.info(message)
content_type = message["message"]["content"]["content_type"]
if message["message"]["author"]["role"] == "tool":
if content_type == "multimodal_text":
resp["message"]["content"]["parts"].extend(message["message"]["content"]["parts"])
elif content_type == "text":
resp["message"]["content"]["parts"].extend(message["message"]["content"]["parts"])
message = messages[message["parent"]]
resp["conversation_id"] = conversation_id
return resp
Expand Down

0 comments on commit 0cc522e

Please sign in to comment.