-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:add img under standing tool and refactor code to add toolkits su…
…pport (#57) * fix:rename model curd function name to solve the confilct between curd and router * feat:add img understanding tools * feat:support toolkits * fix:img understanding icon
- Loading branch information
1 parent
7a664e3
commit 4993b9b
Showing
23 changed files
with
208 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .ask_human import ask_human | ||
|
||
__all__ = ["ask_human"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .googletranslate import googletranslate | ||
|
||
__all__ = ["googletranslate"] |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .math import math | ||
|
||
__all__ = ["math"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .openweather import openweather | ||
|
||
__all__ = ["openweather"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .siliconflow_img_gen import siliconflow_img_generation | ||
|
||
__all__ = ["siliconflow_img_generation"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .spark_img_gen import spark_img_generation | ||
|
||
__all__ = ["spark_img_generation"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
from enum import Enum | ||
from typing import Any, Optional, Union, cast | ||
|
||
from pydantic import BaseModel, Field, field_validator | ||
|
||
|
||
class ToolInvokeMessage(BaseModel): | ||
class MessageType(Enum): | ||
TEXT = "text" | ||
IMAGE = "image" | ||
LINK = "link" | ||
BLOB = "blob" | ||
JSON = "json" | ||
IMAGE_LINK = "image_link" | ||
|
||
type: MessageType = MessageType.TEXT | ||
""" | ||
plain text, image url or link url | ||
""" | ||
message: Union[str, bytes, dict] = None | ||
meta: dict[str, Any] = None | ||
save_as: str = "" | ||
|
||
|
||
def create_image_message(image: str, save_as: str = "") -> ToolInvokeMessage: | ||
""" | ||
create an image message | ||
:param image: the url of the image | ||
:return: the image message | ||
""" | ||
return ToolInvokeMessage( | ||
type=ToolInvokeMessage.MessageType.IMAGE, message=image, save_as=save_as | ||
) | ||
|
||
|
||
def create_link_message(link: str, save_as: str = "") -> ToolInvokeMessage: | ||
""" | ||
create a link message | ||
:param link: the url of the link | ||
:return: the link message | ||
""" | ||
return ToolInvokeMessage( | ||
type=ToolInvokeMessage.MessageType.LINK, message=link, save_as=save_as | ||
) | ||
|
||
|
||
def create_text_message(text: str, save_as: str = "") -> ToolInvokeMessage: | ||
""" | ||
create a text message | ||
:param text: the text | ||
:return: the text message | ||
""" | ||
return ToolInvokeMessage( | ||
type=ToolInvokeMessage.MessageType.TEXT, message=text, save_as=save_as | ||
) | ||
|
||
|
||
def create_blob_message( | ||
blob: bytes, meta: dict = None, save_as: str = "" | ||
) -> ToolInvokeMessage: | ||
""" | ||
create a blob message | ||
:param blob: the blob | ||
:return: the blob message | ||
""" | ||
return ToolInvokeMessage( | ||
type=ToolInvokeMessage.MessageType.BLOB, | ||
message=blob, | ||
meta=meta, | ||
save_as=save_as, | ||
) | ||
|
||
|
||
def create_json_message(object: dict) -> ToolInvokeMessage: | ||
""" | ||
create a json message | ||
""" | ||
return ToolInvokeMessage(type=ToolInvokeMessage.MessageType.JSON, message=object) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .img_4v import img_understanding | ||
|
||
__all__ = ["img_understanding"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import base64 | ||
from zhipuai import ZhipuAI | ||
import os | ||
from langchain.pydantic_v1 import BaseModel, Field | ||
|
||
from langchain.tools import StructuredTool | ||
|
||
|
||
class ImageUnderstandingInput(BaseModel): | ||
"""Input for the Image Understanding tool.""" | ||
|
||
text: str = Field(description="the input text for the Image Understanding tool") | ||
|
||
|
||
def img_4v(text: str): | ||
img_path = "/Users/envys/Downloads/a.jpeg" | ||
with open(img_path, "rb") as img_file: | ||
img_base = base64.b64encode(img_file.read()).decode("utf-8") | ||
|
||
client = ZhipuAI( | ||
api_key=os.environ.get("ZHIPUAI_API_KEY"), | ||
) # 填写您自己的APIKey | ||
response = client.chat.completions.create( | ||
model="glm-4v", # 填写需要调用的模型名称 | ||
messages=[ | ||
{ | ||
"role": "user", | ||
"content": [ | ||
{"type": "image_url", "image_url": {"url": img_base}}, | ||
{"type": "text", "text": text}, | ||
], | ||
} | ||
], | ||
) | ||
|
||
return response.choices[0].message | ||
|
||
|
||
img_understanding = StructuredTool.from_function( | ||
func=img_4v, | ||
name="Image Understanding", | ||
description="Users input an image and a question, and the LLM can identify objects, scenes, and other information in the image to answer the user's question.", | ||
args_schema=ImageUnderstandingInput, | ||
return_direct=True, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.