添加图片输入
This commit is contained in:
@@ -1,12 +1,11 @@
|
|||||||
import os
|
import os
|
||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
|
from agentscope.agent import ReActAgent
|
||||||
from agentscope.agent import ReActAgent, UserAgent
|
|
||||||
from agentscope.formatter import OpenAIChatFormatter
|
from agentscope.formatter import OpenAIChatFormatter
|
||||||
from agentscope.memory import InMemoryMemory
|
from agentscope.memory import InMemoryMemory
|
||||||
from agentscope.model import OpenAIChatModel
|
from agentscope.model import OpenAIChatModel
|
||||||
from agentscope.tool import ToolResponse, Toolkit, execute_python_code
|
from agentscope.tool import Toolkit, execute_python_code
|
||||||
from agentscope.message import ImageBlock, Msg, TextBlock, URLSource
|
from agentscope.message import ImageBlock, Msg, TextBlock, URLSource
|
||||||
|
|
||||||
LLAMA_CPP_BASE_URL = os.getenv("LLAMA_CPP_BASE_URL", "http://yyplab.site:8033/v1")
|
LLAMA_CPP_BASE_URL = os.getenv("LLAMA_CPP_BASE_URL", "http://yyplab.site:8033/v1")
|
||||||
@@ -15,8 +14,6 @@ LLAMA_CPP_API_KEY = os.getenv("LLAMA_CPP_API_KEY", "EMPTY")
|
|||||||
|
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
"""创建一个 ReAct 智能体并运行一个简单任务。"""
|
|
||||||
# 准备工具
|
|
||||||
toolkit = Toolkit()
|
toolkit = Toolkit()
|
||||||
toolkit.register_tool_function(execute_python_code)
|
toolkit.register_tool_function(execute_python_code)
|
||||||
|
|
||||||
@@ -26,22 +23,46 @@ async def main():
|
|||||||
model=OpenAIChatModel(
|
model=OpenAIChatModel(
|
||||||
model_name=LLAMA_CPP_MODEL_NAME,
|
model_name=LLAMA_CPP_MODEL_NAME,
|
||||||
api_key=LLAMA_CPP_API_KEY,
|
api_key=LLAMA_CPP_API_KEY,
|
||||||
client_kwargs={"base_url": LLAMA_CPP_BASE_URL}
|
client_kwargs={"base_url": LLAMA_CPP_BASE_URL},
|
||||||
|
stream=True,
|
||||||
),
|
),
|
||||||
memory=InMemoryMemory(),
|
memory=InMemoryMemory(),
|
||||||
formatter=OpenAIChatFormatter(),
|
formatter=OpenAIChatFormatter(),
|
||||||
toolkit=toolkit,
|
toolkit=toolkit,
|
||||||
)
|
)
|
||||||
|
|
||||||
user = UserAgent(name="user")
|
|
||||||
|
|
||||||
msg = await user()
|
|
||||||
while True:
|
while True:
|
||||||
msg = await yyp_bot(msg)
|
user_text = input("请输入问题:").strip()
|
||||||
msg = await user(msg)
|
if user_text == "exit":
|
||||||
if msg.get_text_content() == "exit":
|
|
||||||
break
|
break
|
||||||
|
|
||||||
|
image_path = input("请输入图片路径,留空则不发送图片:").strip()
|
||||||
|
|
||||||
|
if image_path:
|
||||||
|
msg = Msg(
|
||||||
|
name="user",
|
||||||
|
role="user",
|
||||||
|
content=[
|
||||||
|
TextBlock(
|
||||||
|
type="text",
|
||||||
|
text=user_text,
|
||||||
|
),
|
||||||
|
ImageBlock(
|
||||||
|
type="image",
|
||||||
|
source=URLSource(
|
||||||
|
type="url",
|
||||||
|
url=image_path,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
msg = Msg(
|
||||||
|
name="user",
|
||||||
|
role="user",
|
||||||
|
content=user_text,
|
||||||
|
)
|
||||||
|
|
||||||
|
await yyp_bot(msg)
|
||||||
|
|
||||||
asyncio.run(main())
|
asyncio.run(main())
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user